@datalayer/primer-addons 1.0.36 → 1.0.39

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.
Files changed (90) hide show
  1. package/README.md +69 -19
  2. package/lib/components/appearance/AppearanceControls.d.ts +6 -1
  3. package/lib/components/appearance/AppearanceControls.js +12 -3
  4. package/lib/components/appearance/AppearanceControlsWithStore.d.ts +4 -0
  5. package/lib/components/appearance/AppearanceControlsWithStore.js +4 -4
  6. package/lib/components/appearance/CollaboratorPalette.js +7 -20
  7. package/lib/components/box/Box.d.ts +103 -16
  8. package/lib/components/box/Box.js +157 -2
  9. package/lib/components/box/boxCss.d.ts +805 -0
  10. package/lib/components/box/boxCss.js +492 -0
  11. package/lib/components/box/boxTokens.d.ts +129 -0
  12. package/lib/components/box/boxTokens.js +124 -0
  13. package/lib/components/calendar-picker/CalendarPicker.js +2 -1
  14. package/lib/components/card/Card.js +19 -10
  15. package/lib/components/color/ColorSwatch.js +2 -1
  16. package/lib/components/color-picker/ColorPicker.js +8 -4
  17. package/lib/components/date-picker/DatePicker.d.ts +12 -3
  18. package/lib/components/date-picker/DatePicker.js +24 -8
  19. package/lib/components/index.d.ts +1 -0
  20. package/lib/components/index.js +1 -0
  21. package/lib/components/sheet/Sheet.d.ts +15 -0
  22. package/lib/components/sheet/Sheet.js +21 -0
  23. package/lib/reactor/index.d.ts +1 -1
  24. package/lib/reactor/index.js +1 -1
  25. package/lib/reactor/page-layout/PageLayout.d.ts +6 -0
  26. package/lib/reactor/page-layout/PageLayout.js +30 -6
  27. package/lib/story-helpers.d.ts +2 -2
  28. package/lib/theme/DatalayerThemeProvider.d.ts +21 -0
  29. package/lib/theme/DatalayerThemeProvider.js +13 -4
  30. package/lib/theme/colors/index.d.ts +2 -1
  31. package/lib/theme/colors/index.js +2 -1
  32. package/lib/theme/colors/loopColors.d.ts +2 -48
  33. package/lib/theme/colors/loopColors.js +2 -98
  34. package/lib/theme/colors/themeAccents.d.ts +81 -0
  35. package/lib/theme/colors/themeAccents.js +139 -0
  36. package/lib/theme/css/createThemeCSSVars.d.ts +5 -0
  37. package/lib/theme/css/createThemeCSSVars.js +5 -0
  38. package/lib/theme/css/primerBaseVars.d.ts +1 -0
  39. package/lib/theme/css/primerBaseVars.js +72 -0
  40. package/lib/theme/index.d.ts +2 -0
  41. package/lib/theme/index.js +2 -0
  42. package/lib/theme/loopEyes.d.ts +84 -0
  43. package/lib/theme/loopEyes.js +82 -0
  44. package/lib/theme/portable.d.ts +16 -0
  45. package/lib/theme/portable.js +20 -0
  46. package/lib/theme/portableTheme.d.ts +1 -1
  47. package/lib/theme/portableTheme.js +2 -2
  48. package/lib/theme/themeRegistry.d.ts +3 -75
  49. package/lib/theme/themeRegistry.js +38 -509
  50. package/lib/theme/themeTokens.d.ts +101 -0
  51. package/lib/theme/themeTokens.js +494 -0
  52. package/lib/theme/themes/datalayerTheme.d.ts +2 -9
  53. package/lib/theme/themes/datalayerTheme.js +8 -236
  54. package/lib/theme/themes/datalayerThemeStyles.d.ts +2 -0
  55. package/lib/theme/themes/datalayerThemeStyles.js +234 -0
  56. package/lib/theme/themes/earthTheme.d.ts +2 -9
  57. package/lib/theme/themes/earthTheme.js +8 -235
  58. package/lib/theme/themes/earthThemeStyles.d.ts +2 -0
  59. package/lib/theme/themes/earthThemeStyles.js +233 -0
  60. package/lib/theme/themes/ivoryTheme.d.ts +2 -9
  61. package/lib/theme/themes/ivoryTheme.js +8 -249
  62. package/lib/theme/themes/ivoryThemeStyles.d.ts +2 -0
  63. package/lib/theme/themes/ivoryThemeStyles.js +247 -0
  64. package/lib/theme/themes/loopTheme.d.ts +13 -144
  65. package/lib/theme/themes/loopTheme.js +20 -517
  66. package/lib/theme/themes/loopThemeStyles.d.ts +129 -0
  67. package/lib/theme/themes/loopThemeStyles.js +531 -0
  68. package/lib/theme/themes/lovelyTheme.d.ts +2 -9
  69. package/lib/theme/themes/lovelyTheme.js +8 -231
  70. package/lib/theme/themes/lovelyThemeStyles.d.ts +2 -0
  71. package/lib/theme/themes/lovelyThemeStyles.js +229 -0
  72. package/lib/theme/themes/matrixTheme.d.ts +2 -9
  73. package/lib/theme/themes/matrixTheme.js +8 -237
  74. package/lib/theme/themes/matrixThemeStyles.d.ts +2 -0
  75. package/lib/theme/themes/matrixThemeStyles.js +235 -0
  76. package/lib/theme/themes/sandTheme.d.ts +2 -9
  77. package/lib/theme/themes/sandTheme.js +8 -248
  78. package/lib/theme/themes/sandThemeStyles.d.ts +2 -0
  79. package/lib/theme/themes/sandThemeStyles.js +246 -0
  80. package/lib/theme/themes/spatialTheme.d.ts +2 -9
  81. package/lib/theme/themes/spatialTheme.js +8 -231
  82. package/lib/theme/themes/spatialThemeStyles.d.ts +2 -0
  83. package/lib/theme/themes/spatialThemeStyles.js +229 -0
  84. package/lib/theme/themes/sunTheme.d.ts +2 -9
  85. package/lib/theme/themes/sunTheme.js +8 -249
  86. package/lib/theme/themes/sunThemeStyles.d.ts +2 -0
  87. package/lib/theme/themes/sunThemeStyles.js +247 -0
  88. package/lib/utils/Portals.d.ts +7 -0
  89. package/lib/utils/Portals.js +7 -0
  90. package/package.json +1 -1
