@datalayer/primer-addons 1.0.44 → 1.0.45

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,5 @@
1
- import { type ReactElement } from 'react';
2
- import { type ColorMode, type ThemeVariant } from '../../theme';
1
+ import { type ReactElement } from "react";
2
+ import { type ColorMode, type ThemeVariant } from "../../theme";
3
3
  export interface AppearanceControlsProps {
4
4
  colorMode: ColorMode;
5
5
  themeVariant: ThemeVariant;
@@ -11,6 +11,12 @@ export interface AppearanceControlsProps {
11
11
  showThemeChooser?: boolean;
12
12
  /** With the chooser, the chosen theme's name, description and preview under its swatches. Default: swatches only. */
13
13
  showThemePreviews?: boolean;
14
+ /**
15
+ * The most theme swatches on one line; every line is centred, so a shorter
16
+ * last line sits in the middle under the full ones. Undefined, they wrap
17
+ * freely to the width given, as before.
18
+ */
19
+ themesPerLine?: number;
14
20
  }
15
- export declare function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showColorMode, showThemeChooser, showThemePreviews, }: AppearanceControlsProps): ReactElement;
21
+ export declare function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showColorMode, showThemeChooser, showThemePreviews, themesPerLine, }: AppearanceControlsProps): ReactElement;
16
22
  export default AppearanceControls;
@@ -3,117 +3,131 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
3
3
  * Copyright (c) 2023-2025 Datalayer, Inc.
4
4
  * Distributed under the terms of the Modified BSD License.
5
5
  */
