@canonical/styles 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # @canonical/styles
2
2
 
3
- Global stylesheet for the Canonical Design System. Single import that aggregates normalize, typography, and design tokens into a layered cascade.
3
+ This README is written for two readers: the person adding the design system to an application, and the
4
+ person maintaining this package. Where a claim can be checked by a test, the test is named.
5
+
6
+ The Canonical Design System's global stylesheet. One import brings in the reset, the typographic
7
+ engine, the design tokens, the modifier families and the layout presets. Everything this package
8
+ itself ships is in a named cascade layer, and its element-level rules style the whole page, as a
9
+ reset does — nothing is confined and nothing has to be marked. The typographic engine, which lives in
10
+ `@canonical/styles-typography` and is released with this package, is in the same layers.
4
11
 
5
12
  ## Installation
6
13
 
@@ -10,61 +17,387 @@ bun add @canonical/styles
10
17
 
11
18
  ## Usage
12
19
 
13
- Import once in your application entry point:
20
+ Import once, in your application's entry point:
14
21
 
15
22
  ```css
16
23
  @import url("@canonical/styles");
17
24
  ```
18
25
 
19
- Or in JavaScript/TypeScript:
26
+ Or from JavaScript or TypeScript:
20
27
 
21
28
  ```ts
22
29
  import "@canonical/styles";
23
30
  ```
24
31
 
25
- This single import provides:
32
+ Then declare the surface on your root:
33
+
34
+ ```html
35
+ <html class="app comfortable">
36
+ ```
37
+
38
+ That single line is the root contract, and it has two parts.
39
+
40
+ | Class | What it means | Values |
41
+ | --- | --- | --- |
42
+ | context | The kind of surface, which sets the comfortable/dense pair for every density value. | `app`, `site` or `docs` |
43
+ | density | Which of that pair is in force. | `comfortable` or `dense` |
44
+
45
+ A colour scheme is optional: `light` or `dark` pins one, and leaving both off lets the page follow the
46
+ reader's operating system. Nothing else belongs on the root — in particular there is no class that
47
+ switches the reset on. It applies because the stylesheet is loaded.
48
+
49
+ Both classes set custom properties, which inherit, so they may sit on the document element or on any
50
+ ancestor of the components that read them — the reference application puts `app comfortable` on
51
+ `<body>`.
52
+
53
+ `ds` is not a root class and never has been. It is what every component puts on its own root, so that
54
+ a component rule can select `.ds.button` rather than `.button` and cannot collide with a page's own
55
+ class names.
56
+
57
+ **A page that also runs another CSS framework** cannot let this stylesheet restyle the half it has not
58
+ migrated, and that is what `@canonical/styles-vanilla-adapter` is for: it ships a copy of this
59
+ package's three element-level layers confined to the parts a team has handed over, keeps the two
60
+ systems out of each other's way, and bridges the theme signal between them. A test in that package
61
+ binds its copy to the files here so the two cannot drift. Nothing about that arrangement changes this
62
+ package or the markup of a page that does not need it.
26
63
 