package/README.md CHANGED
@@ -43,23 +43,32 @@ The [gallery example](examples/gallery) is a Reactor app built on the three: `np
43
43
  <img alt="Primer React Addons" src="https://assets.datalayer.tech/primer-addons-example.png" />
44
44
  </div>
45
45
 
46
- ## The `loop` theme
46
+ ## `Box`
47
47
 
48
- `loop` is one of the variants of the theme registry (`ThemeVariant`, `themeConfigs`), beside `datalayer`, `spatial`, `lovely`, `matrix`, `earth`, `sand`, `ivory` and `sun`: the look of an application built in LOOP — **one accent, neutral everything else**. A white (near-black in dark mode) canvas, a deep-grey ink, hairlines, soft shadows, round corners and one filled button in the accent. Success, attention and danger are kept for verdicts and rules only. The [gallery](examples/gallery) shows it on its own page, *Loop Theme*, whatever theme the gallery itself wears.
48
+ `Box` is this package's own, not Primer's deprecated one: the same prop names — space (`p`, `mx`, `gap`…), colour (`color`, `bg`), border (`border`, `borderColor`, `borderRadius`, `borderTop`…), layout, flex and grid, position, type, `boxShadow`, `opacity`, `cursor`, `transition`, `transform`, `listStyle`, and `as` — without styled-components.
49
49
 
50
50
  ```tsx
51
- import { DatalayerThemeProvider, loopTheme, loopThemeStyles } from '@datalayer/primer-addons';
52
-
53
- <DatalayerThemeProvider theme={loopTheme} themeStyles={loopThemeStyles} colorMode="auto">
54
- {app}
55
- </DatalayerThemeProvider>
51
+ import { Box } from '@datalayer/primer-addons';
52
+
53
+ <Box as="section" display="flex" flexDirection="column" gap={2} p={3}
54
+ bg="canvas.default" border="1px solid" borderColor="border.default" borderRadius="large"
55
+ gridTemplateColumns={['1fr', null, 'repeat(3, 1fr)']}
56
+ hover={{ bg: 'canvas.subtle' }} focusVisible={{ outline: '2px solid' }}>
57
+ …
58
+ </Box>
56
59
  ```
57
60
 
58
- The sources: `src/theme/colors/loopColors.ts` (colours and accents), `src/theme/themes/loopTheme.ts` (the theme), `src/theme/css/createThemeCSSVars.ts` (what every theme sets), `src/theme/DatalayerThemeProvider.tsx` and `src/utils/Portals.tsx` (how a theme's stylesheet is applied).
61
+ - **Tokens are Primer's CSS variables, with no fallback.** A colour path is the variable Primer's own theme reads first for it (`bg="canvas.subtle"` is `var(--bgColor-muted)`, `borderColor="border.default"` is `var(--borderColor-default)`, `boxShadow="shadow.medium"` is `var(--shadow-resting-medium)`); a radius by name is Primer's (`small`, `medium`, `large`, `full`) or the theme's shape (`control`, `card`, `bubble`, `frame`); a weight by name is the theme's weight variable. Space, font sizes, radii by index and sizes are Primer's theme scales in pixels (`p={3}` is 16px). The theme provider sets every variable `Box` emits, in every theme — a test holds it — so a theme that wants another hairline or corner sets it on Primer's variable (the `loop` theme's hairline is its `--borderColor-default`, its card corner its `--borderRadius-large`), never a call site with `var(--loop-…, var(--…))`.
62
+ - **Responsive values** are one per breakpoint, Primer's (544, 768, 1012, 1280px), `null` to skip one.
63
+ - **States** take a style of their own: `hover`, `focus`, `focusVisible`, `focusWithin`, `active`, and `reducedMotion`.
64
+ - **One class per style.** The props are resolved and hashed into one class whose rules are inserted once into a stylesheet of the document, or of the shadow root the element is in. No styled-components: this package's own copy once shipped beside Primer's and, wherever a bundler did not dedupe them, `Box` rendered with no theme.
65
+ - **`sx` still works**, typed, while the call sites move to props: its keys are read as the props are (the same tokens) and its nested blocks — `'&:hover'`, `'& svg'`, `'@media (…)'`, `'@keyframes …'` — become rules of the same class, after the props'. No styled-components there either: Primer's `Box` on styled-components 6 passed `sx` on to the DOM. What a codemod cannot move to props (nested selectors, keyframes) stays in `sx`.
66
+
67
+ The sources: `src/components/box/Box.tsx` (the design), `boxCss.ts` (the props and their scales, the stylesheet), `boxTokens.ts` (the tokens, generated from Primer's theme), `src/theme/css/primerBaseVars.ts` (Primer's variables no theme sets).
59
68
 
60
- ### Accents
69
+ ## Theme accents
61
70
 
62
- Six, and an application takes one. Each is five values — the accent, the text that sits on it, the **stage** (the flat tint behind a hosted application), a **quiet** tint (the agent's bubble, a draft) and the accent **as text** (a link, a suggested question) — the last three each with a light and a dark value.
71
+ Six soft accents, and an application takes one — **over any theme**: `datalayer`, `spatial`, `lovely`, `matrix`, `earth`, `sand`, `ivory`, `sun` and `loop` alike (decided 2026-10-07; they coloured `loop` alone before). The [gallery](examples/gallery)'s *Theme Accents* page shows the six in whatever theme and colour mode the gallery wears, beside the theme's own, with its tokens read back. Each is five values — the accent, the text that sits on it, the **stage** (the tint behind a hosted application), a **quiet** tint (the agent's bubble, a draft) and the accent **as text** (a link, a suggested question) — the last three each with a light and a dark value.
63
72
 
64
73
  | Name | Accent | Text on it | Stage (light / dark) | Quiet (light / dark) | As text (light / dark) |
65
74
  |---|---|---|---|---|---|
@@ -70,22 +79,45 @@ Six, and an application takes one. Each is five values — the accent, the text
70
79
  | `sun` | `#F8D469` | `#2E2200` | `#FDF3CD` / `#2A2209` | `#FEF9E7` / `#221F14` | `#8A6A00` / `#F8D469` |
71
80
  | `violet` | `#C0AEF6` | `#1C0F47` | `#ECE7FD` / `#1B1533` | `#F6F3FE` / `#1C1A27` | `#6B53C9` / `#C0AEF6` |
72
81
 
