chiplet 0.4.0 → 0.6.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 (122) hide show
  1. package/dist/assets/{global.css.ts.vanilla-CTwjHiaM.css → global.css.ts.vanilla-C6UQbnfq.css} +8 -2
  2. package/dist/assets/{positioning.css.ts.vanilla-BVOg9Lwj.css → positioning.css.ts.vanilla-BLJ90UCF.css} +12 -12
  3. package/dist/component/button/LinkButton.js +1 -1
  4. package/dist/component/button/LinkButton.js.map +1 -1
  5. package/dist/component/button/index.js +1 -0
  6. package/dist/component/button/index.js.map +1 -1
  7. package/dist/component/button/style/index.css.js +2 -2
  8. package/dist/component/checkbox/Checkbox.js +5 -1
  9. package/dist/component/checkbox/Checkbox.js.map +1 -1
  10. package/dist/component/checkbox/Radio.js +5 -1
  11. package/dist/component/checkbox/Radio.js.map +1 -1
  12. package/dist/component/checkbox/Switch.js +1 -1
  13. package/dist/component/checkbox/Switch.js.map +1 -1
  14. package/dist/component/header/Header.js +1 -1
  15. package/dist/component/header/Header.js.map +1 -1
  16. package/dist/component/index.js +1 -0
  17. package/dist/component/index.js.map +1 -1
  18. package/dist/component/input/Input.js +5 -1
  19. package/dist/component/input/Input.js.map +1 -1
  20. package/dist/component/input/SearchInput.js +4 -7
  21. package/dist/component/input/SearchInput.js.map +1 -1
  22. package/dist/component/input/style/index.css.js +2 -2
  23. package/dist/component/menu/renderMenuItem.js +5 -1
  24. package/dist/component/menu/renderMenuItem.js.map +1 -1
  25. package/dist/component/popover/Portal.js +8 -1
  26. package/dist/component/popover/Portal.js.map +1 -1
  27. package/dist/component/popover/style/index.css.js +4 -4
  28. package/dist/component/select/Group.js +1 -1
  29. package/dist/component/select/Group.js.map +1 -1
  30. package/dist/component/select/Option.js +1 -1
  31. package/dist/component/select/Option.js.map +1 -1
  32. package/dist/component/select/style/index.css.js +2 -2
  33. package/dist/hook/accessibility/keyboard/context/index.js.map +1 -1
  34. package/dist/hook/accessibility/keyboard/hook/useLastFocused.js.map +1 -1
  35. package/dist/hook/document/useHighlight.js +2 -4
  36. package/dist/hook/document/useHighlight.js.map +1 -1
  37. package/dist/hook/form/useGroup.js +17 -16
  38. package/dist/hook/form/useGroup.js.map +1 -1
  39. package/dist/hook/observable/useObservable.js +6 -1
  40. package/dist/hook/observable/useObservable.js.map +1 -1
  41. package/dist/hook/popover/style/positioning.css.js +1 -1
  42. package/dist/hook/popover/useAnchorPositioning.js +1 -1
  43. package/dist/hook/popover/useAnchorPositioning.js.map +1 -1
  44. package/dist/hook/popover/useLightDismiss.js +15 -0
  45. package/dist/hook/popover/useLightDismiss.js.map +1 -1
  46. package/dist/hook/popover/useOverlay.js +8 -5
  47. package/dist/hook/popover/useOverlay.js.map +1 -1
  48. package/dist/hook/popover/usePopover.js +11 -11
  49. package/dist/hook/popover/usePopover.js.map +1 -1
  50. package/dist/hook/popover/usePositioning.js +7 -12
  51. package/dist/hook/popover/usePositioning.js.map +1 -1
  52. package/dist/hook/popover/useReturnFocus.js.map +1 -1
  53. package/dist/hook/popover/useTooltip.js +4 -2
  54. package/dist/hook/popover/useTooltip.js.map +1 -1
  55. package/dist/hook/promise/usePromise.js +1 -1
  56. package/dist/hook/promise/usePromise.js.map +1 -1
  57. package/dist/hook/react/useActivate.js +3 -0
  58. package/dist/hook/react/useActivate.js.map +1 -1
  59. package/dist/hook/react/useConst.js +3 -2
  60. package/dist/hook/react/useConst.js.map +1 -1
  61. package/dist/hook/react/useRefCallback.js.map +1 -1
  62. package/dist/index.js +6 -1
  63. package/dist/index.js.map +1 -1
  64. package/dist/infra/logging/logger.js +6 -2
  65. package/dist/infra/logging/logger.js.map +1 -1
  66. package/dist/theme/ThemeContext.js +8 -0
  67. package/dist/theme/ThemeContext.js.map +1 -0
  68. package/dist/theme/ThemeProvider.js +19 -0
  69. package/dist/theme/ThemeProvider.js.map +1 -0
  70. package/dist/theme/global.css.js +6 -4
  71. package/dist/theme/global.css.js.map +1 -1
  72. package/dist/theme/index.js +5 -1
  73. package/dist/theme/index.js.map +1 -1
  74. package/dist/theme/useSystemColorScheme.js +16 -16
  75. package/dist/theme/useSystemColorScheme.js.map +1 -1
  76. package/dist/util/file/readAsDataURL.js +15 -4
  77. package/dist/util/file/readAsDataURL.js.map +1 -1
  78. package/dist/util/tree/attachChild.js +7 -1
  79. package/dist/util/tree/attachChild.js.map +1 -1
  80. package/dist/util/tree/detachChild.js +3 -0
  81. package/dist/util/tree/detachChild.js.map +1 -1
  82. package/package.json +17 -17
  83. package/src/component/button/LinkButton.tsx +3 -1
  84. package/src/component/button/index.ts +1 -0
  85. package/src/component/checkbox/Checkbox.tsx +7 -1
  86. package/src/component/checkbox/Radio.tsx +5 -1
  87. package/src/component/checkbox/Switch.tsx +1 -1
  88. package/src/component/header/Header.tsx +1 -1
  89. package/src/component/input/Input.tsx +5 -1
  90. package/src/component/input/SearchInput.tsx +9 -8
  91. package/src/component/menu/renderMenuItem.tsx +5 -1
  92. package/src/component/popover/Portal.tsx +20 -1
  93. package/src/component/select/Group.tsx +1 -1
  94. package/src/component/select/Option.tsx +1 -1
  95. package/src/hook/accessibility/keyboard/context/index.ts +1 -1
  96. package/src/hook/accessibility/keyboard/hook/useLastFocused.ts +6 -0
  97. package/src/hook/document/useHighlight.ts +5 -4
  98. package/src/hook/form/useGroup.ts +17 -19
  99. package/src/hook/observable/useObservable.ts +14 -1
  100. package/src/hook/popover/style/positioning.css.ts +1 -1
  101. package/src/hook/popover/useAnchorPositioning.ts +1 -1
  102. package/src/hook/popover/useLightDismiss.ts +27 -0
  103. package/src/hook/popover/useOverlay.ts +15 -6
  104. package/src/hook/popover/usePopover.ts +11 -13
  105. package/src/hook/popover/usePositioning.ts +7 -15
  106. package/src/hook/popover/useReturnFocus.ts +12 -3
  107. package/src/hook/popover/useTooltip.ts +4 -3
  108. package/src/hook/promise/usePromise.ts +4 -1
  109. package/src/hook/react/useActivate.ts +8 -1
  110. package/src/hook/react/useConst.ts +3 -4
  111. package/src/hook/react/useRefCallback.ts +2 -0
  112. package/src/infra/logging/logger.ts +12 -2
  113. package/src/theme/ThemeContext.ts +15 -0
  114. package/src/theme/ThemeProvider.tsx +26 -0
  115. package/src/theme/global.css.ts +14 -28
  116. package/src/theme/index.ts +8 -0
  117. package/src/theme/useSystemColorScheme.ts +22 -20
  118. package/src/util/file/readAsDataURL.ts +17 -4
  119. package/src/util/number/index.ts +2 -0
  120. package/src/util/random/index.ts +4 -0
  121. package/src/util/tree/attachChild.ts +12 -1
  122. package/src/util/tree/detachChild.ts +6 -0
@@ -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,
@@ -0,0 +1,15 @@
1
+ import { createContext } from 'react'
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ export interface ThemeContext {
7
+ className: string
8
+ }
9
+
10
+ /**
11
+ * @internal
12
+ */
13
+ export const ThemeContext = createContext<ThemeContext | null>(null)
14
+
15
+ ThemeContext.displayName = 'ThemeContext'
@@ -0,0 +1,26 @@
1
+ import clsx from 'clsx'
2
+ import type { ColorScheme } from './ColorScheme'
3
+ import { ThemeContext } from './ThemeContext'
4
+ import {
5
+ DarkColorScheme,
6
+ DarkLightColorScheme,
7
+ GlobalContainerStyle,
8
+ LightColorScheme,
9
+ } from './global.css'
10
+
11
+ export interface ThemeProviderProps extends React.PropsWithChildren {
12
+ colorScheme?: ColorScheme | undefined
13
+ }
14
+
15
+ export function ThemeProvider(props: ThemeProviderProps) {
16
+ const { colorScheme = 'auto', children } = props
17
+
18
+ const className = clsx(
19
+ GlobalContainerStyle,
20
+ colorScheme === 'light' && LightColorScheme,
21
+ colorScheme === 'dark' && DarkColorScheme,
22
+ colorScheme === 'dark light' && DarkLightColorScheme
23
+ )
24
+
25
+ return <ThemeContext value={{ className }}>{children}</ThemeContext>
26
+ }
@@ -37,7 +37,10 @@ export const GlobalContainerStyle = style([
37
37
  font: theme.body2,
38
38
  WebkitFontSmoothing: 'antialiased',
39
39
  color: theme.color,
40
- colorScheme: 'light dark', // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark
40
+ // Enables 'light-dark()' in the theme tokens; it resolves only with an
41
+ // explicit 'color-scheme'.
42
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark
43
+ colorScheme: 'light dark',
41
44
  background: theme.paper.background,
42
45
  },
43
46
  ])
@@ -54,6 +57,16 @@ export const DarkColorScheme = style({
54
57
  colorScheme: 'dark',
55
58
  })
56
59
 
60
+ // Standalone 'light dark' opt-in for containers that do not carry
61
+ // 'GlobalContainerStyle'.
62
+ export const LightDarkColorScheme = style({
63
+ colorScheme: 'light dark',
64
+ })
65
+
66
+ export const DarkLightColorScheme = style({
67
+ colorScheme: 'dark light',
68
+ })
69
+
57
70
  export const SquircleStyle = style({
58
71
  borderRadius: radiusVar,
59
72
  '@supports': {
@@ -67,30 +80,3 @@ export const SquircleStyle = style({
67
80
  export const CircleStyle = style({
68
81
  borderRadius: '100vmax',
69
82
  })
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,17 @@
1
1
  export * from './ColorScheme'
2
2
 
3
+ export * from './defineTheme'
4
+
3
5
  export * from './palette'
4
6
 
7
+ export * from './ThemeContext'
8
+
9
+ export * from './ThemeProvider'
10
+
5
11
  export * from './typography'
6
12
 
13
+ export * from './useSystemColorScheme'
14
+
7
15
  export * from './token.css'
8
16
 
9
17
  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
  }
@@ -7,13 +7,26 @@ export function readAsDataURL(blob: Blob, signal?: AbortSignal | undefined) {
7
7
  return new Promise<string>((resolve, reject) => {
8
8
  const reader = new FileReader()
9
9
 
10
- reader.onabort = () => reject(AbortError())
10
+ const onAbort = () => reader.abort()
11
11
 
12
- reader.onerror = () => reject(reader.error)
12
+ const cleanup = () => signal?.removeEventListener('abort', onAbort)
13
13
 
14
- reader.onload = () => resolve(reader.result as string)
14
+ reader.onabort = () => {
15
+ cleanup()
16
+ reject(AbortError())
17
+ }
15
18
 
16
- signal?.addEventListener('abort', () => reader.abort(), { once: true })
19
+ reader.onerror = () => {
20
+ cleanup()
21
+ reject(reader.error)
22
+ }
23
+
24
+ reader.onload = () => {
25
+ cleanup()
26
+ resolve(reader.result as string)
27
+ }
28
+
29
+ signal?.addEventListener('abort', onAbort, { once: true })
17
30
 
18
31
  reader.readAsDataURL(blob)
19
32
 
@@ -1 +1,3 @@
1
1
  export * from './isNumber'
2
+
3
+ export type * from './range'
@@ -1,3 +1,7 @@
1
+ // 'generateDeterministicId' is deliberately not exported from this barrel:
2
+ // it imports '@vanilla-extract/css', which has side effects, so re-exporting
3
+ // it would pull them into every '#util' consumer. Import it by direct
4
+ // relative path instead.
1
5
  export * from './generateDigest'
2
6
  export * from './generateUniqueId'
3
7
  export * from './toHex'
@@ -1,13 +1,24 @@
1
+ import { detachChild } from './detachChild'
1
2
  import type { TreeNode } from './node'
2
3
 
3
4
  export function attachChild<T extends TreeNode<unknown>>(parent: T, child: T) {
5
+ // Detach a child that is still attached to a tree first so re-attaching
6
+ // cannot leave stale or self-referential links behind.
7
+ if (child.parent != null) {
8
+ detachChild(child.parent, child)
9
+ }
10
+
11
+ // Reset the child's sibling links so a node previously attached elsewhere
12
+ // cannot keep stale links into its former tree.
13
+ child.previousSibling = parent.lastChild ?? null
14
+ child.nextSibling = null
15
+
4
16
  if (parent.firstChild == null) {
5
17
  parent.firstChild = child
6
18
  }
7
19
 
8
20
  if (parent.lastChild) {
9
21
  parent.lastChild.nextSibling = child
10
- child.previousSibling = parent.lastChild
11
22
  }
12
23
 
13
24
  parent.lastChild = child
@@ -17,5 +17,11 @@ export function detachChild<T extends TreeNode<unknown>>(parent: T, child: T) {
17
17
  child.nextSibling.previousSibling = child.previousSibling ?? null
18
18
  }
19
19
 
20
+ // Detach the child's own links so it cannot reach back into the tree it
21
+ // was removed from.
22
+ child.parent = null
23
+ child.previousSibling = null
24
+ child.nextSibling = null
25
+
20
26
  return parent
21
27
  }