27
- - **CSS reset** via [normalize.css](https://github.com/necolas/normalize.css)
28
- - **Typography baseline grid** via `@canonical/styles-typography`
29
- - **Design tokens** (colour, spacing, surfaces, states) via `@canonical/design-tokens`
30
- - **`color-scheme: light dark`** on `:root` for `light-dark()` support
64
+ ## Entry points
65
+
66
+ Five. `@canonical/styles` is the one an ordinary page wants: the whole stylesheet, in the order
67
+ above. Three more are that stylesheet in parts, and the fifth is the order statement on its own, for a
68
+ package rather than a page.
69
+
70
+ | Entry | What it is |
71
+ | --- | --- |
72
+ | `@canonical/styles` | everything: the values, the element rules and the layout presets. |
73
+ | `@canonical/styles/tokens.css` | every custom property the design system declares, and not one rule that selects an element. Nothing here changes the page until something reads a value from it, with one exception noted below. |
74
+ | `@canonical/styles/elements.css` | every rule the design system applies to a plain element: the reset, the root's baseline, and the typography with its baseline engine. |
75
+ | `@canonical/styles/layout.css` | the layout presets — `grid`, `subgrid`, `responsive`, `intrinsic` and `content-flow` — which claim those five class names in a page's namespace. |
76
+ | `@canonical/styles/layers.css` | the order statement and nothing else: no rule, no import, no declaration. The same statement the four above open with, for a package that needs the order fixed before it declares a layer of its own. A page never needs it. |
77
+
78
+ One exception to "changes nothing", and it is worth knowing before you import `tokens.css`. The design
79
+ tokens' generated theme sheet declares `color-scheme` on `:root`, `.light` and `.dark` alongside the
80
+ colour tokens, and that property is not inert: it decides the colour of the canvas, the form controls,
81
+ the scrollbars and the system colours, whether or not anything reads a token, and on a mixed page an
82
+ element of the other framework's carrying `light` or `dark` picks it up too. It cannot be split out
83
+ here — the sheet is generated and the declarations sit in the same rules as the tokens — so separating
84
+ them is a change to the tokens package's emitter, tracked as PRA-149. An application that must not have
85
+ it declares `color-scheme` itself, in a layer above `ds.modifiers` or unlayered. Those three
86
+ declarations are the only non-custom properties `tokens.css` carries; everything else in it is a
87
+ custom property, which does nothing until a rule reads it.
88
+
89
+ The parts exist for one reason. A page that also runs another CSS framework cannot take the element
90
+ rules: the other framework has its own `p` rule, and only one of the two can own `line-height`. Such a
91
+ page takes `tokens.css` and `layout.css`, and gets its element rules from that framework's adapter
92
+ instead, in a copy confined to the part of the page the design system owns —
93
+ `@canonical/styles-vanilla-adapter` builds that copy out of the same files `elements.css` imports.
94
+
95
+ Two properties make the parts safe to mix, and both are visible in how the entries are written.
96
+
97
+ **Every entry imports the order first, and the order is written in one file.** `layers.css` holds the
98
+ statement and nothing else; each of the four entries imports it as its first rule. The order has to
99
+ reach the browser before any rule it orders, and writing it once means two entries can never disagree
100
+ — which they could if each repeated the list, and which would leave the same rules arbitrating
101
+ differently depending on the entry a consumer picked. Measured in Chromium: a statement read through
102
+ an `@import` orders the importing sheet exactly as one written in place would.
103
+
104
+ **No entry imports another entry.** An `@layer` statement inside a layer block declares sublayers of
105
+ that layer rather than top-level layers, so an entry that composed another would nest the order
106
+ instead of sharing it. Keeping them independent means a page may load one, two or all three, in any order, and
107
+ get the same result — and that each file is fetched, parsed and applied once, which matters because a
108
+ browser treats every `@import` as its own stylesheet and de-duplicates nothing.
31
109
 
32
110
  ## Cascade Layers
33
111
 
34
- The stylesheet establishes a deterministic layer order:
112
+ Everything this package itself ships is in a named layer, and the order is fixed by one statement,
113
+ which lives in `layers.css` and which every entry imports before anything else. The typographic engine, which this package imports, is in the same
114
+ layers:
35
115
 
36
116
  ```css
37
- @layer normalize, ds.reset, ds.modifiers, ds.components;
117
+ @layer normalize, ds.tokens, ds.reset, ds.typography, ds.modifiers, ds.surfaces,
118
+ ds.states, ds.components, ds.components.global, ds.components.sites,
119
+ ds.components.documentation, ds.components.stores, ds.components.apps;
38
120
  ```
39
121
 
40
- This ensures that component styles always override modifier styles, which always override resets, regardless of source order in consuming applications.
122
+ Read it from the bottom up — each position is an argument.
123
+
124
+ | Layer | What is in it | Why it sits where it does |
125
+ | --- | --- | --- |
126
+ | `normalize` | This package's own reset. | Lowest, because everything else is meant to overrule it. |
127
+ | `ds.tokens` | The primitive values, and the spacing, motion and overflow tokens. | Above the reset, because a token has to exist before anything reads it; below everything that reads one. |
128
+ | `ds.reset` | The declarations the document root makes for itself: font, colour, line height, weight, text wrapping, font smoothing — and border-box sizing for it and everything inside. | Above the tokens because it reads them; below the typographic engine and the components, which refine what it starts. |
129
+ | `ds.typography` | The element rules that apply the typographic mapping, and the baseline engine, from `@canonical/styles-typography`. | Above the reset because it is a more specific statement about text; below the modifiers, which can retune the scale. |
130
+ | `ds.modifiers` | Theme, the typographic scale, the intent families (anticipation, criticality, emphasis, importance) and their shims, and the context and density classes. | Above typography, because a modifier's job is to shift what the layers below produced. |
131
+ | `ds.surfaces` | The surface families: `surface`, `contrasted`, `modal`. | Above the modifiers, because a surface re-points colour channels the modifiers set. |
132
+ | `ds.states` | The derived hover, active and disabled channels. | Above the surfaces, because a state is derived from whatever the surface resolved to. |
133
+ | `ds.components` | Nothing, by rule. It is the parent of the tier layers below and holds no rule of its own. | Highest of the eight top-level layers, so a component is the final word on its own box. A rule written *directly* into a parent layer sits in that layer's implicit final sublayer, which is above every named sublayer — so such a rule would outrank every tier and no component package could override it by layer. Everything this package puts in `ds.components` therefore sits in a tier. |
134
+ | `ds.components.global` | The stylesheets of the shared component packages, and this package's own layout presets. | The base of the tier tree: what every other tier refines. |
135
+ | `ds.components.sites` | The stylesheets of the sites tier. | The four second-level tiers never appear on the same page, so their order among themselves decides nothing. It is fixed here anyway, so that it can never come to depend on which package a bundler emits first. |
136
+ | `ds.components.documentation` | The stylesheets of the documentation tier. | As above. |
137
+ | `ds.components.stores` | The stylesheets of the stores tier. | As above. |
138
+ | `ds.components.apps` | The stylesheets of the shared applications tier; one application's own tier declares a layer of its own above it. | As above. |
139
+
140
+ An order statement fixes the relative order of layers the first time they appear. A later statement
141
+ may introduce new names but can never reorder the ones already fixed, so an application that needs to
142
+ interleave a layer of its own puts its statement before this import.
143
+
144
+ This section is the reference: what is in each layer, and where. The reasoning behind the order — how
145
+ a browser decides, why a rule in no layer beats every layered one, why the confinement a mixed page
146
+ needs lives in the adapter rather than here, and what a bundler does to the statement — is
147
+ [the cascade contract](../../../docs/explanations/STYLES_CASCADE.md).
148
+
149
+ #### The Component Tiers
150
+
151
+ The component layers follow the design system's tier tree: who owns a component decides which layer
152
+ its rules go in, and a deeper tier wins. Three rules make that work, and they are worth stating
153
+ plainly because a package author has to follow them.
154
+
155
+ **The second level is named here, in full.** `global`, `sites`, `documentation`, `stores` and `apps`
156
+ are the design system's four second-level tiers plus the shared base, and this statement fixes their
157
+ order. A package in one of those tiers wraps its stylesheets in its own name and needs to do nothing
158
+ else.
159
+
160
+ **A sub-tier declares its own layer, and that is what sorts it above.** One application's own package
161
+ — the LXD tier, say — writes its CSS entry in this order, and the order is the whole recipe:
162
+
163
+ ```css
164
+ @import url("@canonical/styles/layers.css");
165
+ @layer ds.components.apps-lxd;
166
+
167
+ @import url("./Button/styles.css");
168
+ /* … the rest of the package's sheets, each wrapped in that layer … */
169
+ ```
170
+
171
+ The first line is what makes the second mean the same thing every time. A layer name is placed the
172
+ moment the browser first meets it, so a name met *before* this package's statement is read is placed
173
+ **below** the tier names that statement declares — and the sub-tier would sit under the very tiers it
174
+ is meant to beat. Whether that happened depended on which file the application's bundler emitted
175
+ first, which is not something the package can control from inside itself. Reading the statement first
176
+ settles it: the thirteen names are fixed, and `ds.components.apps-lxd`, being new, is appended after
177
+ them inside `ds.components`, where it sorts above every tier. That is the tree's own rule — the more
178
+ specific tier wins — and it means this stylesheet never has to know which applications exist.
179
+
180
+ Importing `layers.css` costs a page nothing: it is the same statement the other four entries open
181
+ with, and a second identical statement names no layer the first has not already fixed.
182
+
183
+ Measured on a scratch package entry in all four emission orders a bundler can produce: with the import
184
+ first, a rule in `ds.components.apps-lxd` beats the same rule in `ds.components.apps` and in
185
+ `ds.components.global` in 4 of 4; without it, in 1 of 4.
186
+
187
+ **Nothing is ever written directly into `ds.components`.** A declaration in a parent layer sits in
188
+ that layer's implicit final sublayer, which is above every named sublayer, so a rule written there
189
+ would outrank every tier — the opposite of what the tiers are for.
190
+
191
+ The names are tier ids from that tree, lowercased and hyphenated for a sub-tier (`apps-lxd`,
192
+ `sites-webcomponentsprototype`). They are not the context words a page puts on its root (`app`,
193
+ `site`, `docs`): a tier says who owns a component, a context says what kind of page it is being shown
194
+ on, and the two are chosen by different people for different reasons.
195
+
196
+ ### What Is Layered Where
197
+
198
+ | File | Layer | Selects elements? |
199
+ | --- | --- | --- |
200
+ | `normalize.css` | `normalize` | yes |
201
+ | `reset.css` root declarations | `ds.reset` | yes |
202
+ | `reset.css` box-sizing | `ds.reset` | yes — `*`, `::before`, `::after` |
203
+ | `spacing.css` | `ds.tokens` | no |
204
+ | `motion.css` | `ds.tokens` | no |
205
+ | `overflow.css` root default | `ds.tokens` | no |
206
+ | `overflow.css` `.surface` | `ds.surfaces` | no |
207
+ | `grid.css` layout presets, and the content-flow container | `ds.components.global` | yes |
208
+ | `grid.css` `:root` defaults | `ds.components.global` | no — tokens the presets read, kept with them rather than moved to `ds.tokens` |
209
+ | `modifiers.density.css` | `ds.modifiers` | no |
210
+ | `modifiers.states.shim.css`, `modifiers.importance.shim.css`, `modifiers.criticality.shim.css` | `ds.modifiers` | no |
211
+ | `controls.hover.shim.css` | `ds.surfaces` and `ds.states` | no |
212
+ | `@canonical/styles-typography` | `ds.tokens` for its naming shims, `ds.typography` for its element rules and engine, `ds.modifiers` for the typographic scale it imports | its element rules, yes; its tokens, no |
213
+ | `@canonical/design-tokens` distribution files | `ds.tokens`, `ds.modifiers`, `ds.surfaces`, `ds.states` — each file opens its own, except `modifiers.importance.css`, which is empty and opens none | no |
214
+
215
+ The files marked yes are the ones the adapter's confined copy has to mirror. The rest are almost all
216
+ custom properties, which do nothing until a rule reads them, and a rule that reads one matches a
217
+ design-system class. Two exceptions, both deliberate and both in files this package imports
218
+ rather than writes:
219
+
220
+ - **`modifiers.theme.css` sets `color-scheme`** — `light dark` on `:root`, `light` on `.light`, `dark`
221
+ on `.dark` — document-wide. That is the point of it: the colour scheme is what every
222
+ `light-dark()` token resolves against, including the browser's own form controls and scrollbars, and
223
+ it has to reach them.
224
+ - **`states.css` derives its state channels on `*`** — thirty custom properties on every element. It
225
+ is a generated file this package does not author, and it declares nothing but custom properties, so
226
+ its universal selector costs a page nothing it would not pay anyway.
227
+
228
+ ### What Is Deliberately Unlayered
229
+
230
+ One thing: **`@font-face`**, in `fonts.css`. It defines a font for the whole document, not a style for
231
+ an element, and this stylesheet declares each face exactly once, so no layer has anything to order it
232
+ against — putting it in one would change no computed value. The file is opt-in and imported separately
233
+ so that an application already serving the same files does not download them twice.
234
+
235
+ (Layers do sort `@font-face` rules where two of them declare the same family: the higher layer's face
236
+ wins over the later one in source order. There simply are no duplicates here.)
237
+
238
+ ### Where the Element-Level Rules Apply
239
+
240
+ To the whole page. Three of the layers select elements rather than declare custom properties —
241
+ `normalize`, `ds.reset` and `ds.typography` — and they apply the way a reset always has: the
242
+ stylesheet is loaded, so the rules are in force. There is no marker to add and
243
+ no subtree to nominate.
244
+
245
+ That is a deliberate ruling, not an oversight. An earlier draft of this release confined those layers
246
+ to a marked subtree so that a page running two CSS frameworks could keep them apart. The confinement
247
+ works, but it made every ordinary page pay for a problem only some pages have: a class on the root, a
248
+ browser floor it did not need, and a reset that silently did nothing if the class was forgotten.
249
+
250
+ The problem is real, so it is solved where it belongs. `@canonical/styles-vanilla-adapter` ships a
251
+ confined copy of these three layers, applying only inside the parts of a page a team has handed over,
252
+ and a test in that package binds the copy to the files here so the two cannot drift. A page that needs
253
+ it installs it; a page that does not never hears about it.
254
+
255
+ ## What This Package Guarantees
256
+
257
+ | Guarantee | The check behind it |
258
+ | --- | --- |
259
+ | Every rule the package itself ships is in one of the thirteen declared layers, and the statement reaches the browser before any of them, through the import every entry opens with. The typographic engine it imports is in them too. | The order fixtures that arrive with the Vanilla adapter package read the statement out of the resolved stylesheet and check every layer name the package opens against it. A check inside this package — that the set of layers used equals the set declared — is being added separately. |
260
+ | An application tier's rule for a component beats the global tier's rule for the same component, whichever of the two a bundler loads first. | The two sublayers are named in the statement, so their order is fixed there rather than at first appearance, and the same order fixture reads it back. The component packages move into them when their stylesheets are wrapped, which is a separate change; until then both sublayers are empty and the guarantee is vacuous. |
261
+ | The package ships no `!important`. | The same fixture file. An important declaration inverts the layer order and cannot be arbitrated by layers at all, so one of them would undo the guarantee above. |
262
+ | Under `prefers-reduced-motion: reduce`, every motion duration this package defines is `0s`. | `src/motion.css`, and the reduced-motion fixture arriving with the adapter package. Zeroing the token is the mechanism: a component reads the token, so nothing has to out-rank the component's own declaration. Whether a given component honours the tokens is that package's guarantee, not this one's. Sheets that still hard-code a duration, and so still animate under the preference, are being moved onto the tokens separately: in the form package the shared input chrome (`src/index.css`) and eight component sheets (`ChoicesField`, `RichChoicesField`, `CheckboxInput`, `RadioInput`, `ColorInput`, `ComboboxInput` and its list, `FileUploadInput`, `SwitchInput`); in the global package `Tooltip`, `ContextualMenu` and `Popover`, which set their own duration property; and one application-tier sheet, the launchpad diff viewer's file header. |
263
+ | The document root declares the baseline — font, colour, line height, weight, text wrapping, font smoothing — and the page inherits from there rather than from the browser's defaults. | The computed-style fixtures arriving with the adapter package, which read those properties off every element of a rendered block. |
264
+
265
+ Two guarantees that used to sit here are the adapter's, not this package's: that a region handed over
266
+ inside a page running another CSS framework computes as it does on an ordinary page, and that nothing
267
+ reaches a bare element outside such a region. `@canonical/styles-vanilla-adapter` states and tests both.
268
+
269
+ ## What This Package Does Not Guarantee
270
+
271
+ - **Unlayered application CSS beats every rule here.** That is how the cascade is defined: a rule in no
272
+ layer outranks a rule in any layer. It is not a defect, and it is the escape hatch — but it also
273
+ means an application that wants this package's layer order to hold for its own CSS has to put that
274
+ CSS in a layer too. See "Migrating" below.
275
+ - **`!important` in application or third-party CSS beats every rule here**, and for important
276
+ declarations the layer order runs backwards, so the lowest layer wins. Nothing this package can do
277
+ changes that.
278
+ - **This stylesheet styles the whole page.** The reset reaches every `<body>`, `<button>`, `<pre>` and
279
+ `<legend>` in the document, and the layout presets claim the class names `grid`, `subgrid`,
280
+ `responsive`, `intrinsic` and `content-flow` wherever they appear. That is what a design system's
281
+ reset is, and it is why a page that also runs another CSS framework needs the adapter rather than
282
+ this entry point.
283
+ - **`color-scheme` is set on the document root wherever this stylesheet is loaded**, because that is
284
+ what the `light-dark()` tokens and the browser's own controls resolve against. It is the one thing
285
+ here a consumer cannot opt out of by not using a class.
286
+
287
+ ### Browser Floor
288
+
289
+ | Feature | Used by | Chrome | Safari | Firefox |
290
+ | --- | --- | --- | --- | --- |
291
+ | `light-dark()` | every colour token, including the `--color-text` the reset declares on the root | 123 | 17.5 | 120 |
292
+ | `mod()` | the baseline engine | 125 | 15.4 | 118 |
293
+ | `cap` unit | the default baseline engine | 118 | 17.2 | 97 |
294
+
295
+ Read the table as a whole, not row by row: the floor is the highest number in each column, because the
296
+ stylesheet uses all of it. A browser without `light-dark()` drops the root's `color` declaration as
297
+ invalid and falls back to its own text colour, so the reset applies but the page is not themed.
298
+
299
+ That makes the floor for this stylesheet with its default engine **Chrome 125, Safari 17.5,
300
+ Firefox 120**. An application that swaps the default engine for `baseline-trim.css` raises it to
301
+ Chrome 133, Safari 18.2, Firefox 154. The typographic engine also uses `round()`, on the same numbers
302
+ as `mod()`; `@canonical/styles-typography`'s "Browser Support" section is the full table, engine by
303
+ engine.
304
+
305
+ The design system targets current browsers and does not carry compatibility shims for older ones. An
306
+ application that cannot move should pin a version.
307
+
308
+ ## Migrating to the Layered Release
309
+
310
+ This release makes two changes that an application has to answer. Neither is markup: there is nothing
311
+ to add to your root, and the reset applies exactly where it did before.
312
+
313
+ 1. **Your unlayered CSS now beats every rule in this package.** Before this release most of what the
314
+ package shipped was unlayered too, so your overrides competed with it by source order and
315
+ specificity, and sometimes lost. They no longer can. If you were relying on a rule of ours to win,
316
+ it will not any more.
317
+
318
+ 2. **Put your own CSS in a layer, or accept that it wins.** Either is a valid choice, and the second
319
+ needs no work. To take the first, wrap your application's stylesheet and name your layer after the
320
+ package's own:
321
+
322
+ ```css
323
+ @layer normalize, ds.tokens, ds.reset, ds.typography, ds.modifiers,
324
+ ds.surfaces, ds.states, ds.components, ds.components.global,
325
+ ds.components.sites, ds.components.documentation, ds.components.stores,
326
+ ds.components.apps, app;
327
+ @import url("@canonical/styles");
328
+
329
+ @layer app {
330
+ /* your CSS */
331
+ }
332
+ ```
333
+
334
+ Your statement comes first and fixes `app` above every design-system layer, so your rules win by
335
+ layer rather than by accident of order — and a rule of yours that you later want overridden by a
336
+ component can simply be moved down.
337
+
338
+ If you import a subpath rather than the package entry — `@canonical/styles/spacing.css` and the five
339
+ other subpaths this package exports — note that a subpath carries no order statement, so the layers it
340
+ opens are ordered by wherever they first appear in your own stylesheet. Import the entry point unless
341
+ you have a reason not to.
342
+
343
+ ### What Moves on the Page
344
+
345
+ Measured in Chromium, an application that is the design system's throughout, before against after, with
346
+ nothing added to the root. Four things change on every element, and one on `<hr>`; nothing else does.
347
+
348
+ | What | Before | After | Why |
349
+ | --- | --- | --- | --- |
350
+ | `line-height`, everywhere it was not set | `1.15` inherited from the old reset | `normal` | the document root declares its own baseline |
351
+ | `color`, everywhere it was not set | `rgb(0, 0, 0)` | `oklch(0 0 0)` | the root declares `var(--color-text)`: the same black in the light scheme, but it now follows the theme instead of being the browser's default |
352
+ | `box-sizing`, everywhere | `content-box` | `border-box` | the components are authored against it |
353
+ | `font-family` on `<html>` | the browser's serif | the token stack | the root declares the font; `<body>` already had it from the typographic mapper |
354
+ | `overflow` on `<hr>` | `visible` | `hidden` | the browser's own value; a rule has no content to overflow, so nothing shows |
355
+
356
+ Two things that used to be true and are not:
357
+
358
+ - The `normalize.css` package is no longer a dependency. This package writes its own reset, so it can
359
+ be copied into the adapter's confined build, and so that it contains only the rules the design system
360
+ actually relies on. If your
361
+ application imported `normalize.css` through us and wants the rest of it, depend on it directly.
362
+ - The old README said component styles override modifier styles "regardless of source order". They did
363
+ not: the layers were declared but almost nothing was written into them. They do now.
41
364
 
42
365
  ## Design Tokens
43
366
 
44
367
  The following token sets from `@canonical/design-tokens` are included:
45
368
 
46
- | Token Set | Contents |
47
- |-----------|----------|
48
- | `sets.primitive` | Base colour palette, spacing scale, font sizes |
49
- | `modifiers.theme` | Light/dark theme mappings |
50
- | `modifiers.surfaces` | Surface elevation tokens |
51
- | `modifiers.anticipation` | Constructive/destructive/caution intents |
52
- | `modifiers.criticality` | Error/warning/success/information states |
53
- | `modifiers.emphasis` | Branded/highlighted/muted emphasis |
54
- | `modifiers.importance` | Primary/secondary importance levels |
55
- | `states` | Interactive state tokens (hover, active, focus, disabled) |
369
+ | Token set | Contents | Layer it opens |
370
+ | --- | --- | --- |
371
+ | `sets.primitive` | Base colour palette, spacing scale, font sizes | `ds.tokens` |
372
+ | `modifiers.theme` | Light/dark theme mappings, and `color-scheme` on the document root | `ds.modifiers` |
373
+ | `modifiers.surfaces` | Surface elevation tokens | `ds.surfaces` |
374
+ | `modifiers.anticipation` | Constructive/destructive/caution intents | `ds.modifiers` |
375
+ | `modifiers.criticality` | Error/warning/success/information states | `ds.modifiers` |
376
+ | `modifiers.emphasis` | Branded/highlighted/muted emphasis | `ds.modifiers` |
377
+ | `modifiers.importance` | Primary/secondary importance levels — the generated file is empty today, so the shipped mapping is this package's own shim, and a stacked change drops the import until the generated file has content | none |
378
+ | `modifiers.typography` | The typographic scale, through `@canonical/styles-typography` | `ds.modifiers` |
379
+ | `states` | Interactive state tokens (hover, active, focus, disabled) | `ds.states` |
56
380
 
57
381
  ## Dependencies
58
382
 
59
383
  | Package | Role |
60
- |---------|------|
384
+ | --- | --- |
61
385
  | `@canonical/design-tokens` | CSS custom properties for colour, spacing, and states |
62
386
  | `@canonical/styles-typography` | Baseline grid engine and typographic scale |
63
- | `normalize.css` | Cross-browser CSS reset |
64
387
 
65
388
  ## Package Structure
66
389
 
67
390
  ```
68
391
  src/
69
- index.css -- entry point, imports all dependencies
392
+ index.css -- entry point: the layer statement, then the imports
393
+ normalize.css -- this package's own reset
394
+ reset.css -- the document root's baseline
395
+ spacing.css -- spacing tokens
396
+ motion.css -- motion tokens, and reduced motion
397
+ overflow.css -- scroll overflow affordance
398
+ grid.css -- layout presets
399
+ modifiers.density.css -- the context x density family
400
+ modifiers.*.shim.css -- temporary shims for unfinished generated modifiers
401
+ controls.hover.shim.css -- temporary shim for control selected/hover channels
402
+ fonts.css -- opt-in @font-face, unlayered
70
403
  ```
package/package.json CHANGED
@@ -1,11 +1,21 @@
1
1
  {
2
2
  "name": "@canonical/styles",
3
- "description": "Canonical Design System global stylesheet. Aggregates normalize, typography, and design tokens.",
4
- "version": "0.37.0",
5
- "main": "src/index.css",
3
+ "description": "Canonical Design System global stylesheet. The reset, typography, and design tokens, in named cascade layers.",
4
+ "version": "0.38.0",
5
+ "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.css",
8
- "./fonts": "./src/fonts.css"
8
+ "./layers.css": "./src/layers.css",
9
+ "./tokens.css": "./src/tokens.css",
10
+ "./elements.css": "./src/elements.css",
11
+ "./layout.css": "./src/layout.css",
12
+ "./fonts": "./src/fonts.css",
13
+ "./modifiers.states.shim.css": "./src/modifiers.states.shim.css",
14
+ "./modifiers.importance.shim.css": "./src/modifiers.importance.shim.css",
15
+ "./modifiers.criticality.shim.css": "./src/modifiers.criticality.shim.css",
16
+ "./modifiers.density.css": "./src/modifiers.density.css",
17
+ "./spacing.css": "./src/spacing.css",
18
+ "./package.json": "./package.json"
9
19
  },
10
20
  "files": [
11
21
  "src"
@@ -23,19 +33,20 @@
23
33
  "access": "public"
24
34
  },
25
35
  "scripts": {
26
- "check": "bun run check:biome",
36
+ "check": "bun run check:biome && bun run check:webarchitect",
27
37
  "check:fix": "bun run check:biome:fix",
28
38
  "check:biome": "biome check",
29
- "check:biome:fix": "biome check --write"
39
+ "check:biome:fix": "biome check --write",
40
+ "check:webarchitect": "webarchitect assets"
30
41
  },
31
42
  "devDependencies": {
32
- "@biomejs/biome": "2.4.9",
33
- "@canonical/biome-config": "^0.37.0"
43
+ "@biomejs/biome": "2.5.13",
44
+ "@canonical/biome-config": "^0.38.0",
45
+ "@canonical/webarchitect": "^0.38.0"
34
46
  },
35
47
  "dependencies": {
36
- "@canonical/design-tokens": "0.8.1",
37
- "@canonical/styles-typography": "^0.37.0",
38
- "normalize.css": "^8.0.1"
48
+ "@canonical/design-tokens": "0.10.0",
49
+ "@canonical/styles-typography": "^0.38.0"
39
50
  },
40
51
  "peerDependencies": {
41
52
  "@canonical/ds-assets": ">=0.18.0"
@@ -45,5 +56,5 @@
45
56
  "optional": true
46
57
  }
47
58
  },
