chiplet 0.4.0 → 0.5.0

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.
Files changed (106) hide show
  1. package/dist/assets/{positioning.css.ts.vanilla-BVOg9Lwj.css → positioning.css.ts.vanilla-BLJ90UCF.css} +12 -12
  2. package/dist/component/button/LinkButton.js +1 -1
  3. package/dist/component/button/LinkButton.js.map +1 -1
  4. package/dist/component/button/index.js +1 -0
  5. package/dist/component/button/index.js.map +1 -1
  6. package/dist/component/checkbox/Checkbox.js +5 -1
  7. package/dist/component/checkbox/Checkbox.js.map +1 -1
  8. package/dist/component/checkbox/Radio.js +5 -1
  9. package/dist/component/checkbox/Radio.js.map +1 -1
  10. package/dist/component/checkbox/Switch.js +1 -1
  11. package/dist/component/checkbox/Switch.js.map +1 -1
  12. package/dist/component/header/Header.js +1 -1
  13. package/dist/component/header/Header.js.map +1 -1
  14. package/dist/component/index.js +1 -0
  15. package/dist/component/index.js.map +1 -1
  16. package/dist/component/input/Input.js +5 -1
  17. package/dist/component/input/Input.js.map +1 -1
  18. package/dist/component/input/SearchInput.js +4 -7
  19. package/dist/component/input/SearchInput.js.map +1 -1
  20. package/dist/component/menu/renderMenuItem.js +5 -1
  21. package/dist/component/menu/renderMenuItem.js.map +1 -1
  22. package/dist/component/select/Group.js +1 -1
  23. package/dist/component/select/Group.js.map +1 -1
  24. package/dist/component/select/Option.js +1 -1
  25. package/dist/component/select/Option.js.map +1 -1
  26. package/dist/hook/accessibility/keyboard/context/index.js.map +1 -1
  27. package/dist/hook/accessibility/keyboard/hook/useLastFocused.js.map +1 -1
  28. package/dist/hook/document/useHighlight.js +2 -4
  29. package/dist/hook/document/useHighlight.js.map +1 -1
  30. package/dist/hook/form/useGroup.js +17 -16
  31. package/dist/hook/form/useGroup.js.map +1 -1
  32. package/dist/hook/observable/useObservable.js +6 -1
  33. package/dist/hook/observable/useObservable.js.map +1 -1
  34. package/dist/hook/popover/style/positioning.css.js +1 -1
  35. package/dist/hook/popover/useAnchorPositioning.js +1 -1
  36. package/dist/hook/popover/useAnchorPositioning.js.map +1 -1
  37. package/dist/hook/popover/useLightDismiss.js +15 -0
  38. package/dist/hook/popover/useLightDismiss.js.map +1 -1
  39. package/dist/hook/popover/useOverlay.js +8 -5
  40. package/dist/hook/popover/useOverlay.js.map +1 -1
  41. package/dist/hook/popover/usePopover.js +11 -11
  42. package/dist/hook/popover/usePopover.js.map +1 -1
  43. package/dist/hook/popover/usePositioning.js +7 -12
  44. package/dist/hook/popover/usePositioning.js.map +1 -1
  45. package/dist/hook/popover/useReturnFocus.js.map +1 -1
  46. package/dist/hook/popover/useTooltip.js +4 -2
  47. package/dist/hook/popover/useTooltip.js.map +1 -1
  48. package/dist/hook/promise/usePromise.js +1 -1
  49. package/dist/hook/promise/usePromise.js.map +1 -1
  50. package/dist/hook/react/useActivate.js +3 -0
  51. package/dist/hook/react/useActivate.js.map +1 -1
  52. package/dist/hook/react/useConst.js +3 -2
  53. package/dist/hook/react/useConst.js.map +1 -1
  54. package/dist/hook/react/useRefCallback.js.map +1 -1
  55. package/dist/index.js +3 -0
  56. package/dist/index.js.map +1 -1
  57. package/dist/infra/logging/logger.js +6 -2
  58. package/dist/infra/logging/logger.js.map +1 -1
  59. package/dist/theme/index.js +2 -0
  60. package/dist/theme/index.js.map +1 -1
  61. package/dist/theme/useSystemColorScheme.js +16 -16
  62. package/dist/theme/useSystemColorScheme.js.map +1 -1
  63. package/dist/util/file/readAsDataURL.js +15 -4
  64. package/dist/util/file/readAsDataURL.js.map +1 -1
  65. package/dist/util/tree/attachChild.js +7 -1
  66. package/dist/util/tree/attachChild.js.map +1 -1
  67. package/dist/util/tree/detachChild.js +3 -0
  68. package/dist/util/tree/detachChild.js.map +1 -1
  69. package/package.json +17 -17
  70. package/src/component/button/LinkButton.tsx +3 -1
  71. package/src/component/button/index.ts +1 -0
  72. package/src/component/checkbox/Checkbox.tsx +7 -1
  73. package/src/component/checkbox/Radio.tsx +5 -1
  74. package/src/component/checkbox/Switch.tsx +1 -1
  75. package/src/component/header/Header.tsx +1 -1
  76. package/src/component/input/Input.tsx +5 -1
  77. package/src/component/input/SearchInput.tsx +9 -8
  78. package/src/component/menu/renderMenuItem.tsx +5 -1
  79. package/src/component/select/Group.tsx +1 -1
  80. package/src/component/select/Option.tsx +1 -1
  81. package/src/hook/accessibility/keyboard/context/index.ts +1 -1
  82. package/src/hook/accessibility/keyboard/hook/useLastFocused.ts +6 -0
  83. package/src/hook/document/useHighlight.ts +5 -4
  84. package/src/hook/form/useGroup.ts +17 -19
  85. package/src/hook/observable/useObservable.ts +14 -1
  86. package/src/hook/popover/style/positioning.css.ts +1 -1
  87. package/src/hook/popover/useAnchorPositioning.ts +1 -1
  88. package/src/hook/popover/useLightDismiss.ts +27 -0
  89. package/src/hook/popover/useOverlay.ts +15 -6
  90. package/src/hook/popover/usePopover.ts +11 -13
  91. package/src/hook/popover/usePositioning.ts +7 -15
  92. package/src/hook/popover/useReturnFocus.ts +12 -3
  93. package/src/hook/popover/useTooltip.ts +4 -3
  94. package/src/hook/promise/usePromise.ts +4 -1
  95. package/src/hook/react/useActivate.ts +8 -1
  96. package/src/hook/react/useConst.ts +3 -4
  97. package/src/hook/react/useRefCallback.ts +2 -0
  98. package/src/infra/logging/logger.ts +12 -2
  99. package/src/theme/global.css.ts +0 -27
  100. package/src/theme/index.ts +4 -0
  101. package/src/theme/useSystemColorScheme.ts +22 -20
  102. package/src/util/file/readAsDataURL.ts +17 -4
  103. package/src/util/number/index.ts +2 -0
  104. package/src/util/random/index.ts +4 -0
  105. package/src/util/tree/attachChild.ts +12 -1
  106. package/src/util/tree/detachChild.ts +6 -0
@@ -12,7 +12,7 @@ export interface SearchInputProps extends InputProps {
12
12
  }
13
13
 
14
14
  export function SearchInput(props: SearchInputProps) {
15
- const { onSearch, ...attrs } = props
15
+ const { onSearch, onKeyDown, ...attrs } = props
16
16
 
17
17
  attrs.role ||= 'searchbox'
18
18
 
@@ -24,20 +24,21 @@ export function SearchInput(props: SearchInputProps) {
24
24
  )
25
25
  }
26
26
 
27
- const { onKeyDown } = attrs
28
-
29
- attrs.onKeyDown = useCallback<NonNullable<typeof onKeyDown>>(
30
- // eslint-disable-next-line react-hooks/preserve-manual-memoization
27
+ const handleKeyDown = useCallback<NonNullable<typeof onKeyDown>>(
31
28
  (ev) => {
32
29
  onKeyDown?.(ev)
33
30
 
34
- if (!ev.defaultPrevented && isEnterKey(ev)) {
31
+ // Ignore the 'Enter' key that confirms an in-progress IME composition.
32
+ if (
33
+ !ev.defaultPrevented &&
34
+ isEnterKey(ev) &&
35
+ !ev.nativeEvent.isComposing
36
+ ) {
35
37
  onSearch?.(ev, ev.currentTarget.value)
36
38
  }
37
39
  },
38
- // eslint-disable-next-line react-hooks/preserve-manual-memoization
39
40
  [onKeyDown, onSearch]
40
41
  )
41
42
 
42
- return <Input {...attrs} />
43
+ return <Input {...attrs} onKeyDown={handleKeyDown} />
43
44
  }
@@ -30,7 +30,11 @@ export function renderMenuItem(props: MenuItemProps) {
30
30
  <li
31
31
  {...slot?.root}
32
32
  {...attrs}
33
- className={clsx(ItemStyle({ danger }), attrs.className)}
33
+ className={clsx(
34
+ ItemStyle({ danger }),
35
+ slot?.root?.className,
36
+ attrs.className
37
+ )}
34
38
  >
35
39
  <div
36
40
  {...slot?.before}
@@ -35,7 +35,7 @@ export function Group(props: GroupProps) {
35
35
  <li
36
36
  {...slot?.root}
37
37
  {...attrs}
38
- className={clsx(GroupStyle, attrs.className)}
38
+ className={clsx(GroupStyle, slot?.root?.className, attrs.className)}
39
39
  role='group'
40
40
  aria-labelledby={label != null ? labelProps.id : undefined}
41
41
  >
@@ -61,7 +61,7 @@ export function Option<T extends OptionValue>(props: OptionProps<T>) {
61
61
  <li
62
62
  {...slot?.root}
63
63
  {...attrs}
64
- className={clsx(ItemStyle(), attrs.className)}
64
+ className={clsx(ItemStyle(), slot?.root?.className, attrs.className)}
65
65
  role='option'
66
66
  value={value}
67
67
  aria-selected={selected}
@@ -1,7 +1,7 @@
1
1
  'use client'
2
2
  import { createContext } from 'react'
3
3
  import type { Keyboard } from '../Keyboard'
4
- import type { PoolAdd, PoolRemove } from '../hook'
4
+ import type { PoolAdd, PoolRemove } from '../hook/usePool'
5
5
 
6
6
  export interface KeyboardContext extends Keyboard {
7
7
  lastActive: Element | null
@@ -8,6 +8,12 @@ export function useLastFocused(
8
8
  tree: Tree,
9
9
  active: Element | null
10
10
  ) {
11
+ // The memoized context binding is deliberately reassigned: when 'active'
12
+ // is removed from the tree (or missing) the memo yields null or
13
+ // undefined, and the '??=' fallback below restores the last captured
14
+ // context from the ref. Reassigning the binding keeps the ref fallback
15
+ // out of the memo dependencies, which would otherwise propagate ref
16
+ // reads into every downstream memo.
11
17
  // eslint-disable-next-line react-hooks/preserve-manual-memoization
12
18
  let ctx = useMemo(
13
19
  () => (active ? tree.map.get(active)?.context : null),
@@ -1,6 +1,7 @@
1
1
  'use client'
2
2
  import { useCallback, useLayoutEffect, useState } from 'react'
3
- import { refValue, useRegExp, type RefValue } from '../react'
3
+ import { refValue, type RefValue } from '../react/refValue'
4
+ import { useRegExp } from '../react/useRegExp'
4
5
 
5
6
  export function useHighlight(
6
7
  pattern: string | undefined,
@@ -37,9 +38,9 @@ export function useHighlight(
37
38
 
38
39
  traverse(el)
39
40
 
40
- if (highlight.size > 0) {
41
- setHighlight(highlight)
42
- }
41
+ // Report an empty result as 'undefined' so a stale 'Highlight' with
42
+ // detached ranges is not kept around when the matches disappear.
43
+ setHighlight(highlight.size > 0 ? highlight : undefined)
43
44
  },
44
45
  [RE]
45
46
  )
@@ -57,16 +57,6 @@ export function useGroup<T, P extends Props<T, SyntheticEvent>>(props: P) {
57
57
  value: _value,
58
58
  })
59
59
 
60
- // eslint-disable-next-line react-hooks/immutability, react-compiler/react-compiler
61
- props = {
62
- ...props,
63
- name,
64
- defaultValue: undefined,
65
- value,
66
- multiple,
67
- disabled,
68
- }
69
-
70
60
  const [_state, _setState] = useDefault({
71
61
  defaultValue: {},
72
62
  value: context?.value,
@@ -130,20 +120,28 @@ export function useGroup<T, P extends Props<T, SyntheticEvent>>(props: P) {
130
120
  [name, setState]
131
121
  )
132
122
 
133
- props = {
134
- ...props,
135
- onChange: handleChange,
136
- }
137
-
138
123
  const _context = useMemo<NonNullable<typeof context>>(
139
124
  () => ({ name, value: state, multiple, disabled, onChange: setState }),
140
125
  [disabled, multiple, name, setState, state]
141
126
  )
142
127
 
143
- props = {
128
+ const groupProps: P = {
144
129
  ...props,
145
- // eslint-disable-next-line react-hooks/refs
146
- children: createElement(FormContext, { value: _context }, props.children),
130
+ name,
131
+ defaultValue: undefined,
132
+ value,
133
+ multiple,
134
+ disabled,
135
+ onChange: handleChange,
136
+ children: createElement(
137
+ FormContext,
138
+ // The context value bundles 'setState', which reads the latest state
139
+ // from a ref inside event callbacks. The rule conservatively flags any
140
+ // ref-reading function passed through a provider during render.
141
+ // eslint-disable-next-line react-hooks/refs
142
+ { value: _context },
143
+ props.children
144
+ ),
147
145
  }
148
146
 
149
147
  return {
@@ -152,7 +150,7 @@ export function useGroup<T, P extends Props<T, SyntheticEvent>>(props: P) {
152
150
  multiple,
153
151
  disabled,
154
152
  onChange: handleChange,
155
- props,
153
+ props: groupProps,
156
154
  context: _context,
157
155
  }
158
156
  }
@@ -1,7 +1,7 @@
1
1
  'use client'
2
2
  import 'symbol-observable'
3
3
 
4
- import { useCallback, useRef, useSyncExternalStore } from 'react'
4
+ import { useCallback, useEffect, useRef, useSyncExternalStore } from 'react'
5
5
  import type { ObservableInput } from 'rxjs'
6
6
  import { from } from 'rxjs'
7
7
 
@@ -20,10 +20,23 @@ export function useObservable<T>(
20
20
  initialValue?: T | undefined
21
21
  ) {
22
22
  const valueRef = useRef(initialValue)
23
+ const initialValueRef = useRef(initialValue)
24
+
25
+ // Track the latest 'initialValue' in a ref so the subscribe callback below
26
+ // depends only on 'observable'; a new initial value identity must not
27
+ // resubscribe and reset the emitted value.
28
+ useEffect(() => {
29
+ initialValueRef.current = initialValue
30
+ })
23
31
 
24
32
  return useSyncExternalStore(
25
33
  useCallback(
26
34
  (onStoreChange) => {
35
+ // A new 'observable' must not keep reporting the previous source's
36
+ // last emission; fall back to the latest 'initialValue' until it
37
+ // emits.
38
+ valueRef.current = initialValueRef.current
39
+
27
40
  const subscription = from(observable).subscribe((value) => {
28
41
  valueRef.current = value
29
42
  onStoreChange()
@@ -103,9 +103,9 @@ export const positionTryOptionMap: Record<Position, string> = fromEntries(
103
103
 
104
104
  for (const [position, area] of entries(positionAreaMap)) {
105
105
  globalStyle(`@position-try ${positionTryOptionMap[position]}`, {
106
- positionArea: area,
107
106
  inset: 'auto',
108
107
  [positionInsetMap[position]]: `var(${PositionOffset})`,
108
+ positionArea: area,
109
109
  // TODO: extend 'positionTryOptionMap', add '@starting-style' entry
110
110
  // animation, and try to reuse 'PopoverStyle'
111
111
  })
@@ -5,7 +5,7 @@ import {
5
5
  getOppositePlacement,
6
6
  } from '@floating-ui/utils'
7
7
  import { useMemo } from 'react'
8
- import { useUniqueId } from '../react'
8
+ import { useUniqueId } from '../react/useUniqueId'
9
9
  import {
10
10
  placementMap,
11
11
  positionAreaMap,
@@ -28,6 +28,33 @@ export function useLightDismiss<
28
28
  ref.current.source = source
29
29
  })
30
30
 
31
+ {
32
+ const { onKeyDown } = anchorProps
33
+
34
+ anchorProps = {
35
+ ...anchorProps,
36
+ onKeyDown: useCallback<NonNullable<typeof onKeyDown>>(
37
+ (ev) => {
38
+ onKeyDown?.(ev)
39
+
40
+ // A pending open or close claims the escape key to abort
41
+ // itself by reverting to the committed state. A committed
42
+ // open or an idle overlay toggles to a no-op and defers to
43
+ // the document listener below, which closes the topmost
44
+ // overlay instead.
45
+ if (
46
+ !ev.defaultPrevented &&
47
+ isEscapeKey(ev) &&
48
+ ref.current.overlay.toggle(ev, ref.current.overlay.open, 'escape')
49
+ ) {
50
+ ev.preventDefault()
51
+ }
52
+ },
53
+ [onKeyDown]
54
+ ),
55
+ }
56
+ }
57
+
31
58
  {
32
59
  const { onClickCapture } = positionedProps
33
60
 
@@ -9,6 +9,7 @@ export interface OverlayAnchorProps
9
9
  onFocus?: React.FocusEventHandler<Element> | undefined
10
10
  onBlur?: React.FocusEventHandler<Element> | undefined
11
11
  onClick?: React.MouseEventHandler<Element> | undefined
12
+ onKeyDown?: React.KeyboardEventHandler<Element> | undefined
12
13
  onPointerEnter?: React.PointerEventHandler<Element> | undefined
13
14
  onPointerLeave?: React.PointerEventHandler<Element> | undefined
14
15
  }
@@ -33,7 +34,7 @@ export interface Overlay<T extends HTMLElement = HTMLElement> {
33
34
  ev: Event | React.SyntheticEvent,
34
35
  open?: boolean,
35
36
  hint?: string
36
- ) => void
37
+ ) => boolean
37
38
  }
38
39
 
39
40
  export function useOverlay<T extends HTMLElement>(options: OverlayOptions<T>) {
@@ -55,13 +56,21 @@ export function useOverlay<T extends HTMLElement>(options: OverlayOptions<T>) {
55
56
  (ev, open, hint) => {
56
57
  open ??= !ref.current
57
58
 
58
- if (ref.current !== open) {
59
- setOpen(ev, open, hint)
59
+ if (ref.current === open) {
60
+ return false
61
+ }
62
+
63
+ setOpen(ev, open, hint)
60
64
 
61
- if (!ev.defaultPrevented) {
62
- ref.current = open
63
- }
65
+ // A vetoed change (`onToggle` preventing the event) never commits in
66
+ // `useDefault`, so the uncommitted mirror in `ref` must not advance.
67
+ if (ev.defaultPrevented) {
68
+ return false
64
69
  }
70
+
71
+ ref.current = open
72
+
73
+ return true
65
74
  },
66
75
  [setOpen]
67
76
  )
@@ -149,6 +149,12 @@ export function usePopover<T extends React.HTMLElementType = 'div'>(
149
149
  ref: anchorRef,
150
150
  }
151
151
 
152
+ keyboardProps = {
153
+ ...keyboardProps,
154
+ ref: setKeyboardRef,
155
+ children,
156
+ }
157
+
152
158
  positionedProps = {
153
159
  ...positionedProps,
154
160
  role: positionedProps.role || 'dialog',
@@ -160,21 +166,13 @@ export function usePopover<T extends React.HTMLElementType = 'div'>(
160
166
  ...positionedProps.style,
161
167
  },
162
168
  ref: positionedRef,
169
+ children: createElement(
170
+ OverlayContext,
171
+ { value: overlay },
172
+ createElement(Keyboard, keyboardProps)
173
+ ),
163
174
  }
164
175
 
165
- keyboardProps = {
166
- ...keyboardProps,
167
- ref: setKeyboardRef,
168
- children,
169
- }
170
-
171
- // eslint-disable-next-line react-hooks/refs
172
- positionedProps.children = createElement(
173
- OverlayContext,
174
- { value: overlay },
175
- createElement(Keyboard, keyboardProps)
176
- )
177
-
178
176
  return {
179
177
  overlay,
180
178
  position: positioning.position,
@@ -13,9 +13,9 @@ import {
13
13
  } from '@floating-ui/dom'
14
14
  import { getOppositePlacement, getSide } from '@floating-ui/utils'
15
15
  import { isEqual } from 'lodash-es'
16
- import { useEffect, useLayoutEffect, useRef, useState } from 'react'
16
+ import { useLayoutEffect, useRef, useState } from 'react'
17
17
  import { flushSync } from 'react-dom'
18
- import { refValue, type RefValue } from '../react'
18
+ import { refValue, type RefValue } from '../react/refValue'
19
19
  import type { Position } from './style/positioning.css'
20
20
  import { placementMap, positionMap } from './style/positioning.css'
21
21
  import { usePositioningOptions } from './useAnchorPositioning'
@@ -66,22 +66,14 @@ export function usePositioning(
66
66
  arrowRef?: RefValue<HTMLElement> | undefined
67
67
  }
68
68
  ): Positioning {
69
- {
70
- const ref = useRef(_options)
69
+ const [stableOptions, setStableOptions] = useState(_options)
71
70
 
72
- // eslint-disable-next-line react-hooks/refs
73
- if (isEqual(_options, ref.current)) {
74
- // eslint-disable-next-line react-hooks/immutability, react-hooks/refs
75
- _options = ref.current
76
- }
77
-
78
- useEffect(() => {
79
- ref.current = _options
80
- })
71
+ if (!isEqual(_options, stableOptions)) {
72
+ setStableOptions(_options)
81
73
  }
82
74
 
83
- const { anchorRef, positionedRef, arrowRef } = _options
84
- const options = usePositioningOptions(_options)
75
+ const { anchorRef, positionedRef, arrowRef } = stableOptions
76
+ const options = usePositioningOptions(stableOptions)
85
77
  const { position, strategy } = options
86
78
  const [state, setState] = useState<State>({ x: 0, y: 0, position, strategy })
87
79
  const ref = useRef({ state })
@@ -28,8 +28,14 @@ export function useReturnFocus(overlay: Overlay) {
28
28
  }
29
29
  }, [overlay.open])
30
30
 
31
- // See the file "useReturnFocus.test.tsx" for more information,
32
- // the "useLayoutEffect" hook does not work here.
31
+ // React records the active element before its mutation phase and, at the
32
+ // end of that phase, moves focus back to it when focus moved elsewhere
33
+ // and the element is still connected. The 'useLayoutEffect' cleanup of a
34
+ // component deleted during the commit runs inside the mutation phase, so
35
+ // focus it restores is immediately moved back by React. The passive
36
+ // 'useEffect' cleanup runs after the commit, so its restoration survives.
37
+ // See the 'useReturnFocus - unmount while open' test in
38
+ // 'useReturnFocus.browser.test.tsx'.
33
39
  useEffect(() => {
34
40
  const anchor = ref.current.source
35
41
  const { positioned } = ref.current
@@ -70,7 +76,10 @@ export function useReturnFocus(overlay: Overlay) {
70
76
  }
71
77
  }, [overlay.open])
72
78
 
73
- // https://react.dev/link/setstate-in-render
79
+ // The focused element is tracked by the document-level 'focusin' listener
80
+ // above rather than by React state, so the latest value can only be read
81
+ // from the ref. Returning it during render is intentional: consumers use
82
+ // it to know where focus will return.
74
83
  // eslint-disable-next-line react-hooks/refs
75
84
  return ref.current.source
76
85
  }
@@ -48,7 +48,6 @@ export function useTooltip(options: TooltipOptions) {
48
48
 
49
49
  let anchorProps: OverlayAnchorProps = { ...anchor?.props }
50
50
  let positionedProps: React.JSX.IntrinsicElements[TooltipTag] = { ...props }
51
- let arrowProps: React.ComponentProps<'div'>
52
51
 
53
52
  const _id = useUniqueId('tooltip-')
54
53
  positionedProps.id ||= _id
@@ -237,8 +236,7 @@ export function useTooltip(options: TooltipOptions) {
237
236
  ref: positionedRef,
238
237
  }
239
238
 
240
- // eslint-disable-next-line prefer-const
241
- arrowProps = {
239
+ const arrowProps: React.ComponentProps<'div'> = {
242
240
  style: {
243
241
  ...positioning.arrowStyle,
244
242
  },
@@ -248,6 +246,9 @@ export function useTooltip(options: TooltipOptions) {
248
246
  return {
249
247
  overlay,
250
248
  position: positioning.position,
249
+ // Passing the anchor ref through 'cloneElement' is the standard anchor
250
+ // injection pattern; the rule cannot prove the element is not a function
251
+ // component, so it conservatively flags the ref.
251
252
  // eslint-disable-next-line react-hooks/refs
252
253
  anchor: anchor == null ? null : cloneElement(anchor, anchorProps),
253
254
  positioned: { props: positionedProps },
@@ -43,7 +43,10 @@ export function usePromise<T>(
43
43
  return () => {
44
44
  destroyed = true
45
45
  }
46
- }, [promise, signal?.aborted])
46
+ // Depend on the signal instance, not its 'aborted' state, so the effect
47
+ // re-runs and the callbacks close over the latest signal when it is
48
+ // swapped between renders.
49
+ }, [promise, signal])
47
50
 
48
51
  useEffect(() => {
49
52
  if (error) {
@@ -21,12 +21,19 @@ export function useActivate(onActivate?: ActivateCallback) {
21
21
 
22
22
  const ref = useRef({ isActivated, onActivate })
23
23
 
24
+ // Keep the ref in sync so 'activate' always invokes the latest
25
+ // 'onActivate', not the one captured when 'activate' was memoized.
26
+ useLayoutEffect(() => {
27
+ ref.current.onActivate = onActivate
28
+ })
29
+
24
30
  useLayoutEffect(() => {
25
31
  const handleBlur = (ev: FocusEvent) => {
26
32
  setIsActivated((ref.current.isActivated = false))
27
33
  }
28
34
 
29
- // The 'keyup' event may not have been applied to this element.
35
+ // The 'keyup' may not fire on this element, so watch for it on the
36
+ // document.
30
37
  const handleKeyUp = (ev: KeyboardEvent) => {
31
38
  if (ref.current.isActivated && isSpaceKey(ev)) {
32
39
  setIsActivated((ref.current.isActivated = false))
@@ -1,7 +1,6 @@
1
- import { useMemo } from 'react'
1
+ import { useState } from 'react'
2
2
 
3
3
  export function useConst<T>(initialize: () => T): T {
4
- // eslint-disable-next-line react-compiler/react-compiler
5
- // eslint-disable-next-line react-hooks/exhaustive-deps, react-hooks/use-memo
6
- return useMemo(initialize, [])
4
+ const [value] = useState(initialize)
5
+ return value
7
6
  }
@@ -13,6 +13,8 @@ export function useRefCallback<T>(ref: Ref<T> | undefined) {
13
13
  if (typeof ref === 'function') {
14
14
  destroy = ref(current)
15
15
  } else if (ref) {
16
+ // Writing to the forwarded 'ref' argument is the purpose of this
17
+ // hook; the rules conservatively forbid mutating hook arguments.
16
18
  // eslint-disable-next-line react-hooks/immutability, react-compiler/react-compiler
17
19
  ref.current = current
18
20
  }
@@ -336,7 +336,10 @@ class Logger {
336
336
  )
337
337
  }
338
338
 
339
- private parseError(error: unknown): LogAttributes | undefined {
339
+ private parseError(
340
+ error: unknown,
341
+ seen: Set<unknown> = new Set()
342
+ ): LogAttributes | undefined {
340
343
  if (typeof error === 'string') {
341
344
  return {
342
345
  [ATTR_EXCEPTION_MESSAGE]: error,
@@ -344,7 +347,14 @@ class Logger {
344
347
  }
345
348
 
346
349
  if (isError(error)) {
347
- const cause = this.parseError(error.cause)
350
+ // Guard against circular 'cause' chains, which would otherwise recurse
351
+ // until the stack overflows.
352
+ let cause: LogAttributes | undefined
353
+
354
+ if (!seen.has(error)) {
355
+ seen.add(error)
356
+ cause = this.parseError(error.cause, seen)
357
+ }
348
358
 
349
359
  return {
350
360
  [ATTR_EXCEPTION_TYPE]: error.name,
@@ -67,30 +67,3 @@ export const SquircleStyle = style({
67
67
  export const CircleStyle = style({
68
68
  borderRadius: '100vmax',
69
69
  })
70
-
71
- declare module 'csstype' {
72
- export interface StandardShorthandProperties<
73
- TLength = (string & {}) | 0,
74
- TTime = string & {},
75
- > {
76
- /**
77
- * The corner-shape shorthand CSS property specifies the shape of a box's corners, within the area specified by its border-radius property value.
78
- *
79
- * @see https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/corner-shape
80
- */
81
- cornerShape?: Property.CornerShape | undefined
82
- }
83
-
84
- export namespace Property {
85
- export type CornerShape =
86
- | Globals
87
- | 'round'
88
- | 'scoop'
89
- | 'bevel'
90
- | 'notch'
91
- | 'square'
92
- | 'squircle'
93
- | `superellipse(${number})`
94
- | (string & {})
95
- }
96
- }
@@ -1,9 +1,13 @@
1
1
  export * from './ColorScheme'
2
2
 
3
+ export * from './defineTheme'
4
+
3
5
  export * from './palette'
4
6
 
5
7
  export * from './typography'
6
8
 
9
+ export * from './useSystemColorScheme'
10
+
7
11
  export * from './token.css'
8
12
 
9
13
  export * from './theme.css'
@@ -1,29 +1,31 @@
1
- import { useEffect, useState } from 'react'
1
+ import { useConst } from '#hook'
2
+ import { useRef, useSyncExternalStore } from 'react'
2
3
 
3
- export function useSystemColorScheme() {
4
- const [query] = useState(() =>
5
- typeof window !== 'undefined'
6
- ? window.matchMedia('(prefers-color-scheme: dark)')
7
- : null
8
- )
4
+ const QUERY = '(prefers-color-scheme: dark)'
9
5
 
10
- const [isDarkMode, setIsDarkMode] = useState(query?.matches)
11
-
12
- useEffect(() => {
13
- if (!query) {
14
- return
15
- }
6
+ export function useSystemColorScheme() {
7
+ // Reuse one lazily created MediaQueryList per hook instance for both
8
+ // subscribing and snapshot reads; 'getSnapshot' runs on every render, so
9
+ // it must not allocate a fresh query each time.
10
+ const queryRef = useRef<MediaQueryList | null>(null)
16
11
 
17
- const onChange = (event: MediaQueryListEvent) => {
18
- setIsDarkMode(event.matches)
19
- }
12
+ const subscribe = useConst(() => (onStoreChange: () => void) => {
13
+ const query = (queryRef.current ??= window.matchMedia(QUERY))
20
14
 
21
- query.addEventListener('change', onChange)
15
+ query.addEventListener('change', onStoreChange)
22
16
 
23
17
  return () => {
24
- query.removeEventListener('change', onChange)
18
+ query.removeEventListener('change', onStoreChange)
25
19
  }
26
- }, [query])
20
+ })
27
21
 
28
- return isDarkMode ? 'dark' : 'light'
22
+ return useSyncExternalStore(
23
+ subscribe,
24
+ () => (queryRef.current ??= window.matchMedia(QUERY)).matches,
25
+ // Server rendering assumes the default 'light' preference so hydration
26
+ // matches; the client re-renders with the actual value after hydration.
27
+ () => false
28
+ )
29
+ ? 'dark'
30
+ : 'light'
29
31
  }