6
- import { useState } from 'react';
7
- import { SegmentedControl, Tooltip } from '@primer/react';
8
- import { MoonIcon, SunIcon, DeviceDesktopIcon, } from '@primer/octicons-react';
9
- import { Box } from '../box/Box';
10
- import { themeVariants, themeConfigs, } from '../../theme';
11
- import { ThemePreviewCard } from './ThemePreviewCard';
12
- export function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showColorMode = true, showThemeChooser = true, showThemePreviews = false, }) {
6
+ import { useState } from "react";
7
+ import { SegmentedControl, Tooltip } from "@primer/react";
8
+ import { MoonIcon, SunIcon, DeviceDesktopIcon } from "@primer/octicons-react";
9
+ import { Box } from "../box/Box";
10
+ import { themeVariants, themeConfigs, } from "../../theme";
11
+ import { ThemePreviewCard } from "./ThemePreviewCard";
12
+ export function AppearanceControls({ colorMode, themeVariant, onColorModeChange, onThemeChange, showColorMode = true, showThemeChooser = true, showThemePreviews = false, themesPerLine, }) {
13
+ // At most `themesPerLine` swatches on a line when given, each line centred,
14
+ // so a shorter last line sits in the middle under the full ones; one line
15
+ // wrapping freely otherwise. Explicit lines rather than a width: the
16
+ // tooltips' hidden siblings take gaps of their own.
17
+ const swatchLines = themesPerLine !== undefined && themesPerLine > 0
18
+ ? Array.from({ length: Math.ceil(themeVariants.length / themesPerLine) }, (_, line) => themeVariants.slice(line * themesPerLine, (line + 1) * themesPerLine))
19
+ : [[...themeVariants]];
20
+ const swatchRow = { display: "grid", rowGap: 2 };
21
+ const swatchLine = {
22
+ display: "flex",
23
+ justifyContent: "center",
24
+ flexWrap: "wrap",
25
+ gap: 2,
26
+ };
13
27
  const [isPreviewOpen, setIsPreviewOpen] = useState(false);
14
28
  const selectedTheme = themeConfigs[themeVariant];
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
- const modes = ['light', 'dark', 'auto'];
29
+ 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) => {
30
+ const modes = ["light", "dark", "auto"];
17
31
  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" })] }) })) : null, !showThemeChooser ? null : showThemePreviews ? (_jsxs(Box, { sx: {
32
+ }, 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
33
  px: 2,
20
34
  // First in the controls when the colour mode is hidden.
21
35
  pt: showColorMode ? 0 : 2,
22
36
  pb: 2,
23
- display: 'grid',
37
+ display: "grid",
24
38
  gap: 2,
25
39
  }, children: [_jsx(Box, { sx: {
26
- display: 'flex',
27
- justifyContent: 'center',
40
+ ...swatchRow,
28
41
  gap: 2,
29
- flexWrap: 'wrap',
30
- }, children: themeVariants.map((variant) => {
31
- const cfg = themeConfigs[variant];
32
- const isSelected = themeVariant === variant;
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: {
34
- width: 24,
35
- height: 24,
36
- flexShrink: 0,
37
- borderRadius: '50%',
38
- backgroundColor: cfg.brandColor,
39
- border: '2px solid',
40
- borderColor: isSelected ? 'accent.fg' : 'border.default',
41
- cursor: 'pointer',
42
- padding: 0,
43
- outline: 'none',
44
- transition: 'border-color 0.15s ease',
45
- boxShadow: isSelected
46
- ? '0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))'
47
- : 'none',
48
- '&:hover': {
49
- borderColor: 'accent.fg',
50
- },
51
- '&:focus-visible': {
52
- boxShadow: '0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))',
53
- },
54
- } }) }, variant));
55
- }) }), _jsxs(Box, { sx: {
42
+ }, children: swatchLines.map((line) => (_jsx(Box, { sx: swatchLine, children: line.map((variant) => {
43
+ const cfg = themeConfigs[variant];
44
+ const isSelected = themeVariant === variant;
45
+ 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: {
46
+ width: 24,
47
+ height: 24,
48
+ flexShrink: 0,
49
+ borderRadius: "50%",
50
+ backgroundColor: cfg.brandColor,
51
+ border: "2px solid",
52
+ borderColor: isSelected
53
+ ? "accent.fg"
54
+ : "border.default",
55
+ cursor: "pointer",
56
+ padding: 0,
57
+ outline: "none",
58
+ transition: "border-color 0.15s ease",
59
+ boxShadow: isSelected
60
+ ? "0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))"
61
+ : "none",
62
+ "&:hover": {
63
+ borderColor: "accent.fg",
64
+ },
65
+ "&:focus-visible": {
66
+ boxShadow: "0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))",
67
+ },
68
+ } }) }, variant));
69
+ }) }, line.join()))) }), _jsxs(Box, { sx: {
56
70
  px: 1,
57
- display: 'grid',
71
+ display: "grid",
58
72
  gap: 1,
59
- }, children: [_jsx(Box, { sx: { fontSize: 1, fontWeight: 'semibold', color: 'fg.default' }, children: selectedTheme.label }), _jsx(Box, { sx: { fontSize: 0, color: 'fg.muted', lineHeight: 1.5 }, children: selectedTheme.description }), _jsx(Box, { as: "button", type: "button", onClick: () => setIsPreviewOpen((open) => !open), "aria-expanded": isPreviewOpen, sx: {
73
+ }, children: [_jsx(Box, { sx: { fontSize: 1, fontWeight: "semibold", color: "fg.default" }, children: selectedTheme.label }), _jsx(Box, { sx: { fontSize: 0, color: "fg.muted", lineHeight: 1.5 }, children: selectedTheme.description }), _jsx(Box, { as: "button", type: "button", onClick: () => setIsPreviewOpen((open) => !open), "aria-expanded": isPreviewOpen, sx: {
60
74
  mt: 1,
61
- width: 'fit-content',
75
+ width: "fit-content",
62
76
  px: 2,
63
- py: '4px',
77
+ py: "4px",
64
78
  borderRadius: 2,
65
- border: '1px solid',
66
- borderColor: 'border.default',
67
- bg: 'canvas.default',
68
- color: 'fg.default',
79
+ border: "1px solid",
80
+ borderColor: "border.default",
81
+ bg: "canvas.default",
82
+ color: "fg.default",
69
83
  fontSize: 0,
70
- fontWeight: 'semibold',
71
- cursor: 'pointer',
72
- '&:hover': {
73
- borderColor: 'accent.fg',
74
- color: 'accent.fg',
84
+ fontWeight: "semibold",
85
+ cursor: "pointer",
86
+ "&:hover": {
87
+ borderColor: "accent.fg",
88
+ color: "accent.fg",
75
89
  },
76
- '&:focus-visible': {
77
- outline: '2px solid',
78
- outlineColor: 'accent.fg',
90
+ "&:focus-visible": {
91
+ outline: "2px solid",
92
+ outlineColor: "accent.fg",
79
93
  outlineOffset: 1,
80
94
  },
81
- }, children: isPreviewOpen ? 'Hide preview' : 'Show preview' })] }), isPreviewOpen ? (_jsx(ThemePreviewCard, { variant: themeVariant, colorMode: colorMode, selected: true, compact: true, showDescription: true })) : null] })) : (_jsx(Box, { sx: {
95
+ }, children: isPreviewOpen ? "Hide preview" : "Show preview" })] }), isPreviewOpen ? (_jsx(ThemePreviewCard, { variant: themeVariant, colorMode: colorMode, selected: true, compact: true, showDescription: true })) : null] })) : (_jsx(Box, { sx: {
82
96
  px: 2,
83
97
  // First in the controls when the colour mode is hidden.
84
98
  pt: showColorMode ? 0 : 2,
85
99
  pb: 2,
86
- display: 'flex',
87
- justifyContent: 'center',
88
100
  // Narrow, the swatches go on a second line rather than shrink.
89
- flexWrap: 'wrap',
101
+ ...swatchRow,
90
102
  gap: 2,
91
- }, children: themeVariants.map((variant) => {
92
- const cfg = themeConfigs[variant];
93
- const isSelected = themeVariant === variant;
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: {
95
- width: 24,
96
- height: 24,
97
- // A circle at any width: never squeezed into an oval.
98
- flexShrink: 0,
99
- borderRadius: '50%',
100
- backgroundColor: cfg.brandColor,
101
- border: '2px solid',
102
- borderColor: isSelected ? 'accent.fg' : 'border.default',
103
- cursor: 'pointer',
104
- padding: 0,
105
- outline: 'none',
106
- transition: 'border-color 0.15s ease',
107
- boxShadow: isSelected
108
- ? '0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))'
109
- : 'none',
110
- '&:hover': {
111
- borderColor: 'accent.fg',
112
- },
113
- '&:focus-visible': {
114
- boxShadow: '0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))',
115
- },
116
- } }) }, variant));
117
- }) }))] }));
103
+ }, children: swatchLines.map((line) => (_jsx(Box, { sx: swatchLine, children: line.map((variant) => {
104
+ const cfg = themeConfigs[variant];
105
+ const isSelected = themeVariant === variant;
106
+ return (_jsx(Tooltip, { text: cfg.label, direction: "s", children: _jsx(Box, { as: "button", "aria-label": cfg.label, "aria-pressed": isSelected, onClick: () => onThemeChange(variant), sx: {
107
+ width: 24,
108
+ height: 24,
109
+ // A circle at any width: never squeezed into an oval.
110
+ flexShrink: 0,
111
+ borderRadius: "50%",
112
+ backgroundColor: cfg.brandColor,
113
+ border: "2px solid",
114
+ borderColor: isSelected
115
+ ? "accent.fg"
116
+ : "border.default",
117
+ cursor: "pointer",
118
+ padding: 0,
119
+ outline: "none",
120
+ transition: "border-color 0.15s ease",
121
+ boxShadow: isSelected
122
+ ? "0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))"
123
+ : "none",
124
+ "&:hover": {
125
+ borderColor: "accent.fg",
126
+ },
127
+ "&:focus-visible": {
128
+ boxShadow: "0 0 0 2px var(--bgColor-accent-muted, rgba(9,105,218,0.3))",
129
+ },
130
+ } }) }, variant));
131
+ }) }, line.join()))) }))] }));
118
132
  }
119
133
  export default AppearanceControls;
@@ -8,6 +8,13 @@ declare const meta: {
8
8
  layout: string;
9
9
  };
10
10
  argTypes: {
11
+ themesPerLine: {
12
+ control: {
13
+ type: "number";
14
+ min: number;
15
+ max: number;
16
+ };
17
+ };
11
18
  colorMode: {
12
19
  options: string[];
13
20
  control: "radio";
@@ -14,6 +14,9 @@ const meta = {
14
14
  layout: "fullscreen",
15
15
  },
16
16
  argTypes: {
17
+ themesPerLine: {
18
+ control: { type: "number", min: 1, max: 12 },
19
+ },
17
20
  colorMode: {
18
21
  options: ["light", "dark", "auto"],
19
22
  control: "radio",
@@ -1,4 +1,5 @@
1
1
  import { type ReactElement } from 'react';
2
+ import { type HoverOverlayPlacement } from './ColorModeCircle';
2
3
  import type { ColorMode, ThemeVariant } from '../../theme';
3
4
  export interface AppearanceMenuProps {
4
5
  /** Current color mode. */
@@ -14,9 +15,11 @@ export interface AppearanceMenuProps {
14
15
  /** Visual shape of the trigger control. */
15
16
  shape?: 'square' | 'circle';
16
17
  /** Overlay placement relative to the circle. */
17
- hoverOverlayPlacement?: 'bottom-start' | 'bottom-end';
18
+ hoverOverlayPlacement?: HoverOverlayPlacement;
18
19
  /** Whether the overlay shows the live theme previews. */
19
20
  showThemePreviews?: boolean;
21
+ /** The most theme swatches on one line in the overlay; undefined, they wrap freely. */
22
+ themesPerLine?: number;
20
23
  }
21
24
  /**
22
25
  * A single color-mode circle that reveals, on hover, an appearance overlay
@@ -26,5 +29,5 @@ export interface AppearanceMenuProps {
26
29
  * {@link AppearanceControls} overlay so the whole appearance menu can be
27
30
  * dropped into any header with a single component.
28
31
  */
29
- export declare function AppearanceMenu({ colorMode, themeVariant, onColorModeChange, onThemeChange, size, shape, hoverOverlayPlacement, showThemePreviews, }: AppearanceMenuProps): ReactElement;
32
+ export declare function AppearanceMenu({ colorMode, themeVariant, onColorModeChange, onThemeChange, size, shape, hoverOverlayPlacement, showThemePreviews, themesPerLine, }: AppearanceMenuProps): ReactElement;
30
33
  export default AppearanceMenu;
@@ -9,7 +9,7 @@ import { AppearanceControls } from './AppearanceControls';
9
9
  * {@link AppearanceControls} overlay so the whole appearance menu can be
10
10
  * dropped into any header with a single component.
11
11
  */
12
- export function AppearanceMenu({ colorMode, themeVariant, onColorModeChange, onThemeChange, size = 26, shape = 'square', hoverOverlayPlacement = 'bottom-end', showThemePreviews = true, }) {
13
- return (_jsx(ColorModeCircle, { colorMode: colorMode, onColorModeChange: onColorModeChange, cycleOnClick: false, themeVariant: themeVariant, hoverOverlayPlacement: hoverOverlayPlacement, shape: shape, size: size, hoverOverlay: _jsx(AppearanceControls, { colorMode: colorMode, themeVariant: themeVariant, onColorModeChange: onColorModeChange, onThemeChange: onThemeChange, showThemePreviews: showThemePreviews }) }));
12
+ export function AppearanceMenu({ colorMode, themeVariant, onColorModeChange, onThemeChange, size = 26, shape = 'square', hoverOverlayPlacement = 'bottom-end', showThemePreviews = true, themesPerLine, }) {
13
+ return (_jsx(ColorModeCircle, { colorMode: colorMode, onColorModeChange: onColorModeChange, cycleOnClick: false, themeVariant: themeVariant, hoverOverlayPlacement: hoverOverlayPlacement, shape: shape, size: size, hoverOverlay: _jsx(AppearanceControls, { colorMode: colorMode, themeVariant: themeVariant, onColorModeChange: onColorModeChange, onThemeChange: onThemeChange, showThemePreviews: showThemePreviews, themesPerLine: themesPerLine }) }));
14
14
  }
15
15
  export default AppearanceMenu;
@@ -8,6 +8,13 @@ declare const meta: {
8
8
  layout: string;
9
9
  };
10
10
  argTypes: {
11
+ themesPerLine: {
12
+ control: {
13
+ type: "number";
14
+ min: number;
15
+ max: number;
16
+ };
17
+ };
11
18
  colorMode: {
12
19
  options: string[];
13
20
  control: "radio";
@@ -14,6 +14,9 @@ const meta = {
14
14
  layout: "fullscreen",
15
15
  },
16
16
  argTypes: {
17
+ themesPerLine: {
18
+ control: { type: "number", min: 1, max: 12 },
19
+ },
17
20
  colorMode: {
18
21
  options: ["light", "dark", "auto"],
19
22
  control: "radio",
@@ -27,7 +30,7 @@ const meta = {
27
30
  control: "radio",
28
31
  },
29
32
  hoverOverlayPlacement: {
30
- options: ["bottom-start", "bottom-end"],
33
+ options: ["bottom-start", "bottom-end", "right-start"],
31
34
  control: "radio",
32
35
  },
33
36
  },
@@ -1,6 +1,7 @@
1
1
  import { type ReactElement, type ReactNode } from 'react';
2
2
  import { type ColorMode, type ThemeVariant } from '../../theme';
3
- type HoverOverlayPlacement = 'bottom-start' | 'bottom-end';
3
+ /** Where the hover overlay opens: under the circle (aligned at its start or end), or to its right, aligned at its top. */
4
+ export type HoverOverlayPlacement = 'bottom-start' | 'bottom-end' | 'right-start';
4
5
  type ColorModeCircleShape = 'square' | 'circle';
5
6
  export interface ColorModeCircleProps {
6
7
  /** Current color mode. */
@@ -39,6 +39,7 @@ export function ColorModeCircle({ colorMode, onColorModeChange, size = 28, hover
39
39
  const [isHoverOpen, setIsHoverOpen] = useState(false);
40
40
  const closeTimerRef = useRef(null);
41
41
  const isBottomStart = hoverOverlayPlacement === 'bottom-start';
42
+ const isRight = hoverOverlayPlacement === 'right-start';
42
43
  const themeConfig = themeVariant ? themeConfigs[themeVariant] : undefined;
43
44
  const themeBorderColor = themeConfig?.brandColor;
44
45
  const hasThemeColor = Boolean(themeBorderColor);
@@ -113,9 +114,10 @@ export function ColorModeCircle({ colorMode, onColorModeChange, size = 28, hover
113
114
  }
114
115
  return (_jsxs(Box, { sx: { position: 'relative', display: 'inline-flex', alignItems: 'center' }, onMouseEnter: openHoverOverlay, onMouseLeave: scheduleCloseHoverOverlay, children: [circle, _jsx(Box, { sx: {
115
116
  position: 'absolute',
116
- top: `calc(100% + 8px)`,
117
- left: isBottomStart ? 0 : 'auto',
118
- right: isBottomStart ? 'auto' : 0,
117
+ // To the right: beside the circle, its top level with the circle's.
118
+ top: isRight ? 0 : `calc(100% + 8px)`,
119
+ left: isRight ? `calc(100% + 8px)` : isBottomStart ? 0 : 'auto',
120
+ right: isRight || isBottomStart ? 'auto' : 0,
119
121
  zIndex: 220,
120
122
  minWidth: 340,
121
123
  bg: 'canvas.overlay',
@@ -124,7 +126,7 @@ export function ColorModeCircle({ colorMode, onColorModeChange, size = 28, hover
124
126
  borderRadius: 2,
125
127
  boxShadow: 'shadow.large',
126
128
  opacity: isHoverOpen ? 1 : 0,
127
- transform: isHoverOpen ? 'translateY(0)' : 'translateY(-6px)',
129
+ transform: isHoverOpen ? 'none' : isRight ? 'translateX(-6px)' : 'translateY(-6px)',
128
130
  pointerEvents: isHoverOpen ? 'auto' : 'none',
129
131
  transition: 'opacity 0.16s ease, transform 0.16s ease',
130
132
  }, children: hoverOverlay })] }));
@@ -27,7 +27,7 @@ const meta = {
27
27
  control: "radio",
28
28
  },
29
29
  hoverOverlayPlacement: {
30
- options: ["bottom-start", "bottom-end"],
30
+ options: ["bottom-start", "bottom-end", "right-start"],
31
31
  control: "radio",
32
32
  },
33
33
  hoverOverlay: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datalayer/primer-addons",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "license": "BSD-3-Clause",
5
5
  "scripts": {
6
6
  "build": "npm run build:lib",