@primer/react 0.0.0-20260805210748 → 0.0.0-20260807144044

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 CHANGED
@@ -1,14 +1,11 @@
1
1
  # @primer/react
2
2
 
3
- ## 0.0.0-20260805210748
4
-
5
- ### Minor Changes
6
-
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.
3
+ ## 0.0.0-20260807144044
9
4
 
10
5
  ### Patch Changes
11
6
 
7
+ - [#8276](https://github.com/primer/react/pull/8276) [`d2b0e4c`](https://github.com/primer/react/commit/d2b0e4cc059dc6826d70383173b96cdfb4970a93) Thanks [@copilot-swe-agent](https://github.com/apps/copilot-swe-agent)! - Portal: Render nothing while server rendering and during hydration instead of accessing the DOM during render, fixing server rendering support. Client-only renders are unchanged and still mount portaled content in the same commit.
8
+
12
9
  - Fake entry to force publishing
13
10
 
14
11
  - [#8258](https://github.com/primer/react/pull/8258) [`edbcd94`](https://github.com/primer/react/commit/edbcd942354ff45148c2623be8b543ef8789a20f) Thanks [@llastflowers](https://github.com/llastflowers)! - Update `AvatarStack` to not reserve space when collapsed
@@ -1,43 +1,105 @@
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 React, { useContext } from "react";
4
+ import { c } from "react-compiler-runtime";
5
+ import React, { useContext, useSyncExternalStore } from "react";
5
6
  import { createPortal } from "react-dom";
6
7
  //#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
+ }
7
30
  /**
8
31
  * Creates a React Portal, placing all children in a separate physical DOM root node.
9
32
  * @see https://reactjs.org/docs/portals.html
10
33
  */
11
- const Portal = ({ children, onMount, containerName: _containerName }) => {
34
+ const Portal = (t0) => {
35
+ const $ = c(12);
36
+ const { children, onMount, containerName: _containerName } = t0;
12
37
  const { portalContainerName } = useContext(PortalContext);
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;
20
- }
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);
38
+ const isClientRender = useIsClientRender();
39
+ const onMountRef = React.useRef(onMount);
40
+ let t1;
41
+ if ($[0] !== onMount) {
42
+ t1 = () => {
43
+ onMountRef.current = onMount;
34
44
  };
35
- }, [
36
- element,
37
- _containerName,
38
- portalContainerName
39
- ]);
40
- return /*#__PURE__*/ createPortal(children, element);
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];
84
+ }
85
+ useIsomorphicLayoutEffect(t2, t3);
86
+ let t4;
87
+ if ($[8] !== children || $[9] !== element || $[10] !== isClientRender) {
88
+ t4 = element && isClientRender ? /*#__PURE__*/ createPortal(children, element) : null;
89
+ $[8] = children;
90
+ $[9] = element;
91
+ $[10] = isClientRender;
92
+ $[11] = t4;
93
+ } else t4 = $[11];
94
+ return t4;
41
95
  };
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
+ }
42
104
  //#endregion
43
105
  export { Portal };
@@ -21,9 +21,6 @@ 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
- */
27
24
  declare const ThemeProvider: React.FC<React.PropsWithChildren<ThemeProviderProps>>;
28
25
  //#endregion
29
26
  export { ColorMode, ColorModeWithAuto, Theme, ThemeProvider, ThemeProvider as default, ThemeProviderProps };
@@ -1,17 +1,16 @@
1
1
  import theme from "./theme.js";
2
- import { ThemeProviderBase } from "./internal/components/ThemeProviderBase.js";
3
- import { useThemeProvider } from "./internal/hooks/useThemeProvider.js";
2
+ import { useSyncedState } from "./hooks/useSyncedState.js";
3
+ import { ThemeContext } from "./ThemeContext.js";
4
+ import { useTheme } from "./useTheme.js";
4
5
  import { c } from "react-compiler-runtime";
5
6
  import { jsx } from "react/jsx-runtime";
6
- import "react";
7
+ import React from "react";
7
8
  import deepmerge from "deepmerge";