73
- `loopAccents` holds them, `loopAccentNames` lists them in the order a picker shows them, and `loopAccentVars(name, mode)` turns one into the four custom properties an application sets on its root: `--loop-accent`, `--loop-accent-on`, `--loop-stage`, `--loop-quiet`. An unknown name falls back to `green`.
82
+ `themeAccents` holds them, `themeAccentNames` lists them in the order a picker shows them (type `ThemeAccentName`), and `themeAccentVars(name, mode)` turns one into everything it colours, as custom properties an application sets on its root:
83
+
84
+ - the accent's own, which **every theme sets** (its default from its own colours — its accent fill, the text of its filled button, its subtle accent tint as the stage, its subtle canvas as the quiet tint; `loop`'s are mint's): `--theme-accent`, `--theme-accent-on`, `--theme-stage`, `--theme-quiet`, and `--theme-stage-gradient`, the stage lit from the stage tint at the top to the quiet tint at the bottom;
85
+ - Primer's accent: `--bgColor-accent-emphasis`, `--borderColor-accent-emphasis`, `--fgColor-accent`, `--fgColor-link`, `--borderColor-accent-muted`, `--bgColor-accent-muted`, and `--datalayer-icon-fg`;
86
+ - the filled button: `--button-primary-bgColor-rest`, `-hover`, `-active`, their borders, `--button-primary-fgColor-rest`, `--button-primary-iconColor-rest`, `--buttonCounter-primary-fgColor-rest`, and the legacy `--color-btn-primary-bg`, `--color-btn-primary-hover-bg`;
87
+ - a checked switch: `--control-checked-bgColor-rest`, `-hover`, `-active`, `--control-checked-fgColor-rest`, `--controlKnob-borderColor-checked`;
88
+ - the selected tab's underline, `--underlineNav-borderColor-active`.
89
+
90
+ So nothing on an application's page keeps the theme's default accent beside its own. The theme's focus ring (`--focus-outlineColor`) is kept: it is drawn to show around every accent. An unknown name is refused.
91
+
92
+ An application names its accent in its Appspec, as `interface.accent` (one of the six names), and its host applies it, whatever theme the application wears. An application that names none is not recoloured: it wears its theme's own colours — mint in `loop` (decided 2026-10-07).
93
+
94
+ ```tsx
95
+ import { getThemeConfig, themeAccentVars } from '@datalayer/primer-addons';
96
+
97
+ const config = getThemeConfig(app.interface.theme.variant);
98
+ <DatalayerThemeProvider theme={config.primerTheme} themeStyles={config.themeStyles}
99
+ baseStyles={app.interface.accent ? themeAccentVars(app.interface.accent, mode) : undefined}>
100
+ ```
101
+
102
+ The source: `src/theme/colors/themeAccents.ts`.
103
+
104
+ ## The `loop` theme
74
105
 
