@primer/react 0.0.0-20260219220306 → 0.0.0-20260219223341
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 +2 -2
- package/dist/hooks/useSlots.d.ts +6 -50
- package/dist/hooks/useSlots.d.ts.map +1 -1
- package/dist/hooks/useSlots.js +36 -114
- package/generated/components.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# @primer/react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-20260219223341
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
7
|
- Fake entry to force publishing
|
|
8
8
|
|
|
9
|
-
- [#
|
|
9
|
+
- [#7567](https://github.com/primer/react/pull/7567) [`81609c5`](https://github.com/primer/react/commit/81609c54bfded6a401e9786e07deedb3dcd78e51) Thanks [@llastflowers](https://github.com/llastflowers)! - Autocomplete story SR fix
|
|
10
10
|
|
|
11
11
|
## 38.13.0
|
|
12
12
|
|
package/dist/hooks/useSlots.d.ts
CHANGED
|
@@ -1,56 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* useSlots - Extract slot components from children for SSR-compatible slot APIs.
|
|
4
|
-
*
|
|
5
|
-
* Given a list of children and a config mapping slot names to component types,
|
|
6
|
-
* separates children into two groups: matched slots and the rest.
|
|
7
|
-
*
|
|
8
|
-
* Config: { leadingVisual: LeadingVisual, description: Description }
|
|
9
|
-
*
|
|
10
|
-
* Children: Matching: Output:
|
|
11
|
-
* +----------------+ slots = {
|
|
12
|
-
* | LeadingVisual | -> matches slot 0 --> leadingVisual: <LeadingVisual />
|
|
13
|
-
* | "Project name" | -> no match --> description: <Description />
|
|
14
|
-
* | Description | -> matches slot 1 --> }
|
|
15
|
-
* | TrailingVisual | -> no match --> rest = ["Project name", <TrailingVisual />]
|
|
16
|
-
* +----------------+
|
|
17
|
-
*
|
|
18
|
-
* Performance-sensitive: called per item in lists (e.g. 100-item ActionList
|
|
19
|
-
* calls this 101 times per render).
|
|
20
|
-
*
|
|
21
|
-
* Once all slots are filled, remaining children skip matching entirely in
|
|
22
|
-
* production (single integer comparison). In dev, we still scan for duplicates
|
|
23
|
-
* to emit a warning.
|
|
24
|
-
*
|
|
25
|
-
* Flow per child:
|
|
26
|
-
*
|
|
27
|
-
* child ──> isValidElement? ──no──> rest[]
|
|
28
|
-
* |
|
|
29
|
-
* yes
|
|
30
|
-
* |
|
|
31
|
-
* v
|
|
32
|
-
* all slots filled? ──yes──> rest[] (prod)
|
|
33
|
-
* | |
|
|
34
|
-
* no [dev: check for
|
|
35
|
-
* | duplicate warning]
|
|
36
|
-
* v
|
|
37
|
-
* match against unfilled slots
|
|
38
|
-
* |
|
|
39
|
-
* found match? ──no──> rest[]
|
|
40
|
-
* |
|
|
41
|
-
* yes
|
|
42
|
-
* |
|
|
43
|
-
* v
|
|
44
|
-
* slots[key] = child, slotsFound++
|
|
45
|
-
*
|
|
46
|
-
* Slot config supports two matcher styles:
|
|
47
|
-
* 1. Component reference: { visual: LeadingVisual }
|
|
48
|
-
* 2. Component + test fn: { block: [Description, props => props.variant === 'block'] }
|
|
49
|
-
*/
|
|
50
|
-
type Props = any;
|
|
51
2
|
type ComponentMatcher = React.ElementType<Props>;
|
|
52
3
|
type ComponentAndPropsMatcher = [ComponentMatcher, (props: Props) => boolean];
|
|
53
4
|
export type SlotConfig = Record<string, ComponentMatcher | ComponentAndPropsMatcher>;
|
|
5
|
+
type Props = any;
|
|
54
6
|
type SlotElements<Config extends SlotConfig> = {
|
|
55
7
|
[Property in keyof Config]: SlotValue<Config, Property>;
|
|
56
8
|
};
|
|
@@ -58,7 +10,11 @@ type SlotValue<Config, Property extends keyof Config> = Config[Property] extends
|
|
|
58
10
|
infer ElementType extends React.ElementType,
|
|
59
11
|
infer _testFn
|
|
60
12
|
] ? React.ReactElement<React.ComponentPropsWithoutRef<ElementType>, ElementType> : never;
|
|
61
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Extract components from `children` so we can render them in different places,
|
|
15
|
+
* allowing us to implement components with SSR-compatible slot APIs.
|
|
16
|
+
* Note: We can only extract direct children, not nested ones.
|
|
17
|
+
*/
|
|
62
18
|
export declare function useSlots<Config extends SlotConfig>(children: React.ReactNode, config: Config): [Partial<SlotElements<Config>>, React.ReactNode[]];
|
|
63
19
|
export {};
|
|
64
20
|
//# sourceMappingURL=useSlots.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSlots.d.ts","sourceRoot":"","sources":["../../src/hooks/useSlots.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"useSlots.d.ts","sourceRoot":"","sources":["../../src/hooks/useSlots.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAOzB,KAAK,gBAAgB,GAAG,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA;AAEhD,KAAK,wBAAwB,GAAG,CAAC,gBAAgB,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,CAAA;AAE7E,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,GAAG,wBAAwB,CAAC,CAAA;AAIpF,KAAK,KAAK,GAAG,GAAG,CAAA;AAEhB,KAAK,YAAY,CAAC,MAAM,SAAS,UAAU,IAAI;KAC5C,QAAQ,IAAI,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC;CACxD,CAAA;AAED,KAAK,SAAS,CAAC,MAAM,EAAE,QAAQ,SAAS,MAAM,MAAM,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,KAAK,CAAC,WAAW,GAC9F,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,wBAAwB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,GACtF,MAAM,CAAC,QAAQ,CAAC,SAAS,SAAS;IAC9B,MAAM,WAAW,SAAS,KAAK,CAAC,WAAW;IAE3C,MAAM,OAAO;CACd,GACD,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,wBAAwB,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC,GAC5E,KAAK,CAAA;AAEX;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,MAAM,SAAS,UAAU,EAChD,QAAQ,EAAE,KAAK,CAAC,SAAS,EACzB,MAAM,EAAE,MAAM,GACb,CAAC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,SAAS,EAAE,CAAC,CA8CpD"}
|
package/dist/hooks/useSlots.js
CHANGED
|
@@ -2,85 +2,29 @@ import React from 'react';
|
|
|
2
2
|
import { warning } from '../utils/warning.js';
|
|
3
3
|
import { isSlot } from '../utils/is-slot.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
* Given a list of children and a config mapping slot names to component types,
|
|
9
|
-
* separates children into two groups: matched slots and the rest.
|
|
10
|
-
*
|
|
11
|
-
* Config: { leadingVisual: LeadingVisual, description: Description }
|
|
12
|
-
*
|
|
13
|
-
* Children: Matching: Output:
|
|
14
|
-
* +----------------+ slots = {
|
|
15
|
-
* | LeadingVisual | -> matches slot 0 --> leadingVisual: <LeadingVisual />
|
|
16
|
-
* | "Project name" | -> no match --> description: <Description />
|
|
17
|
-
* | Description | -> matches slot 1 --> }
|
|
18
|
-
* | TrailingVisual | -> no match --> rest = ["Project name", <TrailingVisual />]
|
|
19
|
-
* +----------------+
|
|
20
|
-
*
|
|
21
|
-
* Performance-sensitive: called per item in lists (e.g. 100-item ActionList
|
|
22
|
-
* calls this 101 times per render).
|
|
23
|
-
*
|
|
24
|
-
* Once all slots are filled, remaining children skip matching entirely in
|
|
25
|
-
* production (single integer comparison). In dev, we still scan for duplicates
|
|
26
|
-
* to emit a warning.
|
|
27
|
-
*
|
|
28
|
-
* Flow per child:
|
|
29
|
-
*
|
|
30
|
-
* child ──> isValidElement? ──no──> rest[]
|
|
31
|
-
* |
|
|
32
|
-
* yes
|
|
33
|
-
* |
|
|
34
|
-
* v
|
|
35
|
-
* all slots filled? ──yes──> rest[] (prod)
|
|
36
|
-
* | |
|
|
37
|
-
* no [dev: check for
|
|
38
|
-
* | duplicate warning]
|
|
39
|
-
* v
|
|
40
|
-
* match against unfilled slots
|
|
41
|
-
* |
|
|
42
|
-
* found match? ──no──> rest[]
|
|
43
|
-
* |
|
|
44
|
-
* yes
|
|
45
|
-
* |
|
|
46
|
-
* v
|
|
47
|
-
* slots[key] = child, slotsFound++
|
|
48
|
-
*
|
|
49
|
-
* Slot config supports two matcher styles:
|
|
50
|
-
* 1. Component reference: { visual: LeadingVisual }
|
|
51
|
-
* 2. Component + test fn: { block: [Description, props => props.variant === 'block'] }
|
|
52
|
-
*/
|
|
5
|
+
// slot config allows 2 options:
|
|
6
|
+
// 1. Component to match, example: { leadingVisual: LeadingVisual }
|
|
53
7
|
|
|
54
|
-
//
|
|
8
|
+
// 2. Component to match + a test function, example: { blockDescription: [Description, props => props.variant === 'block'] }
|
|
55
9
|
|
|
10
|
+
// We don't know what the props are yet, we set them later based on slot config
|
|
56
11
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
12
|
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
/** Check if a child element matches a slot config entry, either by direct type comparison or slot symbol. */
|
|
61
|
-
function childMatchesSlot(child, slotValue) {
|
|
62
|
-
if (Array.isArray(slotValue)) {
|
|
63
|
-
const [component, testFn] = slotValue;
|
|
64
|
-
return (child.type === component || isSlot(child, component)) && testFn(child.props);
|
|
65
|
-
}
|
|
66
|
-
return child.type === slotValue || isSlot(child, slotValue);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// --- Hook ---
|
|
13
|
+
// useful for narrowing types, third option is not possible
|
|
70
14
|
|
|
71
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Extract components from `children` so we can render them in different places,
|
|
17
|
+
* allowing us to implement components with SSR-compatible slot APIs.
|
|
18
|
+
* Note: We can only extract direct children, not nested ones.
|
|
19
|
+
*/
|
|
72
20
|
function useSlots(children, config) {
|
|
21
|
+
// Object mapping slot names to their elements
|
|
22
|
+
const slots = mapValues(config, () => undefined);
|
|
23
|
+
|
|
24
|
+
// Array of elements that are not slots
|
|
73
25
|
const rest = [];
|
|
74
26
|
const keys = Object.keys(config);
|
|
75
27
|
const values = Object.values(config);
|
|
76
|
-
const totalSlots = keys.length;
|
|
77
|
-
|
|
78
|
-
// Initialize all slot keys to undefined so callers can check `slots.x === undefined`
|
|
79
|
-
const slots = {};
|
|
80
|
-
for (let i = 0; i < totalSlots; i++) {
|
|
81
|
-
slots[keys[i]] = undefined;
|
|
82
|
-
}
|
|
83
|
-
let slotsFound = 0;
|
|
84
28
|
|
|
85
29
|
// eslint-disable-next-line github/array-foreach
|
|
86
30
|
React.Children.forEach(children, child => {
|
|
@@ -88,63 +32,41 @@ function useSlots(children, config) {
|
|
|
88
32
|
rest.push(child);
|
|
89
33
|
return;
|
|
90
34
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
35
|
+
const index = values.findIndex(value => {
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
const [component, testFn] = value;
|
|
38
|
+
return (child.type === component || isSlot(child, component)) && testFn(child.props);
|
|
39
|
+
} else {
|
|
40
|
+
return child.type === value || isSlot(child, value);
|
|
96
41
|
}
|
|
97
|
-
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
42
|
+
});
|
|
100
43
|
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
if (matchedIndex === -1) {
|
|
44
|
+
// If the child is not a slot, add it to the `rest` array
|
|
45
|
+
if (index === -1) {
|
|
104
46
|
rest.push(child);
|
|
105
47
|
return;
|
|
106
48
|
}
|
|
107
|
-
const slotKey = keys[
|
|
49
|
+
const slotKey = keys[index];
|
|
108
50
|
|
|
109
|
-
//
|
|
110
|
-
if (slots[slotKey]
|
|
111
|
-
|
|
112
|
-
process.env.NODE_ENV !== "production" ? warning(true, `Found duplicate "${String(slotKey)}" slot. Only the first will be rendered.`) : void 0;
|
|
113
|
-
}
|
|
51
|
+
// If slot is already filled, ignore duplicates
|
|
52
|
+
if (slots[slotKey]) {
|
|
53
|
+
process.env.NODE_ENV !== "production" ? warning(true, `Found duplicate "${String(slotKey)}" slot. Only the first will be rendered.`) : void 0;
|
|
114
54
|
return;
|
|
115
55
|
}
|
|
56
|
+
|
|
57
|
+
// If the child is a slot, add it to the `slots` object
|
|
58
|
+
|
|
116
59
|
slots[slotKey] = child;
|
|
117
|
-
slotsFound++;
|
|
118
60
|
});
|
|
119
61
|
return [slots, rest];
|
|
120
62
|
}
|
|
121
63
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
function findMatchingSlot(child, values, totalSlots) {
|
|
129
|
-
for (let i = 0; i < totalSlots; i++) {
|
|
130
|
-
if (childMatchesSlot(child, values[i])) return i;
|
|
131
|
-
}
|
|
132
|
-
return -1;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Dev-only: check if a child duplicates an already-filled slot.
|
|
137
|
-
* Returns true (and warns) if a duplicate is found, false otherwise.
|
|
138
|
-
* Used in the short-circuit path where all slots are already filled.
|
|
139
|
-
*/
|
|
140
|
-
function warnIfDuplicate(child, keys, values, totalSlots) {
|
|
141
|
-
for (let i = 0; i < totalSlots; i++) {
|
|
142
|
-
if (childMatchesSlot(child, values[i])) {
|
|
143
|
-
process.env.NODE_ENV !== "production" ? warning(true, `Found duplicate "${String(keys[i])}" slot. Only the first will be rendered.`) : void 0;
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return false;
|
|
64
|
+
/** Map the values of an object */
|
|
65
|
+
function mapValues(obj, fn) {
|
|
66
|
+
return Object.keys(obj).reduce((result, key) => {
|
|
67
|
+
result[key] = fn(obj[key]);
|
|
68
|
+
return result;
|
|
69
|
+
}, {});
|
|
148
70
|
}
|
|
149
71
|
|
|
150
72
|
export { useSlots };
|
|
@@ -1027,7 +1027,7 @@
|
|
|
1027
1027
|
},
|
|
1028
1028
|
{
|
|
1029
1029
|
"id": "components-autocomplete-features--in-overlay-with-custom-scroll-container-ref",
|
|
1030
|
-
"code": "() => {\n const scrollContainerRef = useRef<HTMLElement>(null)\n const inputRef = useRef<HTMLInputElement>(null)\n const triggerRef = useRef<HTMLButtonElement>(null)\n const [isOpen, setIsOpen] = useState(false)\n const [selectedItem, setSelectedItem] = useState<Datum>()\n const handleOpen = () => {\n setIsOpen(true)\n inputRef.current && inputRef.current.focus()\n }\n const selectChange = (item: Datum[] | Datum) => {\n setIsOpen(false)\n if (Array.isArray(item) && item.length) setSelectedItem(item[0])\n triggerRef.current?.focus()\n }\n return (\n <form className={classes.FormPadding}>\n Selected item: {selectedItem ? selectedItem.text : 'none'}\n <AnchoredOverlay\n open={isOpen}\n onOpen={handleOpen}\n onClose={() => setIsOpen(false)}\n width=\"large\"\n focusTrapSettings={{\n initialFocusRef: inputRef,\n }}\n side=\"inside-top\"\n anchorRef={triggerRef}\n renderAnchor={(props) => <Button {...props}
|
|
1030
|
+
"code": "() => {\n const scrollContainerRef = useRef<HTMLElement>(null)\n const inputRef = useRef<HTMLInputElement>(null)\n const triggerRef = useRef<HTMLButtonElement>(null)\n const [isOpen, setIsOpen] = useState(false)\n const [selectedItem, setSelectedItem] = useState<Datum>()\n const handleOpen = () => {\n setIsOpen(true)\n inputRef.current && inputRef.current.focus()\n }\n const selectChange = (item: Datum[] | Datum) => {\n setIsOpen(false)\n if (Array.isArray(item) && item.length) setSelectedItem(item[0])\n triggerRef.current?.focus()\n }\n return (\n <form className={classes.FormPadding}>\n <span id=\"selected-item-status\">\n Selected item: {selectedItem ? selectedItem.text : 'none'}\n </span>\n\n <AnchoredOverlay\n open={isOpen}\n onOpen={handleOpen}\n onClose={() => setIsOpen(false)}\n width=\"large\"\n focusTrapSettings={{\n initialFocusRef: inputRef,\n }}\n side=\"inside-top\"\n anchorRef={triggerRef}\n renderAnchor={(props) => (\n <Button {...props} aria-describedby=\"selected-item-status\">\n open overlay\n </Button>\n )}\n preventOverflow={false}\n >\n <Autocomplete>\n <div className={classes.OverlayFlexCol}>\n <div className={classes.OverlayInputBar}>\n <Autocomplete.Input\n ref={inputRef}\n className={classes.OverlayInput}\n block\n aria-label=\"Search\"\n />\n </div>\n <div\n ref={scrollContainerRef as RefObject<HTMLDivElement>}\n className={classes.OverlayScroll}\n >\n <Autocomplete.Menu\n items={items}\n selectedItemIds={[]}\n customScrollContainerRef={scrollContainerRef}\n aria-labelledby=\"autocompleteLabel\"\n onSelectedChange={selectChange}\n />\n </div>\n </div>\n </Autocomplete>\n </AnchoredOverlay>\n </form>\n )\n}"
|
|
1031
1031
|
},
|
|
1032
1032
|
{
|
|
1033
1033
|
"id": "components-autocomplete-features--in-a-dialog",
|
package/package.json
CHANGED