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

Add custom-properties-alphabetical-order rule #104

Open
hgl opened this issue Dec 20, 2019 · 8 comments
Open

Add custom-properties-alphabetical-order rule #104

hgl opened this issue Dec 20, 2019 · 8 comments

Comments

@hgl
Copy link

hgl commented Dec 20, 2019

It seems that there is no option to sort custom properties. Is it an intentional decision or it's planned?

@hudochenkov
Copy link
Owner

I'm not sure if sorting custom properties is a safe operation and it won't break resulting CSS.

@hgl
Copy link
Author

hgl commented Dec 20, 2019

@hudochenkov thanks for the quick reply.

Do you happen to know any case where it can break? I just checked the csswg, it doesn't seem to mention anything about order being important.

@hudochenkov
Copy link
Owner

I don't have experience with custom properties, so I don't know of any cases when the order matters.

@larsenwork
Copy link

I don't think the order is important — e.g. this

:root {
  --width: 40px;
  --max-width: calc(50vw - var(--width));
}

and this

:root {
  --max-width: calc(50vw - var(--width));
  --width: 40px;
}

does the exact same thing.

Let's say one would have alphabetic sorting then --max-width would be first but since it uses --width it seems a bit weird.

Not sure I'd ever want my custom props sorted but maybe I'm just missing a good use case.

@hgl
Copy link
Author

hgl commented Jan 14, 2020

I’m in the hope that they might compress better, just like why you’d want to sort regular properties.

@felixfbecker
Copy link

I have a use case where I'd really like to order custom declarations, and I'd like to order them by their value (with natural sorting). The use case is I have a file with all the z-indexes of the app defined as custom properties, and I'd like to have them in order to make it easy to see how they will stack. Example:

/* stylelint-enable order/order */
:root {
    --z-index-panel: 1;
	--z-index-autocomplete: 2;
 	--z-index-tooltip: 3;
}

@shirotech
Copy link

+1 it's much cleaner in alphabetical order, right now have to do it manually.

@hudochenkov
Copy link
Owner

hudochenkov commented Jun 16, 2020

Looks like sorting custom properties wouldn't break CSS code logic, because they are properties. Unlike variables like in preprocessors.

I'm open for a pull request to add new rule.

  • Name: custom-properties-alphabetical-order
  • Primary option: true
  • Secondary options: None
  • Message: Expected ${first} to come before ${second}. E. g. “Expected --a-property to come before --b-property”.
  • Description: "Specify the alphabetical order of custom properties within declaration blocks."

It could be based on properties-alphabetical-order. However, properties-alphabetical-order has one bug and one improvement, which could be considered a bug. I would suggest fixing this two problems, and then base new rule on properties-alphabetical-order. New rule should not have these bugs.

For autofixing new option should be added to postcss-sorting (this plugin powers all order-related stylesheet transforms):

  • Name: custom-properties-order
  • Primary option: "alphabetical"

This rule won't add much value without autofix.

@hudochenkov hudochenkov changed the title Order for custom properties? Add custom-properties-alphabetical-order rule Jun 16, 2020
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