75
- An application names its accent in its Appspec, as `interface.accent` (one of the six names; `green` when it says nothing), and its host applies it:
106
+ `loop` is one of the variants of the theme registry (`ThemeVariant`, `themeConfigs`), beside `datalayer`, `spatial`, `lovely`, `matrix`, `earth`, `sand`, `ivory` and `sun`: the look of an application built in LOOP — **one accent, neutral everything else**. A white (near-black in dark mode) canvas, a deep-grey ink, hairlines, soft shadows, round corners and one filled button in the accent. Success, attention and danger are kept for verdicts and rules only. The [gallery](examples/gallery) shows it on the *Theme Accents* page when the gallery wears it. Its accent is mint, `green` of the [theme accents](#theme-accents).
76
107
 
77
108
  ```tsx
78
- import { loopAccentVars } from '@datalayer/primer-addons';
109
+ import { DatalayerThemeProvider, loopTheme, loopThemeStyles } from '@datalayer/primer-addons';
79
110
 
80
- <DatalayerThemeProvider theme={loopTheme} themeStyles={loopThemeStyles}
81
- baseStyles={loopAccentVars(app.interface.accent, mode)}>
111
+ <DatalayerThemeProvider theme={loopTheme} themeStyles={loopThemeStyles} colorMode="auto">
112
+ {app}
113
+ </DatalayerThemeProvider>
82
114
  ```
83
115
 
84
- `loopAccentVars` sets the four `--loop-*` properties and nothing else: what reads them (the stage, a bubble drawn with them) takes the application's accent, while Primer's own accent tokens — the filled button, `--bgColor-accent-emphasis` — stay the theme's mint. `loopAccentStyles(name, mode)` sets everything the accent colours instead: those four, the accent fill and the filled button with its text, and Primer's accent text, links, muted border and muted tint (`--fgColor-accent`, `--fgColor-link`, `--borderColor-accent-muted`, `--bgColor-accent-muted`), and the selected tab's underline (`--underlineNav-borderColor-active`, which the theme itself also sets to its mint in place of Primer's coral) — so that nothing on an application's page keeps mint beside its own accent.
116
+ The sources: `src/theme/colors/loopColors.ts` (colours), `src/theme/themes/loopTheme.ts` (the theme), `src/theme/css/createThemeCSSVars.ts` (what every theme sets), `src/theme/DatalayerThemeProvider.tsx` and `src/utils/Portals.tsx` (how a theme's stylesheet is applied).
85
117
 
86
118
  ### Shape, shadow, type and motion
87
119
 
88
- **Every theme** sets these, so that a component reads its shape the way it reads its colour (`shapeVars`, `DEFAULT_THEME_SHAPE`). The eight other themes take the defaults — today's corners and no motion at all — and keep every other value they had, which `themeShape.test.ts` holds by a snapshot of each.
120
+ **Every theme** sets these, so that a component reads its shape — and how a link reads in a message — the way it reads its colour (`shapeVars`, `DEFAULT_THEME_SHAPE`). The eight other themes take the defaults — today's corners and no motion at all — and keep every other value they had, which `themeShape.test.ts` holds by a snapshot of each.
89
121
 
90
122
  | Token | Default (every other theme) | `loop` |
91
123
  |---|---|---|
@@ -99,6 +131,7 @@ import { loopAccentVars } from '@datalayer/primer-addons';
99
131
  | `--theme-motion-message` | `0ms` | `200ms` |
100
132
  | `--theme-motion-pane` | `0ms` | `320ms` |
101
133
  | `--theme-motion-easing` | `ease` | `cubic-bezier(0.2, 0, 0, 1)` |
134
+ | `--theme-message-link` | `var(--fgColor-accent, #0969da)` | `currentColor`: a link in a message is plain, the colour of its words, and underlined |
102
135
 
103
136
  **The `loop` theme's own**, beside them (`loopShapeVars`, `loopShadows`, `loopMotionVars`, `loopTypeVars`):
104
137
 
@@ -109,7 +142,7 @@ import { loopAccentVars } from '@datalayer/primer-addons';
109
142
  - **Type**: one face, Inter — open licence (SIL OFL 1.1) and variable — then a metric-matched fallback, then the system sans-serif (`loopFontFamily`: `"Inter Variable", "Inter Fallback", ui-sans-serif, system-ui, …`). Two weights, 400 and 600 (`loopFontWeights`; Primer's light weight is 400 and its medium 600), and five sizes, one line-height for each (`loopTypeScale`, every line a multiple of 4px): 2rem/1.25 (display, title large), 1.25rem/1.4 (title medium, subtitle), 1rem/1.5 (title small, body large), 0.875rem/1.4286 (body medium, the theme's body), 0.75rem/1.3333 (body small, caption) — set on Primer's shorthands and on its size and line-height tokens alike, and held by `loopType.test.ts`. The titles are set tighter and the tracking is a token a component applies: `--loop-tracking-display` (-0.02em), `--loop-tracking-title` (-0.012em), `--loop-tracking-body` (0).
110
143
  - **The face, from the page's own origin**: a page wearing the theme imports `@datalayer/primer-addons/style/loop-face.css` once. It declares `Inter Variable` from `@fontsource-variable/inter` — the upright variable file, latin and latin-ext only, `font-display: swap` — which the page's bundler emits as its own asset, so the face is fetched from the page's origin and never from a third party; and `Inter Fallback`, a local Arial (or Liberation Sans or Arimo, metric-compatible with it) drawn to Inter's measure — `size-adjust: 107.4%`, `ascent-override: 90.2%`, `descent-override: 22.48%`, `line-gap-override: 0%`, next/font's values for Inter, computed by Capsize — so the text paints in the fallback first and does not move when Inter arrives. A page that does not import it shows the system face.
111
144
  - **Space**: no scale of its own — Primer's, in steps of 4 and 8 (`sx` 1 to 6 are 4, 8, 16, 24, 32, 40px), and the air of the look comes from where it is spent: a frame 24px from the edge of its stage, 16px inside a conversation (the header, the messages, the composer), 24px around a pane of work, 8px between two pills, and one hairline (`--theme-hairline`) where two surfaces meet instead of a gap and a border.
112
- - **The stage, lit**: `--loop-stage-gradient`, from the stage tint at the top to the quiet tint at the bottom, both the accent's.
145
+ - **The stage, lit**: `--theme-stage-gradient`, from the stage tint at the top to the quiet tint at the bottom, both the accent's — mint's in `loop` (every theme sets it, see [Theme accents](#theme-accents)).
113
146
  - **The focus ring**: `--focus-outlineColor`, the deep mint `#0F6B4F` in light mode and a mid grey `#636363` in dark mode.
114
147
 
115
148
  ### The reference screens
@@ -128,6 +161,23 @@ How `DatalayerThemeProvider` applies it:
128
161
 
129
162
  `themeStylesheet.test.tsx` tests each of these.
130
163
 
164
+ ### The rules of clean
165
+
166
+ The look is kept by eight rules, written here once and applied to every screen in review (LOOP T-17, Q-09) — a screen that breaks one is changed, or the rule is, never let through as an exception:
167
+
168
+ 1. **One accent per application.** Its own (`interface.accent`), on everything the accent colours (`themeAccentVars`); never a second hue beside it. Success, attention and danger are a verdict's colours, and only a verdict's.
169
+ 2. **No gradient on a surface.** A frame, a card, a bubble, a pill, a button and a menu are flat. The one gradient is the stage behind the frame, lit by the accent's own two tints (`--theme-stage-gradient`), and a stage may as well be flat (`--theme-stage`).
170
+ 3. **No border where space separates.** Air first; where two surfaces meet, one hairline (`--theme-hairline`, `--loop-hairline`), never a gap and a border both, never a box drawn round a group that space already groups.
171
+ 4. **No shadow beyond the one level.** `--loop-shadow-frame` (`--theme-shadow`): a frame, a menu and a dialog float on it; nothing else has a shadow of its own.
172
+ 5. **Two weights.** 400 and 600 (`loopFontWeights`), in the five sizes of `loopTypeScale` — by name in `sx` too, where `loopTheme.fontWeights` makes Primer's `light` and `normal` the 400, `semibold` and `bold` the 600.
173
+ 6. **One filled button per screen.** The action that matters is the filled pill in the accent; every other is quiet — outlined, or invisible until hovered.
174
+ 7. **A control is a line icon.** Octicons, at one weight, in the ink: no colour on a control, no emoji on a button.
175
+ 8. **An identity is an emoji.** An application, an agent and a character are shown by their face (the emoji or the drawing of T-19), never by a line icon.
176
+
177
+ Motion obeys the same spirit: three things move — a message arriving, a status changing, a pane opening — at `--theme-motion-message`, `-status` and `-pane`, and nothing else does.
178
+
179
+ What a test can hold, a test holds: no gradient but the stage's, the one shadow a component asks the theme for, two weights (the tokens and `sx`'s names) and a link plain in a message (`loopClean.test.ts`); the sizes (`loopType.test.ts`); the three durations (`loopContrast.test.ts`); the pills (`themeStylesheet.test.tsx`); in the products that wear the theme, the screens' colours are scanned against these rules too (the landing's `studioVerdictColors.spec.ts`). The rest — space against borders, one filled button — is the reviewer's, against the reference screens (T-16).
180
+
131
181
  ### Contrast, as tested
132
182
 
133
183
  `src/theme/__tests__/loopContrast.test.ts` fails the build when, for any of the six accents, in either mode:
@@ -143,7 +193,7 @@ The same test holds the motion to three durations and one easing.
143
193
 
144
194
  ### What a host may override
145
195
 
146
- Today a host overrides through the provider: `baseStyles` is merged on top of the theme's properties, so it may set an accent (`loopAccentVars`), or any `--loop-*` or `--theme-*` property, for the element and — for the outermost provider — the portal root; and it may pass a theme of its own to `themeStyles`, built with `buildThemeStyles`. A person's choice in an appearance menu (`useThemeStore`) is the host's to honour.
196
+ Today a host overrides through the provider: `baseStyles` is merged on top of the theme's properties, so it may set an accent (`themeAccentVars`), or any `--loop-*` or `--theme-*` property, for the element and — for the outermost provider — the portal root; and it may pass a theme of its own to `themeStyles`, built with `buildThemeStyles`. A person's choice in an appearance menu (`useThemeStore`) is the host's to honour.
147
197
 
148
198
  **Not built yet** (LOOP T-13): the theme travelling inside an embedded application's web component as CSS variables isolated from the host page's styles, with the accent, the face and the mode as the only things the host overrides. Until then an embedded application is themed as any React tree under `DatalayerThemeProvider` is, and the host page's own CSS is not kept out.
149
199
 
@@ -5,7 +5,12 @@ export interface AppearanceControlsProps {
5
5
  themeVariant: ThemeVariant;
6
6
  onColorModeChange: (mode: ColorMode) => void;
7
7
  onThemeChange: (theme: ThemeVariant) => void;
8
+ /** Show the light / dark / auto switch. Default: shown. */
9
+ showColorMode?: boolean;
10
+ /** Show the theme chooser. Default: shown. */
11
+ showThemeChooser?: boolean;
12
+ /** With the chooser, the chosen theme's name, description and preview under its swatches. Default: swatches only. */
8
13
  showThemePreviews?: boolean;
9
14
  }
10
- export declare function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showThemePreviews, }: AppearanceControlsProps): ReactElement;
15
+ export declare function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showColorMode, showThemeChooser, showThemePreviews, }: AppearanceControlsProps): ReactElement;
11
16
  export default AppearanceControls;
@@ -9,14 +9,16 @@ import { MoonIcon, SunIcon, DeviceDesktopIcon, } from '@primer/octicons-react';
9
9
  import { Box } from '../box/Box';
10
10
  import { themeVariants, themeConfigs, } from '../../theme';
11
11
  import { ThemePreviewCard } from './ThemePreviewCard';