48
- "gitHead": "ac8383bdb4be1bcd6fa6c4960a1e3d78c0279688"
59
+ "gitHead": "4d10a68a9b299af6fe3d1d7d053e54ca128944ad"
49
60
  }
@@ -33,11 +33,19 @@
33
33
  *
34
34
  * Components read the CHANNELS, never re-derive the maths. Part 1 is zero-
35
35
  * specificity (`:where()`) so it self-upgrades when upstream lands; Part 2 can be
36
- * dropped once the `--hover--…-selected` tokens are generated. Imported into
37
- * `ds.modifiers` after the generated tokens.
36
+ * dropped once the `--hover--…-selected` tokens are generated.
37
+ *
38
+ * Layers: this file shims two different generated files, so it opens two.
39
+ * Part 1 mirrors `modifiers.surfaces.css`, which opens `@layer ds.surfaces`, so
40
+ * Part 1 goes there — that is also what keeps its self-upgrade working, since
41
+ * the generated rules it defers to are in the same layer at higher specificity.
42
+ * Part 2 mirrors the `--hover--` tokens of `states.css`, which opens
43
+ * `@layer ds.states`. Neither generated file defines any of the tokens added
44
+ * here, so both blocks are purely additive. Custom properties only, so nothing
45
+ * here selects an element.
38
46
  */
