@primer/react 0.0.0-20260807141221 → 0.0.0-20260809202139
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/CHANGELOG.md +6 -3
- package/dist/Portal/Portal.js +29 -91
- package/dist/ThemeContext.d.ts +20 -0
- package/dist/ThemeProvider.d.ts +3 -0
- package/dist/ThemeProvider.js +39 -129
- package/dist/hooks/useMenuInitialFocus.js +57 -72
- package/dist/internal/components/ThemeProviderBase.js +50 -0
- package/dist/internal/hooks/useThemeProvider.js +71 -0
- package/dist/legacy-theme/ts/color-schemes.js +3942 -3942
- package/dist/next/ThemeProvider.d.ts +7 -0
- package/dist/next/ThemeProvider.js +35 -0
- package/dist/next/index.d.ts +4 -1
- package/dist/next/index.js +3 -1
- package/dist/theme.d.ts +16 -54
- package/dist/useTheme.d.ts +2 -14
- package/generated/hooks.json +2 -7
- package/package.json +6 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# @primer/react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-20260809202139
|
|
4
4
|
|
|
5
|
-
###
|
|
5
|
+
### Minor Changes
|
|
6
6
|
|
|
7
|
-
- [#
|
|
7
|
+
- [#8212](https://github.com/primer/react/pull/8212) [`6a4c131`](https://github.com/primer/react/commit/6a4c1319a70a5f3b5abc7b81b044bd26d042aa75) Thanks [@mattcosta7](https://github.com/mattcosta7)! - ThemeProvider: Add a CSS-variable-based implementation to `@primer/react/next`
|
|
8
|
+
and deprecate the legacy JavaScript theme implementation.
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
8
11
|
|
|
9
12
|
- Fake entry to force publishing
|
|
10
13
|
|
package/dist/Portal/Portal.js
CHANGED
|
@@ -1,105 +1,43 @@
|
|
|
1
1
|
import useIsomorphicLayoutEffect from "../utils/useIsomorphicLayoutEffect.js";
|
|
2
2
|
import { PortalContext } from "./PortalContext.js";
|
|
3
3
|
import { DEFAULT_PORTAL_CONTAINER_NAME, ensureDefaultPortal, getPortalRoot } from "./portalRoot.js";
|
|
4
|
-
import {
|
|
5
|
-
import React, { useContext, useSyncExternalStore } from "react";
|
|
4
|
+
import React, { useContext } from "react";
|
|
6
5
|
import { createPortal } from "react-dom";
|
|
7
6
|
//#region src/Portal/Portal.tsx
|
|
8
|
-
const subscribe = () => () => {};
|
|
9
|
-
const getSnapshot = () => true;
|
|
10
|
-
const getServerSnapshot = () => false;
|
|
11
|
-
/**
|
|
12
|
-
* Returns `true` only when React is rendering on the client, and not as part of
|
|
13
|
-
* hydrating server-rendered markup.
|
|
14
|
-
*
|
|
15
|
-
* `Portal` needs this because React's server renderer throws when it encounters a
|
|
16
|
-
* portal, and portaled content is never part of the server-rendered markup.
|
|
17
|
-
*
|
|
18
|
-
* `useSyncExternalStore` is used rather than a `useEffect`/`useState` "is mounted"
|
|
19
|
-
* flag because it only defers the portal for the renders that need it: it returns
|
|
20
|
-
* `false` while server rendering and for the hydration pass (keeping hydration
|
|
21
|
-
* consistent with the server markup), but `true` from the very first render of a
|
|
22
|
-
* client-only render. That way client-rendered portals — the overwhelmingly common
|
|
23
|
-
* case for overlays, dialogs and menus — still mount their children in the same
|
|
24
|
-
* commit as the `Portal` itself, so refs into portaled content are populated by the
|
|
25
|
-
* time the parent's layout effects run.
|
|
26
|
-
*/
|
|
27
|
-
function useIsClientRender() {
|
|
28
|
-
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
29
|
-
}
|
|
30
7
|
/**
|
|
31
8
|
* Creates a React Portal, placing all children in a separate physical DOM root node.
|
|
32
9
|
* @see https://reactjs.org/docs/portals.html
|
|
33
10
|
*/
|
|
34
|
-
const Portal = (
|
|
35
|
-
const $ = c(12);
|
|
36
|
-
const { children, onMount, containerName: _containerName } = t0;
|
|
11
|
+
const Portal = ({ children, onMount, containerName: _containerName }) => {
|
|
37
12
|
const { portalContainerName } = useContext(PortalContext);
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
$[0] = onMount;
|
|
46
|
-
$[1] = t1;
|
|
47
|
-
} else t1 = $[1];
|
|
48
|
-
useIsomorphicLayoutEffect(t1);
|
|
49
|
-
const [element] = React.useState(_temp);
|
|
50
|
-
let t2;
|
|
51
|
-
let t3;
|
|
52
|
-
if ($[2] !== _containerName || $[3] !== element || $[4] !== isClientRender || $[5] !== portalContainerName) {
|
|
53
|
-
t2 = () => {
|
|
54
|
-
var _onMountRef$current;
|
|
55
|
-
if (!element || !isClientRender) return;
|
|
56
|
-
let containerName = _containerName !== null && _containerName !== void 0 ? _containerName : portalContainerName;
|
|
57
|
-
if (containerName === void 0) {
|
|
58
|
-
containerName = DEFAULT_PORTAL_CONTAINER_NAME;
|
|
59
|
-
ensureDefaultPortal();
|
|
60
|
-
}
|
|
61
|
-
const parentElement = getPortalRoot(containerName);
|
|
62
|
-
if (!parentElement) throw new Error(`Portal container '${containerName}' is not yet registered. Container must be registered with registerPortalRoot before use.`);
|
|
63
|
-
parentElement.appendChild(element);
|
|
64
|
-
(_onMountRef$current = onMountRef.current) === null || _onMountRef$current === void 0 || _onMountRef$current.call(onMountRef);
|
|
65
|
-
return () => {
|
|
66
|
-
parentElement.removeChild(element);
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
t3 = [
|
|
70
|
-
element,
|
|
71
|
-
isClientRender,
|
|
72
|
-
_containerName,
|
|
73
|
-
portalContainerName
|
|
74
|
-
];
|
|
75
|
-
$[2] = _containerName;
|
|
76
|
-
$[3] = element;
|
|
77
|
-
$[4] = isClientRender;
|
|
78
|
-
$[5] = portalContainerName;
|
|
79
|
-
$[6] = t2;
|
|
80
|
-
$[7] = t3;
|
|
81
|
-
} else {
|
|
82
|
-
t2 = $[6];
|
|
83
|
-
t3 = $[7];
|
|
13
|
+
const elementRef = React.useRef(null);
|
|
14
|
+
if (!elementRef.current) {
|
|
15
|
+
const div = document.createElement("div");
|
|
16
|
+
div.setAttribute("data-component", "Portal");
|
|
17
|
+
div.style.position = "relative";
|
|
18
|
+
div.style.zIndex = "1";
|
|
19
|
+
elementRef.current = div;
|
|
84
20
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
21
|
+
const element = elementRef.current;
|
|
22
|
+
useIsomorphicLayoutEffect(() => {
|
|
23
|
+
let containerName = _containerName !== null && _containerName !== void 0 ? _containerName : portalContainerName;
|
|
24
|
+
if (containerName === void 0) {
|
|
25
|
+
containerName = DEFAULT_PORTAL_CONTAINER_NAME;
|
|
26
|
+
ensureDefaultPortal();
|
|
27
|
+
}
|
|
28
|
+
const parentElement = getPortalRoot(containerName);
|
|
29
|
+
if (!parentElement) throw new Error(`Portal container '${containerName}' is not yet registered. Container must be registered with registerPortalRoot before use.`);
|
|
30
|
+
parentElement.appendChild(element);
|
|
31
|
+
onMount === null || onMount === void 0 || onMount();
|
|
32
|
+
return () => {
|
|
33
|
+
parentElement.removeChild(element);
|
|
34
|
+
};
|
|
35
|
+
}, [
|
|
36
|
+
element,
|
|
37
|
+
_containerName,
|
|
38
|
+
portalContainerName
|
|
39
|
+
]);
|
|
40
|
+
return /*#__PURE__*/ createPortal(children, element);
|
|
95
41
|
};
|
|
96
|
-
function _temp() {
|
|
97
|
-
if (typeof document === "undefined") return null;
|
|
98
|
-
const div = document.createElement("div");
|
|
99
|
-
div.setAttribute("data-component", "Portal");
|
|
100
|
-
div.style.position = "relative";
|
|
101
|
-
div.style.zIndex = "1";
|
|
102
|
-
return div;
|
|
103
|
-
}
|
|
104
42
|
//#endregion
|
|
105
43
|
export { Portal };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ColorMode, ColorModeWithAuto, Theme } from "./ThemeProvider.js";
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/ThemeContext.d.ts
|
|
5
|
+
interface ThemeContextValue {
|
|
6
|
+
/** @deprecated Use `ThemeProvider` and `useTheme` from `@primer/react/next` to migrate away from JavaScript theme values. */
|
|
7
|
+
theme?: Theme;
|
|
8
|
+
colorScheme?: string;
|
|
9
|
+
colorMode?: ColorModeWithAuto;
|
|
10
|
+
resolvedColorMode?: ColorMode;
|
|
11
|
+
/** @deprecated Use `ThemeProvider` and `useTheme` from `@primer/react/next` to migrate away from JavaScript theme values. */
|
|
12
|
+
resolvedColorScheme?: string;
|
|
13
|
+
dayScheme?: string;
|
|
14
|
+
nightScheme?: string;
|
|
15
|
+
setColorMode: React.Dispatch<React.SetStateAction<ColorModeWithAuto>>;
|
|
16
|
+
setDayScheme: React.Dispatch<React.SetStateAction<string>>;
|
|
17
|
+
setNightScheme: React.Dispatch<React.SetStateAction<string>>;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { ThemeContextValue };
|
package/dist/ThemeProvider.d.ts
CHANGED
|
@@ -21,6 +21,9 @@ type ThemeProviderProps = {
|
|
|
21
21
|
*/
|
|
22
22
|
contextOnly?: boolean;
|
|
23
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated Use `ThemeProvider` from `@primer/react/next` to migrate away from JavaScript theme values.
|
|
26
|
+
*/
|
|
24
27
|
declare const ThemeProvider: React.FC<React.PropsWithChildren<ThemeProviderProps>>;
|
|
25
28
|
//#endregion
|
|
26
29
|
export { ColorMode, ColorModeWithAuto, Theme, ThemeProvider, ThemeProvider as default, ThemeProviderProps };
|
package/dist/ThemeProvider.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import theme from "./theme.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { useTheme } from "./useTheme.js";
|
|
2
|
+
import { ThemeProviderBase } from "./internal/components/ThemeProviderBase.js";
|
|
3
|
+
import { useThemeProvider } from "./internal/hooks/useThemeProvider.js";
|
|
5
4
|
import { c } from "react-compiler-runtime";
|
|
6
5
|
import { jsx } from "react/jsx-runtime";
|
|
7
|
-
import
|
|
6
|
+
import "react";
|
|
8
7
|
import deepmerge from "deepmerge";
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
//#region src/ThemeProvider.tsx
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `ThemeProvider` from `@primer/react/next` to migrate away from JavaScript theme values.
|
|
11
|
+
*/
|
|
11
12
|
const ThemeProvider = (t0) => {
|
|
12
|
-
var
|
|
13
|
-
const $ = c(
|
|
13
|
+
var _parentValue$theme;
|
|
14
|
+
const $ = c(14);
|
|
14
15
|
let children;
|
|
15
16
|
let props;
|
|
16
17
|
if ($[0] !== t0) {
|
|
@@ -22,134 +23,43 @@ const ThemeProvider = (t0) => {
|
|
|
22
23
|
children = $[1];
|
|
23
24
|
props = $[2];
|
|
24
25
|
}
|
|
25
|
-
const {
|
|
26
|
-
const theme$1 =
|
|
27
|
-
const [colorMode, setColorMode] = useSyncedState((_ref = (_props$colorMode = props.colorMode) !== null && _props$colorMode !== void 0 ? _props$colorMode : fallbackColorMode) !== null && _ref !== void 0 ? _ref : "day");
|
|
28
|
-
const [dayScheme, setDayScheme] = useSyncedState((_ref2 = (_props$dayScheme = props.dayScheme) !== null && _props$dayScheme !== void 0 ? _props$dayScheme : fallbackDayScheme) !== null && _ref2 !== void 0 ? _ref2 : defaultDayScheme);
|
|
29
|
-
const [nightScheme, setNightScheme] = useSyncedState((_ref3 = (_props$nightScheme = props.nightScheme) !== null && _props$nightScheme !== void 0 ? _props$nightScheme : fallbackNightScheme) !== null && _ref3 !== void 0 ? _ref3 : defaultNightScheme);
|
|
30
|
-
const systemColorMode = useSystemColorMode();
|
|
31
|
-
let resolvedColorMode;
|
|
26
|
+
const { parentValue, value } = useThemeProvider(props);
|
|
27
|
+
const theme$1 = (_parentValue$theme = parentValue.theme) !== null && _parentValue$theme !== void 0 ? _parentValue$theme : theme;
|
|
32
28
|
let t1;
|
|
33
|
-
if ($[3] !==
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
$[
|
|
37
|
-
$[
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
$[7] = resolvedColorMode;
|
|
41
|
-
$[8] = t1;
|
|
42
|
-
} else {
|
|
43
|
-
resolvedColorMode = $[7];
|
|
44
|
-
t1 = $[8];
|
|
45
|
-
}
|
|
46
|
-
const colorScheme = t1;
|
|
29
|
+
if ($[3] !== theme$1 || $[4] !== value.colorScheme) {
|
|
30
|
+
t1 = applyColorScheme(theme$1, value.colorScheme);
|
|
31
|
+
$[3] = theme$1;
|
|
32
|
+
$[4] = value.colorScheme;
|
|
33
|
+
$[5] = t1;
|
|
34
|
+
} else t1 = $[5];
|
|
35
|
+
const { resolvedTheme, resolvedColorScheme } = t1;
|
|
47
36
|
let t2;
|
|
48
|
-
if ($[
|
|
49
|
-
t2 =
|
|
50
|
-
|
|
51
|
-
$[10] = theme$1;
|
|
52
|
-
$[11] = t2;
|
|
53
|
-
} else t2 = $[11];
|
|
54
|
-
const { resolvedTheme, resolvedColorScheme } = t2;
|
|
55
|
-
let t3;
|
|
56
|
-
if ($[12] !== colorMode || $[13] !== colorScheme || $[14] !== dayScheme || $[15] !== nightScheme || $[16] !== resolvedColorMode || $[17] !== resolvedColorScheme || $[18] !== resolvedTheme || $[19] !== setColorMode || $[20] !== setDayScheme || $[21] !== setNightScheme) {
|
|
57
|
-
t3 = {
|
|
37
|
+
if ($[6] !== resolvedColorScheme || $[7] !== resolvedTheme || $[8] !== value) {
|
|
38
|
+
t2 = {
|
|
39
|
+
...value,
|
|
58
40
|
theme: resolvedTheme,
|
|
59
|
-
|
|
60
|
-
colorMode,
|
|
61
|
-
resolvedColorMode,
|
|
62
|
-
resolvedColorScheme,
|
|
63
|
-
dayScheme,
|
|
64
|
-
nightScheme,
|
|
65
|
-
setColorMode,
|
|
66
|
-
setDayScheme,
|
|
67
|
-
setNightScheme
|
|
41
|
+
resolvedColorScheme
|
|
68
42
|
};
|
|
69
|
-
$[
|
|
70
|
-
$[
|
|
71
|
-
$[
|
|
72
|
-
$[
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
$[22] = t3;
|
|
80
|
-
} else t3 = $[22];
|
|
81
|
-
const contextValue = t3;
|
|
82
|
-
if (props.contextOnly) {
|
|
83
|
-
let t4;
|
|
84
|
-
if ($[23] !== children || $[24] !== contextValue) {
|
|
85
|
-
t4 = /*#__PURE__*/ jsx(ThemeContext.Provider, {
|
|
86
|
-
value: contextValue,
|
|
87
|
-
children
|
|
88
|
-
});
|
|
89
|
-
$[23] = children;
|
|
90
|
-
$[24] = contextValue;
|
|
91
|
-
$[25] = t4;
|
|
92
|
-
} else t4 = $[25];
|
|
93
|
-
return t4;
|
|
94
|
-
}
|
|
95
|
-
const t4 = colorMode === "auto" ? "auto" : colorScheme.includes("dark") ? "dark" : "light";
|
|
96
|
-
let t5;
|
|
97
|
-
if ($[26] !== children || $[27] !== dayScheme || $[28] !== nightScheme || $[29] !== t4) {
|
|
98
|
-
t5 = /*#__PURE__*/ jsx("div", {
|
|
99
|
-
"data-component": "ThemeProvider",
|
|
100
|
-
"data-color-mode": t4,
|
|
101
|
-
"data-light-theme": dayScheme,
|
|
102
|
-
"data-dark-theme": nightScheme,
|
|
103
|
-
children
|
|
104
|
-
});
|
|
105
|
-
$[26] = children;
|
|
106
|
-
$[27] = dayScheme;
|
|
107
|
-
$[28] = nightScheme;
|
|
108
|
-
$[29] = t4;
|
|
109
|
-
$[30] = t5;
|
|
110
|
-
} else t5 = $[30];
|
|
111
|
-
let t6;
|
|
112
|
-
if ($[31] !== contextValue || $[32] !== t5) {
|
|
113
|
-
t6 = /*#__PURE__*/ jsx(ThemeContext.Provider, {
|
|
43
|
+
$[6] = resolvedColorScheme;
|
|
44
|
+
$[7] = resolvedTheme;
|
|
45
|
+
$[8] = value;
|
|
46
|
+
$[9] = t2;
|
|
47
|
+
} else t2 = $[9];
|
|
48
|
+
const contextValue = t2;
|
|
49
|
+
let t3;
|
|
50
|
+
if ($[10] !== children || $[11] !== contextValue || $[12] !== props.contextOnly) {
|
|
51
|
+
t3 = /*#__PURE__*/ jsx(ThemeProviderBase, {
|
|
52
|
+
contextOnly: props.contextOnly,
|
|
114
53
|
value: contextValue,
|
|
115
|
-
children
|
|
54
|
+
children
|
|
116
55
|
});
|
|
117
|
-
$[
|
|
118
|
-
$[
|
|
119
|
-
$[
|
|
120
|
-
|
|
121
|
-
|
|
56
|
+
$[10] = children;
|
|
57
|
+
$[11] = contextValue;
|
|
58
|
+
$[12] = props.contextOnly;
|
|
59
|
+
$[13] = t3;
|
|
60
|
+
} else t3 = $[13];
|
|
61
|
+
return t3;
|
|
122
62
|
};
|
|
123
|
-
function subscribeToSystemColorMode(callback) {
|
|
124
|
-
var _window, _window$matchMedia;
|
|
125
|
-
const media = (_window = window) === null || _window === void 0 ? void 0 : (_window$matchMedia = _window.matchMedia) === null || _window$matchMedia === void 0 ? void 0 : _window$matchMedia.call(_window, "(prefers-color-scheme: dark)");
|
|
126
|
-
media === null || media === void 0 || media.addEventListener("change", callback);
|
|
127
|
-
return () => media === null || media === void 0 ? void 0 : media.removeEventListener("change", callback);
|
|
128
|
-
}
|
|
129
|
-
function useSystemColorMode() {
|
|
130
|
-
return React.useSyncExternalStore(subscribeToSystemColorMode, getSystemColorMode, _temp);
|
|
131
|
-
}
|
|
132
|
-
function _temp() {
|
|
133
|
-
return "day";
|
|
134
|
-
}
|
|
135
|
-
function getSystemColorMode() {
|
|
136
|
-
var _window2, _window2$matchMedia, _window2$matchMedia$c;
|
|
137
|
-
return (_window2 = window) !== null && _window2 !== void 0 && (_window2$matchMedia = _window2.matchMedia) !== null && _window2$matchMedia !== void 0 && (_window2$matchMedia$c = _window2$matchMedia.call(_window2, "(prefers-color-scheme: dark)")) !== null && _window2$matchMedia$c !== void 0 && _window2$matchMedia$c.matches ? "night" : "day";
|
|
138
|
-
}
|
|
139
|
-
function resolveColorMode(colorMode, systemColorMode) {
|
|
140
|
-
switch (colorMode) {
|
|
141
|
-
case "auto": return systemColorMode;
|
|
142
|
-
default: return colorMode;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function chooseColorScheme(colorMode, dayScheme, nightScheme) {
|
|
146
|
-
switch (colorMode) {
|
|
147
|
-
case "day":
|
|
148
|
-
case "light": return dayScheme;
|
|
149
|
-
case "dark":
|
|
150
|
-
case "night": return nightScheme;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
63
|
function applyColorScheme(theme, colorScheme) {
|
|
154
64
|
if (!theme.colorSchemes) return {
|
|
155
65
|
resolvedTheme: theme,
|
|
@@ -1,83 +1,68 @@
|
|
|
1
|
-
import { c } from "react-compiler-runtime";
|
|
2
1
|
import React from "react";
|
|
3
2
|
import { iterateFocusableElements } from "@primer/behaviors/utils";
|
|
4
3
|
//#region src/hooks/useMenuInitialFocus.ts
|
|
5
4
|
const useMenuInitialFocus = (open, containerRef, anchorRef) => {
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* We need to pick the first element to focus based on how the menu was opened,
|
|
7
|
+
* however, we need to wait for the menu to be open to set focus.
|
|
8
|
+
* This is why we use set openingKey in state and have 2 effects
|
|
9
|
+
*/
|
|
7
10
|
const [openingGesture, setOpeningGesture] = React.useState(void 0);
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const anchorElement = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current;
|
|
13
|
-
const clickHandler = (event) => {
|
|
14
|
-
if (event.detail !== 0) setOpeningGesture("mouse-click");
|
|
15
|
-
};
|
|
16
|
-
const keydownHandler = (event_0) => {
|
|
17
|
-
if ([
|
|
18
|
-
"Space",
|
|
19
|
-
"Enter",
|
|
20
|
-
"ArrowDown",
|
|
21
|
-
"ArrowUp"
|
|
22
|
-
].includes(event_0.code)) setOpeningGesture(event_0.code);
|
|
23
|
-
};
|
|
24
|
-
anchorElement === null || anchorElement === void 0 || anchorElement.addEventListener("click", clickHandler);
|
|
25
|
-
anchorElement === null || anchorElement === void 0 || anchorElement.addEventListener("keydown", keydownHandler);
|
|
26
|
-
return () => {
|
|
27
|
-
anchorElement === null || anchorElement === void 0 || anchorElement.removeEventListener("click", clickHandler);
|
|
28
|
-
anchorElement === null || anchorElement === void 0 || anchorElement.removeEventListener("keydown", keydownHandler);
|
|
29
|
-
};
|
|
11
|
+
React.useEffect(function inferOpeningKey() {
|
|
12
|
+
const anchorElement = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current;
|
|
13
|
+
const clickHandler = (event) => {
|
|
14
|
+
if (event.detail !== 0) setOpeningGesture("mouse-click");
|
|
30
15
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
$[1] = t0;
|
|
34
|
-
$[2] = t1;
|
|
35
|
-
} else {
|
|
36
|
-
t0 = $[1];
|
|
37
|
-
t1 = $[2];
|
|
38
|
-
}
|
|
39
|
-
React.useEffect(t0, t1);
|
|
40
|
-
let t2;
|
|
41
|
-
let t3;
|
|
42
|
-
if ($[3] !== anchorRef || $[4] !== containerRef || $[5] !== open || $[6] !== openingGesture) {
|
|
43
|
-
t2 = function moveFocusOnOpen() {
|
|
44
|
-
if (!open || !(containerRef !== null && containerRef !== void 0 && containerRef.current)) return;
|
|
45
|
-
const iterable = iterateFocusableElements(containerRef.current);
|
|
46
|
-
if (openingGesture === "mouse-click") if (anchorRef !== null && anchorRef !== void 0 && anchorRef.current) anchorRef.current.focus();
|
|
47
|
-
else throw new Error("For focus management, please attach anchorRef");
|
|
48
|
-
else if (openingGesture && [
|
|
49
|
-
"ArrowDown",
|
|
16
|
+
const keydownHandler = (event) => {
|
|
17
|
+
if ([
|
|
50
18
|
"Space",
|
|
51
|
-
"Enter"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
setTimeout(() => firstElement_0 === null || firstElement_0 === void 0 ? void 0 : firstElement_0.focus());
|
|
62
|
-
}
|
|
19
|
+
"Enter",
|
|
20
|
+
"ArrowDown",
|
|
21
|
+
"ArrowUp"
|
|
22
|
+
].includes(event.code)) setOpeningGesture(event.code);
|
|
23
|
+
};
|
|
24
|
+
anchorElement === null || anchorElement === void 0 || anchorElement.addEventListener("click", clickHandler);
|
|
25
|
+
anchorElement === null || anchorElement === void 0 || anchorElement.addEventListener("keydown", keydownHandler);
|
|
26
|
+
return () => {
|
|
27
|
+
anchorElement === null || anchorElement === void 0 || anchorElement.removeEventListener("click", clickHandler);
|
|
28
|
+
anchorElement === null || anchorElement === void 0 || anchorElement.removeEventListener("keydown", keydownHandler);
|
|
63
29
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
30
|
+
}, [anchorRef]);
|
|
31
|
+
/**
|
|
32
|
+
* Pick the first element to focus based on the key used to open the Menu
|
|
33
|
+
* Click: anchor
|
|
34
|
+
* ArrowDown | Space | Enter: first element
|
|
35
|
+
* ArrowUp: last element
|
|
36
|
+
*/
|
|
37
|
+
React.useEffect(function moveFocusOnOpen() {
|
|
38
|
+
if (!open || !(containerRef !== null && containerRef !== void 0 && containerRef.current)) return;
|
|
39
|
+
const iterable = iterateFocusableElements(containerRef.current);
|
|
40
|
+
if (openingGesture === "mouse-click") if (anchorRef !== null && anchorRef !== void 0 && anchorRef.current) anchorRef.current.focus();
|
|
41
|
+
else throw new Error("For focus management, please attach anchorRef");
|
|
42
|
+
else if (openingGesture && [
|
|
43
|
+
"ArrowDown",
|
|
44
|
+
"Space",
|
|
45
|
+
"Enter"
|
|
46
|
+
].includes(openingGesture)) {
|
|
47
|
+
const firstElement = iterable.next().value;
|
|
48
|
+
/** We push imperative focus to the next tick to prevent React's batching */
|
|
49
|
+
setTimeout(() => firstElement === null || firstElement === void 0 ? void 0 : firstElement.focus());
|
|
50
|
+
} else if ("ArrowUp" === openingGesture) {
|
|
51
|
+
const elements = [...iterable];
|
|
52
|
+
const lastElement = elements[elements.length - 1];
|
|
53
|
+
setTimeout(() => lastElement.focus());
|
|
54
|
+
} else {
|
|
55
|
+
/** if the menu was not opened with the anchor, we default to the first element
|
|
56
|
+
* for example: with keyboard shortcut (see stories/fixtures)
|
|
57
|
+
*/
|
|
58
|
+
const firstElement = iterable.next().value;
|
|
59
|
+
setTimeout(() => firstElement === null || firstElement === void 0 ? void 0 : firstElement.focus());
|
|
60
|
+
}
|
|
61
|
+
}, [
|
|
62
|
+
open,
|
|
63
|
+
openingGesture,
|
|
64
|
+
anchorRef
|
|
65
|
+
]);
|
|
81
66
|
};
|
|
82
67
|
//#endregion
|
|
83
68
|
export { useMenuInitialFocus };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ThemeContext } from "../../ThemeContext.js";
|
|
2
|
+
import { c } from "react-compiler-runtime";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region src/internal/components/ThemeProviderBase.tsx
|
|
5
|
+
const ThemeProviderBase = (t0) => {
|
|
6
|
+
const $ = c(11);
|
|
7
|
+
const { children, contextOnly, value } = t0;
|
|
8
|
+
if (contextOnly) {
|
|
9
|
+
let t1;
|
|
10
|
+
if ($[0] !== children || $[1] !== value) {
|
|
11
|
+
t1 = /*#__PURE__*/ jsx(ThemeContext.Provider, {
|
|
12
|
+
value,
|
|
13
|
+
children
|
|
14
|
+
});
|
|
15
|
+
$[0] = children;
|
|
16
|
+
$[1] = value;
|
|
17
|
+
$[2] = t1;
|
|
18
|
+
} else t1 = $[2];
|
|
19
|
+
return t1;
|
|
20
|
+
}
|
|
21
|
+
const t1 = value.colorMode === "auto" ? "auto" : value.colorScheme.includes("dark") ? "dark" : "light";
|
|
22
|
+
let t2;
|
|
23
|
+
if ($[3] !== children || $[4] !== t1 || $[5] !== value.dayScheme || $[6] !== value.nightScheme) {
|
|
24
|
+
t2 = /*#__PURE__*/ jsx("div", {
|
|
25
|
+
"data-component": "ThemeProvider",
|
|
26
|
+
"data-color-mode": t1,
|
|
27
|
+
"data-light-theme": value.dayScheme,
|
|
28
|
+
"data-dark-theme": value.nightScheme,
|
|
29
|
+
children
|
|
30
|
+
});
|
|
31
|
+
$[3] = children;
|
|
32
|
+
$[4] = t1;
|
|
33
|
+
$[5] = value.dayScheme;
|
|
34
|
+
$[6] = value.nightScheme;
|
|
35
|
+
$[7] = t2;
|
|
36
|
+
} else t2 = $[7];
|
|
37
|
+
let t3;
|
|
38
|
+
if ($[8] !== t2 || $[9] !== value) {
|
|
39
|
+
t3 = /*#__PURE__*/ jsx(ThemeContext.Provider, {
|
|
40
|
+
value,
|
|
41
|
+
children: t2
|
|
42
|
+
});
|
|
43
|
+
$[8] = t2;
|
|
44
|
+
$[9] = value;
|
|
45
|
+
$[10] = t3;
|
|
46
|
+
} else t3 = $[10];
|
|
47
|
+
return t3;
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
export { ThemeProviderBase };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useSyncedState } from "../../hooks/useSyncedState.js";
|
|
2
|
+
import { useTheme } from "../../useTheme.js";
|
|
3
|
+
import { c } from "react-compiler-runtime";
|
|
4
|
+
import React from "react";
|
|
5
|
+
//#region src/internal/hooks/useThemeProvider.ts
|
|
6
|
+
const defaultColorMode = "day";
|
|
7
|
+
const defaultDayScheme = "light";
|
|
8
|
+
const defaultNightScheme = "dark";
|
|
9
|
+
function useThemeProvider(props) {
|
|
10
|
+
var _ref, _props$colorMode, _ref2, _props$dayScheme, _ref3, _props$nightScheme;
|
|
11
|
+
const $ = c(12);
|
|
12
|
+
const parentValue = useTheme();
|
|
13
|
+
const [colorMode, setColorMode] = useSyncedState((_ref = (_props$colorMode = props.colorMode) !== null && _props$colorMode !== void 0 ? _props$colorMode : parentValue.colorMode) !== null && _ref !== void 0 ? _ref : defaultColorMode);
|
|
14
|
+
const [dayScheme, setDayScheme] = useSyncedState((_ref2 = (_props$dayScheme = props.dayScheme) !== null && _props$dayScheme !== void 0 ? _props$dayScheme : parentValue.dayScheme) !== null && _ref2 !== void 0 ? _ref2 : defaultDayScheme);
|
|
15
|
+
const [nightScheme, setNightScheme] = useSyncedState((_ref3 = (_props$nightScheme = props.nightScheme) !== null && _props$nightScheme !== void 0 ? _props$nightScheme : parentValue.nightScheme) !== null && _ref3 !== void 0 ? _ref3 : defaultNightScheme);
|
|
16
|
+
const systemColorMode = useSystemColorMode();
|
|
17
|
+
const resolvedColorMode = colorMode === "auto" ? systemColorMode : colorMode;
|
|
18
|
+
const colorScheme = resolvedColorMode === "day" || resolvedColorMode === "light" ? dayScheme : nightScheme;
|
|
19
|
+
let t0;
|
|
20
|
+
if ($[0] !== colorMode || $[1] !== colorScheme || $[2] !== dayScheme || $[3] !== nightScheme || $[4] !== resolvedColorMode || $[5] !== setColorMode || $[6] !== setDayScheme || $[7] !== setNightScheme) {
|
|
21
|
+
t0 = {
|
|
22
|
+
colorScheme,
|
|
23
|
+
colorMode,
|
|
24
|
+
resolvedColorMode,
|
|
25
|
+
dayScheme,
|
|
26
|
+
nightScheme,
|
|
27
|
+
setColorMode,
|
|
28
|
+
setDayScheme,
|
|
29
|
+
setNightScheme
|
|
30
|
+
};
|
|
31
|
+
$[0] = colorMode;
|
|
32
|
+
$[1] = colorScheme;
|
|
33
|
+
$[2] = dayScheme;
|
|
34
|
+
$[3] = nightScheme;
|
|
35
|
+
$[4] = resolvedColorMode;
|
|
36
|
+
$[5] = setColorMode;
|
|
37
|
+
$[6] = setDayScheme;
|
|
38
|
+
$[7] = setNightScheme;
|
|
39
|
+
$[8] = t0;
|
|
40
|
+
} else t0 = $[8];
|
|
41
|
+
const value = t0;
|
|
42
|
+
let t1;
|
|
43
|
+
if ($[9] !== parentValue || $[10] !== value) {
|
|
44
|
+
t1 = {
|
|
45
|
+
parentValue,
|
|
46
|
+
value
|
|
47
|
+
};
|
|
48
|
+
$[9] = parentValue;
|
|
49
|
+
$[10] = value;
|
|
50
|
+
$[11] = t1;
|
|
51
|
+
} else t1 = $[11];
|
|
52
|
+
return t1;
|
|
53
|
+
}
|
|
54
|
+
function subscribeToSystemColorMode(callback) {
|
|
55
|
+
var _window, _window$matchMedia;
|
|
56
|
+
const media = (_window = window) === null || _window === void 0 ? void 0 : (_window$matchMedia = _window.matchMedia) === null || _window$matchMedia === void 0 ? void 0 : _window$matchMedia.call(_window, "(prefers-color-scheme: dark)");
|
|
57
|
+
media === null || media === void 0 || media.addEventListener("change", callback);
|
|
58
|
+
return () => media === null || media === void 0 ? void 0 : media.removeEventListener("change", callback);
|
|
59
|
+
}
|
|
60
|
+
function useSystemColorMode() {
|
|
61
|
+
return React.useSyncExternalStore(subscribeToSystemColorMode, getSystemColorMode, _temp);
|
|
62
|
+
}
|
|
63
|
+
function _temp() {
|
|
64
|
+
return "day";
|
|
65
|
+
}
|
|
66
|
+
function getSystemColorMode() {
|
|
67
|
+
var _window2, _window2$matchMedia, _window2$matchMedia$c;
|
|
68
|
+
return (_window2 = window) !== null && _window2 !== void 0 && (_window2$matchMedia = _window2.matchMedia) !== null && _window2$matchMedia !== void 0 && (_window2$matchMedia$c = _window2$matchMedia.call(_window2, "(prefers-color-scheme: dark)")) !== null && _window2$matchMedia$c !== void 0 && _window2$matchMedia$c.matches ? "night" : "day";
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { useThemeProvider };
|