12
- export function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showThemePreviews = false, }) {
12
+ export function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showColorMode = true, showThemeChooser = true, showThemePreviews = false, }) {
13
13
  const [isPreviewOpen, setIsPreviewOpen] = useState(false);
14
14
  const selectedTheme = themeConfigs[themeVariant];
15
- return (_jsxs(_Fragment, { children: [_jsx(Box, { sx: { px: 2, py: 2, display: 'flex', justifyContent: 'center' }, children: _jsxs(SegmentedControl, { "aria-label": "Color mode", size: "small", onChange: (index) => {
15
+ return (_jsxs(_Fragment, { children: [showColorMode ? (_jsx(Box, { sx: { px: 2, py: 2, display: 'flex', justifyContent: 'center' }, children: _jsxs(SegmentedControl, { "aria-label": "Color mode", size: "small", onChange: (index) => {
16
16
  const modes = ['light', 'dark', 'auto'];
17
17
  onColorModeChange(modes[index]);
18
- }, children: [_jsx(SegmentedControl.IconButton, { selected: colorMode === 'light', icon: SunIcon, "aria-label": "Light" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'dark', icon: MoonIcon, "aria-label": "Dark" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'auto', icon: DeviceDesktopIcon, "aria-label": "Auto" })] }) }), showThemePreviews ? (_jsxs(Box, { sx: {
18
+ }, children: [_jsx(SegmentedControl.IconButton, { selected: colorMode === 'light', icon: SunIcon, "aria-label": "Light" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'dark', icon: MoonIcon, "aria-label": "Dark" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'auto', icon: DeviceDesktopIcon, "aria-label": "Auto" })] }) })) : null, !showThemeChooser ? null : showThemePreviews ? (_jsxs(Box, { sx: {
19
19
  px: 2,
20
+ // First in the controls when the colour mode is hidden.
21
+ pt: showColorMode ? 0 : 2,
20
22
  pb: 2,
21
23
  display: 'grid',
22
24
  gap: 2,
@@ -31,6 +33,7 @@ export function AppearanceControls({ colorMode, themeVariant, onColorModeChange,
31
33
  return (_jsx(Tooltip, { text: `${cfg.label} — ${cfg.description}`, direction: "s", children: _jsx(Box, { as: "button", "aria-label": cfg.label, "aria-pressed": isSelected, onClick: () => onThemeChange(variant), sx: {
32
34
  width: 24,
33
35
  height: 24,
36
+ flexShrink: 0,
34
37
  borderRadius: '50%',
35
38
  backgroundColor: cfg.brandColor,
36
39
  border: '2px solid',
@@ -77,9 +80,13 @@ export function AppearanceControls({ colorMode, themeVariant, onColorModeChange,
77
80
  },
78
81
  }, children: isPreviewOpen ? 'Hide preview' : 'Show preview' })] }), isPreviewOpen ? (_jsx(ThemePreviewCard, { variant: themeVariant, colorMode: colorMode, selected: true, compact: true, showDescription: true })) : null] })) : (_jsx(Box, { sx: {
79
82
  px: 2,
83
+ // First in the controls when the colour mode is hidden.
84
+ pt: showColorMode ? 0 : 2,
80
85
  pb: 2,
81
86
  display: 'flex',
82
87
  justifyContent: 'center',
88
+ // Narrow, the swatches go on a second line rather than shrink.
89
+ flexWrap: 'wrap',
83
90
  gap: 2,
84
91
  }, children: themeVariants.map((variant) => {
85
92
  const cfg = themeConfigs[variant];
@@ -87,6 +94,8 @@ export function AppearanceControls({ colorMode, themeVariant, onColorModeChange,
87
94
  return (_jsx(Tooltip, { text: cfg.label, direction: "s", children: _jsx(Box, { as: "button", "aria-label": cfg.label, "aria-pressed": isSelected, onClick: () => onThemeChange(variant), sx: {
88
95
  width: 24,
89
96
  height: 24,
97
+ // A circle at any width: never squeezed into an oval.
98
+ flexShrink: 0,
90
99
  borderRadius: '50%',
91
100
  backgroundColor: cfg.brandColor,
92
101
  border: '2px solid',
@@ -3,6 +3,10 @@ import type { UseBoundStore, StoreApi } from 'zustand';
3
3
  import type { ThemeState } from '../../theme/useThemeStore';
4
4
  export interface AppearanceControlsWithStoreProps {
5
5
  useStore: UseBoundStore<StoreApi<ThemeState>>;
6
+ /** Show the light / dark / auto switch. Default: shown. */
7
+ showColorMode?: boolean;
8
+ /** Show the theme chooser (one swatch per theme). Default: shown. */
9
+ showThemeChooser?: boolean;
6
10
  }
7
11
  export declare const AppearanceControlsWithStore: React.FC<AppearanceControlsWithStoreProps>;
8
12
  export default AppearanceControlsWithStore;
@@ -3,9 +3,9 @@ import { IconButton, SegmentedControl, Tooltip } from '@primer/react';
3
3
  import { SunIcon, MoonIcon, DeviceDesktopIcon, CircleIcon, } from '@primer/octicons-react';
4
4
  import { Box } from '../box/Box';
5
5
  import { themeConfigs, themeVariants } from '../../theme/themeRegistry';
6
- export const AppearanceControlsWithStore = ({ useStore }) => {
6
+ export const AppearanceControlsWithStore = ({ useStore, showColorMode = true, showThemeChooser = true, }) => {
7
7
  const { colorMode, theme: themeVariant } = useStore();
8
- return (_jsxs(Box, { sx: { display: 'flex', alignItems: 'center', gap: 3 }, children: [_jsx(Box, { sx: { display: 'flex', alignItems: 'center', gap: 1 }, children: themeVariants.map(variant => {
8
+ return (_jsxs(Box, { sx: { display: 'flex', alignItems: 'center', gap: 3 }, children: [showThemeChooser ? (_jsx(Box, { sx: { display: 'flex', alignItems: 'center', gap: 1 }, children: themeVariants.map(variant => {
9
9
  const tcfg = themeConfigs[variant];
10
10
  const isSelected = themeVariant === variant;
11
11
  return (_jsx(Tooltip, { text: tcfg.label, direction: "s", children: _jsx(IconButton, { "aria-label": tcfg.label, "aria-pressed": isSelected, icon: () => _jsx(CircleIcon, { size: 16, fill: tcfg.brandColor }), size: "small", variant: isSelected ? 'default' : 'invisible', onClick: () => useStore.getState().setTheme(variant, false), sx: {
@@ -13,9 +13,9 @@ export const AppearanceControlsWithStore = ({ useStore }) => {
13
13
  borderColor: isSelected ? 'accent.fg' : 'border.muted',
14
14
  borderRadius: '50%',
15
15
  } }) }, variant));
16
- }) }), _jsxs(SegmentedControl, { "aria-label": "Color mode", size: "small", onChange: (index) => {
16
+ }) })) : null, showColorMode ? (_jsxs(SegmentedControl, { "aria-label": "Color mode", size: "small", onChange: (index) => {
17
17
  const modes = ['light', 'dark', 'auto'];
18
18
  useStore.getState().setColorMode(modes[index]);
19
- }, children: [_jsx(SegmentedControl.IconButton, { selected: colorMode === 'light', icon: SunIcon, "aria-label": "Light" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'dark', icon: MoonIcon, "aria-label": "Dark" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'auto', icon: DeviceDesktopIcon, "aria-label": "Auto" })] })] }));
19
+ }, children: [_jsx(SegmentedControl.IconButton, { selected: colorMode === 'light', icon: SunIcon, "aria-label": "Light" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'dark', icon: MoonIcon, "aria-label": "Dark" }), _jsx(SegmentedControl.IconButton, { selected: colorMode === 'auto', icon: DeviceDesktopIcon, "aria-label": "Auto" })] })) : null] }));
20
20
  };
21
21
  export default AppearanceControlsWithStore;
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  /*
3
3
  * Copyright (c) 2023-2026 Datalayer, Inc.
4
4
  * Distributed under the terms of the Modified BSD License.
@@ -30,7 +30,8 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
30
30
  * @module components/appearance/CollaboratorPalette
31
31
  */
32
32
  import { useMemo } from "react";
33
- import { Box, Text } from "@primer/react";
33
+ import { Box } from "@primer/react";
34
+ import { ColorSwatch } from "../color/ColorSwatch";
34
35
  import { getBrightPalette, } from "../../theme/themeRegistry";
35
36
  import { useThemeStore } from "../../theme/useThemeStore";
36
37
  import { useSystemColorMode } from "../../theme/useSystemColorMode";
@@ -138,23 +139,9 @@ export function CollaboratorPalette({ people, variant, colorMode, showValues = t
138
139
  display: "grid",
139
140
  gridTemplateColumns: "repeat(auto-fill, minmax(96px, 1fr))",
140
141
  gap: 2,
141
- }, children: entries.map((entry) => (_jsxs(Box, { role: "listitem", sx: {
142
- display: "grid",
143
- gap: 1,
144
- border: "1px solid",
145
- borderColor: "var(--borderColor-default)",
146
- borderRadius: 2,
147
- overflow: "hidden",
148
- }, children: [_jsx(Box, { style: { backgroundColor: entry.color, height } }), _jsxs(Box, { sx: { px: 2, pb: 2, display: "grid", gap: 1 }, children: [_jsx(Text, { sx: {
149
- fontSize: 0,
150
- color: "var(--fgColor-default)",
151
- whiteSpace: "nowrap",
152
- overflow: "hidden",
153
- textOverflow: "ellipsis",
154
- }, title: entry.label, children: entry.label }), showValues ? (_jsx(Text, { sx: {
155
- fontSize: 0,
156
- fontFamily: "mono",
157
- color: "var(--fgColor-muted)",
158
- }, children: entry.color })) : null] })] }, entry.key))) }));
142
+ }, children: entries.map((entry) => (
143
+ // The swatch card of the palettes: its hairline and its corner are
144
+ // the theme's, so the palette follows whatever theme the page wears.
145
+ _jsx(Box, { role: "listitem", sx: { minWidth: 0 }, children: _jsx(ColorSwatch, { color: entry.color, label: entry.label, showValue: showValues, height: height }) }, entry.key))) }));
159
146
  }
160
147
  export default CollaboratorPalette;
@@ -1,19 +1,106 @@
1
1
  /**
2
- * `Box`: Primer's, re-exported.
3
- *
4
- * This used to be a `styled.div` of our own, built on this package's own
5
- * styled-components. That copy is v5 while `@primer/react` runs on v6, so
6
- * wherever a bundler did not dedupe the two — webpack in the landing, Rsbuild
7
- * in the decks app — our Box rendered under a runtime with no Primer theme:
8
- * `sx` was never resolved and landed on the DOM as `sx="[object Object]"`,
9
- * and a card that asked for `canvas.subtle` came out white on a dark page.
10
- * agent-runtimes' Vite config carried a shim aliasing this module to Primer's
11
- * Box for exactly that reason; making the module *be* that is the fix for
12
- * every consumer at once. The system props (`p`, `bg`, `display`, …) and `sx`
13
- * are the same API on Primer's Box.
2
+ * `Box`: this package's own, no longer Primer's deprecated one.
3
+ *
4
+ * ## The API
5
+ *
6
+ * The props are Primer's names, so a `Box` reads as it always did:
7
+ *
8
+ * - space — `m`, `mt`…`my`, `p`, `pt`…`py` and the long names, `gap`,
9
+ * `rowGap`, `columnGap`; numbers index Primer's space scale (`p={3}` is
10
+ * 16px, `mt={-2}` is -8px), past its end they are pixels;
11
+ * - colour — `color`, `bg`, `backgroundColor`, `fill`, `stroke`,
12
+ * `outlineColor`, `opacity`;
13
+ * - border — `border`, `borderTop`…, `borderColor`, `borderTopColor`…,
14
+ * `borderWidth`, `borderStyle`, `borderRadius` and its corners,
15
+ * `boxShadow`, `outline`;
16
+ * - layout — `display`, `width`, `height`, `min…`/`max…`, `size`,
17
+ * `overflow`…, `verticalAlign`, `aspectRatio`, `objectFit`;
18
+ * - flex and grid — `flex…`, `align…`, `justify…`, `place…`, `order`,
19
+ * `gridTemplateColumns`, `gridColumn`, `gridArea`, `gridAuto…`;
20
+ * - position — `position`, `zIndex`, `top`, `right`, `bottom`, `left`,
21
+ * `inset`;
22
+ * - type — `fontFamily`, `fontSize`, `fontWeight`, `lineHeight`,
23
+ * `letterSpacing`, `textAlign`, `whiteSpace`, `textDecoration`,
24
+ * `textTransform`, `textOverflow`, `wordBreak`, `listStyle`…;
25
+ * - the rest — `cursor`, `pointerEvents`, `userSelect`, `transition`,
26
+ * `transform`, `animation`, `filter`, `backdropFilter`…
27
+ *
28
+ * `boxCss.ts` has the full list (`BOX_STYLE_PROPS`) with each prop's scale.
29
+ *
30
+ * Every prop takes one value or one per breakpoint — `[base, 544px,
31
+ * 768px, 1012px, 1280px]`, Primer's, `null` to skip one — and the pseudo
32
+ * states take a style of their own: `hover={{ bg: 'canvas.subtle' }}`,
33
+ * `focus`, `focusVisible`, `focusWithin`, `active`, and `reducedMotion` for
34
+ * the reader who asked for less motion. `as`, `className`, `style`, a ref
35
+ * and every other prop go to the element.
36
+ *
37
+ * ## Tokens are Primer's variables, with no fallback
38
+ *
39
+ * A colour path is the CSS variable Primer's own theme reads first for it,
40
+ * and only that: `bg="canvas.subtle"` is `var(--bgColor-muted)`,
41
+ * `borderColor="border.default"` is `var(--borderColor-default)`,
42
+ * `boxShadow="shadow.medium"` is `var(--shadow-resting-medium)` — the table
43
+ * is generated from Primer's color schemes (`boxTokens.ts`). A radius by
44
+ * name is Primer's (`small`, `medium`, `large`, `full`) or the theme's shape
45
+ * (`control`, `card`, `bubble`, `frame`, LOOP T-03); a font weight by name
46
+ * is the theme's weight variable. Space, font sizes, radii by index and
47
+ * sizes are Primer's theme scales, written as pixels: they are the same in
48
+ * every theme and need no variable.
49
+ *
50
+ * No fallback: a page is drawn by its theme, and the theme provider sets
51
+ * every variable `Box` emits (`BOX_EMITTED_VARS`, proven by a test against
52
+ * every theme). A theme that wants a different hairline or corner sets it
53
+ * on Primer's variable — the `loop` theme's hairline *is* its
54
+ * `--borderColor-default` — never a call site with a
55
+ * `var(--loop-…, var(--…))` chain.
56
+ *
57
+ * ## The implementation: one class per style, no styled-components
58
+ *
59
+ * The style props are resolved to CSS and hashed into one class, whose
60
+ * rules are inserted once (`insertRule`) into a stylesheet of the document
61
+ * — or of the shadow root the element is in, for an application embedded
62
+ * in another page. Equal styles share a class; responsive values are
63
+ * `@media` rules and pseudo states `:hover`… rules of the same class, in a
64
+ * fixed order (base, breakpoints, then the states), so a state always wins
65
+ * over the base.
66
+ *
67
+ * Not styled-components, on purpose: this package's own copy of it once
68
+ * shipped beside Primer's (v5 beside v6), and wherever a bundler did not
69
+ * dedupe the two — webpack in the landing, Rsbuild in the decks app — a
70
+ * `Box` rendered under a runtime with no Primer theme: `sx` landed on the
71
+ * DOM as `sx="[object Object]"` and `canvas.subtle` came out white on a
72
+ * dark page. Resolving to CSS variables needs no theme object at runtime,
73
+ * so it needs no styled-components at all. Reusing Primer's instance would
74
+ * tie `Box` to Primer's deprecated styling, which Primer is removing.
75
+ *
76
+ * ## `sx`, while the call sites move to props
77
+ *
78
+ * `sx` still works, typed, and through the same class: its keys are read
79
+ * as Primer's `sx` read them (the same tokens, here with no fallback), and
80
+ * its nested blocks — `'&:hover'`, `'& svg'`, `label`, `'@media (…)'`,
81
+ * `'@keyframes …'` — become rules of that class, after the props', so `sx`
82
+ * wins where both say something, as it did. No styled-components there
83
+ * either: Primer's `Box` on styled-components 6 passed `sx` on to the DOM
84
+ * (`sx="[object Object]"`). A codemod moves `sx` keys to props; what it
85
+ * cannot — nested selectors, keyframes — stays in `sx`.
14
86
  */
15
- import type { ComponentProps } from 'react';
16
- import { Box } from '@primer/react';
17
- export { Box };
18
- export type BoxProps = ComponentProps<typeof Box>;
87
+ import { type CSSProperties, type ElementType, type ForwardRefExoticComponent, type HTMLAttributes, type ReactNode, type RefAttributes } from 'react';
88
+ import type { BetterSystemStyleObject } from './sx';
89
+ import { type BoxPseudoProps, type BoxStyleProps } from './boxCss';
90
+ export * from './boxCss';
91
+ export * from './boxTokens';
92
+ /** The props of a `Box`: its style, its states, the element's own. */
93
+ export type BoxProps = BoxStyleProps & BoxPseudoProps & Omit<HTMLAttributes<HTMLElement>, keyof BoxStyleProps | 'as'> & {
94
+ /** The element or component to render, `div` by default. */
95
+ as?: ElementType;
96
+ /** Primer's `sx`, while the call sites move to props. */
97
+ sx?: BetterSystemStyleObject;
98
+ className?: string;
99
+ style?: CSSProperties;
100
+ children?: ReactNode;
101
+ /** Whatever the element (or the `as` component) takes besides. */
102
+ [prop: string]: unknown;
103
+ };
104
+ /** A `div`, or `as`, drawn by its style props; see above. */
105
+ export declare const Box: ForwardRefExoticComponent<BoxProps & RefAttributes<any>>;
19
106
  export default Box;
@@ -1,7 +1,162 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
1
2
  /*
2
3
  * Copyright (c) 2023-2026 Datalayer, Inc.
3
4
  * Distributed under the terms of the Modified BSD License.
4
5
  */
5
- import { Box } from '@primer/react';
6
- export { Box };
6
+ /**
7
+ * `Box`: this package's own, no longer Primer's deprecated one.
8
+ *
9
+ * ## The API
10
+ *
11
+ * The props are Primer's names, so a `Box` reads as it always did:
12
+ *
13
+ * - space — `m`, `mt`…`my`, `p`, `pt`…`py` and the long names, `gap`,
14
+ * `rowGap`, `columnGap`; numbers index Primer's space scale (`p={3}` is
15
+ * 16px, `mt={-2}` is -8px), past its end they are pixels;
16
+ * - colour — `color`, `bg`, `backgroundColor`, `fill`, `stroke`,
17
+ * `outlineColor`, `opacity`;
18
+ * - border — `border`, `borderTop`…, `borderColor`, `borderTopColor`…,
19
+ * `borderWidth`, `borderStyle`, `borderRadius` and its corners,
20
+ * `boxShadow`, `outline`;
21
+ * - layout — `display`, `width`, `height`, `min…`/`max…`, `size`,
22
+ * `overflow`…, `verticalAlign`, `aspectRatio`, `objectFit`;
23
+ * - flex and grid — `flex…`, `align…`, `justify…`, `place…`, `order`,
24
+ * `gridTemplateColumns`, `gridColumn`, `gridArea`, `gridAuto…`;
25
+ * - position — `position`, `zIndex`, `top`, `right`, `bottom`, `left`,
26
+ * `inset`;
27
+ * - type — `fontFamily`, `fontSize`, `fontWeight`, `lineHeight`,
28
+ * `letterSpacing`, `textAlign`, `whiteSpace`, `textDecoration`,
29
+ * `textTransform`, `textOverflow`, `wordBreak`, `listStyle`…;
30
+ * - the rest — `cursor`, `pointerEvents`, `userSelect`, `transition`,
31
+ * `transform`, `animation`, `filter`, `backdropFilter`…
32
+ *
33
+ * `boxCss.ts` has the full list (`BOX_STYLE_PROPS`) with each prop's scale.
34
+ *
35
+ * Every prop takes one value or one per breakpoint — `[base, 544px,
36
+ * 768px, 1012px, 1280px]`, Primer's, `null` to skip one — and the pseudo
37
+ * states take a style of their own: `hover={{ bg: 'canvas.subtle' }}`,
38
+ * `focus`, `focusVisible`, `focusWithin`, `active`, and `reducedMotion` for
39
+ * the reader who asked for less motion. `as`, `className`, `style`, a ref
40
+ * and every other prop go to the element.
41
+ *
42
+ * ## Tokens are Primer's variables, with no fallback
43
+ *
44
+ * A colour path is the CSS variable Primer's own theme reads first for it,
45
+ * and only that: `bg="canvas.subtle"` is `var(--bgColor-muted)`,
46
+ * `borderColor="border.default"` is `var(--borderColor-default)`,
47
+ * `boxShadow="shadow.medium"` is `var(--shadow-resting-medium)` — the table
48
+ * is generated from Primer's color schemes (`boxTokens.ts`). A radius by
49
+ * name is Primer's (`small`, `medium`, `large`, `full`) or the theme's shape
50
+ * (`control`, `card`, `bubble`, `frame`, LOOP T-03); a font weight by name
51
+ * is the theme's weight variable. Space, font sizes, radii by index and
52
+ * sizes are Primer's theme scales, written as pixels: they are the same in
53
+ * every theme and need no variable.
54
+ *
55
+ * No fallback: a page is drawn by its theme, and the theme provider sets
56
+ * every variable `Box` emits (`BOX_EMITTED_VARS`, proven by a test against
57
+ * every theme). A theme that wants a different hairline or corner sets it
58
+ * on Primer's variable — the `loop` theme's hairline *is* its
59
+ * `--borderColor-default` — never a call site with a
60
+ * `var(--loop-…, var(--…))` chain.
61
+ *
62
+ * ## The implementation: one class per style, no styled-components
63
+ *
64
+ * The style props are resolved to CSS and hashed into one class, whose
65
+ * rules are inserted once (`insertRule`) into a stylesheet of the document
66
+ * — or of the shadow root the element is in, for an application embedded
67
+ * in another page. Equal styles share a class; responsive values are
68
+ * `@media` rules and pseudo states `:hover`… rules of the same class, in a
69
+ * fixed order (base, breakpoints, then the states), so a state always wins
70
+ * over the base.
71
+ *
72
+ * Not styled-components, on purpose: this package's own copy of it once
73
+ * shipped beside Primer's (v5 beside v6), and wherever a bundler did not
74
+ * dedupe the two — webpack in the landing, Rsbuild in the decks app — a
75
+ * `Box` rendered under a runtime with no Primer theme: `sx` landed on the
76
+ * DOM as `sx="[object Object]"` and `canvas.subtle` came out white on a
77
+ * dark page. Resolving to CSS variables needs no theme object at runtime,
78
+ * so it needs no styled-components at all. Reusing Primer's instance would
79
+ * tie `Box` to Primer's deprecated styling, which Primer is removing.
80
+ *
81
+ * ## `sx`, while the call sites move to props
82
+ *
83
+ * `sx` still works, typed, and through the same class: its keys are read
84
+ * as Primer's `sx` read them (the same tokens, here with no fallback), and
85
+ * its nested blocks — `'&:hover'`, `'& svg'`, `label`, `'@media (…)'`,
86
+ * `'@keyframes …'` — become rules of that class, after the props', so `sx`
87
+ * wins where both say something, as it did. No styled-components there
88
+ * either: Primer's `Box` on styled-components 6 passed `sx` on to the DOM
89
+ * (`sx="[object Object]"`). A codemod moves `sx` keys to props; what it
90
+ * cannot — nested selectors, keyframes — stays in `sx`.
91
+ */
92
+ import { forwardRef, useCallback, useEffect, useInsertionEffect, useLayoutEffect, useRef, version as reactVersion, } from 'react';
93
+ import { BOX_PSEUDO_PROPS, BOX_STYLE_PROPS, boxClassOf, injectBoxClass, } from './boxCss';
94
+ export * from './boxCss';
95
+ export * from './boxTokens';
96
+ const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
97
+ const STYLE_KEYS = new Set([
98
+ ...Object.keys(BOX_STYLE_PROPS),
99
+ ...Object.keys(BOX_PSEUDO_PROPS),
100
+ 'reducedMotion',
101
+ ]);
102
+ /** React 19 hands a ref to a function component as a prop; React 18 warns. */
103
+ const REF_AS_PROP = Number.parseInt(reactVersion, 10) >= 19;
104
+ /** True for a component React 18 cannot give a ref to: a plain function. */
105
+ const isPlainFunction = (component) => typeof component === 'function' && !component.prototype?.isReactComponent;
106
+ /** Elements whose `width` and `height` are attributes too: they keep them. */
107
+ const SIZED_ELEMENTS = new Set(['img', 'svg', 'video', 'canvas', 'iframe', 'embed', 'object']);
108
+ function assignRef(ref, value) {
109
+ if (typeof ref === 'function') {
110
+ ref(value);
111
+ }
112
+ else if (ref) {
113
+ ref.current = value;
114
+ }
115
+ }
116
+ const BoxImpl = forwardRef(function Box(props, ref) {
117
+ const { as, sx: sxProp, className, ...rest } = props;
118
+ // The index signature widens what destructuring reads: typed back here.
119
+ const Component = (as ?? 'div');
120
+ const sx = sxProp;
121
+ const style = {};
122
+ const element = {};
123
+ for (const key of Object.keys(rest)) {
124
+ (STYLE_KEYS.has(key) ? style : element)[key] = rest[key];
125
+ }
126
+ if (typeof Component === 'string' && SIZED_ELEMENTS.has(Component)) {
127
+ for (const key of ['width', 'height']) {
128
+ const value = rest[key];
129
+ if (typeof value === 'string' || typeof value === 'number') {
130
+ element[key] = value;
131
+ }
132
+ }
133
+ }
134
+ const box = boxClassOf(style, sx);
135
+ const node = useRef(null);
136
+ const setNode = useCallback((value) => {
137
+ node.current = value instanceof Element ? value : null;
138
+ assignRef(ref, value);
139
+ }, [ref]);
140
+ // Into the document's stylesheet before the DOM is changed…
141
+ useInsertionEffect(() => {
142
+ if (box && typeof document !== 'undefined') {
143
+ injectBoxClass(document, box);
144
+ }
145
+ }, [box]);
146
+ // …and into a shadow root's, for an element rendered in one.
147
+ useIsomorphicLayoutEffect(() => {
148
+ const root = node.current?.getRootNode();
149
+ if (box && typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot) {
150
+ injectBoxClass(root, box);
151
+ }
152
+ }, [box]);
153
+ const classes = [box?.className, className].filter(Boolean).join(' ') || undefined;
154
+ // Our ref (for the shadow root) unless React 18 would warn: then the
155
+ // caller's, if any.
156
+ const elementRef = REF_AS_PROP || !isPlainFunction(Component) ? setNode : ref || undefined;
157
+ return _jsx(Component, { ref: elementRef, className: classes, ...element });
158
+ });
159
+ BoxImpl.displayName = 'Box';
160
+ /** A `div`, or `as`, drawn by its style props; see above. */
161
+ export const Box = BoxImpl;
7
162
  export default Box;