39
47
 
40
- @layer ds.modifiers {
48
+ @layer ds.surfaces {
41
49
  /* Part 1 — surface-provided selected tokens, per surface level (mirrors
42
50
  * modifiers.surfaces.css). No -selected-layerN primitives exist yet, so each
43
51
  * level aliases the flat base.
@@ -51,12 +59,12 @@
51
59
  * upstream has none. (Removing it once upstream lands is still tidy, but no
52
60
  * longer load-bearing.) */
53
61
  :where(
54
- .surface,
55
- .surface .surface,
56
- .surface .surface .surface,
57
- .contrasted,
58
- .modal
59
- ) {
62
+ .surface,
63
+ .surface .surface,
64
+ .surface .surface .surface,
65
+ .contrasted,
66
+ .modal
67
+ ) {
60
68
  --surface-color-foreground-checkbox-selected: var(
61
69
  --color-foreground-checkbox-selected
62
70
  );
@@ -70,7 +78,9 @@
70
78
  --color-foreground-switch-unselected
71
79
  );
72
80
  }
81
+ }
73
82
 
83
+ @layer ds.states {
74
84
  /* Part 2 — hover channels, derived from the surface-aware base above. */
75
85
  :root {
76
86
  /* Checkbox — selected fill on :checked / :indeterminate hover. */
@@ -0,0 +1,80 @@
1
+ /* @canonical/styles — the element rules
2
+ *
3
+ * Every rule the design system applies to a plain element, and nothing else: the
4
+ * reset, the baseline the page inherits from its root, and the typography —
5
+ * `body`, `h1`–`h6`, `p` and the rest — with the engine that puts each line on
6
+ * the baseline grid.
7
+ *
8
+ * These rules select elements by name, so they apply to the whole document, as
9
+ * a reset always has. A page that also runs another CSS framework cannot let
10
+ * them: the other framework has its own `p` rule and only one of the two can own
11
+ * `line-height`. Such a page takes `tokens.css` and `layout.css` and gets its
12
+ * element rules from that framework's adapter instead, in a copy confined to the
13
+ * part of the page the design system owns. This file is what that copy is built
14
+ * from, together with the two files it imports from the typography package.
15
+ *
16
+ * One of four entry points. `index.css` is the whole stylesheet and is what an
17
+ * ordinary page imports; `tokens.css`, `elements.css` and `layout.css` are it
18
+ * in three parts, for a page that wants some of it and supplies the rest.
19
+ *
20
+ * Every entry imports `layers.css` first, and none of them imports another
21
+ * entry. Both of those matter. The order has to reach the browser before any
22
+ * rule of the page it orders, and it is declared in exactly one file so that
23
+ * two entries can never disagree — if they did, the same rules would arbitrate
24
+ * differently depending on which entry a consumer picked. Measured in Chromium:
25
+ * a statement read through an `@import` orders the importing sheet exactly as
26
+ * one written in place would. Entries stay independent of each other because an
27
+ * `@layer` statement inside a layer block would declare sublayers rather than
28
+ * top-level layers, so an entry composing another would nest the order instead
29
+ * of sharing it; independent, a page may load one, two or all three, in any
30
+ * order, and get the same result.
31
+ */
32
+
33
+ /* Read from the bottom up, each position is an argument:
34
+ * normalize pragma's own reset
35
+ * ds.tokens primitive values, spacing, motion, overflow
36
+ * ds.reset the page's baseline: font, colour, box-sizing
37
+ * ds.typography the semantic mapper and the baseline engines
38
+ * ds.modifiers theme, the typographic scale, the channel families and
39
+ * their shims, the density and context classes
40
+ * ds.surfaces the surface families
41
+ * ds.states the derived hover / active / disabled channels
42
+ * ds.components the parent of the tier layers. Nothing is written directly
43
+ * into it, by rule: a declaration in a parent layer sits in
44
+ * that layer's implicit final sublayer, above every named
45
+ * sublayer, so it would outrank every tier
46
+ * ds.components.global the shared component packages, and this
47
+ * package's own layout presets
48
+ * ds.components.sites } the four second-level tiers of the design
49
+ * ds.components.documentation } system's tier tree. They never share a
50
+ * ds.components.stores } page, so their order among themselves is
51
+ * ds.components.apps } arbitrary — but it is fixed here so that
52
+ * it can never depend on which package a bundler emits first
53
+ *
54
+ * The component layers follow the tier tree of the design system: who owns a
55
+ * component decides which layer its rules go in, and a deeper tier wins.
56
+ *
57
+ * The second level is named here in full, and only the second level. A package
58
+ * below it — one application's own tier, say — declares its own layer as the
59
+ * first rule of its CSS entry, `@layer ds.components.apps-lxd;`, and wraps its
60
+ * sheets in it. Because this statement always comes first, that name is new
61
+ * when the browser meets it and is appended after the names above, inside
62
+ * `ds.components`, so it sorts above them. That is the tree's own rule — the
63
+ * more specific tier wins — and it means this stylesheet never has to know
64
+ * which applications exist.
65
+ *
66
+ * The names are tier ids from that tree, lowercased and hyphenated for a
67
+ * sub-tier (`apps-lxd`), not the context words a page puts on its root
68
+ * (`app`, `site`, `docs`). A tier says who owns a component; a context says
69
+ * what kind of page it is being shown on. */
70
+ @import url("./layers.css");
71
+
72
+ /* Reset: pragma's own */
73
+ @import url("./normalize.css");
74
+ @import url("./reset.css");
75
+
76
+ /* Typography: the rules that put the scale on elements, then the engine that
77
+ * computes each element's nudge onto the baseline grid. The values they read
78
+ * are in tokens.css. */
79
+ @import url("@canonical/styles-typography/elements.css");
80
+ @import url("@canonical/styles-typography/baseline-cap.css");