@justanarthur/modo-ui 2.2.0 → 2.4.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 (179) hide show
  1. package/components/data-display/diff/index.tsx +19 -3
  2. package/components/inputs/choice-list/index.tsx +4 -3
  3. package/components/navigation/sidebar/menu-scope.tsx +142 -53
  4. package/components/navigation/sidebar/sidebar-menu-actions.tsx +5 -5
  5. package/components/navigation/sidebar/sidebar-menu-button.tsx +4 -7
  6. package/components/navigation/sidebar/sidebar-menu-item.tsx +39 -36
  7. package/components/navigation/sidebar/sidebar-menu-sub.tsx +3 -2
  8. package/components/overlays/command/index.tsx +70 -12
  9. package/components/overlays/dropdown/menu-popup.tsx +11 -2
  10. package/components/overlays/tooltip/index.tsx +9 -1
  11. package/dist/blocks/desktop-shell/index.js +28 -28
  12. package/dist/blocks/sidebar-app/index.js +38 -38
  13. package/dist/components/actions/button/index.js +4 -4
  14. package/dist/components/actions/copy/index.js +12 -12
  15. package/dist/components/containers/accordion/index.js +17 -17
  16. package/dist/components/containers/card/index.js +15 -15
  17. package/dist/components/data-display/avatar/index.js +3 -3
  18. package/dist/components/data-display/badge/index.js +2 -2
  19. package/dist/components/data-display/diff/index.d.ts +1 -1
  20. package/dist/components/data-display/diff/index.js +4 -4
  21. package/dist/components/data-display/list/index.js +3 -3
  22. package/dist/components/data-display/log-view/index.js +8 -8
  23. package/dist/components/data-display/markdown/index.js +16 -16
  24. package/dist/components/data-display/table/index.js +10 -10
  25. package/dist/components/feedback/empty-state/index.js +3 -3
  26. package/dist/components/feedback/meter/index.js +2 -2
  27. package/dist/components/feedback/progress/index.js +2 -2
  28. package/dist/components/inputs/checkbox/index.js +14 -14
  29. package/dist/components/inputs/choice-list/index.js +11 -11
  30. package/dist/components/inputs/color-picker/index.js +23 -23
  31. package/dist/components/inputs/combobox/index.js +21 -21
  32. package/dist/components/inputs/date-time/calendar/index.js +10 -10
  33. package/dist/components/inputs/date-time/date-picker/index.js +28 -28
  34. package/dist/components/inputs/date-time/date-range-picker/index.js +28 -28
  35. package/dist/components/inputs/date-time/duration-picker/index.js +27 -27
  36. package/dist/components/inputs/date-time/month-picker/index.js +26 -26
  37. package/dist/components/inputs/date-time/time-picker/index.js +27 -27
  38. package/dist/components/inputs/editable-text/index.js +2 -2
  39. package/dist/components/inputs/field/index.js +5 -5
  40. package/dist/components/inputs/file-dropzone/index.js +8 -8
  41. package/dist/components/inputs/input/index.js +6 -6
  42. package/dist/components/inputs/key-value/index.js +12 -12
  43. package/dist/components/inputs/number-input/index.js +7 -7
  44. package/dist/components/inputs/radio/index.js +13 -13
  45. package/dist/components/inputs/search/index.js +24 -24
  46. package/dist/components/inputs/select/index.js +22 -22
  47. package/dist/components/inputs/slider/index.js +4 -4
  48. package/dist/components/inputs/switch/index.js +10 -10
  49. package/dist/components/inputs/textarea/index.js +4 -4
  50. package/dist/components/navigation/breadcrumb/index.js +26 -26
  51. package/dist/components/navigation/navigation-menu/index.js +18 -18
  52. package/dist/components/navigation/pagination/index.js +17 -17
  53. package/dist/components/navigation/sidebar/index.js +26 -26
  54. package/dist/components/navigation/tabs/index.js +14 -14
  55. package/dist/components/navigation/tree/index.js +10 -10
  56. package/dist/components/overlays/alert-dialog/index.js +15 -15
  57. package/dist/components/overlays/command/index.d.ts +6 -3
  58. package/dist/components/overlays/command/index.js +22 -22
  59. package/dist/components/overlays/context-menu/index.js +26 -26
  60. package/dist/components/overlays/dialog/index.js +14 -14
  61. package/dist/components/overlays/dropdown/index.js +25 -25
  62. package/dist/components/overlays/lightbox/index.js +12 -12
  63. package/dist/components/overlays/popover/index.js +10 -10
  64. package/dist/components/overlays/preview-card/index.js +10 -10
  65. package/dist/components/overlays/sheet/index.js +15 -15
  66. package/dist/components/overlays/toast/index.js +11 -11
  67. package/dist/components/overlays/tooltip/index.js +7 -7
  68. package/dist/docs/index.js +17 -17
  69. package/dist/index.d.ts +16 -8
  70. package/dist/index.js +80 -80
  71. package/dist/lib/fluid-hover-highlight.js +5 -4
  72. package/dist/lib/focus-ring.d.ts +5 -2
  73. package/dist/lib/focus-ring.js +3 -2
  74. package/dist/lib/goo-indicator.d.ts +5 -2
  75. package/dist/lib/goo-indicator.js +4 -3
  76. package/dist/lib/morph-layers.js +4 -4
  77. package/dist/lib/morph-part.js +2 -2
  78. package/dist/lib/overlay.d.ts +8 -1
  79. package/dist/lib/overlay.js +6 -4
  80. package/dist/lib/popup-rows.js +2 -2
  81. package/dist/lib/popup-surface.js +7 -7
  82. package/dist/lib/scroll-column.js +7 -7
  83. package/dist/lib/use-fluid-hover.d.ts +3 -1
  84. package/dist/lib/use-fluid-hover.js +3 -1
  85. package/dist/lib/use-merge-split.js +5 -4
  86. package/dist/lib/use-morph.js +2 -2
  87. package/dist/primitives/content/code/index.js +3 -3
  88. package/dist/primitives/layout/resizable/index.d.ts +4 -2
  89. package/dist/primitives/layout/resizable/index.js +2 -2
  90. package/dist/primitives/layout/sortable/index.js +5 -5
  91. package/dist/primitives/systems/craft/index.js +3 -3
  92. package/dist/primitives/systems/fluid-hover/index.js +7 -7
  93. package/dist/primitives/systems/morph/index.js +13 -13
  94. package/dist/primitives/systems/motion/index.js +6 -6
  95. package/dist/primitives/systems/sizes/index.js +2 -2
  96. package/dist/shared/{components-19e2wpxc.js → components-0bvxny7s.js} +5 -5
  97. package/dist/shared/{components-v3c3madd.js → components-0ep66sg9.js} +6 -6
  98. package/dist/shared/{components-nd3f803g.js → components-0v4jxw7z.js} +3 -3
  99. package/dist/shared/{components-9hysh6d3.js → components-1f5624hb.js} +1 -1
  100. package/dist/shared/{components-8qwbwtv9.js → components-1qw8hqwq.js} +9 -9
  101. package/dist/shared/{components-7c4a0egq.js → components-21hcwp85.js} +78 -43
  102. package/dist/shared/{components-qhk2m7fj.js → components-3qcgeay1.js} +2 -2
  103. package/dist/shared/{components-zk1tj5za.js → components-3rt5c1qt.js} +3 -3
  104. package/dist/shared/{components-ppc2dmxs.js → components-3sb31vw5.js} +1 -1
  105. package/dist/shared/{components-qs0cwe8p.js → components-4fay97sm.js} +10 -10
  106. package/dist/shared/{components-09n5hgmx.js → components-4fhjk3jz.js} +2 -2
  107. package/dist/shared/{components-vkrp4nb9.js → components-4m9stj7d.js} +11 -11
  108. package/dist/shared/{components-pgs3p035.js → components-4t2cjav8.js} +1 -1
  109. package/dist/shared/{components-v1m0xjbw.js → components-5wykqxph.js} +6 -1
  110. package/dist/shared/{components-w5p7mfb4.js → components-5xs8pkkj.js} +1 -1
  111. package/dist/shared/{components-rj4nnqkz.js → components-7rn454aq.js} +13 -4
  112. package/dist/shared/{components-zktkxnzq.js → components-895m1f3s.js} +8 -8
  113. package/dist/shared/{components-t4eb37vd.js → components-8m5e493q.js} +2 -2
  114. package/dist/shared/{components-1nqp7wqr.js → components-8nnhzeb3.js} +5 -5
  115. package/dist/shared/{components-mcnyaaep.js → components-8xe4h118.js} +6 -6
  116. package/dist/shared/{components-a3edrqpb.js → components-9am5511q.js} +3 -3
  117. package/dist/shared/{components-95kp19zn.js → components-9b2m90st.js} +1 -1
  118. package/dist/shared/{components-qd27dvgj.js → components-9yrajpy0.js} +3 -3
  119. package/dist/shared/{components-ajgwbabq.js → components-abqfs5fh.js} +4 -4
  120. package/dist/shared/{components-dr5c27yd.js → components-ac0x7zpq.js} +4 -4
  121. package/dist/shared/{components-y07de1qv.js → components-af44r4y9.js} +2 -2
  122. package/dist/shared/{components-90y8qwxv.js → components-br6t88cb.js} +5 -5
  123. package/dist/shared/{components-gvwxswpr.js → components-bxqmbcz2.js} +7 -3
  124. package/dist/shared/{components-9e5fpy3v.js → components-cc9kfhkb.js} +171 -115
  125. package/dist/shared/{components-rts2mggt.js → components-d4gkhy26.js} +1 -1
  126. package/dist/shared/{components-veaxns17.js → components-eawe4nxm.js} +2 -2
  127. package/dist/shared/{components-qe8yp3vf.js → components-fpgfs01m.js} +3 -3
  128. package/dist/shared/{components-xvf8jcza.js → components-ft5dgjg6.js} +10 -3
  129. package/dist/shared/{components-pbx77hzb.js → components-fv9y76b0.js} +15 -3
  130. package/dist/shared/{components-afj2ajea.js → components-fymenepq.js} +8 -8
  131. package/dist/shared/{components-rmhdr4g1.js → components-gd8gmr6w.js} +1 -1
  132. package/dist/shared/{components-k9qbcmaa.js → components-gr82tz50.js} +21 -3
  133. package/dist/shared/{components-nc8wggew.js → components-greka6z2.js} +59 -19
  134. package/dist/shared/{components-qkt4h7x4.js → components-gzve0nkp.js} +8 -8
  135. package/dist/shared/{components-4xd21p2p.js → components-hjdv4y8d.js} +3 -3
  136. package/dist/shared/{components-v81eejq6.js → components-hjk6dwsp.js} +2 -2
  137. package/dist/shared/{components-151vndgm.js → components-mb80a9wg.js} +3 -3
  138. package/dist/shared/{components-gfw4zk8b.js → components-mzdbtgvz.js} +12 -12
  139. package/dist/shared/{components-9s9vb2w0.js → components-nmbcfwx8.js} +8 -8
  140. package/dist/shared/{components-90sfqz5t.js → components-nxqrw789.js} +1 -1
  141. package/dist/shared/{components-2q0m4mxf.js → components-pw6gzev8.js} +2 -2
  142. package/dist/shared/{components-fqdh9ysk.js → components-pwsqp2np.js} +24 -14
  143. package/dist/shared/{components-yn8bw8tv.js → components-qmkfpcw0.js} +12 -12
  144. package/dist/shared/{components-z9gc2weg.js → components-qrtcc8aw.js} +3 -3
  145. package/dist/shared/{components-jd21f3mb.js → components-rgdspnth.js} +5 -5
  146. package/dist/shared/{components-r6ejdtpr.js → components-rtwfh1gc.js} +4 -4
  147. package/dist/shared/{components-16wx5av1.js → components-s0megs1g.js} +6 -6
  148. package/dist/shared/{components-8qkd3659.js → components-s2pnh8h9.js} +1 -1
  149. package/dist/shared/{components-2zr49v82.js → components-sh8j39z3.js} +7 -7
  150. package/dist/shared/{components-7v6869ga.js → components-t0c1rw40.js} +1 -1
  151. package/dist/shared/{components-mamtvenk.js → components-t4j0r23n.js} +2 -2
  152. package/dist/shared/{components-qc1fae2s.js → components-v1zzr668.js} +6 -6
  153. package/dist/shared/{components-vv420geb.js → components-vcmky8ns.js} +7 -7
  154. package/dist/shared/{components-313rmbxy.js → components-wq9hrera.js} +2 -2
  155. package/dist/shared/{components-mpx3yq5c.js → components-xq8j9c2f.js} +12 -12
  156. package/dist/shared/{components-3cvwrzc1.js → components-y9se9mx5.js} +3 -3
  157. package/dist/shared/{components-nehs80xd.js → components-yfx0skxc.js} +1 -1
  158. package/dist/shared/{components-s25rehbv.js → components-zckgawt6.js} +5 -5
  159. package/dist/shared/{components-85dz3xx3.js → components-zv63s4fh.js} +3 -3
  160. package/dist/styles.css +1 -1
  161. package/lib/focus-ring.tsx +15 -8
  162. package/lib/goo-indicator.tsx +20 -8
  163. package/lib/overlay.tsx +22 -1
  164. package/lib/use-fluid-hover.ts +96 -72
  165. package/package.json +1 -1
  166. package/primitives/layout/resizable/index.tsx +35 -7
  167. /package/dist/shared/{components-mp7en54n.js → components-8pnvkrbw.js} +0 -0
  168. /package/dist/shared/{components-zwgxvktt.js → components-92mvpt9h.js} +0 -0
  169. /package/dist/shared/{components-02683eqd.js → components-jw45ck66.js} +0 -0
  170. /package/dist/shared/{components-hbqyf35a.js → components-knryyjme.js} +0 -0
  171. /package/dist/shared/{components-yy0a9b02.js → components-m3vxpz2z.js} +0 -0
  172. /package/dist/shared/{components-ffng9rpa.js → components-nz14rwtd.js} +0 -0
  173. /package/dist/shared/{components-wddr0r6a.js → components-pa43g2r2.js} +0 -0
  174. /package/dist/shared/{components-6c52v58g.js → components-q0hs5rqn.js} +0 -0
  175. /package/dist/shared/{components-h9se5cz5.js → components-vx286kq7.js} +0 -0
  176. /package/dist/shared/{components-fa61vwag.js → components-y4gsk1jt.js} +0 -0
  177. /package/dist/shared/{components-31pkxwfz.js → components-ycwhrqe1.js} +0 -0
  178. /package/dist/shared/{components-wqac4434.js → components-ymq8tbq9.js} +0 -0
  179. /package/dist/shared/{components-sxstx1x7.js → components-ypxst3cn.js} +0 -0
@@ -37,16 +37,32 @@ const SIGN: Record<DiffLineKind, string> = {
37
37
  }
38
38
  const SIGN_CHAR: Record<DiffLineKind, string> = { add: '+', remove: '−', context: ' ', hunk: '' }
39
39
 
40
- /** Reads a unified diff (`@@ -a,b +c,d @@` hunks, `+`/`-`/space lines); file headers are skipped. */
40
+ // What git prints above a file's hunks: never code.
41
+ const GIT_HEADER =
42
+ /^(diff --git |index |(new|deleted) file mode |(old|new) mode |(dis)?similarity index |rename (from|to) |copy (from|to) |Binary files )/
43
+
44
+ /** Reads a unified diff (`@@ -a,b +c,d @@` hunks, `+`/`-`/space lines); file headers (`git diff`'s too) are skipped. */
41
45
  function parsePatch(patch: string): DiffLine[] {
42
46
  const lines: DiffLine[] = []
43
47
  let oldNo = 0
44
48
  let newNo = 0
45
- for (const raw of patch.replace(/^\n+|\n+$/g, '').split('\n')) {
46
- if (raw.startsWith('+++') || raw.startsWith('---')) continue
49
+ // Between a file's first header line and its first hunk; a patch may also start straight with lines.
50
+ let header = true
51
+ const raws = patch.replace(/^\n+|\n+$/g, '').split('\n')
52
+ for (const [i, raw] of raws.entries()) {
53
+ if (raw.startsWith('diff --git ')) header = true
54
+ if (header && (GIT_HEADER.test(raw) || raw.startsWith('---') || raw.startsWith('+++'))) continue
55
+ // Inside hunks, a `---` / `+++` pair starts the next file (`diff -u` of several); alone it is a line that began with `--` or `++`.
56
+ if (raw.startsWith('--- ') && raws[i + 1]?.startsWith('+++ ')) {
57
+ header = true
58
+ continue
59
+ }
60
+ // "" annotates the line above it.
61
+ if (raw.startsWith('\\')) continue
47
62
 
48
63
  const hunk = raw.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@(.*)$/)
49
64
  if (hunk) {
65
+ header = false
50
66
  oldNo = Number(hunk[1])
51
67
  newNo = Number(hunk[2])
52
68
  lines.push({ kind: 'hunk', text: raw })
@@ -9,7 +9,7 @@
9
9
 
10
10
  'use client'
11
11
 
12
- import { forwardRef, type ReactNode, useRef } from 'react'
12
+ import { forwardRef, memo, type ReactNode, useRef } from 'react'
13
13
  import { FluidHoverHighlight } from '../../../lib/fluid-hover-highlight'
14
14
  import { FocusRing } from '../../../lib/focus-ring'
15
15
  import { GooIndicator } from '../../../lib/goo-indicator'
@@ -48,7 +48,8 @@ interface RowProps {
48
48
  rowClassName?: string
49
49
  }
50
50
 
51
- function Row({ option, index, selected, tabbable, register, onPick, rowClassName }: RowProps) {
51
+ // Memoized: the list re-renders on every hover and resize step; a row only when its own props change.
52
+ const Row = memo(function Row({ option, index, selected, tabbable, register, onPick, rowClassName }: RowProps) {
52
53
  const ref = useRef<HTMLDivElement | null>(null)
53
54
  const shape = useShape()
54
55
  const size = useSize()
@@ -98,7 +99,7 @@ function Row({ option, index, selected, tabbable, register, onPick, rowClassName
98
99
  </div>
99
100
  </div>
100
101
  )
101
- }
102
+ })
102
103
 
103
104
  interface ChoiceListProps {
104
105
  /** The rows, in order. */
@@ -14,19 +14,22 @@ import {
14
14
  type KeyboardEvent,
15
15
  type MouseEvent,
16
16
  type MutableRefObject,
17
+ memo,
17
18
  type ReactNode,
18
19
  type RefObject,
19
20
  useCallback,
21
+ useContext,
20
22
  useMemo,
21
23
  useRef,
22
24
  useState,
25
+ useSyncExternalStore,
23
26
  } from 'react'
24
27
  import { FluidHoverHighlight } from '../../../lib/fluid-hover-highlight'
25
28
  import { FocusRing } from '../../../lib/focus-ring'
26
29
  import { GooIndicator } from '../../../lib/goo-indicator'
27
30
  import { useShape } from '../../../lib/shape-context'
28
31
  import { spring } from '../../../lib/springs'
29
- import { type ItemRect, useFluidHover } from '../../../lib/use-fluid-hover'
32
+ import { type ItemRect, sameRect, useFluidHover } from '../../../lib/use-fluid-hover'
30
33
  import { useIsoLayoutEffect } from '../../../lib/use-iso-layout-effect'
31
34
  import { cn } from '../../../lib/utils'
32
35
 
@@ -36,17 +39,24 @@ import { cn } from '../../../lib/utils'
36
39
  // as one piece. The active background stays one per level, so a current
37
40
  // section and the current page inside it can both be lit.
38
41
 
42
+ /** What rows read from the scope: the hovered row, every visible active row (DOM order) and the first row. */
43
+ interface MenuScopeSnapshot {
44
+ hovered: HTMLElement | null
45
+ active: HTMLElement[]
46
+ first: HTMLElement | null
47
+ }
48
+
49
+ // Stable for the scope's life: rows subscribe to the snapshot instead of
50
+ // reading it from context, so a hover re-renders the two rows it moves
51
+ // between, not every row.
39
52
  interface MenuScopeValue {
40
53
  registerRow: (el: HTMLElement) => () => void
41
54
  setRowButton: (row: HTMLElement, button: HTMLElement | null) => void
42
55
  setRowActive: (row: HTMLElement, active: boolean) => void
43
- hoveredRowEl: HTMLElement | null
44
- /** Every visible active row, in DOM order. */
45
- activeRows: HTMLElement[]
46
- firstRowEl: HTMLElement | null
47
- hasActive: boolean
48
56
  /** A sub-menu toggled: re-read what is visible. */
49
57
  refreshVisibility: () => void
58
+ subscribe: (listener: () => void) => () => void
59
+ getSnapshot: () => MenuScopeSnapshot
50
60
  }
51
61
 
52
62
  export const MenuScopeContext = createContext<MenuScopeValue | null>(null)
@@ -63,8 +73,6 @@ export interface MenuItemContextValue {
63
73
  rowRef: MutableRefObject<HTMLLIElement | null>
64
74
  /** The row's `<li>` ref callback; replays its active flag to the scope. */
65
75
  attachRow: (node: HTMLLIElement | null) => void
66
- isHovered: boolean
67
- isActiveRow: boolean
68
76
  /** Inside `SidebarMenuSubItem`: actions centre on the shorter row. */
69
77
  isSubRow: boolean
70
78
  setActive: (active: boolean) => void
@@ -82,9 +90,42 @@ export interface MenuItemContextValue {
82
90
 
83
91
  export const MenuItemContext = createContext<MenuItemContextValue | null>(null)
84
92
 
93
+ /** The row's stable part, for the actions and the badge: they re-render with neither the gutter nor the sub-menu. */
94
+ export type MenuRowContextValue = Pick<MenuItemContextValue, 'rowRef' | 'isSubRow' | 'setActions' | 'setHasBadge'>
95
+
96
+ export const MenuRowContext = createContext<MenuRowContextValue | null>(null)
97
+
85
98
  /** True inside a SidebarMenuActions cluster, where each action flows in the row. */
86
99
  export const MenuActionsClusterContext = createContext(false)
87
100
 
101
+ const NO_SUBSCRIPTION = () => () => {}
102
+ const HOVERED = 1
103
+ const ACTIVE = 2
104
+ const TAB_STOP = 4
105
+
106
+ /** The row's hover, active and tab-stop flags, re-rendering only when one of its own flips. */
107
+ export function useRowState(rowRef: MutableRefObject<HTMLElement | null> | undefined) {
108
+ const scope = useContext(MenuScopeContext)
109
+ const flags = useSyncExternalStore(
110
+ scope?.subscribe ?? NO_SUBSCRIPTION,
111
+ () => {
112
+ const row = rowRef?.current
113
+ if (!scope || !row) return 0
114
+ const { hovered, active, first } = scope.getSnapshot()
115
+ const isActive = active.includes(row)
116
+ // Roving tabindex: the active rows are the tab stops, else the first row.
117
+ const tabStop = active.length > 0 ? isActive : row === first
118
+ return (row === hovered ? HOVERED : 0) | (isActive ? ACTIVE : 0) | (tabStop ? TAB_STOP : 0)
119
+ },
120
+ () => 0,
121
+ )
122
+ return {
123
+ isHovered: (flags & HOVERED) !== 0,
124
+ isActiveRow: (flags & ACTIVE) !== 0,
125
+ isTabStop: (flags & TAB_STOP) !== 0,
126
+ }
127
+ }
128
+
88
129
  /** Inside a collapsed sub-tree. Such rows stay registered: unregistering on every toggle would churn the measurements and blink the overlays. */
89
130
  function rowHidden(el: HTMLElement) {
90
131
  return el.closest('[data-sidebar="menu-sub"][data-state="closed"]') !== null
@@ -153,6 +194,41 @@ export function gutterVars(item: MenuItemContextValue | null) {
153
194
  return { '--row-gutter': `${rest}px`, '--row-gutter-hover': `${hover}px` }
154
195
  }
155
196
 
197
+ interface ActiveBackground {
198
+ key: string
199
+ rect: ItemRect
200
+ rowChanged: boolean
201
+ }
202
+
203
+ function sameBackgrounds(a: ActiveBackground[], b: ActiveBackground[]) {
204
+ return (
205
+ a.length === b.length &&
206
+ a.every((x, i) => x.key === b[i]?.key && x.rowChanged === b[i]?.rowChanged && sameRect(x.rect, b[i]?.rect))
207
+ )
208
+ }
209
+
210
+ /** The active rows' backgrounds. The menu re-renders on every hover; these skip it unless a selection moved. */
211
+ const ActiveBackgrounds = memo(
212
+ function ActiveBackgrounds({ items, className }: { items: ActiveBackground[]; className: string }) {
213
+ return (
214
+ <AnimatePresence>
215
+ {items.map(({ key, rect, rowChanged }) => (
216
+ <motion.div
217
+ key={key}
218
+ className="pointer-events-none absolute top-0 left-0"
219
+ initial={false}
220
+ animate={{ opacity: 1 }}
221
+ exit={{ opacity: 0, transition: spring.moderate.exit }}
222
+ >
223
+ <GooIndicator rect={rect} className={className} transition={rowChanged ? spring.moderate : false} />
224
+ </motion.div>
225
+ ))}
226
+ </AnimatePresence>
227
+ )
228
+ },
229
+ (a, b) => a.className === b.className && sameBackgrounds(a.items, b.items),
230
+ )
231
+
156
232
  interface MenuScopeOptions {
157
233
  /** Draw the travelling keyboard focus ring. Off, keyboard focus moves the hover only (a settings dialog's section list). Defaults to `true`. */
158
234
  focusRing?: boolean
@@ -169,16 +245,10 @@ export function useMenuScope(containerRef: RefObject<HTMLElement | null>, { focu
169
245
  const activeMapRef = useRef<Map<HTMLElement, boolean>>(new Map())
170
246
  const [orderedRows, setOrderedRows] = useState<HTMLElement[]>([])
171
247
  const orderedRowsRef = useRef(orderedRows)
172
- orderedRowsRef.current = orderedRows
173
248
  const registeredCountRef = useRef(0)
174
249
  const [activeRows, setActiveRows] = useState<HTMLElement[]>([])
175
250
  const [focusedRowEl, setFocusedRowEl] = useState<HTMLElement | null>(null)
176
251
 
177
- const recomputeActive = useCallback(() => {
178
- const next = orderedRowsRef.current.filter(el => activeMapRef.current.get(el) && !rowSkipped(el))
179
- setActiveRows(prev => (sameElements(prev, next) ? prev : next))
180
- }, [])
181
-
182
252
  const rowButton = useCallback(
183
253
  (row: HTMLElement) =>
184
254
  rowButtonsRef.current.get(row) ??
@@ -191,55 +261,78 @@ export function useMenuScope(containerRef: RefObject<HTMLElement | null>, { focu
191
261
  // Indexes come from DOM order, so rows take no index and conditional rows
192
262
  // just work. The hover measures the row's button, not the `<li>`: a row
193
263
  // hosting an expanded sub-tree is tall, and its gaps would go to the parent.
264
+ // Every change only marks the order stale; one sort runs per batch (a
265
+ // microtask after the commit that mounted a list of rows), or at once when
266
+ // something reads the order first.
267
+ const staleRef = useRef(false)
194
268
  const syncRows = useCallback(() => {
269
+ staleRef.current = false
195
270
  const sorted = [...rowsRef.current].sort(byDomOrder)
196
- // Synchronously: callers in the same commit must see the set they changed.
197
- orderedRowsRef.current = sorted
198
- setOrderedRows(prev => (sameElements(prev, sorted) ? prev : sorted))
271
+ if (!sameElements(orderedRowsRef.current, sorted)) {
272
+ orderedRowsRef.current = sorted
273
+ setOrderedRows(sorted)
274
+ }
199
275
  for (const [i, el] of sorted.entries()) registerItem(i, rowButton(el) ?? el)
200
276
  for (let i = sorted.length; i < registeredCountRef.current; i++) registerItem(i, null)
201
277
  registeredCountRef.current = sorted.length
202
- recomputeActive()
203
- }, [registerItem, recomputeActive, rowButton])
278
+ const active = sorted.filter(el => activeMapRef.current.get(el) && !rowSkipped(el))
279
+ setActiveRows(prev => (sameElements(prev, active) ? prev : active))
280
+ }, [registerItem, rowButton])
281
+
282
+ const rows = useCallback(() => {
283
+ if (staleRef.current) syncRows()
284
+ return orderedRowsRef.current
285
+ }, [syncRows])
286
+
287
+ const invalidate = useCallback(() => {
288
+ if (staleRef.current) return
289
+ staleRef.current = true
290
+ queueMicrotask(() => {
291
+ if (staleRef.current) syncRows()
292
+ })
293
+ }, [syncRows])
204
294
 
205
295
  const registerRow = useCallback(
206
296
  (el: HTMLElement) => {
207
297
  rowsRef.current.add(el)
208
- syncRows()
298
+ invalidate()
209
299
  return () => {
210
300
  rowsRef.current.delete(el)
211
301
  rowButtonsRef.current.delete(el)
212
302
  activeMapRef.current.delete(el)
213
- syncRows()
303
+ invalidate()
214
304
  }
215
305
  },
216
- [syncRows],
306
+ [invalidate],
217
307
  )
218
308
 
219
309
  const setRowButton = useCallback(
220
310
  (row: HTMLElement, button: HTMLElement | null) => {
221
311
  if (button) rowButtonsRef.current.set(row, button)
222
312
  else rowButtonsRef.current.delete(row)
223
- syncRows()
313
+ invalidate()
224
314
  },
225
- [syncRows],
315
+ [invalidate],
226
316
  )
227
317
 
228
318
  const setRowActive = useCallback(
229
319
  (row: HTMLElement, active: boolean) => {
320
+ if (activeMapRef.current.get(row) === active) return
230
321
  activeMapRef.current.set(row, active)
231
- recomputeActive()
322
+ invalidate()
232
323
  },
233
- [recomputeActive],
324
+ [invalidate],
234
325
  )
235
326
 
236
327
  const refreshVisibility = useCallback(() => {
237
- recomputeActive()
328
+ const ordered = rows()
329
+ const active = ordered.filter(el => activeMapRef.current.get(el) && !rowSkipped(el))
330
+ setActiveRows(prev => (sameElements(prev, active) ? prev : active))
238
331
  setActiveIndex(prev => {
239
- const row = prev !== null ? orderedRowsRef.current[prev] : undefined
332
+ const row = prev !== null ? ordered[prev] : undefined
240
333
  return row && rowSkipped(row) ? null : prev
241
334
  })
242
- }, [recomputeActive, setActiveIndex])
335
+ }, [rows, setActiveIndex])
243
336
 
244
337
  // A row's rect spans its `<li>`, which grows with an expanded sub-menu: the
245
338
  // overlays clamp to the button (32px, the tallest row, if it isn't found).
@@ -282,12 +375,12 @@ export function useMenuScope(containerRef: RefObject<HTMLElement | null>, { focu
282
375
  if (!target.closest(ROW_BUTTON)) return
283
376
  const row = target.closest<HTMLElement>('[data-sidebar="menu-item"],[data-sidebar="menu-sub-item"]')
284
377
  if (!row) return
285
- const idx = orderedRowsRef.current.indexOf(row)
378
+ const idx = rows().indexOf(row)
286
379
  if (idx === -1) return
287
380
  setActiveIndex(idx)
288
381
  setFocusedRowEl(target.matches(':focus-visible') ? row : null)
289
382
  },
290
- [setActiveIndex],
383
+ [rows, setActiveIndex],
291
384
  )
292
385
 
293
386
  const onBlur = useCallback(
@@ -325,18 +418,30 @@ export function useMenuScope(containerRef: RefObject<HTMLElement | null>, { focu
325
418
 
326
419
  const hoveredRowEl = activeIndex !== null ? (orderedRows[activeIndex] ?? null) : null
327
420
 
421
+ // The snapshot rows read, published after each commit that changed it.
422
+ const snapshotRef = useRef<MenuScopeSnapshot>({ hovered: null, active: [], first: null })
423
+ const listenersRef = useRef(new Set<() => void>())
424
+ const firstRowEl = orderedRows[0] ?? null
425
+ useIsoLayoutEffect(() => {
426
+ const prev = snapshotRef.current
427
+ if (prev.hovered === hoveredRowEl && prev.active === activeRows && prev.first === firstRowEl) return
428
+ snapshotRef.current = { hovered: hoveredRowEl, active: activeRows, first: firstRowEl }
429
+ for (const listener of listenersRef.current) listener()
430
+ }, [hoveredRowEl, activeRows, firstRowEl])
431
+
328
432
  const value = useMemo<MenuScopeValue>(
329
433
  () => ({
330
434
  registerRow,
331
435
  setRowButton,
332
436
  setRowActive,
333
- hoveredRowEl,
334
- activeRows,
335
- firstRowEl: orderedRows[0] ?? null,
336
- hasActive: activeRows.length > 0,
337
437
  refreshVisibility,
438
+ subscribe: listener => {
439
+ listenersRef.current.add(listener)
440
+ return () => listenersRef.current.delete(listener)
441
+ },
442
+ getSnapshot: () => snapshotRef.current,
338
443
  }),
339
- [registerRow, setRowButton, setRowActive, hoveredRowEl, activeRows, orderedRows, refreshVisibility],
444
+ [registerRow, setRowButton, setRowActive, refreshVisibility],
340
445
  )
341
446
 
342
447
  const shape = useShape()
@@ -393,23 +498,7 @@ export function useMenuScope(containerRef: RefObject<HTMLElement | null>, { focu
393
498
 
394
499
  const overlays: ReactNode = isMeasured ? (
395
500
  <>
396
- <AnimatePresence>
397
- {activeRects.map(({ key, rect, rowChanged }) => (
398
- <motion.div
399
- key={key}
400
- className="pointer-events-none absolute top-0 left-0"
401
- initial={false}
402
- animate={{ opacity: 1 }}
403
- exit={{ opacity: 0, transition: spring.moderate.exit }}
404
- >
405
- <GooIndicator
406
- rect={rect}
407
- className={cn('bg-active', shape.bg)}
408
- transition={rowChanged ? spring.moderate : false}
409
- />
410
- </motion.div>
411
- ))}
412
- </AnimatePresence>
501
+ <ActiveBackgrounds items={activeRects} className={cn('bg-active', shape.bg)} />
413
502
  <FluidHoverHighlight
414
503
  rect={hoverRect}
415
504
  session={sessionRef.current}
@@ -24,7 +24,7 @@ import { useSize } from '../../../lib/size-context'
24
24
  import { resolveSlotTemplate, slotElement } from '../../../lib/slot'
25
25
  import { useIsoLayoutEffect } from '../../../lib/use-iso-layout-effect'
26
26
  import { cn } from '../../../lib/utils'
27
- import { MenuActionsClusterContext, MenuItemContext } from './menu-scope'
27
+ import { MenuActionsClusterContext, MenuRowContext, useRowState } from './menu-scope'
28
28
 
29
29
  export interface SidebarMenuActionProps extends ButtonHTMLAttributes<HTMLButtonElement> {
30
30
  /** Hidden until the row is hovered or focused, so the label keeps the room at rest. Defaults to `false`. */
@@ -39,7 +39,7 @@ const SidebarMenuAction = forwardRef<HTMLButtonElement, SidebarMenuActionProps>(
39
39
  ({ className, showOnHover = false, render, asChild, children, onClick, style, ...props }, ref) => {
40
40
  const shape = useShape()
41
41
  const size = useSize()
42
- const item = useContext(MenuItemContext)
42
+ const item = useContext(MenuRowContext)
43
43
  const inCluster = useContext(MenuActionsClusterContext)
44
44
  const { template, content } = resolveSlotTemplate(render, asChild, children)
45
45
 
@@ -106,7 +106,7 @@ export interface SidebarMenuActionsProps extends HTMLAttributes<HTMLDivElement>
106
106
  /** A row's cluster of `SidebarMenuAction`s: it positions them and reports the count, so the button reserves the exact gutter. */
107
107
  const SidebarMenuActions = forwardRef<HTMLDivElement, SidebarMenuActionsProps>(
108
108
  ({ className, showOnHover = false, children, ...props }, ref) => {
109
- const item = useContext(MenuItemContext)
109
+ const item = useContext(MenuRowContext)
110
110
  const size = useSize()
111
111
  const count = Children.count(children)
112
112
 
@@ -144,9 +144,9 @@ SidebarMenuActions.displayName = 'SidebarMenuActions'
144
144
  export type SidebarMenuBadgeProps = HTMLAttributes<HTMLDivElement>
145
145
 
146
146
  const SidebarMenuBadge = forwardRef<HTMLDivElement, SidebarMenuBadgeProps>(({ className, ...props }, ref) => {
147
- const item = useContext(MenuItemContext)
147
+ const item = useContext(MenuRowContext)
148
148
  const size = useSize()
149
- const lit = item?.isActiveRow ?? false
149
+ const lit = useRowState(item?.rowRef).isActiveRow
150
150
 
151
151
  const setHasBadge = item?.setHasBadge
152
152
  useIsoLayoutEffect(() => {
@@ -26,7 +26,7 @@ import { resolveSlotTemplate, slotElement } from '../../../lib/slot'
26
26
  import { spring } from '../../../lib/springs'
27
27
  import { useIsoLayoutEffect } from '../../../lib/use-iso-layout-effect'
28
28
  import { cn } from '../../../lib/utils'
29
- import { gutterVars, MenuItemContext, MenuScopeContext, SidebarSelection } from './menu-scope'
29
+ import { gutterVars, MenuItemContext, SidebarSelection, useRowState } from './menu-scope'
30
30
  import { MenuRowLabel } from './row-label'
31
31
 
32
32
  export const sidebarMenuButtonVariants = cva(
@@ -87,7 +87,6 @@ const SidebarMenuButton = forwardRef<HTMLButtonElement, SidebarMenuButtonProps>(
87
87
  },
88
88
  ref,
89
89
  ) => {
90
- const scope = useContext(MenuScopeContext)
91
90
  const item = useContext(MenuItemContext)
92
91
  const selection = useContext(SidebarSelection)
93
92
  const shape = useShape()
@@ -111,11 +110,9 @@ const SidebarMenuButton = forwardRef<HTMLButtonElement, SidebarMenuButtonProps>(
111
110
  return () => setButtonEl?.(null)
112
111
  }, [setButtonEl])
113
112
  const resolvedDot = dot ?? (status ? (status === 'idle' ? 'ring' : 'filled') : undefined)
114
- const lit = effectiveActive || (item?.isHovered ?? false)
115
-
116
- // Roving tabindex: the active rows are the tab stops, else the first row.
117
- const row = item?.rowRef.current ?? null
118
- const tabIdx = effectiveActive ? 0 : scope?.hasActive ? -1 : row !== null && row === scope?.firstRowEl ? 0 : -1
113
+ const row = useRowState(item?.rowRef)
114
+ const lit = effectiveActive || row.isHovered
115
+ const tabIdx = effectiveActive || row.isTabStop ? 0 : -1
119
116
 
120
117
  const { template, content } = resolveSlotTemplate(render, asChild, children)
121
118
 
@@ -24,7 +24,14 @@ import { useControllableState } from '../../../lib/use-controllable-state'
24
24
  import { useIsoLayoutEffect } from '../../../lib/use-iso-layout-effect'
25
25
  import { cn } from '../../../lib/utils'
26
26
  import { SizeScope } from '../../../primitives/systems/sizes'
27
- import { MenuItemContext, type MenuItemContextValue, MenuScopeContext, useMenuScope } from './menu-scope'
27
+ import {
28
+ MenuItemContext,
29
+ type MenuItemContextValue,
30
+ MenuRowContext,
31
+ type MenuRowContextValue,
32
+ MenuScopeContext,
33
+ useMenuScope,
34
+ } from './menu-scope'
28
35
 
29
36
  export interface SidebarMenuProps extends HTMLAttributes<HTMLUListElement> {
30
37
  /** Pins the menu's rows to one step of the size ladder. Omitted, they follow the surrounding SizeProvider. */
@@ -74,7 +81,7 @@ function useMenuRow(
74
81
  rowRef: MutableRefObject<HTMLLIElement | null>,
75
82
  isSubRow: boolean,
76
83
  sub?: Pick<SidebarMenuItemProps, 'collapsible' | 'open' | 'defaultOpen' | 'onOpenChange'>,
77
- ): MenuItemContextValue {
84
+ ): { item: MenuItemContextValue; row: MenuRowContextValue } {
78
85
  const scope = useContext(MenuScopeContext)
79
86
  const registerRow = scope?.registerRow
80
87
  const setRowButton = scope?.setRowButton
@@ -115,9 +122,6 @@ function useMenuRow(
115
122
  [setRowButton, rowRef],
116
123
  )
117
124
 
118
- const isHovered = rowRef.current !== null && scope?.hoveredRowEl === rowRef.current
119
- const isActiveRow = rowRef.current !== null && (scope?.activeRows.includes(rowRef.current) ?? false)
120
-
121
125
  const [trailing, setTrailing] = useState({ actionCount: 0, actionsShowOnHover: false, hasBadge: false })
122
126
  const setActions = useCallback(
123
127
  (count: number, showOnHover: boolean) =>
@@ -137,12 +141,14 @@ function useMenuRow(
137
141
  const subOpen = sub?.collapsible ? open : undefined
138
142
  const toggleSub = useCallback(() => setOpen(prev => !prev), [setOpen])
139
143
 
140
- return useMemo(
144
+ const row = useMemo(
145
+ () => ({ rowRef, isSubRow, setActions, setHasBadge }),
146
+ [rowRef, isSubRow, setActions, setHasBadge],
147
+ )
148
+ const item = useMemo(
141
149
  () => ({
142
150
  rowRef,
143
151
  attachRow,
144
- isHovered,
145
- isActiveRow,
146
152
  isSubRow,
147
153
  setActive,
148
154
  setButtonEl,
@@ -152,35 +158,25 @@ function useMenuRow(
152
158
  subOpen,
153
159
  toggleSub,
154
160
  }),
155
- [
156
- rowRef,
157
- attachRow,
158
- isHovered,
159
- isActiveRow,
160
- isSubRow,
161
- setActive,
162
- setButtonEl,
163
- trailing,
164
- setActions,
165
- setHasBadge,
166
- subOpen,
167
- toggleSub,
168
- ],
161
+ [rowRef, attachRow, isSubRow, setActive, setButtonEl, trailing, setActions, setHasBadge, subOpen, toggleSub],
169
162
  )
163
+ return { item, row }
170
164
  }
171
165
 
172
166
  const SidebarMenuItem = forwardRef<HTMLLIElement, SidebarMenuItemProps>(
173
167
  ({ collapsible, open, defaultOpen, onOpenChange, className, children, ...props }, ref) => {
174
168
  const rowRef = useRef<HTMLLIElement | null>(null)
175
- const item = useMenuRow(rowRef, false, { collapsible, open, defaultOpen, onOpenChange })
169
+ const { item, row } = useMenuRow(rowRef, false, { collapsible, open, defaultOpen, onOpenChange })
176
170
  // Stable: an inline ref is detached around every re-render, and child layout effects fire in that window.
177
- const row = useMergeRefs(item.attachRow, ref)
171
+ const attach = useMergeRefs(item.attachRow, ref)
178
172
  return (
179
- <MenuItemContext.Provider value={item}>
180
- <li ref={row} data-sidebar="menu-item" className={cn('group/menu-item relative', className)} {...props}>
181
- {children}
182
- </li>
183
- </MenuItemContext.Provider>
173
+ <MenuRowContext.Provider value={row}>
174
+ <MenuItemContext.Provider value={item}>
175
+ <li ref={attach} data-sidebar="menu-item" className={cn('group/menu-item relative', className)} {...props}>
176
+ {children}
177
+ </li>
178
+ </MenuItemContext.Provider>
179
+ </MenuRowContext.Provider>
184
180
  )
185
181
  },
186
182
  )
@@ -191,14 +187,21 @@ export type SidebarMenuSubItemProps = LiHTMLAttributes<HTMLLIElement>
191
187
  const SidebarMenuSubItem = forwardRef<HTMLLIElement, SidebarMenuSubItemProps>(
192
188
  ({ className, children, ...props }, ref) => {
193
189
  const rowRef = useRef<HTMLLIElement | null>(null)
194
- const item = useMenuRow(rowRef, true)
195
- const row = useMergeRefs(item.attachRow, ref)
190
+ const { item, row } = useMenuRow(rowRef, true)
191
+ const attach = useMergeRefs(item.attachRow, ref)
196
192
  return (
197
- <MenuItemContext.Provider value={item}>
198
- <li ref={row} data-sidebar="menu-sub-item" className={cn('group/menu-sub-item relative', className)} {...props}>
199
- {children}
200
- </li>
201
- </MenuItemContext.Provider>
193
+ <MenuRowContext.Provider value={row}>
194
+ <MenuItemContext.Provider value={item}>
195
+ <li
196
+ ref={attach}
197
+ data-sidebar="menu-sub-item"
198
+ className={cn('group/menu-sub-item relative', className)}
199
+ {...props}
200
+ >
201
+ {children}
202
+ </li>
203
+ </MenuItemContext.Provider>
204
+ </MenuRowContext.Provider>
202
205
  )
203
206
  },
204
207
  )
@@ -26,7 +26,7 @@ import { type SizeVariant, useSize } from '../../../lib/size-context'
26
26
  import { resolveSlotTemplate, slotElement } from '../../../lib/slot'
27
27
  import { useIsoLayoutEffect } from '../../../lib/use-iso-layout-effect'
28
28
  import { cn } from '../../../lib/utils'
29
- import { gutterVars, MenuItemContext, MenuScopeContext, SidebarSelection } from './menu-scope'
29
+ import { gutterVars, MenuItemContext, MenuScopeContext, SidebarSelection, useRowState } from './menu-scope'
30
30
  import { MenuRowLabel } from './row-label'
31
31
  import { useMeasuredCollapse } from './use-measured-collapse'
32
32
 
@@ -130,7 +130,8 @@ const SidebarMenuSubButton = forwardRef<HTMLAnchorElement, SidebarMenuSubButtonP
130
130
  return () => setButtonEl?.(null)
131
131
  }, [setButtonEl])
132
132
 
133
- const lit = isActive || (item?.isHovered ?? false)
133
+ const { isHovered } = useRowState(item?.rowRef)
134
+ const lit = isActive || isHovered
134
135
  const { template, content } = resolveSlotTemplate(render, asChild, children)
135
136
  const asButton = !template && value !== undefined && props.href === undefined
136
137