Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically remove @keyframes duplicates #1506

Closed
Justineo opened this issue Aug 22, 2013 · 15 comments
Closed

Automatically remove @keyframes duplicates #1506

Justineo opened this issue Aug 22, 2013 · 15 comments

Comments

@Justineo
Copy link
Contributor

Hi,

When writing animations I don't want @keyframes rules appear in final .css files if they are not used (because if we support lots of animations that will be great amount of code). So I put a @keyframes rule inside a mixin, like this:

.play() {
  @keyframes play {
    from {
      margin-top: 0;
    }

    to {
      margin-top: 100px;
    }
  }
}

But if I call that mixin for several times, the @keyframes rule will also be repeated.

.test {
  .play();
}

.test2 {
  .play();
}

will output (in global scope):

@keyframes play {
  from {
    margin-top: 0;
  }
  to {
    margin-top: 100px;
  }
}
@keyframes play {
  from {
    margin-top: 0;
  }
  to {
    margin-top: 100px;
  }
}

Is there a way to avoid this? (I'm using lessc 1.4.2)

@Soviut
Copy link

Soviut commented Aug 22, 2013

This seems to fall into the same area as grouping media queries.

#950

However, instead of grouping it's more of a "collapse" situation. It would also need to be optional, however, since there may be a valid reason someone might want to override a @keyframe rule.

It seems that in both cases, some sort of syntax to indicate that a rule is allowed to be "grouped" or "collapsed" would be necessary.

@Justineo
Copy link
Contributor Author

@keyframes rules don't cascade so the last one always wins. I think overriding is not a problem because you just need to pick the last rule.

@lukeapage
Copy link
Member

You could get round this by setting a global variable and using a guard so
the mixin doesn't run if the variable is set...

@Justineo
Copy link
Contributor Author

@lukeapage
I had considered that but it doesn't really solve the problem. I hope to 'Lessify' animation libraries like animate.css but it contains huge amount of @keyframes rules. If they cannot be produced into .css files upon mixin calls, I will have to output all of them. If I put them in mixins, I will only get those are actually used but with duplicates. I can't figure out a perfect solution in this case...

@lukeapage
Copy link
Member

it would need to be an option - some people might want a key frame referenced inline or in another stylesheet.

@Justineo
Copy link
Contributor Author

Justineo commented Sep 2, 2013

@lukeapage
Agree. So what I expect is that perhaps Less can offer some mechanism (maybe compilation option?) to automatically remove duplicate @keyframes rules (or even more general, other potential uncascaded rules) except the last one (by default it is turned off).

@lukeapage
Copy link
Member

I thought your problem was unused keyframes, not duplicates?

@Justineo
Copy link
Contributor Author

Justineo commented Sep 2, 2013

I can think of two choices:

  1. directly write all @keyframes rules in the global scope, some of which may be unused, or
  2. put @keyframes rules in mixins and output them only when mixins are called, which generates duplicates.

Your last comment is right, unused @keyframes rules cannot be removed because they might be referenced elsewhere. So I think there should be a way to remove duplicates.

@Justineo
Copy link
Contributor Author

I found @counter-style rules being a similar case as this one.
http://dev.w3.org/csswg/css-counter-styles-3/#the-counter-style-rule

@lukeapage
Copy link
Member

Feature will be in next minor release - 1.7

@Justineo
Copy link
Contributor Author

@lukeapage
The problem discussed in this issue doesn't seem to be solved in 1.7. The problem here is duplication after each mixin call, while 1.7 supported using rulesets as variables.

@seven-phases-max
Copy link
Member

OK, I'll reopen this (probably @lukeapage mean some other @keyframes related feature not "removing duplicates"). So far I have not seen anyone was planning to implement this.

@Justineo
Copy link
Contributor Author

Justineo commented Dec 7, 2014

@seven-phases-max seven-phases-max changed the title @keyframes inside mixins Automatically remove @keyframes duplicates Jan 16, 2016
@stale
Copy link

stale bot commented Nov 14, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 14, 2017
@stale stale bot closed this as completed Nov 28, 2017
@atang-gfm
Copy link

Hi @Justineo , wondering if you found a better solution to this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants