@primer/react 0.0.0-20260326162136 → 0.0.0-20260326174549
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-
|
|
3
|
+
## 0.0.0-20260326174549
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
@@ -18,15 +18,10 @@
|
|
|
18
18
|
|
|
19
19
|
- Fake entry to force publishing
|
|
20
20
|
|
|
21
|
-
- [#7695](https://github.com/primer/react/pull/7695) [`8cb69d1`](https://github.com/primer/react/commit/8cb69d13c85938f99a6daf52938fb30fc31a8ecb) Thanks [@mattcosta7](https://github.com/mattcosta7)! - perf(ThemeProvider): Reduce unnecessary renders and effect cascades
|
|
22
|
-
|
|
23
|
-
- Replace `useState` + `useEffect` SSR hydration handoff with `useSyncExternalStore` — eliminates post-hydration re-render
|
|
24
|
-
- Replace `useState` + `useEffect` in `useSystemColorMode` with `useSyncExternalStore` — eliminates effect gap and stale-then-update flicker
|
|
25
|
-
- Cache `getServerHandoff` DOM read + JSON.parse per ID (runs once, not on every call)
|
|
26
|
-
- Memoize context value object to prevent unnecessary re-renders of all consumers
|
|
27
|
-
|
|
28
21
|
- [#7683](https://github.com/primer/react/pull/7683) [`f05fcac`](https://github.com/primer/react/commit/f05fcacedafe8a9a1af9caa6a762403d47851250) Thanks [@liuliu-dev](https://github.com/liuliu-dev)! - Dialog: dynamically switch footer button layout based on available height.
|
|
29
22
|
|
|
23
|
+
- [#7549](https://github.com/primer/react/pull/7549) [`a5ccd0c`](https://github.com/primer/react/commit/a5ccd0c98e1153c0dadf48eaf5d50bd95e624428) Thanks [@hectahertz](https://github.com/hectahertz)! - perf(Autocomplete): eliminate re-renders on arrow-key navigation by converting highlightedItem to a ref
|
|
24
|
+
|
|
30
25
|
- [#7676](https://github.com/primer/react/pull/7676) [`35ca056`](https://github.com/primer/react/commit/35ca05690910ffb8286a2aeef01c043af377a6c2) Thanks [@copilot-swe-agent](https://github.com/apps/copilot-swe-agent)! - Remove link underline effect on hover for both Link (inline) and Button with variant link (no visuals)
|
|
31
26
|
|
|
32
27
|
- [#7631](https://github.com/primer/react/pull/7631) [`0ecaf6f`](https://github.com/primer/react/commit/0ecaf6f64c50235081416515b7d5b6b2e364b33f) Thanks [@copilot-swe-agent](https://github.com/apps/copilot-swe-agent)! - Add test to validate that the `muted` prop is not leaked as a bare DOM attribute on `Link`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AutocompleteMenu.d.ts","sourceRoot":"","sources":["../../src/Autocomplete/AutocompleteMenu.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyD,MAAM,OAAO,CAAA;AAK7E,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,eAAe,CAAA;AAGtD,OAAO,KAAK,EAAC,cAAc,EAAE,YAAY,EAAW,MAAM,gBAAgB,CAAA;AAI1E,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,wBAAwB,CAAA;AAOrD,KAAK,gBAAgB,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAI,CAAA;AAClD,MAAM,MAAM,oBAAoB,GAAG,YAAY,CAAC,mBAAmB,EAAE,IAAI,CAAC,GAAG;IAE3E,aAAa,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;IACrG,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,cAAc,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;CACvG,CAAA;
|
|
1
|
+
{"version":3,"file":"AutocompleteMenu.d.ts","sourceRoot":"","sources":["../../src/Autocomplete/AutocompleteMenu.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyD,MAAM,OAAO,CAAA;AAK7E,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,eAAe,CAAA;AAGtD,OAAO,KAAK,EAAC,cAAc,EAAE,YAAY,EAAW,MAAM,gBAAgB,CAAA;AAI1E,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,wBAAwB,CAAA;AAOrD,KAAK,gBAAgB,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAI,CAAA;AAClD,MAAM,MAAM,oBAAoB,GAAG,YAAY,CAAC,mBAAmB,EAAE,IAAI,CAAC,GAAG;IAE3E,aAAa,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;IACrG,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,cAAc,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;CACvG,CAAA;AAoED,KAAK,qBAAqB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,oBAAoB,GAAG;IAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;CAAC,CAAA;AAE3F,MAAM,MAAM,6BAA6B,CAAC,CAAC,SAAS,qBAAqB,IAAI;IAC3E;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,GAAG;QACf,aAAa,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,EAAE,UAAU,GAAG,eAAe,CAAC,KAAK,IAAI,CAAA;KACrE,CAAA;IAED;;OAEG;IACH,cAAc,CAAC,EAAE,KAAK,CAAC,SAAS,GAAG,KAAK,GAAG,IAAI,CAAA;IAE/C;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO,CAAA;IAE1C;;;OAGG;IACH,KAAK,EAAE,CAAC,EAAE,CAAA;IAEV;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IAEjB;;OAEG;IAEH,eAAe,EAAE,MAAM,EAAE,CAAA;IAEzB;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,CAAA;IAE5D;;OAEG;IACH,gBAAgB,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAA;IAExC;;OAEG;IACH,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAA;IAEtC;;OAEG;IACH,gBAAgB,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAA;IAEtC;;;;OAIG;IACH,wBAAwB,CAAC,EAAE,KAAK,CAAC,gBAAgB,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;IAErE,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAUD,iBAAS,gBAAgB,CAAC,CAAC,SAAS,qBAAqB,EAAE,KAAK,EAAE,6BAA6B,CAAC,CAAC,CAAC,qBAmRjG;kBAnRQ,gBAAgB;;;;AAuRzB,MAAM,MAAM,qBAAqB,GAAG,cAAc,CAAC,OAAO,gBAAgB,CAAC,CAAA;AAC3E,eAAe,gBAAgB,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useContext, useRef, useState, useMemo
|
|
1
|
+
import React, { useContext, useRef, useEffect, useState, useMemo } from 'react';
|
|
2
2
|
import { debounce } from '@github/mini-throttle';
|
|
3
3
|
import { announce } from '@primer/live-region-element';
|
|
4
4
|
import { scrollIntoView } from '@primer/behaviors';
|
|
@@ -33,6 +33,41 @@ function getdefaultCheckedSelectionChange(setInputValueFn) {
|
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
const isItemSelected = (itemId, selectedItemIds) => selectedItemIds.includes(itemId);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Memoized wrapper around ActionList.Item that skips re-rendering when props
|
|
39
|
+
* haven't changed. Combined with the `highlightedItem` ref (instead of state),
|
|
40
|
+
* arrow-key navigation no longer triggers re-renders of the item list.
|
|
41
|
+
*/
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
const MemoizedAutocompleteItem = /*#__PURE__*/React.memo(function MemoizedAutocompleteItem({
|
|
44
|
+
item
|
|
45
|
+
}) {
|
|
46
|
+
const {
|
|
47
|
+
id,
|
|
48
|
+
onAction,
|
|
49
|
+
children,
|
|
50
|
+
text,
|
|
51
|
+
leadingVisual: LeadingVisual,
|
|
52
|
+
trailingVisual: TrailingVisual,
|
|
53
|
+
role,
|
|
54
|
+
...itemProps
|
|
55
|
+
} = item;
|
|
56
|
+
// Remove key from itemProps so it doesn't spread onto the DOM
|
|
57
|
+
delete itemProps.key;
|
|
58
|
+
return /*#__PURE__*/jsxs(ActionList.Item, {
|
|
59
|
+
onSelect: () => onAction(item),
|
|
60
|
+
...itemProps,
|
|
61
|
+
id: id,
|
|
62
|
+
"data-id": id,
|
|
63
|
+
role: role,
|
|
64
|
+
children: [LeadingVisual && /*#__PURE__*/jsx(ActionList.LeadingVisual, {
|
|
65
|
+
children: isElement(LeadingVisual) ? LeadingVisual : /*#__PURE__*/jsx(LeadingVisual, {})
|
|
66
|
+
}), children !== null && children !== void 0 ? children : text, TrailingVisual && /*#__PURE__*/jsx(ActionList.TrailingVisual, {
|
|
67
|
+
children: isElement(TrailingVisual) ? TrailingVisual : /*#__PURE__*/jsx(TrailingVisual, {})
|
|
68
|
+
})]
|
|
69
|
+
});
|
|
70
|
+
});
|
|
36
71
|
function getItemById(itemId, items) {
|
|
37
72
|
return items.find(item => item.id === itemId);
|
|
38
73
|
}
|
|
@@ -85,7 +120,19 @@ function AutocompleteMenu(props) {
|
|
|
85
120
|
} = props;
|
|
86
121
|
const listContainerRef = useRef(null);
|
|
87
122
|
const allItemsToRenderRef = useRef([]);
|
|
88
|
-
|
|
123
|
+
// Using a ref instead of state to avoid triggering a re-render of all items
|
|
124
|
+
// on every arrow-key press. The active styling was already handled via DOM by
|
|
125
|
+
// useFocusZone (data-is-active-descendant) before this change, but the
|
|
126
|
+
// previous useState caused React to re-render the entire item list anyway.
|
|
127
|
+
const highlightedItemRef = useRef();
|
|
128
|
+
const deferredInputValueRef = useRef(deferredInputValue);
|
|
129
|
+
const selectedItemIdsRef = useRef(selectedItemIds);
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
deferredInputValueRef.current = deferredInputValue;
|
|
132
|
+
}, [deferredInputValue]);
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
selectedItemIdsRef.current = selectedItemIds;
|
|
135
|
+
}, [selectedItemIds]);
|
|
89
136
|
const [sortedItemIds, setSortedItemIds] = useState(items.map(({
|
|
90
137
|
id: itemId
|
|
91
138
|
}) => itemId));
|
|
@@ -95,7 +142,6 @@ function AutocompleteMenu(props) {
|
|
|
95
142
|
...selectableItem,
|
|
96
143
|
role: 'option',
|
|
97
144
|
id: selectableItem.id,
|
|
98
|
-
active: (highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.id) === selectableItem.id,
|
|
99
145
|
selected: selectionVariant === 'multiple' ? selectedItemIds.includes(selectableItem.id) : undefined,
|
|
100
146
|
onAction: item => {
|
|
101
147
|
const otherSelectedItemIds = selectedItemIds.filter(selectedItemId => selectedItemId !== item.id);
|
|
@@ -112,7 +158,7 @@ function AutocompleteMenu(props) {
|
|
|
112
158
|
}
|
|
113
159
|
}
|
|
114
160
|
};
|
|
115
|
-
}), [
|
|
161
|
+
}), [items, selectedItemIds, inputRef, onSelectedChange, selectionVariant, setAutocompleteSuggestion, setInputValue, setShowMenu]);
|
|
116
162
|
const itemSortOrderData = useMemo(() => sortedItemIds.reduce((acc, curr, i) => {
|
|
117
163
|
acc[curr] = i;
|
|
118
164
|
return acc;
|
|
@@ -126,7 +172,6 @@ function AutocompleteMenu(props) {
|
|
|
126
172
|
...addNewItem,
|
|
127
173
|
role: 'option',
|
|
128
174
|
key: addNewItem.id,
|
|
129
|
-
active: (highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.id) === addNewItem.id,
|
|
130
175
|
selected: selectionVariant === 'multiple' ? selectedItemIds.includes(addNewItem.id) : undefined,
|
|
131
176
|
leadingVisual: () => /*#__PURE__*/jsx(PlusIcon, {}),
|
|
132
177
|
onAction: item => {
|
|
@@ -141,7 +186,7 @@ function AutocompleteMenu(props) {
|
|
|
141
186
|
setAutocompleteSuggestion('');
|
|
142
187
|
}
|
|
143
188
|
}
|
|
144
|
-
}] : [])], [sortedAndFilteredItemsToRender, addNewItem, setAutocompleteSuggestion, selectionVariant, setInputValue, generatedUniqueId,
|
|
189
|
+
}] : [])], [sortedAndFilteredItemsToRender, addNewItem, setAutocompleteSuggestion, selectionVariant, setInputValue, generatedUniqueId, selectedItemIds]);
|
|
145
190
|
React.useEffect(() => {
|
|
146
191
|
allItemsToRenderRef.current = allItemsToRender;
|
|
147
192
|
});
|
|
@@ -159,13 +204,28 @@ function AutocompleteMenu(props) {
|
|
|
159
204
|
activeDescendantFocus: inputRef,
|
|
160
205
|
onActiveDescendantChanged: (current, _previous, directlyActivated) => {
|
|
161
206
|
activeDescendantRef.current = current || null;
|
|
207
|
+
|
|
208
|
+
// Active styling was already handled via DOM before this change:
|
|
209
|
+
// useFocusZone sets `data-is-active-descendant` on the active element,
|
|
210
|
+
// and ActionList styles that attribute in CSS. The key change is that
|
|
211
|
+
// we no longer call setHighlightedItem (state), avoiding re-renders.
|
|
212
|
+
|
|
162
213
|
if (current) {
|
|
214
|
+
var _selectedItem$text;
|
|
215
|
+
const currentLi = current.closest('li');
|
|
163
216
|
const selectedItem = allItemsToRenderRef.current.find(item => {
|
|
164
|
-
|
|
165
|
-
return item.id === ((_current$closest = current.closest('li')) === null || _current$closest === void 0 ? void 0 : _current$closest.getAttribute('data-id'));
|
|
217
|
+
return item.id === (currentLi === null || currentLi === void 0 ? void 0 : currentLi.getAttribute('data-id'));
|
|
166
218
|
});
|
|
167
|
-
|
|
219
|
+
highlightedItemRef.current = selectedItem;
|
|
168
220
|
setIsMenuDirectlyActivated(directlyActivated);
|
|
221
|
+
|
|
222
|
+
// Update autocomplete suggestion inline (moved from the useEffect
|
|
223
|
+
// that previously depended on highlightedItem state)
|
|
224
|
+
if (selectedItem !== null && selectedItem !== void 0 && (_selectedItem$text = selectedItem.text) !== null && _selectedItem$text !== void 0 && _selectedItem$text.startsWith(deferredInputValueRef.current) && !selectedItemIdsRef.current.includes(selectedItem.id)) {
|
|
225
|
+
setAutocompleteSuggestion(selectedItem.text);
|
|
226
|
+
} else {
|
|
227
|
+
setAutocompleteSuggestion('');
|
|
228
|
+
}
|
|
169
229
|
}
|
|
170
230
|
if (current && customScrollContainerRef && customScrollContainerRef.current && directlyActivated) {
|
|
171
231
|
scrollIntoView(current, customScrollContainerRef.current, menuScrollMargins);
|
|
@@ -175,15 +235,16 @@ function AutocompleteMenu(props) {
|
|
|
175
235
|
}
|
|
176
236
|
}, [loading]);
|
|
177
237
|
useEffect(() => {
|
|
178
|
-
var
|
|
179
|
-
//
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
|
|
238
|
+
var _highlighted$text;
|
|
239
|
+
// When the input value changes, update the autocomplete suggestion based
|
|
240
|
+
// on the currently highlighted item (tracked via ref, not state).
|
|
241
|
+
const highlighted = highlightedItemRef.current;
|
|
242
|
+
if (highlighted !== null && highlighted !== void 0 && (_highlighted$text = highlighted.text) !== null && _highlighted$text !== void 0 && _highlighted$text.startsWith(deferredInputValue) && !selectedItemIds.includes(highlighted.id)) {
|
|
243
|
+
setAutocompleteSuggestion(highlighted.text);
|
|
183
244
|
} else {
|
|
184
245
|
setAutocompleteSuggestion('');
|
|
185
246
|
}
|
|
186
|
-
}, [
|
|
247
|
+
}, [deferredInputValue, selectedItemIds, setAutocompleteSuggestion]);
|
|
187
248
|
useEffect(() => {
|
|
188
249
|
const itemIdSortResult = [...sortedItemIds].sort(sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(itemId => isItemSelected(itemId, selectedItemIds)));
|
|
189
250
|
const sortResultMatchesState = itemIdSortResult.length === sortedItemIds.length && itemIdSortResult.every((element, index) => element === sortedItemIds[index]);
|
|
@@ -215,29 +276,10 @@ function AutocompleteMenu(props) {
|
|
|
215
276
|
id: `${id}-listbox`,
|
|
216
277
|
"aria-labelledby": ariaLabelledBy,
|
|
217
278
|
children: allItemsToRender.map(item => {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
text,
|
|
223
|
-
leadingVisual: LeadingVisual,
|
|
224
|
-
trailingVisual: TrailingVisual,
|
|
225
|
-
key,
|
|
226
|
-
role,
|
|
227
|
-
...itemProps
|
|
228
|
-
} = item;
|
|
229
|
-
return /*#__PURE__*/jsxs(ActionList.Item, {
|
|
230
|
-
onSelect: () => onAction(item),
|
|
231
|
-
...itemProps,
|
|
232
|
-
id: id,
|
|
233
|
-
"data-id": id,
|
|
234
|
-
role: role,
|
|
235
|
-
children: [LeadingVisual && /*#__PURE__*/jsx(ActionList.LeadingVisual, {
|
|
236
|
-
children: isElement(LeadingVisual) ? LeadingVisual : /*#__PURE__*/jsx(LeadingVisual, {})
|
|
237
|
-
}), children !== null && children !== void 0 ? children : text, TrailingVisual && /*#__PURE__*/jsx(ActionList.TrailingVisual, {
|
|
238
|
-
children: isElement(TrailingVisual) ? TrailingVisual : /*#__PURE__*/jsx(TrailingVisual, {})
|
|
239
|
-
})]
|
|
240
|
-
}, key !== null && key !== void 0 ? key : id);
|
|
279
|
+
var _item$key;
|
|
280
|
+
return /*#__PURE__*/jsx(MemoizedAutocompleteItem, {
|
|
281
|
+
item: item
|
|
282
|
+
}, (_item$key = item.key) !== null && _item$key !== void 0 ? _item$key : item.id);
|
|
241
283
|
})
|
|
242
284
|
}) : emptyStateText !== false && emptyStateText !== null ? /*#__PURE__*/jsx("div", {
|
|
243
285
|
className: classes.EmptyStateWrapper,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../src/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../src/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAOzB,eAAO,MAAM,gBAAgB,QAAQ,CAAA;AAKrC,MAAM,MAAM,KAAK,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,CAAA;AACxC,KAAK,SAAS,GAAG,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;AACnD,MAAM,MAAM,iBAAiB,GAAG,SAAS,GAAG,MAAM,CAAA;AAElD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,CAAC,EAAE,iBAAiB,CAAA;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B,CAAA;AA8BD,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAsF/E,CAAA;AAED,wBAAgB,QAAQ;YAnHd,KAAK;kBACC,MAAM;gBACR,iBAAiB;wBACT,SAAS;0BACP,MAAM;gBAChB,MAAM;kBACJ,MAAM;kBACN,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;kBACvD,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;oBAC1C,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;EA4G7D;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,UAG1F;AAqGD,eAAe,aAAa,CAAA"}
|
package/dist/ThemeProvider.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { c } from 'react-compiler-runtime';
|
|
2
2
|
import React from 'react';
|
|
3
|
+
import ReactDOM from 'react-dom';
|
|
3
4
|
import theme from './theme.js';
|
|
4
5
|
import deepmerge from 'deepmerge';
|
|
5
6
|
import { useSyncedState } from './hooks/useSyncedState.js';
|
|
@@ -19,115 +20,71 @@ const ThemeContext = /*#__PURE__*/React.createContext({
|
|
|
19
20
|
});
|
|
20
21
|
|
|
21
22
|
// inspired from __NEXT_DATA__, we use application/json to avoid CSRF policy with inline scripts
|
|
22
|
-
const serverHandoffCache = new Map();
|
|
23
|
-
const emptyHandoff = {};
|
|
24
23
|
const getServerHandoff = id => {
|
|
25
|
-
if (typeof document === 'undefined') return emptyHandoff;
|
|
26
|
-
const cached = serverHandoffCache.get(id);
|
|
27
|
-
if (cached !== undefined) return cached;
|
|
28
24
|
try {
|
|
29
25
|
var _document$getElementB;
|
|
30
26
|
const serverData = (_document$getElementB = document.getElementById(`__PRIMER_DATA_${id}__`)) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.textContent;
|
|
31
|
-
if (serverData)
|
|
32
|
-
const parsed = JSON.parse(serverData);
|
|
33
|
-
serverHandoffCache.set(id, parsed);
|
|
34
|
-
return parsed;
|
|
35
|
-
}
|
|
27
|
+
if (serverData) return JSON.parse(serverData);
|
|
36
28
|
} catch (_error) {
|
|
37
29
|
// if document/element does not exist or JSON is invalid, suppress error
|
|
38
30
|
}
|
|
39
|
-
|
|
40
|
-
serverHandoffCache.set(id, empty);
|
|
41
|
-
return empty;
|
|
31
|
+
return {};
|
|
42
32
|
};
|
|
43
|
-
const
|
|
44
|
-
|
|
33
|
+
const ThemeProvider = ({
|
|
34
|
+
children,
|
|
35
|
+
...props
|
|
36
|
+
}) => {
|
|
45
37
|
var _ref, _props$colorMode, _ref2, _props$dayScheme, _ref3, _props$nightScheme;
|
|
46
|
-
|
|
47
|
-
let children;
|
|
48
|
-
let props;
|
|
49
|
-
if ($[0] !== t0) {
|
|
50
|
-
({
|
|
51
|
-
children,
|
|
52
|
-
...props
|
|
53
|
-
} = t0);
|
|
54
|
-
$[0] = t0;
|
|
55
|
-
$[1] = children;
|
|
56
|
-
$[2] = props;
|
|
57
|
-
} else {
|
|
58
|
-
children = $[1];
|
|
59
|
-
props = $[2];
|
|
60
|
-
}
|
|
38
|
+
// Get fallback values from parent ThemeProvider (if exists)
|
|
61
39
|
const {
|
|
62
40
|
theme: fallbackTheme,
|
|
63
41
|
colorMode: fallbackColorMode,
|
|
64
42
|
dayScheme: fallbackDayScheme,
|
|
65
43
|
nightScheme: fallbackNightScheme
|
|
66
44
|
} = useTheme();
|
|
45
|
+
|
|
46
|
+
// Initialize state
|
|
67
47
|
const theme$1 = fallbackTheme !== null && fallbackTheme !== void 0 ? fallbackTheme : theme;
|
|
68
48
|
const uniqueDataId = useId();
|
|
49
|
+
const {
|
|
50
|
+
resolvedServerColorMode
|
|
51
|
+
} = getServerHandoff(uniqueDataId);
|
|
52
|
+
const resolvedColorModePassthrough = React.useRef(resolvedServerColorMode);
|
|
69
53
|
const [colorMode, setColorMode] = useSyncedState((_ref = (_props$colorMode = props.colorMode) !== null && _props$colorMode !== void 0 ? _props$colorMode : fallbackColorMode) !== null && _ref !== void 0 ? _ref : defaultColorMode);
|
|
70
54
|
const [dayScheme, setDayScheme] = useSyncedState((_ref2 = (_props$dayScheme = props.dayScheme) !== null && _props$dayScheme !== void 0 ? _props$dayScheme : fallbackDayScheme) !== null && _ref2 !== void 0 ? _ref2 : defaultDayScheme);
|
|
71
55
|
const [nightScheme, setNightScheme] = useSyncedState((_ref3 = (_props$nightScheme = props.nightScheme) !== null && _props$nightScheme !== void 0 ? _props$nightScheme : fallbackNightScheme) !== null && _ref3 !== void 0 ? _ref3 : defaultNightScheme);
|
|
72
56
|
const systemColorMode = useSystemColorMode();
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
$[3] = colorMode;
|
|
77
|
-
$[4] = systemColorMode;
|
|
78
|
-
$[5] = t1;
|
|
79
|
-
} else {
|
|
80
|
-
t1 = $[5];
|
|
81
|
-
}
|
|
82
|
-
const clientColorMode = t1;
|
|
83
|
-
let t2;
|
|
84
|
-
if ($[6] !== clientColorMode) {
|
|
85
|
-
t2 = () => clientColorMode;
|
|
86
|
-
$[6] = clientColorMode;
|
|
87
|
-
$[7] = t2;
|
|
88
|
-
} else {
|
|
89
|
-
t2 = $[7];
|
|
90
|
-
}
|
|
91
|
-
let t3;
|
|
92
|
-
if ($[8] !== clientColorMode || $[9] !== uniqueDataId) {
|
|
93
|
-
t3 = () => {
|
|
94
|
-
var _getServerHandoff$res;
|
|
95
|
-
return (_getServerHandoff$res = getServerHandoff(uniqueDataId).resolvedServerColorMode) !== null && _getServerHandoff$res !== void 0 ? _getServerHandoff$res : clientColorMode;
|
|
96
|
-
};
|
|
97
|
-
$[8] = clientColorMode;
|
|
98
|
-
$[9] = uniqueDataId;
|
|
99
|
-
$[10] = t3;
|
|
100
|
-
} else {
|
|
101
|
-
t3 = $[10];
|
|
102
|
-
}
|
|
103
|
-
const resolvedColorMode = React.useSyncExternalStore(emptySubscribe, t2, t3);
|
|
104
|
-
let t4;
|
|
105
|
-
if ($[11] !== dayScheme || $[12] !== nightScheme || $[13] !== resolvedColorMode) {
|
|
106
|
-
t4 = chooseColorScheme(resolvedColorMode, dayScheme, nightScheme);
|
|
107
|
-
$[11] = dayScheme;
|
|
108
|
-
$[12] = nightScheme;
|
|
109
|
-
$[13] = resolvedColorMode;
|
|
110
|
-
$[14] = t4;
|
|
111
|
-
} else {
|
|
112
|
-
t4 = $[14];
|
|
113
|
-
}
|
|
114
|
-
const colorScheme = t4;
|
|
115
|
-
let t5;
|
|
116
|
-
if ($[15] !== colorScheme || $[16] !== theme$1) {
|
|
117
|
-
t5 = applyColorScheme(theme$1, colorScheme);
|
|
118
|
-
$[15] = colorScheme;
|
|
119
|
-
$[16] = theme$1;
|
|
120
|
-
$[17] = t5;
|
|
121
|
-
} else {
|
|
122
|
-
t5 = $[17];
|
|
123
|
-
}
|
|
57
|
+
// eslint-disable-next-line react-hooks/refs
|
|
58
|
+
const resolvedColorMode = resolvedColorModePassthrough.current || resolveColorMode(colorMode, systemColorMode);
|
|
59
|
+
const colorScheme = chooseColorScheme(resolvedColorMode, dayScheme, nightScheme);
|
|
124
60
|
const {
|
|
125
61
|
resolvedTheme,
|
|
126
62
|
resolvedColorScheme
|
|
127
|
-
} =
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
63
|
+
} = React.useMemo(() => applyColorScheme(theme$1, colorScheme), [theme$1, colorScheme]);
|
|
64
|
+
|
|
65
|
+
// this effect will only run on client
|
|
66
|
+
React.useEffect(function updateColorModeAfterServerPassthrough() {
|
|
67
|
+
const resolvedColorModeOnClient = resolveColorMode(colorMode, systemColorMode);
|
|
68
|
+
if (resolvedColorModePassthrough.current) {
|
|
69
|
+
// if the resolved color mode passed on from the server is not the resolved color mode on client, change it!
|
|
70
|
+
if (resolvedColorModePassthrough.current !== resolvedColorModeOnClient) {
|
|
71
|
+
window.setTimeout(() => {
|
|
72
|
+
// use ReactDOM.flushSync to prevent automatic batching of state updates since React 18
|
|
73
|
+
// ref: https://github.com/reactwg/react-18/discussions/21
|
|
74
|
+
ReactDOM.flushSync(() => {
|
|
75
|
+
// override colorMode to whatever is resolved on the client to get a re-render
|
|
76
|
+
setColorMode(resolvedColorModeOnClient);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// immediately after that, set the colorMode to what the user passed to respond to system color mode changes
|
|
80
|
+
setColorMode(colorMode);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
resolvedColorModePassthrough.current = null;
|
|
84
|
+
}
|
|
85
|
+
}, [colorMode, systemColorMode, setColorMode]);
|
|
86
|
+
return /*#__PURE__*/jsx(ThemeContext.Provider, {
|
|
87
|
+
value: {
|
|
131
88
|
theme: resolvedTheme,
|
|
132
89
|
colorScheme,
|
|
133
90
|
colorMode,
|
|
@@ -138,72 +95,24 @@ const ThemeProvider = t0 => {
|
|
|
138
95
|
setColorMode,
|
|
139
96
|
setDayScheme,
|
|
140
97
|
setNightScheme
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
$[20] = dayScheme;
|
|
145
|
-
$[21] = nightScheme;
|
|
146
|
-
$[22] = resolvedColorMode;
|
|
147
|
-
$[23] = resolvedColorScheme;
|
|
148
|
-
$[24] = resolvedTheme;
|
|
149
|
-
$[25] = setColorMode;
|
|
150
|
-
$[26] = setDayScheme;
|
|
151
|
-
$[27] = setNightScheme;
|
|
152
|
-
$[28] = t6;
|
|
153
|
-
} else {
|
|
154
|
-
t6 = $[28];
|
|
155
|
-
}
|
|
156
|
-
const contextValue = t6;
|
|
157
|
-
const t7 = colorMode === "auto" ? "auto" : colorScheme.includes("dark") ? "dark" : "light";
|
|
158
|
-
let t8;
|
|
159
|
-
if ($[29] !== props.preventSSRMismatch || $[30] !== resolvedColorMode || $[31] !== uniqueDataId) {
|
|
160
|
-
t8 = props.preventSSRMismatch ? /*#__PURE__*/jsx("script", {
|
|
161
|
-
type: "application/json",
|
|
162
|
-
id: `__PRIMER_DATA_${uniqueDataId}__`,
|
|
163
|
-
dangerouslySetInnerHTML: {
|
|
164
|
-
__html: JSON.stringify({
|
|
165
|
-
resolvedServerColorMode: resolvedColorMode
|
|
166
|
-
})
|
|
167
|
-
}
|
|
168
|
-
}) : null;
|
|
169
|
-
$[29] = props.preventSSRMismatch;
|
|
170
|
-
$[30] = resolvedColorMode;
|
|
171
|
-
$[31] = uniqueDataId;
|
|
172
|
-
$[32] = t8;
|
|
173
|
-
} else {
|
|
174
|
-
t8 = $[32];
|
|
175
|
-
}
|
|
176
|
-
let t9;
|
|
177
|
-
if ($[33] !== children || $[34] !== dayScheme || $[35] !== nightScheme || $[36] !== t7 || $[37] !== t8) {
|
|
178
|
-
t9 = /*#__PURE__*/jsxs("div", {
|
|
179
|
-
"data-color-mode": t7,
|
|
98
|
+
},
|
|
99
|
+
children: /*#__PURE__*/jsxs("div", {
|
|
100
|
+
"data-color-mode": colorMode === 'auto' ? 'auto' : colorScheme.includes('dark') ? 'dark' : 'light',
|
|
180
101
|
"data-light-theme": dayScheme,
|
|
181
102
|
"data-dark-theme": nightScheme,
|
|
182
|
-
children: [children,
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
let t10;
|
|
194
|
-
if ($[39] !== contextValue || $[40] !== t9) {
|
|
195
|
-
t10 = /*#__PURE__*/jsx(ThemeContext.Provider, {
|
|
196
|
-
value: contextValue,
|
|
197
|
-
children: t9
|
|
198
|
-
});
|
|
199
|
-
$[39] = contextValue;
|
|
200
|
-
$[40] = t9;
|
|
201
|
-
$[41] = t10;
|
|
202
|
-
} else {
|
|
203
|
-
t10 = $[41];
|
|
204
|
-
}
|
|
205
|
-
return t10;
|
|
103
|
+
children: [children, props.preventSSRMismatch ? /*#__PURE__*/jsx("script", {
|
|
104
|
+
type: "application/json",
|
|
105
|
+
id: `__PRIMER_DATA_${uniqueDataId}__`,
|
|
106
|
+
dangerouslySetInnerHTML: {
|
|
107
|
+
__html: JSON.stringify({
|
|
108
|
+
resolvedServerColorMode: resolvedColorMode
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
}) : null]
|
|
112
|
+
})
|
|
113
|
+
});
|
|
206
114
|
};
|
|
115
|
+
ThemeProvider.displayName = "ThemeProvider";
|
|
207
116
|
function useTheme() {
|
|
208
117
|
return React.useContext(ThemeContext);
|
|
209
118
|
}
|
|
@@ -215,25 +124,57 @@ function useColorSchemeVar(values, fallback) {
|
|
|
215
124
|
const colorScheme = t0 === undefined ? "" : t0;
|
|
216
125
|
return (_values$colorScheme = values[colorScheme]) !== null && _values$colorScheme !== void 0 ? _values$colorScheme : fallback;
|
|
217
126
|
}
|
|
218
|
-
function subscribeToSystemColorMode(callback) {
|
|
219
|
-
var _window, _window$matchMedia;
|
|
220
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
221
|
-
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)');
|
|
222
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
223
|
-
media === null || media === void 0 ? void 0 : media.addEventListener('change', callback);
|
|
224
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
225
|
-
return () => media === null || media === void 0 ? void 0 : media.removeEventListener('change', callback);
|
|
226
|
-
}
|
|
227
127
|
function useSystemColorMode() {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
128
|
+
const $ = c(2);
|
|
129
|
+
const [systemColorMode, setSystemColorMode] = React.useState(getSystemColorMode);
|
|
130
|
+
let t0;
|
|
131
|
+
let t1;
|
|
132
|
+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
|
133
|
+
t0 = () => {
|
|
134
|
+
var _window, _window$matchMedia;
|
|
135
|
+
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)");
|
|
136
|
+
const matchesMediaToColorMode = function matchesMediaToColorMode(matches) {
|
|
137
|
+
return matches ? "night" : "day";
|
|
138
|
+
};
|
|
139
|
+
const handleChange = function handleChange(event) {
|
|
140
|
+
const isNight = event.matches;
|
|
141
|
+
setSystemColorMode(matchesMediaToColorMode(isNight));
|
|
142
|
+
};
|
|
143
|
+
if (media) {
|
|
144
|
+
const isNight_0 = media.matches;
|
|
145
|
+
setSystemColorMode(matchesMediaToColorMode(isNight_0));
|
|
146
|
+
if (media.addEventListener !== undefined) {
|
|
147
|
+
media.addEventListener("change", handleChange);
|
|
148
|
+
return function cleanup() {
|
|
149
|
+
media.removeEventListener("change", handleChange);
|
|
150
|
+
};
|
|
151
|
+
} else {
|
|
152
|
+
if (media.addListener !== undefined) {
|
|
153
|
+
media.addListener(handleChange);
|
|
154
|
+
return function cleanup() {
|
|
155
|
+
media.removeListener(handleChange);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
t1 = [];
|
|
162
|
+
$[0] = t0;
|
|
163
|
+
$[1] = t1;
|
|
164
|
+
} else {
|
|
165
|
+
t0 = $[0];
|
|
166
|
+
t1 = $[1];
|
|
167
|
+
}
|
|
168
|
+
React.useEffect(t0, t1);
|
|
169
|
+
return systemColorMode;
|
|
232
170
|
}
|
|
233
171
|
function getSystemColorMode() {
|
|
234
|
-
var
|
|
172
|
+
var _window$matchMedia2, _window2, _window$matchMedia2$c;
|
|
235
173
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
236
|
-
|
|
174
|
+
if (typeof window !== 'undefined' && (_window$matchMedia2 = (_window2 = window).matchMedia) !== null && _window$matchMedia2 !== void 0 && (_window$matchMedia2$c = _window$matchMedia2.call(_window2, '(prefers-color-scheme: dark)')) !== null && _window$matchMedia2$c !== void 0 && _window$matchMedia2$c.matches) {
|
|
175
|
+
return 'night';
|
|
176
|
+
}
|
|
177
|
+
return 'day';
|
|
237
178
|
}
|
|
238
179
|
function resolveColorMode(colorMode, systemColorMode) {
|
|
239
180
|
switch (colorMode) {
|
package/package.json
CHANGED