8
- //#region src/ThemeProvider.tsx
9
- /**
10
- * @deprecated Use `ThemeProvider` from `@primer/react/next` to migrate away from JavaScript theme values.
11
- */
9
+ const defaultDayScheme = "light";
10
+ const defaultNightScheme = "dark";
12
11
  const ThemeProvider = (t0) => {
13
- var _parentValue$theme;
14
- const $ = c(14);
12
+ var _ref, _props$colorMode, _ref2, _props$dayScheme, _ref3, _props$nightScheme;
13
+ const $ = c(34);
15
14
  let children;
16
15
  let props;
17
16
  if ($[0] !== t0) {
@@ -23,43 +22,134 @@ const ThemeProvider = (t0) => {
23
22
  children = $[1];
24
23
  props = $[2];
25
24
  }
26
- const { parentValue, value } = useThemeProvider(props);
27
- const theme$1 = (_parentValue$theme = parentValue.theme) !== null && _parentValue$theme !== void 0 ? _parentValue$theme : theme;
25
+ const { theme: fallbackTheme, colorMode: fallbackColorMode, dayScheme: fallbackDayScheme, nightScheme: fallbackNightScheme } = useTheme();
26
+ const theme$1 = fallbackTheme !== null && fallbackTheme !== void 0 ? fallbackTheme : theme;
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;
28
32
  let 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;
33
+ if ($[3] !== colorMode || $[4] !== dayScheme || $[5] !== nightScheme || $[6] !== systemColorMode) {
34
+ resolvedColorMode = resolveColorMode(colorMode, systemColorMode);
35
+ t1 = chooseColorScheme(resolvedColorMode, dayScheme, nightScheme);
36
+ $[3] = colorMode;
37
+ $[4] = dayScheme;
38
+ $[5] = nightScheme;
39
+ $[6] = systemColorMode;
40
+ $[7] = resolvedColorMode;
41
+ $[8] = t1;
42
+ } else {
43
+ resolvedColorMode = $[7];
44
+ t1 = $[8];
45
+ }
46
+ const colorScheme = t1;
36
47
  let t2;
37
- if ($[6] !== resolvedColorScheme || $[7] !== resolvedTheme || $[8] !== value) {
38
- t2 = {
39
- ...value,
48
+ if ($[9] !== colorScheme || $[10] !== theme$1) {
49
+ t2 = applyColorScheme(theme$1, colorScheme);
50
+ $[9] = colorScheme;
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 = {
40
58
  theme: resolvedTheme,
41
- resolvedColorScheme
59
+ colorScheme,
60
+ colorMode,
61
+ resolvedColorMode,
62
+ resolvedColorScheme,
63
+ dayScheme,
64
+ nightScheme,
65
+ setColorMode,
66
+ setDayScheme,
67
+ setNightScheme
42
68
  };
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,
53
- value: contextValue,
69
+ $[12] = colorMode;
70
+ $[13] = colorScheme;
71
+ $[14] = dayScheme;
72
+ $[15] = nightScheme;
73
+ $[16] = resolvedColorMode;
74
+ $[17] = resolvedColorScheme;
75
+ $[18] = resolvedTheme;
76
+ $[19] = setColorMode;
77
+ $[20] = setDayScheme;
78
+ $[21] = setNightScheme;
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,
54
103
  children
55
104
  });
56
- $[10] = children;
57
- $[11] = contextValue;
58
- $[12] = props.contextOnly;
59
- $[13] = t3;
60
- } else t3 = $[13];
61
- return t3;
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, {
114
+ value: contextValue,
115
+ children: t5
116
+ });
117
+ $[31] = contextValue;
118
+ $[32] = t5;
119
+ $[33] = t6;
120
+ } else t6 = $[33];
121
+ return t6;
62
122
  };
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
+ }
63
153
  function applyColorScheme(theme, colorScheme) {
64
154
  if (!theme.colorSchemes) return {
65
155
  resolvedTheme: theme,
@@ -1,68 +1,83 @@
1
+ import { c } from "react-compiler-runtime";
1
2
  import React from "react";
2
3
  import { iterateFocusableElements } from "@primer/behaviors/utils";
3
4
  //#region src/hooks/useMenuInitialFocus.ts
4
5
  const useMenuInitialFocus = (open, containerRef, anchorRef) => {
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
- */
6
+ const $ = c(9);
10
7
  const [openingGesture, setOpeningGesture] = React.useState(void 0);
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");
8
+ let t0;
9
+ let t1;
10
+ if ($[0] !== anchorRef) {
11
+ t0 = 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");
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
+ };
15
30
  };
16
- const keydownHandler = (event) => {
17
- if ([
18
- "Space",
19
- "Enter",
31
+ t1 = [anchorRef];
32
+ $[0] = anchorRef;
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 && [
20
49
  "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);
50
+ "Space",
51
+ "Enter"
52
+ ].includes(openingGesture)) {
53
+ const firstElement = iterable.next().value;
54
+ setTimeout(() => firstElement === null || firstElement === void 0 ? void 0 : firstElement.focus());
55
+ } else if ("ArrowUp" === openingGesture) {
56
+ const elements = [...iterable];
57
+ const lastElement = elements[elements.length - 1];
58
+ setTimeout(() => lastElement.focus());
59
+ } else {
60
+ const firstElement_0 = iterable.next().value;
61
+ setTimeout(() => firstElement_0 === null || firstElement_0 === void 0 ? void 0 : firstElement_0.focus());
62
+ }
29
63
  };
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
- ]);
64
+ t3 = [
65
+ open,
66
+ openingGesture,
67
+ anchorRef,
68
+ containerRef
69
+ ];
70
+ $[3] = anchorRef;
71
+ $[4] = containerRef;
72
+ $[5] = open;
73
+ $[6] = openingGesture;
74
+ $[7] = t2;
75
+ $[8] = t3;
76
+ } else {
77
+ t2 = $[7];
78
+ t3 = $[8];
79
+ }
80
+ React.useEffect(t2, t3);
66
81
  };
67
82
  //#endregion
68
83
  export { useMenuInitialFocus };