@primer/react 0.0.0-20260219144314 → 0.0.0-20260219144324

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,6 +1,6 @@
1
1
  # @primer/react
2
2
 
3
- ## 0.0.0-20260219144314
3
+ ## 0.0.0-20260219144324
4
4
 
5
5
  ### Minor Changes
6
6
 
@@ -24,10 +24,10 @@
24
24
 
25
25
  - [#7556](https://github.com/primer/react/pull/7556) [`6dc1858`](https://github.com/primer/react/commit/6dc1858538e3b7ea66b78dd9d37c391c530d78ae) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(ActionList): replace `:has([aria-disabled])` child scan with `data-is-disabled` attribute for faster style recalculation
26
26
 
27
- - [#7555](https://github.com/primer/react/pull/7555) [`91ac722`](https://github.com/primer/react/commit/91ac722ad4d2706d15d984aa5985aaf172911ce5) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(useSlots): short-circuit iteration and skip filled slots
28
-
29
27
  - [#7548](https://github.com/primer/react/pull/7548) [`d4b32c3`](https://github.com/primer/react/commit/d4b32c3672d3d5421711324654c1028527dcdf38) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(useIsMacOS): replace useState+useEffect with useSyncExternalStore to eliminate unnecessary re-render
30
28
 
29
+ - [#7547](https://github.com/primer/react/pull/7547) [`36ee7ce`](https://github.com/primer/react/commit/36ee7ce5d5fd074e137d11338d6b852721130d29) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(useScrollFlash): defer scroll read/write to requestAnimationFrame to avoid forced reflow
30
+
31
31
  ## 38.12.0
32
32
 
33
33
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"useScrollFlash.d.ts","sourceRoot":"","sources":["../../src/hooks/useScrollFlash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,kBAAkB,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,QAe7F"}
1
+ {"version":3,"file":"useScrollFlash.d.ts","sourceRoot":"","sources":["../../src/hooks/useScrollFlash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,kBAAkB,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,QAoB7F"}
@@ -15,11 +15,13 @@ function useScrollFlash(scrollContainerRef) {
15
15
  if (!scrollContainer) {
16
16
  return;
17
17
  }
18
- const currentScroll = scrollContainer.scrollTop;
19
- const maxScroll = scrollContainer.scrollHeight;
20
- const altScroll = currentScroll < Math.min(1, maxScroll) ? currentScroll + 1 : currentScroll - 1;
21
- scrollContainer.scrollTop = altScroll;
22
- scrollContainer.scrollTop = currentScroll;
18
+ const id = requestAnimationFrame(() => {
19
+ const currentScroll = scrollContainer.scrollTop;
20
+ const altScroll = currentScroll < 1 ? currentScroll + 1 : currentScroll - 1;
21
+ scrollContainer.scrollTop = altScroll;
22
+ scrollContainer.scrollTop = currentScroll;
23
+ });
24
+ return () => cancelAnimationFrame(id);
23
25
  };
24
26
  t1 = [scrollContainerRef];
25
27
  $[0] = scrollContainerRef;
@@ -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
- /** Extract slot components from children. See file header for details. */
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":[],"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from 'react';
2
2
  import { warning } from '../utils/warning.js';
3
3
  import { isSlot } from '../utils/is-slot.js';
4
4
 
5
- /**
6
- * useSlots - Extract slot components from children for SSR-compatible slot APIs.
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
- // --- Types ---
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
- // --- Matching ---
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
- /** Extract slot components from children. See file header for details. */
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
- // Short-circuit: all slots filled, no more matching needed
93
- if (slotsFound === totalSlots) {
94
- if (process.env.NODE_ENV !== "production" && warnIfDuplicate(child, keys, values, totalSlots)) {
95
- return;
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
- rest.push(child);
98
- return;
99
- }
42
+ });
100
43
 
101
- // Try to match child against a slot
102
- const matchedIndex = findMatchingSlot(child, values, totalSlots);
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[matchedIndex];
49
+ const slotKey = keys[index];
108
50
 
109
- // Duplicate: slot already filled by an earlier child
110
- if (slots[slotKey] !== undefined) {
111
- if (process.env.NODE_ENV !== "production") {
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
- // --- Helpers ---
123
-
124
- /**
125
- * Find the first slot config entry matching this child.
126
- * Returns the config index, or -1 if no match.
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 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@primer/react",
3
3
  "type": "module",
4
- "version": "0.0.0-20260219144314",
4
+ "version": "0.0.0-20260219144324",
5
5
  "description": "An implementation of GitHub's Primer Design System using React",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",