-
Notifications
You must be signed in to change notification settings - Fork 329
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
refactor: improve theme specificity #506
Conversation
Co-authored-by: Kevin Granger <[email protected]>
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit c2ee4cf:
|
I'll resolve conflicts once #497 is merged. |
Thanks for the exhaustive review!
|
This PR takes care of the theme's specificity issues, and restructures it for clarity.
Specificity
Here's the before/after of the compiled theme's specificity:
Before
After
There are still specificity spikes but they're lower, more even (less "levels" of specificity) and there are many more flat segments.
The remaining specificity spikes are caused by:
.aa-Form:focus-within
,.aa-ClearButton:hover
). These are unavoidable.body.dark
,.aa-ClearButton[hidden]
). These are mostly unavoidable, or a refactor wouldn't bring much value..aa-Panel button
,.aa-ItemContent mark
). These can be avoided by adding a class on the element instead. At this stage, I don't think it's necessary but we can keep it in mind for later improvements..aa-ItemContent--dual .aa-ItemContentTitle
,.aa-DetachedContainer .aa-Panel
). This makes sense in the case of modifiers, or context classes like.aa-DetachedContainer
, and we should make sure to limit this practice to these two use cases..aa-ItemActionButton:hover svg
,.aa-Item[aria-selected=true] .aa-ItemActionButton
).Specificity is reduced by bringing most of the selectors that don't need any specificity at the root of the style sheet (using
@at-root
). Sometimes this isn't possible, because we need context nesting (e.g.,.aa-DetachedContainer .aa-SourceHeader
). In this case, I manually brought all selectors at the same level (the first) because there's no Sass directive that lets you "dedent" levels of specificity.Structure
With specificity issues fixed, I reworked the nested structures so that they make more sense for users when they read it, and it's easier to iterate on.
A typical unit looks like this:
The idea is to have, in order:
.element[hidden], .element.dark
) and their descendantsThis structure allows us to have all element-related styles together (e.g., avoiding lone pseudo-classes and media queries at the bottom, without knowing who they belong to) and use specificity without clashing with source order.
Found issues
I've noticed a couple of issues in the styles which would require to "break" the current API if we tackle them.
.aa-ItemIcon
vs..aa-Visual
From the markup in the playground, it seems they're meant to be used together (
.aa-Visual
relies on styles from.aa-ItemIcon
). If this is the case, then I'd suggest going with a modifier class instead of.aa-Visual
, to convey that importance.For example:
.aa-TouchOnly
From the styles, it seems that
.aa-TouchOnly
is supposed to hide elements that make no sense on mobile (we use it on the "Select" button and it hasdisplay: none
). If this is the case, then the name is contradictory, it should be.aa-DesktopOnly
..aa-TouchOnly
and.aa-ActiveOnly
These are utility classes, yet they're nested under
.aa-Item
(but brought at root). Are they meant to be reused elsewhere? If yes, they shouldn't be nested, but kept at the end and made important to always defeat specificity. Otherwise, if they're only meant to be used with.aa-ItemActionButton
, they should be modifiers..aa-ItemContentSubtitleInline
This class is used in parallel of
.aa-ItemContentSubtitle
as if they were two different concerns, but the former is actually a modifier of the latter. If we make it a proper.aa-ItemContentSubtitle--inline
modifier (which must then be used together with.aa-ItemContentSubtitle
), then can remove the specificity of.aa-ItemContentSubtitle .aa-ItemContentSubtitleIcon
and bring.aa-ItemContentSubtitleIcon
at root, then stylistically scope it under.aa-ItemContentSubtitle--inline
.Review highlights
I've tested the changes in the playground and as far as I can tell, nothing broke.
Since you know the different states better than me, feel free to do a pass to ensure nothing moved.
What's next