@datalayer/primer-addons 1.0.30 → 1.0.33

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
@@ -43,6 +43,103 @@ 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
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.
49
+
50
+ ```tsx
51
+ import { DatalayerThemeProvider, loopTheme, loopThemeStyles } from '@datalayer/primer-addons';
52
+
53
+ <DatalayerThemeProvider theme={loopTheme} themeStyles={loopThemeStyles} colorMode="auto">
54
+ {app}
55
+ </DatalayerThemeProvider>
56
+ ```
57
+
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).
59
+
60
+ ### Accents
61
+
62
+ Six, and an application takes one. Each is four values — the accent, the text that sits on it, the **stage** (the flat tint behind a hosted application) and a **quiet** tint (the agent's bubble, a draft) — the stage and the quiet tint each with a light and a dark value.
63
+
64
+ | Name | Accent | Text on it | Stage (light / dark) | Quiet (light / dark) |
65
+ |---|---|---|---|---|
66
+ | `green` (mint, the default) | `#7ADBB8` | `#06281E` | `#DDF5EC` / `#10261F` | `#F0FAF6` / `#17211D` |
67
+ | `rose` | `#F6A5C1` | `#3A0A1C` | `#FCE3EC` / `#2B141C` | `#FDF2F6` / `#22181C` |
68
+ | `sky` | `#8CCBF9` | `#06243B` | `#DFF0FD` / `#0F2333` | `#F0F8FE` / `#161E25` |
69
+ | `lime` | `#BEDC55` | `#1F2A00` | `#EEF6CC` / `#1E2609` | `#F7FBE6` / `#1C2014` |
70
+ | `sun` | `#F8D469` | `#2E2200` | `#FDF3CD` / `#2A2209` | `#FEF9E7` / `#221F14` |
71
+ | `violet` | `#C0AEF6` | `#1C0F47` | `#ECE7FD` / `#1B1533` | `#F6F3FE` / `#1C1A27` |
72
+
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`.
74
+
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:
76
+
77
+ ```tsx
78
+ import { loopAccentVars } from '@datalayer/primer-addons';
79
+
80
+ <DatalayerThemeProvider theme={loopTheme} themeStyles={loopThemeStyles}
81
+ baseStyles={loopAccentVars(app.interface.accent, mode)}>
82
+ ```
83
+
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.
85
+
86
+ ### Shape, shadow, type and motion
87
+
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.
89
+
90
+ | Token | Default (every other theme) | `loop` |
91
+ |---|---|---|
92
+ | `--theme-radius-control` | `var(--borderRadius-medium, 6px)` | `999px` |
93
+ | `--theme-radius-card` | `var(--borderRadius-medium, 6px)` | `20px` |
94
+ | `--theme-radius-bubble` | `var(--borderRadius-large, 12px)` | `22px` |
95
+ | `--theme-radius-frame` | `var(--borderRadius-large, 12px)` | `28px` |
96
+ | `--theme-hairline` | `var(--borderWidth-thin, 1px)` | the same |
97
+ | `--theme-shadow` | `var(--shadow-resting-small, none)` | the frame's shadow, per mode |
98
+ | `--theme-motion-status` | `0ms` | `120ms` |
99
+ | `--theme-motion-message` | `0ms` | `200ms` |
100
+ | `--theme-motion-pane` | `0ms` | `320ms` |
101
+ | `--theme-motion-easing` | `ease` | `cubic-bezier(0.2, 0, 0, 1)` |
102
+
103
+ **The `loop` theme's own**, beside them (`loopShapeVars`, `loopShadows`, `loopMotionVars`, `loopTypeVars`):
104
+
105
+ - **Radii**: `--loop-radius-control` (999px), `--loop-radius-card` (20px), `--loop-radius-bubble` (22px), `--loop-radius-frame` (28px); Primer's own `--borderRadius-small`, `-medium`, `-large` are made rounder (10, 14, 20px).
106
+ - **The face** of an application, at three sizes and no other: `--loop-face-large` (72px, a page), `--loop-face-medium` (40px, a header or a card), `--loop-face-small` (20px, a line of a list).
107
+ - **Shadow**: one level, `--loop-shadow-frame`. In light mode a surface floats on a soft, low shadow; in dark mode, where a shadow shows little, on a hairline of light and a deeper shadow. Primer's `--shadow-resting-*` and `--shadow-floating-*` take the same hand, so a menu or a dialog floats as a frame does. `--loop-hairline` is the line between two surfaces (7% of the ink in light, 10% of white in dark).
108
+ - **Motion**: three durations for three things — `--loop-motion-status` (a status changing), `--loop-motion-message` (a message arriving), `--loop-motion-pane` (a pane opening) — and one easing, `--loop-motion-easing`. Nothing else moves; a component honours `prefers-reduced-motion` itself.
109
+ - **Type**: one face, Inter, then the system sans-serif (`loopFontFamily`); two weights, 400 and 600. The titles are set tighter (`--text-display-shorthand` 2.75rem/1.15 down to `--text-title-shorthand-small` 1rem/1.4) and the tracking is a token a component applies: `--loop-tracking-display` (-0.02em), `--loop-tracking-title` (-0.012em), `--loop-tracking-body` (0). The package does not ship the face: a page that has not loaded Inter shows the system face, and nothing but the letterforms changes.
110
+ - **The stage, lit**: `--loop-stage-gradient`, from the stage tint at the top to the quiet tint at the bottom, both the accent's.
111
+ - **The focus ring**: `--focus-outlineColor`, the deep mint `#0F6B4F` in light mode and a mid grey `#636363` in dark mode.
112
+
113
+ ### The controls, as pills
114
+
115
+ Every Primer control takes its corners from one token, `--borderRadius-medium`, which cards and list items take too — so "the controls alone are pills" cannot be said with a custom property. A theme may therefore carry **a stylesheet of its own**, `ThemeStyles.css`, written relative to `:scope` (`buildThemeStyles(light, dark, { css })`). `loop` is the only theme that has one (`loopControlsCss`): buttons, text inputs, selects, segmented controls, button groups (their two ends), toggle switches and underline tabs are pills; a multi-line text area keeps `--borderRadius-medium`, and menus and overlays keep the card's radius. The selectors match the stable prefixes of Primer's class names (`loopControlsSelectors`), and a test reads each back from the installed `@primer/react`, so an upgrade that renames one fails the build.
116
+
117
+ How `DatalayerThemeProvider` applies it:
118
+
119
+ - It stamps its own element with a unique `data-datalayer-theme-scope` and injects one `<style data-datalayer-theme-stylesheet>` into the document's head, wrapping the theme's stylesheet in `@scope (<its element>) to (<any other provider's element>)` — so the rules stop at a provider nested inside, and a widget in its own theme keeps its shapes (`scopeThemeCss`, `injectThemeStylesheet`).
120
+ - The **outermost** provider owns Primer's portal root (`__primerPortalRoot__`, where menus, overlays and dialogs are drawn) and scopes the stylesheet to it too; a nested provider leaves the portal root alone.
121
+ - The stylesheet is removed when the theme changes and when the provider unmounts; it is injected in a layout effect, so the first paint is already in the theme's shapes. A theme without one gets nothing injected.
122
+
123
+ `themeStylesheet.test.tsx` tests each of these.
124
+
125
+ ### Contrast, as tested
126
+
127
+ `src/theme/__tests__/loopContrast.test.ts` fails the build when, for any of the six accents, in either mode:
128
+
129
+ - the text on a bubble and on a filled pill (the accent's `on` on the accent) is under **4.5 to 1** (WCAG AA for body text);
130
+ - the ink on the stage or on the quiet tint, light and dark, is under 4.5 to 1;
131
+ - the focus ring is under **3 to 1** against the page or around a pill of any accent — but on the subtle dark surface, the one place the dark ring falls short, where it holds 2.7 to 1;
132
+ - the filled button — rest, hovered, pressed — or the accent's text on white is under 4.5 to 1, or the accent's fill and the filled button stop being one colour;
133
+ - the secondary text is under 4.5 to 1 on white or on near-black.
134
+
135
+ The same test holds the motion to three durations and one easing.
136
+
137
+ ### What a host may override
138
+
139
+ 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.
140
+
141
+ **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.
142
+
46
143
  ## Develop
47
144
 
48
145
  Install the dependencies.
@@ -1,5 +1,6 @@
1
1
  import { type CSSProperties } from "react";
2
2
  import { ThemeProviderProps } from "@primer/react";
3
+ import { type ThemeStyles } from "./css/createThemeCSSVars";
3
4
  export interface IDatalayerThemeProviderProps extends Omit<ThemeProviderProps, "theme" | "colorMode"> {
4
5
  /**
5
6
  * Color mode to use.
@@ -27,10 +28,10 @@ export interface IDatalayerThemeProviderProps extends Omit<ThemeProviderProps, "
27
28
  *
28
29
  * Use the `buildThemeStyles` helper from `./css/createThemeCSSVars`
29
30
  * to generate comprehensive overrides from a `ThemeColorDefs` pair.
31
+ *
32
+ * A theme's own stylesheet comes with them (`css`): injected for this
33
+ * provider's element, and for the portal root when this provider owns it.
30
34
  */
31
- themeStyles?: {
32
- light: CSSProperties;
33
- dark: CSSProperties;
34
- };
35
+ themeStyles?: ThemeStyles;
35
36
  }
36
37
  export declare function DatalayerThemeProvider(props: React.PropsWithChildren<IDatalayerThemeProviderProps>): React.JSX.Element;
@@ -3,12 +3,12 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  * Copyright (c) 2023-2025 Datalayer, Inc.
4
4
  * Distributed under the terms of the Modified BSD License.
5
5
  */
6
- import { useEffect, useLayoutEffect, useMemo, useRef, useState, } from "react";
6
+ import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState, } from "react";
7
7
  import { BaseStyles, ThemeProvider } from "@primer/react";
8
8
  import { useSystemColorMode } from "./useSystemColorMode";
9
9
  import { datalayerTheme, datalayerThemeStyles } from "./themes/datalayerTheme";
10
10
  import { systemFontStack } from "./fontStacks";
11
- import { setupPrimerPortals, syncPortalThemeStyles } from "../utils/Portals";
11
+ import { THEME_SCOPE_ATTRIBUTE, injectThemeStylesheet, setupPrimerPortals, syncPortalThemeStyles, } from "../utils/Portals";
12
12
  /**
13
13
  * System sans-serif font stack — shared between `fontFamily` and
14
14
  * the Primer `--fontStack-*` CSS custom properties so that
@@ -135,6 +135,9 @@ export function DatalayerThemeProvider(props) {
135
135
  return () => observer.disconnect();
136
136
  }, []);
137
137
  const nested = themedAncestor !== null;
138
+ // This provider's scope: what its theme's stylesheet is scoped to, and
139
+ // where an outer provider's stylesheet stops.
140
+ const scope = useId().replace(/[^A-Za-z0-9_-]/g, "");
138
141
  const resolvedColorMode = colorMode === "auto"
139
142
  ? systemMode
140
143
  : (colorMode ?? (nested ? (ancestorMode ?? "light") : "light"));
@@ -174,7 +177,13 @@ export function DatalayerThemeProvider(props) {
174
177
  : resolvedColorMode);
175
178
  syncPortalThemeStyles(portalStyles);
176
179
  }, [nested, resolvedColorMode, portalStyles]);
177
- return (_jsx(ThemeProvider, { colorMode: resolvedColorMode, theme: resolvedTheme, ...rest, children: _jsxs(BaseStyles, { style: {
180
+ // The theme's own stylesheet, if it has one: for this provider's element,
181
+ // and for the portal root when this provider owns it (see above). Removed
182
+ // when the theme changes, or the provider goes. A layout effect, so the
183
+ // first paint is already in the theme's shapes.
184
+ const themeCss = styles.css;
185
+ useLayoutEffect(() => injectThemeStylesheet(scope, themeCss, !nested), [scope, themeCss, nested]);
186
+ return (_jsx(ThemeProvider, { colorMode: resolvedColorMode, theme: resolvedTheme, ...rest, children: _jsxs(BaseStyles, { [THEME_SCOPE_ATTRIBUTE]: scope, style: {
178
187
  lineHeight: "1.7",
179
188
  transition: "background-color 0.25s ease, color 0.25s ease",
180
189
  ...typographyVars,
@@ -6,7 +6,7 @@ export { earthColors } from './earthColors';
6
6
  export { sandColors } from './sandColors';
7
7
  export { ivoryColors } from './ivoryColors';
8
8
  export { sunColors } from './sunColors';
9
- export { loopColors, loopAccents, loopAccentNames, loopAccentVars, type LoopAccent, type LoopAccentName, } from './loopColors';
9
+ export { loopColors, loopAccents, loopAccentNames, loopAccentVars, loopAccentStyles, type LoopAccent, type LoopAccentName, } from './loopColors';
10
10
  import type { ThemeVariant } from '../themeRegistry';
11
11
  /**
12
12
  * Themed color palettes — maps each `ThemeVariant` to its corresponding
@@ -10,7 +10,7 @@ export { earthColors } from './earthColors';
10
10
  export { sandColors } from './sandColors';
11
11
  export { ivoryColors } from './ivoryColors';
12
12
  export { sunColors } from './sunColors';
13
- export { loopColors, loopAccents, loopAccentNames, loopAccentVars, } from './loopColors';
13
+ export { loopColors, loopAccents, loopAccentNames, loopAccentVars, loopAccentStyles, } from './loopColors';
14
14
  import { datalayerColors } from './datalayerColors';
15
15
  import { spatialColors } from './spatialColors';
16
16
  import { lovelyColors } from './lovelyColors';
@@ -92,3 +92,11 @@ export declare const loopAccentNames: LoopAccentName[];
92
92
  * application sets on its root to take its colour.
93
93
  */
94
94
  export declare function loopAccentVars(name: LoopAccentName, mode?: 'light' | 'dark'): Record<string, string>;
95
+ /**
96
+ * An application's accent as everything it colours (LOOP T-05): the four
97
+ * `--loop-*` properties of `loopAccentVars`, and Primer's accent fill and
98
+ * filled button — the person's bubble, the assistant's avatar, the one
99
+ * button to press — in the accent with the text that sits on it. Set on an
100
+ * application's root, it wears its own colour; the page around keeps mint.
101
+ */
102
+ export declare function loopAccentStyles(name: LoopAccentName, mode?: 'light' | 'dark'): Record<string, string>;
@@ -130,3 +130,29 @@ export function loopAccentVars(name, mode = 'light') {
130
130
  '--loop-quiet': accent.quiet[mode],
131
131
  };
132
132
  }
133
+ /**
134
+ * An application's accent as everything it colours (LOOP T-05): the four
135
+ * `--loop-*` properties of `loopAccentVars`, and Primer's accent fill and
136
+ * filled button — the person's bubble, the assistant's avatar, the one
137
+ * button to press — in the accent with the text that sits on it. Set on an
138
+ * application's root, it wears its own colour; the page around keeps mint.
139
+ */
140
+ export function loopAccentStyles(name, mode = 'light') {
141
+ const accent = loopAccents[name];
142
+ if (!accent) {
143
+ throw new Error(`There is no accent "${name}"; the accents are ${loopAccentNames.join(', ')}.`);
144
+ }
145
+ return {
146
+ ...loopAccentVars(name, mode),
147
+ '--bgColor-accent-emphasis': accent.accent,
148
+ '--borderColor-accent-emphasis': accent.accent,
149
+ '--button-primary-bgColor-rest': accent.accent,
150
+ '--button-primary-borderColor-rest': accent.accent,
151
+ '--button-primary-bgColor-hover': accent.accent,
152
+ '--button-primary-borderColor-hover': accent.accent,
153
+ '--button-primary-bgColor-active': accent.accent,
154
+ '--button-primary-borderColor-active': accent.accent,
155
+ '--button-primary-fgColor-rest': accent.on,
156
+ '--button-primary-iconColor-rest': accent.on,
157
+ };
158
+ }
@@ -155,6 +155,20 @@ export declare function colorDefsToCSS(defs: ThemeColorDefs, mode?: 'light' | 'd
155
155
  export interface ThemeStyles {
156
156
  light: CSSProperties;
157
157
  dark: CSSProperties;
158
+ /**
159
+ * A stylesheet of the theme's own, for what a custom property cannot say
160
+ * (LOOP T-09): every Primer control takes its corners from one token,
161
+ * `--borderRadius-medium`, which cards and list items take too, so a theme
162
+ * that wants its controls alone in another shape has to say so with
163
+ * selectors.
164
+ *
165
+ * Written relative to `:scope`. The theme provider wraps it in an
166
+ * `@scope` rule for its own element and, when it owns it, for Primer's
167
+ * portal root (menus, overlays, dialogs), stopping at any theme provider
168
+ * nested inside; and removes it when the theme changes. A theme without
169
+ * one gets no stylesheet at all.
170
+ */
171
+ css?: string;
158
172
  }
159
173
  /**
160
174
  * Build a complete `ThemeStyles` object from light / dark
@@ -206,4 +220,6 @@ export declare function buildThemeStyles(light: ThemeColorDefs, dark: ThemeColor
206
220
  light?: Record<string, string>;
207
221
  dark?: Record<string, string>;
208
222
  };
223
+ /** A stylesheet of the theme's own, relative to `:scope`; see `ThemeStyles.css`. */
224
+ css?: string;
209
225
  }): ThemeStyles;
@@ -320,5 +320,6 @@ export function buildThemeStyles(light, dark, options) {
320
320
  ...shapeVars(options?.shape),
321
321
  ...(options?.variables?.dark ?? {}),
322
322
  },
323
+ ...(options?.css ? { css: options.css } : {}),
323
324
  };
324
325
  }
@@ -1,5 +1,5 @@
1
- import { type CSSProperties } from 'react';
2
1
  import { type ColorMode } from './DatalayerBrandThemeProvider';
2
+ import { type ThemeStyles } from './css/createThemeCSSVars';
3
3
  /** Available theme variants. */
4
4
  export type ThemeVariant = 'datalayer' | 'spatial' | 'lovely' | 'matrix' | 'earth' | 'sand' | 'ivory' | 'sun' | 'loop';
5
5
  /** A pair of colours for a two-stop gradient. */
@@ -48,11 +48,11 @@ export interface ThemeConfig {
48
48
  defaultColorMode: ColorMode;
49
49
  /** Primer theme object passed to `<ThemeProvider theme={…}>`. */
50
50
  primerTheme: Record<string, any>;
51
- /** Per-mode CSS-property overrides for `<DatalayerThemeProvider themeStyles={…}>`. */
52
- themeStyles: {
53
- light: CSSProperties;
54
- dark: CSSProperties;
55
- };
51
+ /**
52
+ * Per-mode CSS-property overrides for `<DatalayerThemeProvider themeStyles={…}>`,
53
+ * and the theme's own scoped stylesheet when it has one (`ThemeStyles.css`).
54
+ */
55
+ themeStyles: ThemeStyles;
56
56
  /**
57
57
  * Per-mode gradient pair for card backgrounds, banners, and other
58
58
  * decorative surfaces that need a themed two-colour gradient.
@@ -1142,6 +1142,38 @@ export declare const loopFocusRing: {
1142
1142
  readonly light: string;
1143
1143
  readonly dark: "#636363";
1144
1144
  };
1145
+ /**
1146
+ * The controls, as one family (LOOP T-09): every single-line control is a
1147
+ * pill — a button, a text input, a select, a segmented control (outside and
1148
+ * each segment), a button group's two ends, a toggle switch, an underline
1149
+ * tab's hover. A multi-line text area keeps `--borderRadius-medium`: a pill
1150
+ * of many lines is a lozenge. Menus and overlays keep the card's radius,
1151
+ * `--borderRadius-large`, which the theme already sets.
1152
+ *
1153
+ * Primer takes every control's corners from `--borderRadius-medium`, which
1154
+ * cards and list items take too, so this cannot be a token: it is a
1155
+ * stylesheet, scoped by the theme provider (`ThemeStyles.css`). Primer's
1156
+ * class names are CSS-module hashes (`prc-Button-ButtonBase-c50BI`), so the
1157
+ * selectors match their stable prefix; the toggle switch is a
1158
+ * styled-component, matched by its display name, which styled-components
1159
+ * puts in its class (`ToggleSwitch__SwitchButton-sc-e6gszy-0`). A test reads each of them
1160
+ * back from the installed `@primer/react`.
1161
+ *
1162
+ * Where a control draws more than one element from the token — a segmented
1163
+ * control's track and segments, a button group's ends — the token itself is
1164
+ * set on the control, and Primer's own rules draw the pill.
1165
+ */
1166
+ export declare const loopControlsSelectors: {
1167
+ readonly button: "[class*=\"prc-Button-ButtonBase\"]";
1168
+ readonly textInput: "[class*=\"prc-components-TextInputBaseWrapper\"]";
1169
+ readonly textArea: "[class*=\"prc-Textarea-TextArea\"]";
1170
+ readonly segmentedControl: "[class*=\"prc-SegmentedControl-SegmentedControl\"]";
1171
+ readonly buttonGroup: "[class*=\"prc-ButtonGroup-ButtonGroup\"]";
1172
+ readonly underlineTab: "[class*=\"prc-components-UnderlineItem\"]";
1173
+ readonly toggleTrack: "[class*=\"ToggleSwitch__SwitchButton-\"]";
1174
+ readonly toggleKnob: "[class*=\"ToggleSwitch__ToggleKnob-\"]";
1175
+ };
1176
+ export declare const loopControlsCss: string;
1145
1177
  /** Comprehensive Primer CSS-variable overrides for light & dark mode. */
1146
1178
  export declare const loopThemeStyles: import("../css/createThemeCSSVars").ThemeStyles;
1147
1179
  export default loopTheme;
@@ -373,9 +373,56 @@ export const loopMotionVars = {
373
373
  * the one place it falls short.
374
374
  */
375
375
  export const loopFocusRing = { light: loopColors.loopRing, dark: '#636363' };
376
+ /**
377
+ * The controls, as one family (LOOP T-09): every single-line control is a
378
+ * pill — a button, a text input, a select, a segmented control (outside and
379
+ * each segment), a button group's two ends, a toggle switch, an underline
380
+ * tab's hover. A multi-line text area keeps `--borderRadius-medium`: a pill
381
+ * of many lines is a lozenge. Menus and overlays keep the card's radius,
382
+ * `--borderRadius-large`, which the theme already sets.
383
+ *
384
+ * Primer takes every control's corners from `--borderRadius-medium`, which
385
+ * cards and list items take too, so this cannot be a token: it is a
386
+ * stylesheet, scoped by the theme provider (`ThemeStyles.css`). Primer's
387
+ * class names are CSS-module hashes (`prc-Button-ButtonBase-c50BI`), so the
388
+ * selectors match their stable prefix; the toggle switch is a
389
+ * styled-component, matched by its display name, which styled-components
390
+ * puts in its class (`ToggleSwitch__SwitchButton-sc-e6gszy-0`). A test reads each of them
391
+ * back from the installed `@primer/react`.
392
+ *
393
+ * Where a control draws more than one element from the token — a segmented
394
+ * control's track and segments, a button group's ends — the token itself is
395
+ * set on the control, and Primer's own rules draw the pill.
396
+ */
397
+ export const loopControlsSelectors = {
398
+ button: '[class*="prc-Button-ButtonBase"]',
399
+ textInput: '[class*="prc-components-TextInputBaseWrapper"]',
400
+ textArea: '[class*="prc-Textarea-TextArea"]',
401
+ segmentedControl: '[class*="prc-SegmentedControl-SegmentedControl"]',
402
+ buttonGroup: '[class*="prc-ButtonGroup-ButtonGroup"]',
403
+ underlineTab: '[class*="prc-components-UnderlineItem"]',
404
+ toggleTrack: '[class*="ToggleSwitch__SwitchButton-"]',
405
+ toggleKnob: '[class*="ToggleSwitch__ToggleKnob-"]',
406
+ };
407
+ const sel = loopControlsSelectors;
408
+ export const loopControlsCss = `
409
+ :scope ${sel.button},
410
+ :scope ${sel.textInput}:not(:has(${sel.textArea})),
411
+ :scope ${sel.segmentedControl},
412
+ :scope ${sel.underlineTab},
413
+ :scope ${sel.toggleTrack},
414
+ :scope ${sel.toggleKnob} {
415
+ --borderRadius-medium: var(--loop-radius-control);
416
+ border-radius: var(--loop-radius-control);
417
+ }
418
+ :scope ${sel.buttonGroup} {
419
+ --borderRadius-medium: var(--loop-radius-control);
420
+ }
421
+ `;
376
422
  /** Comprehensive Primer CSS-variable overrides for light & dark mode. */
377
423
  export const loopThemeStyles = buildThemeStyles(loopLight, loopDark, {
378
424
  fontFamily: loopFontFamily,
425
+ css: loopControlsCss,
379
426
  // Rounder than the others: a pill for a control, soft cards, round bubbles.
380
427
  shape: {
381
428
  radiusControl: loopShapeVars['--loop-radius-control'],
@@ -40,4 +40,28 @@ export declare const getPrimerPortalRoot: () => HTMLElement | null;
40
40
  * portals stay in sync whenever theme or color-mode changes.
41
41
  */
42
42
  export declare function syncPortalThemeStyles(styles: CSSProperties): void;
43
+ /**
44
+ * The attribute every theme provider stamps on its own element, its value
45
+ * unique to that provider: what a theme's stylesheet is scoped to, and where
46
+ * it stops — at the element of any provider nested inside.
47
+ */
48
+ export declare const THEME_SCOPE_ATTRIBUTE = "data-datalayer-theme-scope";
49
+ /** The attribute on the `<style>` element a provider injects, its value the provider's scope. */
50
+ export declare const THEME_STYLESHEET_ATTRIBUTE = "data-datalayer-theme-stylesheet";
51
+ /** The selector of a provider's own element, from its scope. */
52
+ export declare const themeScopeSelector: (scope: string) => string;
53
+ /**
54
+ * A theme's stylesheet — written relative to `:scope` — scoped to each of
55
+ * `roots`, and stopping at the element of any other theme provider inside
56
+ * one, so a widget nested in its own theme keeps its own shapes.
57
+ */
58
+ export declare function scopeThemeCss(css: string, roots: string[]): string;
59
+ /**
60
+ * Inject a theme's stylesheet for one provider: scoped to its own element
61
+ * and, when `portal` is true (the provider owns the portal root), to the
62
+ * portal root as well — overlays, menus and dialogs are drawn there, outside
63
+ * the provider's element. Returns what removes it; a theme change calls it
64
+ * before injecting the next theme's. Without `css`, nothing is injected.
65
+ */
66
+ export declare function injectThemeStylesheet(scope: string, css: string | undefined, portal: boolean): () => void;
43
67
  export default setupPrimerPortals;
@@ -188,4 +188,46 @@ export function syncPortalThemeStyles(styles) {
188
188
  target[PORTAL_THEME_KEYS] = tracked;
189
189
  }
190
190
  }
191
+ /* ─── A theme's own stylesheet ───────────────────────────────────────── */
192
+ /**
193
+ * The attribute every theme provider stamps on its own element, its value
194
+ * unique to that provider: what a theme's stylesheet is scoped to, and where
195
+ * it stops — at the element of any provider nested inside.
196
+ */
197
+ export const THEME_SCOPE_ATTRIBUTE = 'data-datalayer-theme-scope';
198
+ /** The attribute on the `<style>` element a provider injects, its value the provider's scope. */
199
+ export const THEME_STYLESHEET_ATTRIBUTE = 'data-datalayer-theme-stylesheet';
200
+ /** The selector of a provider's own element, from its scope. */
201
+ export const themeScopeSelector = (scope) => `[${THEME_SCOPE_ATTRIBUTE}="${scope}"]`;
202
+ /**
203
+ * A theme's stylesheet — written relative to `:scope` — scoped to each of
204
+ * `roots`, and stopping at the element of any other theme provider inside
205
+ * one, so a widget nested in its own theme keeps its own shapes.
206
+ */
207
+ export function scopeThemeCss(css, roots) {
208
+ return roots
209
+ .map((root) => `@scope (${root}) to ([${THEME_SCOPE_ATTRIBUTE}]:not(${root})) {\n${css}\n}`)
210
+ .join('\n');
211
+ }
212
+ /**
213
+ * Inject a theme's stylesheet for one provider: scoped to its own element
214
+ * and, when `portal` is true (the provider owns the portal root), to the
215
+ * portal root as well — overlays, menus and dialogs are drawn there, outside
216
+ * the provider's element. Returns what removes it; a theme change calls it
217
+ * before injecting the next theme's. Without `css`, nothing is injected.
218
+ */
219
+ export function injectThemeStylesheet(scope, css, portal) {
220
+ if (!css || typeof document === 'undefined') {
221
+ return () => undefined;
222
+ }
223
+ const roots = [themeScopeSelector(scope)];
224
+ if (portal) {
225
+ roots.push(`#${PRIMER_PORTAL_ROOT_ID}`);
226
+ }
227
+ const style = document.createElement('style');
228
+ style.setAttribute(THEME_STYLESHEET_ATTRIBUTE, scope);
229
+ style.textContent = scopeThemeCss(css, roots);
230
+ document.head.appendChild(style);
231
+ return () => style.remove();
232
+ }
191
233
  export default setupPrimerPortals;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datalayer/primer-addons",
3
- "version": "1.0.30",
3
+ "version": "1.0.33",
4
4
  "license": "BSD-3-Clause",
5
5
  "scripts": {
6
6
  "build": "npm run build:lib",
@@ -50,6 +50,7 @@
50
50
  "devDependencies": {
51
51
  "react": "19.2.8",
52
52
  "react-dom": "19.2.8",
53
+ "jsdom": "^26.1.0",
53
54
  "rimraf": "^6.0.1",
54
55
  "vitest": "^3.2.7",
55
56
  "@storybook/addon-docs": "^9.0.15",