@primer/react 0.0.0-20260811201638 → 0.0.0-20260812170837
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 +3 -3
- package/dist/Banner/Banner.d.ts +4 -4
- package/dist/Banner/Banner.js +4 -4
- package/dist/Portal/Portal.js +29 -91
- package/dist/hooks/useMenuInitialFocus.js +57 -72
- package/generated/components.json +2 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# @primer/react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-20260812170837
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
7
7
|
- [#8212](https://github.com/primer/react/pull/8212) [`9ac9343`](https://github.com/primer/react/commit/9ac9343a70fd0f70413a42cd27baf562a74dd4fc) Thanks [@mattcosta7](https://github.com/mattcosta7)! - ThemeProvider: Add a CSS-variable-based implementation to `@primer/react/next`
|
|
8
8
|
and deprecate the legacy JavaScript theme implementation.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
- [#8275](https://github.com/primer/react/pull/8275) [`e4a379f`](https://github.com/primer/react/commit/e4a379f3dbd4307bfe17d5275429e0cf7a2397db) Thanks [@TylerJDev](https://github.com/TylerJDev)! - Banner: Add support for rendering `Banner.Title` as a paragraph.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
### Patch Changes
|
|
13
13
|
|
|
14
14
|
- Fake entry to force publishing
|
|
15
15
|
|
package/dist/Banner/Banner.d.ts
CHANGED
|
@@ -67,12 +67,12 @@ type BannerProps = React.ComponentPropsWithoutRef<'section'> & {
|
|
|
67
67
|
*/
|
|
68
68
|
flush?: boolean;
|
|
69
69
|
};
|
|
70
|
-
type
|
|
71
|
-
type BannerTitleProps<As extends
|
|
70
|
+
type TitleElement = 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p';
|
|
71
|
+
type BannerTitleProps<As extends TitleElement = 'h2'> = {
|
|
72
72
|
as?: As;
|
|
73
73
|
className?: string;
|
|
74
|
-
} & React.ComponentPropsWithoutRef<As
|
|
75
|
-
declare function BannerTitle<As extends
|
|
74
|
+
} & React.ComponentPropsWithoutRef<As>;
|
|
75
|
+
declare function BannerTitle<As extends TitleElement = 'h2'>(props: BannerTitleProps<As>): React.JSX.Element;
|
|
76
76
|
type BannerDescriptionProps = React.ComponentPropsWithoutRef<'div'>;
|
|
77
77
|
declare function BannerDescription({
|
|
78
78
|
children,
|
package/dist/Banner/Banner.js
CHANGED
|
@@ -271,7 +271,7 @@ function BannerTitle(props) {
|
|
|
271
271
|
rest = $[4];
|
|
272
272
|
t0 = $[5];
|
|
273
273
|
}
|
|
274
|
-
const
|
|
274
|
+
const Element = t0 === void 0 ? "h2" : t0;
|
|
275
275
|
const context = React.useContext(BannerContext);
|
|
276
276
|
const titleId = (_id = id) !== null && _id !== void 0 ? _id : context === null || context === void 0 ? void 0 : context.titleId;
|
|
277
277
|
let t1;
|
|
@@ -281,8 +281,8 @@ function BannerTitle(props) {
|
|
|
281
281
|
$[7] = t1;
|
|
282
282
|
} else t1 = $[7];
|
|
283
283
|
let t2;
|
|
284
|
-
if ($[8] !==
|
|
285
|
-
t2 = /*#__PURE__*/ jsx(
|
|
284
|
+
if ($[8] !== Element || $[9] !== children || $[10] !== rest || $[11] !== t1 || $[12] !== titleId) {
|
|
285
|
+
t2 = /*#__PURE__*/ jsx(Element, {
|
|
286
286
|
...rest,
|
|
287
287
|
id: titleId,
|
|
288
288
|
className: t1,
|
|
@@ -290,7 +290,7 @@ function BannerTitle(props) {
|
|
|
290
290
|
"data-banner-title": "",
|
|
291
291
|
children
|
|
292
292
|
});
|
|
293
|
-
$[8] =
|
|
293
|
+
$[8] = Element;
|
|
294
294
|
$[9] = children;
|
|
295
295
|
$[10] = rest;
|
|
296
296
|
$[11] = t1;
|
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 };
|
|
@@ -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 };
|
package/package.json
CHANGED