Closed Bug 1878343 Opened 5 months ago Closed 4 months ago

Refactor tokens CSS files to use @layer

Categories

(Toolkit :: Themes, task, P3)

task

Tracking

()

RESOLVED FIXED
125 Branch
Tracking Status
firefox125 --- fixed

People

(Reporter: hjones, Assigned: tgiles)

References

(Depends on 1 open bug, Blocks 2 open bugs)

Details

(Whiteboard: [recomp])

Attachments

(3 files)

We've implemented slightly convoluted @media not (prefers-contrast) querys in all of our tokens CSS files (one example). This was necessary in order to prevent redefined tokens in tokens-brand.css or tokens-platform.css from taking precedence over HCM token values from tokens-shared.css. Media queries don't confer extra specificity, and because tokens-brand.css and tokens-platform.css both import tokens-shared.css, any redefinitions in those files would effectively overwrite HCM values defined in tokens-shared.css.

This came up recently in the work for Bug 1850611 since it would be challenging to find a good way to represent a @media not (prefers-contrast) media query in our JSON.

:mstriemer has suggested using @layer to simplify the logic/work around CSS cascade order issues:

/* tokens-shared.css */
@layer forced-colors, prefers-contrast, base;

@layer base {
  :root {
    --color-accent-primary: blue;
  }
}

@layer forced-colors {
  @media (forced-colors) {
    :root {
      --color-accent-primary: AccentColor;
    }
  }
}
/* tokens-brand.css */

@layer base {
  :root {
    /* Without @layer this would overwrite the @media (forced-colors) block. */
    --color-accent-primary: green;
  }
}
Whiteboard: [fidefe-reusable-components]
Assignee: nobody → tgiles
See Also: → 1878919
Severity: -- → N/A
Priority: -- → P3

By using the CSS layers feature, we can simplify our prefers-contrast
and forced-colors media queries by ensuring that:

  • Forced colors styling has the highest precedence
  • Prefers contrast styling has the next highest precedence
  • Base styling has the lowest precedence

This allows us to remove the "@media not" queries from the various
tokens files without breaking the cascade and causing the brand or
platform tokens file to unintentionally overwrite token values in the
shared tokens file.

Additionally, this change aligns with the accessibility recommendation
to keep the prefers-contrast and forced-colors media queries in the
:root selector for more maintainable code.

By using the CSS layers feature, we can simplify our prefers-contrast
and forced-colors media queries by ensuring that:

  • Forced colors styling has the highest precedence
  • Prefers contrast styling has the next highest precedence
  • Base styling has the lowest precedence

This allows us to remove the "@media not" queries from the various
tokens files without breaking the cascade and causing the brand or
platform tokens file to unintentionally overwrite token values in the
shared tokens file.

Additionally, this change aligns with the accessibility recommendation
to keep the prefers-contrast and forced-colors media queries in the
:root selector for more maintainable code.

Depends on D201168

By using the CSS layers feature, we can simplify our prefers-contrast
and forced-colors media queries by ensuring that:

  • Forced colors styling has the highest precedence
  • Prefers contrast styling has the next highest precedence
  • Base styling has the lowest precedence

This allows us to remove the "@media not" queries from the various
tokens files without breaking the cascade and causing the brand or
platform tokens file to unintentionally overwrite token values in the
shared tokens file.

Additionally, this change aligns with the accessibility recommendation
to keep the prefers-contrast and forced-colors media queries in the
:root selector for more maintainable code.

Depends on D201169

Blocks: 1879900
Whiteboard: [fidefe-reusable-components] → [recomp]
Depends on: 1881098
Pushed by tgiles@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/be410560d1b8
Refactor tokens-shared to use CSS layers. r=reusable-components-reviewers,desktop-theme-reviewers,emilio,hjones
https://hg.mozilla.org/integration/autoland/rev/81fa5e91d692
Refactor tokens-platform to use CSS layers. r=reusable-components-reviewers,desktop-theme-reviewers,emilio,hjones
https://hg.mozilla.org/integration/autoland/rev/f9f660122979
Refactor tokens-brand to use CSS layers. r=reusable-components-reviewers,desktop-theme-reviewers,emilio,hjones
Status: NEW → RESOLVED
Closed: 4 months ago
Resolution: --- → FIXED
Target Milestone: --- → 125 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: