@datalayer/primer-addons 1.0.30 → 1.0.32

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.
@@ -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,
@@ -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.32",
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",