@opencode/ui 2.2.4 → 2.2.6

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 (127) hide show
  1. package/dist/actions/button/button.d.ts +2 -2
  2. package/dist/actions/icon-button/icon-button.d.ts +1 -1
  3. package/dist/components/animated-number-machine.d.ts +95 -0
  4. package/dist/components/file-icon-machine.d.ts +40 -0
  5. package/dist/components/file-icon.d.ts +0 -2
  6. package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
  7. package/dist/components/image-flight-machine.d.ts +135 -0
  8. package/dist/components/image-zoom-machine.d.ts +95 -0
  9. package/dist/components/image-zoom.d.ts +6 -3
  10. package/dist/components/list.d.ts +2 -6
  11. package/dist/components/menu-scroll.d.ts +12 -0
  12. package/dist/components/popover-machine.d.ts +96 -0
  13. package/dist/components/popover.d.ts +2 -2
  14. package/dist/components/resize-handle-machine.d.ts +71 -0
  15. package/dist/components/resize-handle.d.ts +2 -0
  16. package/dist/components/scroll-view-machine.d.ts +68 -0
  17. package/dist/components/scroll-view.d.ts +2 -2
  18. package/dist/components/spinner.d.ts +11 -3
  19. package/dist/components/text-field-machine.d.ts +62 -0
  20. package/dist/components/text-reveal-machine.d.ts +90 -0
  21. package/dist/components/typewriter-machine.d.ts +62 -0
  22. package/dist/context/dialog-machine.d.ts +125 -0
  23. package/dist/context/dialog.d.ts +17 -16
  24. package/dist/data-display/line-comment/line-comment.d.ts +9 -0
  25. package/dist/data-display/line-comment/machine.d.ts +157 -0
  26. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  27. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  28. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  29. package/dist/feedback/toast/machine.d.ts +57 -0
  30. package/dist/feedback/toast/toast.d.ts +1 -1
  31. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  32. package/dist/forms/select/machine.d.ts +45 -0
  33. package/dist/forms/select/select.d.ts +4 -0
  34. package/dist/forms/text-input/text-input.d.ts +4 -2
  35. package/dist/forms/textarea/textarea.d.ts +1 -0
  36. package/dist/forms/typeahead/machine.d.ts +85 -0
  37. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  38. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  39. package/dist/hooks/list-machine.d.ts +150 -0
  40. package/dist/hooks/use-filtered-list.d.ts +21 -35
  41. package/dist/i18n/en.d.ts +5 -0
  42. package/dist/icons/catalog.d.ts +8 -0
  43. package/dist/navigation/menu/machine.d.ts +49 -0
  44. package/dist/navigation/menu/menu.d.ts +5 -3
  45. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  46. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  47. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  48. package/dist/overlays/tooltip/machine.d.ts +146 -0
  49. package/dist/theme/context.d.ts +38 -14
  50. package/dist/theme/machine.d.ts +174 -0
  51. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  52. package/package.json +19 -3
  53. package/src/actions/button/button.tsx +11 -6
  54. package/src/actions/icon-button/icon-button.tsx +17 -4
  55. package/src/components/animated-number-machine.ts +214 -0
  56. package/src/components/animated-number.tsx +134 -83
  57. package/src/components/file-icon-machine.ts +78 -0
  58. package/src/components/file-icon.tsx +96 -579
  59. package/src/components/file-icons/choose-icon-name.ts +574 -0
  60. package/src/components/image-flight-machine.ts +247 -0
  61. package/src/components/image-preview.css +5 -2
  62. package/src/components/image-preview.tsx +390 -252
  63. package/src/components/image-zoom-machine.ts +191 -0
  64. package/src/components/image-zoom.ts +22 -14
  65. package/src/components/list.tsx +46 -137
  66. package/src/components/menu-scroll.css +70 -0
  67. package/src/components/menu-scroll.tsx +161 -0
  68. package/src/components/popover-machine.ts +198 -0
  69. package/src/components/popover.tsx +157 -89
  70. package/src/components/resize-handle-machine.ts +174 -0
  71. package/src/components/resize-handle.tsx +116 -67
  72. package/src/components/scroll-view-machine.ts +244 -0
  73. package/src/components/scroll-view.tsx +163 -116
  74. package/src/components/spinner.css +18 -5
  75. package/src/components/spinner.tsx +28 -14
  76. package/src/components/text-field-machine.ts +102 -0
  77. package/src/components/text-field.tsx +78 -24
  78. package/src/components/text-reveal-machine.ts +184 -0
  79. package/src/components/text-reveal.tsx +96 -93
  80. package/src/components/typewriter-machine.ts +134 -0
  81. package/src/components/typewriter.tsx +50 -39
  82. package/src/context/dialog-machine.ts +280 -0
  83. package/src/context/dialog.tsx +168 -177
  84. package/src/data-display/line-comment/line-comment.tsx +196 -132
  85. package/src/data-display/line-comment/machine.ts +391 -0
  86. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  87. package/src/data-display/project-avatar/animation.ts +67 -38
  88. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  89. package/src/feedback/toast/machine.ts +115 -0
  90. package/src/feedback/toast/toast.tsx +55 -59
  91. package/src/forms/checkbox/checkbox-compact.css +169 -0
  92. package/src/forms/checkbox/checkbox.tsx +93 -31
  93. package/src/forms/search-list/search-list.css +5 -1
  94. package/src/forms/search-list/search-list.tsx +4 -4
  95. package/src/forms/select/machine.ts +81 -0
  96. package/src/forms/select/select.css +59 -17
  97. package/src/forms/select/select.tsx +67 -31
  98. package/src/forms/text-input/text-input.css +30 -16
  99. package/src/forms/text-input/text-input.tsx +6 -2
  100. package/src/forms/textarea/textarea.css +13 -8
  101. package/src/forms/textarea/textarea.tsx +3 -1
  102. package/src/forms/typeahead/machine.ts +253 -0
  103. package/src/forms/typeahead/typeahead.tsx +75 -66
  104. package/src/hooks/auto-scroll-machine.ts +261 -0
  105. package/src/hooks/create-auto-scroll.tsx +110 -205
  106. package/src/hooks/list-machine.ts +359 -0
  107. package/src/hooks/use-filtered-list.tsx +101 -112
  108. package/src/i18n/en.ts +6 -1
  109. package/src/icons/catalog.ts +8 -0
  110. package/src/navigation/menu/machine.ts +109 -0
  111. package/src/navigation/menu/menu.tsx +130 -22
  112. package/src/navigation/segmented-control/machine.ts +72 -0
  113. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  114. package/src/overlays/dialog/dialog.tsx +7 -1
  115. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  116. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  117. package/src/overlays/tooltip/machine.ts +303 -0
  118. package/src/overlays/tooltip/tooltip.tsx +157 -92
  119. package/src/styles/theme.css +10 -2
  120. package/src/styles/tokens/theme.css +13 -0
  121. package/src/theme/context.tsx +192 -269
  122. package/src/theme/machine.ts +508 -0
  123. package/src/theme/themes/oc-2.json +2 -0
  124. package/src/theme/v2/mapping.ts +12 -8
  125. package/src/typography/text-shimmer/machine.ts +90 -0
  126. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  127. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -0,0 +1,174 @@
1
+ export type ResizeDirection = "horizontal" | "vertical"
2
+
3
+ export type ResizeEdge = "start" | "end"
4
+
5
+ /** Fixed for one press: where it started, which way the size grows, and the limits it resizes within. */
6
+ export type ResizeSession = {
7
+ readonly pointerId: number
8
+ readonly direction: ResizeDirection
9
+ readonly sign: 1 | -1
10
+ readonly startPos: number
11
+ readonly startSize: number
12
+ readonly min: number
13
+ readonly max: number
14
+ readonly threshold: number
15
+ }
16
+
17
+ /**
18
+ * `pressed` holds the pointer before it moves, so a click never reports a drag. `dragging` reports the size on each
19
+ * move; `collapsed` is set while the raw size sits below the collapse threshold.
20
+ */
21
+ export type ResizeHandleState =
22
+ | { readonly status: "idle" }
23
+ | { readonly status: "pressed"; readonly session: ResizeSession }
24
+ | { readonly status: "dragging"; readonly session: ResizeSession; readonly collapsed: boolean }
25
+ | { readonly status: "disposed" }
26
+
27
+ export type ResizeHandleEvent =
28
+ | {
29
+ readonly type: "pointer.down"
30
+ readonly button: number
31
+ readonly ctrlKey: boolean
32
+ readonly detail: number
33
+ readonly session: ResizeSession
34
+ }
35
+ // The driver reuses one move event object, so a drag allocates nothing per move.
36
+ | { type: "pointer.moved"; pointerId: number; pos: number }
37
+ | { readonly type: "pointer.up"; readonly pointerId: number }
38
+ /** Blur, `pointercancel` or lost capture; no `pointerId` cancels whichever pointer holds the handle. */
39
+ | { readonly type: "drag.cancelled"; readonly pointerId?: number }
40
+ | { readonly type: "disposed" }
41
+
42
+ export type ResizeHandleEffect =
43
+ | "lock-body"
44
+ | "unlock-body"
45
+ | "dragging-true"
46
+ | "dragging-false"
47
+ | "collapse-true"
48
+ | "collapse-false"
49
+ | "collapse-commit"
50
+ | "resize"
51
+
52
+ export type ResizeHandleTransition = {
53
+ readonly state: ResizeHandleState
54
+ readonly effects: readonly ResizeHandleEffect[]
55
+ }
56
+
57
+ const IDLE: ResizeHandleState = Object.freeze({ status: "idle" })
58
+
59
+ const DISPOSED: ResizeHandleState = Object.freeze({ status: "disposed" })
60
+
61
+ export const INITIAL_RESIZE_HANDLE_STATE: ResizeHandleState = IDLE
62
+
63
+ export const RESIZE_HANDLE_DISPOSED: ResizeHandleEvent = Object.freeze({ type: "disposed" })
64
+
65
+ export const RESIZE_DRAG_CANCELLED: ResizeHandleEvent = Object.freeze({ type: "drag.cancelled" })
66
+
67
+ const NONE: readonly ResizeHandleEffect[] = Object.freeze([])
68
+
69
+ const LOCK: readonly ResizeHandleEffect[] = Object.freeze(["lock-body"])
70
+
71
+ const UNLOCK: readonly ResizeHandleEffect[] = Object.freeze(["unlock-body"])
72
+
73
+ const RESIZE: readonly ResizeHandleEffect[] = Object.freeze(["resize"])
74
+
75
+ const START: readonly ResizeHandleEffect[] = Object.freeze(["dragging-true", "resize"])
76
+
77
+ const START_COLLAPSED: readonly ResizeHandleEffect[] = Object.freeze(["dragging-true", "collapse-true", "resize"])
78
+
79
+ const COLLAPSE: readonly ResizeHandleEffect[] = Object.freeze(["collapse-true", "resize"])
80
+
81
+ const EXPAND: readonly ResizeHandleEffect[] = Object.freeze(["collapse-false", "resize"])
82
+
83
+ const END: readonly ResizeHandleEffect[] = Object.freeze(["unlock-body", "dragging-false"])
84
+
85
+ const END_COLLAPSED: readonly ResizeHandleEffect[] = Object.freeze(["unlock-body", "dragging-false", "collapse-false"])
86
+
87
+ const COMMIT: readonly ResizeHandleEffect[] = Object.freeze(["unlock-body", "dragging-false", "collapse-commit"])
88
+
89
+ export function resizeSign(direction: ResizeDirection, edge: ResizeEdge, rtl: boolean): 1 | -1 {
90
+ if (direction === "vertical") return edge === "end" ? 1 : -1
91
+
92
+ return (edge === "start") !== rtl ? -1 : 1
93
+ }
94
+
95
+ /** The size before clamping, which decides collapse, so dragging past `min` can still collapse the panel. */
96
+ export function rawResizeSize(session: ResizeSession, pos: number) {
97
+ return session.startSize + session.sign * (pos - session.startPos)
98
+ }
99
+
100
+ export function resizeSize(session: ResizeSession, pos: number) {
101
+ return Math.min(session.max, Math.max(session.min, rawResizeSize(session, pos)))
102
+ }
103
+
104
+ export function transitionResizeHandle(state: ResizeHandleState, event: ResizeHandleEvent): ResizeHandleTransition {
105
+ if (state.status === "disposed") return { state, effects: NONE }
106
+
107
+ switch (event.type) {
108
+ case "disposed":
109
+ return { state: DISPOSED, effects: release(state) }
110
+ case "pointer.down":
111
+ if (state.status !== "idle" || event.button !== 0 || event.ctrlKey || event.detail > 1)
112
+ return { state, effects: NONE }
113
+
114
+ return { state: { status: "pressed", session: event.session }, effects: LOCK }
115
+ case "pointer.moved":
116
+ return move(state, event)
117
+ case "pointer.up":
118
+ if (state.status === "idle" || event.pointerId !== state.session.pointerId) return { state, effects: NONE }
119
+
120
+ return { state: IDLE, effects: state.status === "dragging" && state.collapsed ? COMMIT : release(state) }
121
+ case "drag.cancelled":
122
+ if (state.status === "idle") return { state, effects: NONE }
123
+
124
+ if (event.pointerId !== undefined && event.pointerId !== state.session.pointerId) return { state, effects: NONE }
125
+
126
+ return { state: IDLE, effects: release(state) }
127
+ }
128
+ }
129
+
130
+ export function selectResizeHandleActive(state: ResizeHandleState) {
131
+ return state.status === "pressed" || state.status === "dragging"
132
+ }
133
+
134
+ // Collapse state is reported only while it is in effect: a press that never collapsed reports nothing on release.
135
+ function release(state: Exclude<ResizeHandleState, { status: "disposed" }>) {
136
+ if (state.status === "idle") return NONE
137
+
138
+ if (state.status === "pressed") return UNLOCK
139
+
140
+ return state.collapsed ? END_COLLAPSED : END
141
+ }
142
+
143
+ const KEEP = new WeakMap<ResizeHandleState, ResizeHandleTransition>()
144
+
145
+ function move(
146
+ state: ResizeHandleState,
147
+ event: Extract<ResizeHandleEvent, { type: "pointer.moved" }>,
148
+ ): ResizeHandleTransition {
149
+ if (state.status !== "pressed" && state.status !== "dragging") return { state, effects: NONE }
150
+
151
+ if (event.pointerId !== state.session.pointerId) return { state, effects: NONE }
152
+
153
+ if (state.status === "pressed" && event.pos === state.session.startPos) return { state, effects: NONE }
154
+
155
+ const collapsed = state.session.threshold > 0 && rawResizeSize(state.session, event.pos) < state.session.threshold
156
+
157
+ if (state.status === "pressed")
158
+ return {
159
+ state: { status: "dragging", session: state.session, collapsed },
160
+ effects: collapsed ? START_COLLAPSED : START,
161
+ }
162
+
163
+ if (collapsed !== state.collapsed)
164
+ return { state: { status: "dragging", session: state.session, collapsed }, effects: collapsed ? COLLAPSE : EXPAND }
165
+
166
+ // A move that keeps the phase returns one cached transition per state, so `pointermove` allocates nothing.
167
+ const cached = KEEP.get(state)
168
+
169
+ if (cached) return cached
170
+ const kept = Object.freeze({ state, effects: RESIZE })
171
+ KEEP.set(state, kept)
172
+
173
+ return kept
174
+ }
@@ -1,4 +1,15 @@
1
- import { splitProps, type JSX } from "solid-js"
1
+ import { createSignal, onCleanup, splitProps, untrack, type JSX } from "solid-js"
2
+ import {
3
+ INITIAL_RESIZE_HANDLE_STATE,
4
+ RESIZE_DRAG_CANCELLED,
5
+ RESIZE_HANDLE_DISPOSED,
6
+ resizeSign,
7
+ resizeSize,
8
+ selectResizeHandleActive,
9
+ transitionResizeHandle,
10
+ type ResizeHandleEvent,
11
+ type ResizeHandleState,
12
+ } from "./resize-handle-machine"
2
13
 
3
14
  export interface ResizeHandleProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "onResize"> {
4
15
  direction: "horizontal" | "vertical"
@@ -11,6 +22,8 @@ export interface ResizeHandleProps extends Omit<JSX.HTMLAttributes<HTMLDivElemen
11
22
  /** Called while dragging when size crosses `collapseThreshold`. */
12
23
  onCollapseChange?: (collapsed: boolean) => void
13
24
  collapseThreshold?: number
25
+ /** Called with true on the first move of a drag and with false when it ends; a click without moving calls neither. */
26
+ onDraggingChange?: (dragging: boolean) => void
14
27
  }
15
28
 
16
29
  export function ResizeHandle(props: ResizeHandleProps) {
@@ -24,92 +37,128 @@ export function ResizeHandle(props: ResizeHandleProps) {
24
37
  "onCollapse",
25
38
  "onCollapseChange",
26
39
  "collapseThreshold",
40
+ "onDraggingChange",
27
41
  "class",
28
42
  "classList",
29
43
  ])
30
44
 
31
- const handleMouseDown = (e: MouseEvent) => {
32
- if (e.detail > 1) return
33
- e.preventDefault()
34
- const handle = e.currentTarget instanceof HTMLElement ? e.currentTarget : undefined
35
- const edge = local.edge ?? (local.direction === "vertical" ? "start" : "end")
36
- const start = local.direction === "horizontal" ? e.clientX : e.clientY
37
-
38
- const rtl =
39
- local.direction === "horizontal" &&
40
- e.currentTarget instanceof Element &&
41
- getComputedStyle(e.currentTarget).direction === "rtl"
42
-
43
- const startSize = local.size
44
- const min = local.min
45
- const max = local.max
46
- const threshold = local.collapseThreshold ?? 0
47
- const onResize = local.onResize
48
- const onCollapse = local.onCollapse
49
- const onCollapseChange = local.onCollapseChange
50
- let current = startSize
51
- let collapsed = false
52
-
53
- document.body.style.userSelect = "none"
54
- document.body.style.overflow = "hidden"
55
- document.body.style.cursor = local.direction === "horizontal" ? "col-resize" : "row-resize"
56
- handle?.setAttribute("data-dragging", "")
57
-
58
- const onMouseMove = (moveEvent: MouseEvent) => {
59
- const pos = local.direction === "horizontal" ? moveEvent.clientX : moveEvent.clientY
60
-
61
- const delta =
62
- local.direction === "vertical"
63
- ? edge === "end"
64
- ? pos - start
65
- : start - pos
66
- : (edge === "start") !== rtl
67
- ? start - pos
68
- : pos - start
69
-
70
- current = startSize + delta
71
- const nextCollapsed = threshold > 0 && current < threshold
72
-
73
- if (nextCollapsed !== collapsed) {
74
- collapsed = nextCollapsed
75
- onCollapseChange?.(collapsed)
45
+ const [dragState, setDragState] = createSignal<ResizeHandleState>(INITIAL_RESIZE_HANDLE_STATE)
46
+ const edge = () => local.edge ?? (local.direction === "vertical" ? "start" : "end")
47
+ const moved = { type: "pointer.moved" as const, pointerId: 0, pos: 0 }
48
+ let handle: HTMLDivElement | undefined
49
+ let body: { userSelect: string; overflow: string; cursor: string } | undefined
50
+
51
+ const dispatch = (event: ResizeHandleEvent) => {
52
+ const prev = untrack(dragState)
53
+ const next = transitionResizeHandle(prev, event)
54
+
55
+ if (next.state !== prev) setDragState(next.state)
56
+
57
+ for (const effect of next.effects) {
58
+ switch (effect) {
59
+ case "lock-body":
60
+ if (next.state.status !== "pressed") break
61
+ body = {
62
+ userSelect: document.body.style.userSelect,
63
+ overflow: document.body.style.overflow,
64
+ cursor: document.body.style.cursor,
65
+ }
66
+ document.body.style.userSelect = "none"
67
+ document.body.style.overflow = "hidden"
68
+ document.body.style.cursor = next.state.session.direction === "horizontal" ? "col-resize" : "row-resize"
69
+ window.addEventListener("pointermove", onPointerMove)
70
+ window.addEventListener("pointerup", onPointerUp)
71
+ window.addEventListener("pointercancel", onPointerCancel)
72
+ window.addEventListener("blur", onBlur)
73
+ break
74
+ case "unlock-body":
75
+ window.removeEventListener("pointermove", onPointerMove)
76
+ window.removeEventListener("pointerup", onPointerUp)
77
+ window.removeEventListener("pointercancel", onPointerCancel)
78
+ window.removeEventListener("blur", onBlur)
79
+
80
+ if (prev.status === "pressed" || prev.status === "dragging") {
81
+ if (handle?.hasPointerCapture(prev.session.pointerId)) handle.releasePointerCapture(prev.session.pointerId)
82
+ }
83
+
84
+ if (body) Object.assign(document.body.style, body)
85
+ body = undefined
86
+ break
87
+ case "dragging-true":
88
+ case "dragging-false":
89
+ local.onDraggingChange?.(effect === "dragging-true")
90
+ break
91
+ case "collapse-true":
92
+ case "collapse-false":
93
+ local.onCollapseChange?.(effect === "collapse-true")
94
+ break
95
+ case "collapse-commit":
96
+ local.onCollapse?.()
97
+ break
98
+ case "resize":
99
+ if (next.state.status === "dragging") local.onResize(resizeSize(next.state.session, moved.pos))
100
+ break
76
101
  }
77
-
78
- onResize(Math.min(max, Math.max(min, current)))
79
102
  }
103
+ }
80
104
 
81
- const onMouseUp = () => {
82
- document.body.style.userSelect = ""
83
- document.body.style.overflow = ""
84
- document.body.style.cursor = ""
85
- handle?.removeAttribute("data-dragging")
86
- document.removeEventListener("mousemove", onMouseMove)
87
- document.removeEventListener("mouseup", onMouseUp)
88
-
89
- if (collapsed) {
90
- onCollapse?.()
105
+ const onPointerMove = (e: PointerEvent) => {
106
+ const state = untrack(dragState)
91
107
 
92
- return
93
- }
108
+ if (state.status !== "pressed" && state.status !== "dragging") return
109
+ moved.pointerId = e.pointerId
110
+ moved.pos = state.session.direction === "horizontal" ? e.clientX : e.clientY
111
+ dispatch(moved)
112
+ }
94
113
 
95
- onCollapseChange?.(false)
96
- }
114
+ const onPointerUp = (e: PointerEvent) => dispatch({ type: "pointer.up", pointerId: e.pointerId })
115
+ const onPointerCancel = (e: PointerEvent) => dispatch({ type: "drag.cancelled", pointerId: e.pointerId })
116
+ const onBlur = () => dispatch(RESIZE_DRAG_CANCELLED)
97
117
 
98
- document.addEventListener("mousemove", onMouseMove)
99
- document.addEventListener("mouseup", onMouseUp)
100
- }
118
+ onCleanup(() => dispatch(RESIZE_HANDLE_DISPOSED))
101
119
 
102
120
  return (
103
121
  <div
104
122
  {...rest}
123
+ ref={handle}
105
124
  data-component="resize-handle"
106
125
  data-direction={local.direction}
107
- data-edge={local.edge ?? (local.direction === "vertical" ? "start" : "end")}
126
+ data-edge={edge()}
127
+ data-dragging={selectResizeHandleActive(dragState()) ? "" : undefined}
108
128
  classList={{
109
129
  ...local.classList,
110
130
  [local.class ?? ""]: !!local.class,
111
131
  }}
112
- onMouseDown={handleMouseDown}
132
+ // Bubbles on purpose: wrappers start their own resize state from this `pointerdown`.
133
+ onPointerDown={(e) => {
134
+ const rtl = local.direction === "horizontal" && getComputedStyle(e.currentTarget).direction === "rtl"
135
+
136
+ dispatch({
137
+ type: "pointer.down",
138
+ button: e.button,
139
+ ctrlKey: e.ctrlKey,
140
+ detail: e.detail,
141
+ session: {
142
+ pointerId: e.pointerId,
143
+ direction: local.direction,
144
+ sign: resizeSign(local.direction, edge(), rtl),
145
+ startPos: local.direction === "horizontal" ? e.clientX : e.clientY,
146
+ startSize: local.size,
147
+ min: local.min,
148
+ max: local.max,
149
+ threshold: local.collapseThreshold ?? 0,
150
+ },
151
+ })
152
+
153
+ // The handle holds the pointer until the button comes up, so hover stays on it rather than following the
154
+ // pointer across the panels it resizes, each crossing restyling their contents.
155
+ if (e.isTrusted && untrack(dragState).status === "pressed") e.currentTarget.setPointerCapture(e.pointerId)
156
+ }}
157
+ // Keeps focus and text selection where they are; canceling `pointerdown` would not.
158
+ onMouseDown={(e) => {
159
+ if (selectResizeHandleActive(untrack(dragState))) e.preventDefault()
160
+ }}
161
+ onLostPointerCapture={(e) => dispatch({ type: "drag.cancelled", pointerId: e.pointerId })}
113
162
  />
114
163
  )
115
164
  }
@@ -0,0 +1,244 @@
1
+ export const SCROLL_IDLE_MS = 800
2
+
3
+ export type ScrollAxis = "vertical" | "horizontal"
4
+
5
+ /**
6
+ * `scrolling` shows the thumb until `SCROLL_IDLE_MS` after the last scroll. `dragging` holds the pointer on one thumb;
7
+ * `scrolled` records a scroll during the drag, so its release keeps the thumb up for the idle delay. A thumb exists
8
+ * only while its axis overflows, so losing that overflow ends a drag on it.
9
+ */
10
+ export type ScrollThumbState =
11
+ | {
12
+ readonly status: "idle" | "scrolling"
13
+ readonly hovered: boolean
14
+ readonly verticalOverflow: boolean
15
+ readonly horizontalOverflow: boolean
16
+ }
17
+ | {
18
+ readonly status: "dragging"
19
+ readonly axis: ScrollAxis
20
+ readonly pointerId: number
21
+ readonly grabOffset: number
22
+ readonly rtl: boolean
23
+ readonly hovered: boolean
24
+ readonly scrolled: boolean
25
+ readonly verticalOverflow: boolean
26
+ readonly horizontalOverflow: boolean
27
+ }
28
+ | { readonly status: "disposed" }
29
+
30
+ export type ScrollThumbEvent =
31
+ | { readonly type: "hover.changed"; readonly hovered: boolean }
32
+ | { readonly type: "scrolled" }
33
+ | { readonly type: "idle.elapsed" }
34
+ | { readonly type: "overflow.changed"; readonly vertical: boolean; readonly horizontal: boolean }
35
+ | {
36
+ readonly type: "drag.started"
37
+ readonly axis: ScrollAxis
38
+ readonly pointerId: number
39
+ readonly grabOffset: number
40
+ readonly rtl: boolean
41
+ }
42
+ | { readonly type: "drag.ended"; readonly pointerId: number }
43
+ | { readonly type: "disposed" }
44
+
45
+ export type ScrollThumbEffect = "reset-idle-timer" | "clear-idle-timer" | "release-capture"
46
+
47
+ export type ScrollThumbTransition = {
48
+ readonly state: ScrollThumbState
49
+ readonly effects: readonly ScrollThumbEffect[]
50
+ }
51
+
52
+ type Resting = Extract<ScrollThumbState, { readonly status: "idle" | "scrolling" }>
53
+
54
+ type Dragging = Extract<ScrollThumbState, { readonly status: "dragging" }>
55
+
56
+ // Scroll, wheel and resize fire every frame. Every resting state and the common transitions are prebuilt, so those
57
+ // events allocate nothing.
58
+ const RESTING = (["idle", "scrolling"] as const).flatMap((status) =>
59
+ [false, true].flatMap((hovered) =>
60
+ [false, true].flatMap((verticalOverflow) =>
61
+ [false, true].map(
62
+ (horizontalOverflow): Resting => Object.freeze({ status, hovered, verticalOverflow, horizontalOverflow }),
63
+ ),
64
+ ),
65
+ ),
66
+ )
67
+
68
+ const DISPOSED: ScrollThumbState = Object.freeze({ status: "disposed" })
69
+
70
+ export const INITIAL_SCROLL_THUMB_STATE: ScrollThumbState = resting("idle", false, false, false)
71
+
72
+ export const SCROLLED: ScrollThumbEvent = Object.freeze({ type: "scrolled" })
73
+
74
+ export const SCROLL_IDLE_ELAPSED: ScrollThumbEvent = Object.freeze({ type: "idle.elapsed" })
75
+
76
+ export const HOVER_ENTERED: ScrollThumbEvent = Object.freeze({ type: "hover.changed", hovered: true })
77
+
78
+ export const HOVER_LEFT: ScrollThumbEvent = Object.freeze({ type: "hover.changed", hovered: false })
79
+
80
+ export const SCROLL_THUMB_DISPOSED: ScrollThumbEvent = Object.freeze({ type: "disposed" })
81
+
82
+ const OVERFLOW = [false, true].flatMap((vertical) =>
83
+ [false, true].map(
84
+ (horizontal): ScrollThumbEvent => Object.freeze({ type: "overflow.changed", vertical, horizontal }),
85
+ ),
86
+ )
87
+
88
+ const NONE: readonly ScrollThumbEffect[] = Object.freeze([])
89
+
90
+ const RESET: readonly ScrollThumbEffect[] = Object.freeze(["reset-idle-timer"])
91
+
92
+ const CLEAR: readonly ScrollThumbEffect[] = Object.freeze(["clear-idle-timer"])
93
+
94
+ const RELEASE: readonly ScrollThumbEffect[] = Object.freeze(["release-capture"])
95
+
96
+ const RELEASE_AND_RESET: readonly ScrollThumbEffect[] = Object.freeze(["release-capture", "reset-idle-timer"])
97
+
98
+ const RELEASE_AND_CLEAR: readonly ScrollThumbEffect[] = Object.freeze(["release-capture", "clear-idle-timer"])
99
+
100
+ // A drag's state is built per drag, so its no-op transition is cached on first use; pointer moves then allocate nothing.
101
+ const KEEP = new WeakMap<ScrollThumbState, ScrollThumbTransition>(
102
+ [...RESTING, DISPOSED].map((state) => [state, Object.freeze({ state, effects: NONE })]),
103
+ )
104
+
105
+ const RESTART = new Map<ScrollThumbState, ScrollThumbTransition>(
106
+ RESTING.map((state) => [state, Object.freeze({ state, effects: RESET })]),
107
+ )
108
+
109
+ export function overflowChanged(vertical: boolean, horizontal: boolean) {
110
+ return OVERFLOW[(vertical ? 2 : 0) + (horizontal ? 1 : 0)]
111
+ }
112
+
113
+ export function transitionScrollThumb(state: ScrollThumbState, event: ScrollThumbEvent): ScrollThumbTransition {
114
+ if (state.status === "disposed") return to(state, NONE)
115
+
116
+ if (event.type === "disposed")
117
+ return to(DISPOSED, state.status === "dragging" ? RELEASE_AND_CLEAR : state.status === "scrolling" ? CLEAR : NONE)
118
+
119
+ if (state.status === "dragging") return transitionDragging(state, event)
120
+
121
+ return transitionResting(state, event)
122
+ }
123
+
124
+ export function selectShowVerticalThumb(state: ScrollThumbState) {
125
+ return state.status !== "disposed" && state.verticalOverflow
126
+ }
127
+
128
+ export function selectShowHorizontalThumb(state: ScrollThumbState) {
129
+ return state.status !== "disposed" && state.horizontalOverflow
130
+ }
131
+
132
+ /** Scroll-only reveal: a drag shows the thumb through `selectDraggingAxis` instead. */
133
+ export function selectThumbScrolling(state: ScrollThumbState) {
134
+ return state.status === "scrolling"
135
+ }
136
+
137
+ export function selectDraggingAxis(state: ScrollThumbState) {
138
+ return state.status === "dragging" ? state.axis : undefined
139
+ }
140
+
141
+ /** `hoverReveals` is the `hover` visibility mode; in `scroll` mode only scrolling and dragging show the thumb. */
142
+ export function selectThumbVisible(state: ScrollThumbState, hoverReveals: boolean) {
143
+ if (state.status === "dragging" || state.status === "scrolling") return true
144
+
145
+ return state.status === "idle" && hoverReveals && state.hovered
146
+ }
147
+
148
+ function transitionResting(
149
+ state: Resting,
150
+ event: Exclude<ScrollThumbEvent, { type: "disposed" }>,
151
+ ): ScrollThumbTransition {
152
+ switch (event.type) {
153
+ case "hover.changed":
154
+ return to(resting(state.status, event.hovered, state.verticalOverflow, state.horizontalOverflow), NONE)
155
+ case "scrolled":
156
+ if (!state.verticalOverflow && !state.horizontalOverflow) return to(state, NONE)
157
+
158
+ return to(resting("scrolling", state.hovered, state.verticalOverflow, state.horizontalOverflow), RESET)
159
+ case "idle.elapsed":
160
+ if (state.status !== "scrolling") return to(state, NONE)
161
+
162
+ return to(resting("idle", state.hovered, state.verticalOverflow, state.horizontalOverflow), NONE)
163
+ case "overflow.changed":
164
+ if (state.status === "scrolling" && !event.vertical && !event.horizontal)
165
+ return to(resting("idle", state.hovered, false, false), CLEAR)
166
+
167
+ return to(resting(state.status, state.hovered, event.vertical, event.horizontal), NONE)
168
+ case "drag.started":
169
+ if (!(event.axis === "vertical" ? state.verticalOverflow : state.horizontalOverflow)) return to(state, NONE)
170
+
171
+ return {
172
+ state: {
173
+ status: "dragging",
174
+ axis: event.axis,
175
+ pointerId: event.pointerId,
176
+ grabOffset: event.grabOffset,
177
+ rtl: event.rtl,
178
+ hovered: state.hovered,
179
+ scrolled: state.status === "scrolling",
180
+ verticalOverflow: state.verticalOverflow,
181
+ horizontalOverflow: state.horizontalOverflow,
182
+ },
183
+ effects: state.status === "scrolling" ? CLEAR : NONE,
184
+ }
185
+ case "drag.ended":
186
+ return to(state, NONE)
187
+ }
188
+ }
189
+
190
+ function transitionDragging(
191
+ state: Dragging,
192
+ event: Exclude<ScrollThumbEvent, { type: "disposed" }>,
193
+ ): ScrollThumbTransition {
194
+ switch (event.type) {
195
+ case "hover.changed":
196
+ if (event.hovered === state.hovered) return to(state, NONE)
197
+
198
+ return to({ ...state, hovered: event.hovered }, NONE)
199
+ case "scrolled":
200
+ if (state.scrolled) return to(state, NONE)
201
+
202
+ return to({ ...state, scrolled: true }, NONE)
203
+ case "overflow.changed":
204
+ // The thumb unmounts with its overflow, and a removed element drops pointer capture without a `pointerup`.
205
+ if (!(state.axis === "vertical" ? event.vertical : event.horizontal))
206
+ return release(state, event.vertical, event.horizontal)
207
+
208
+ if (event.vertical === state.verticalOverflow && event.horizontal === state.horizontalOverflow)
209
+ return to(state, NONE)
210
+
211
+ return to({ ...state, verticalOverflow: event.vertical, horizontalOverflow: event.horizontal }, NONE)
212
+ case "drag.ended":
213
+ if (event.pointerId !== state.pointerId) return to(state, NONE)
214
+
215
+ return release(state, state.verticalOverflow, state.horizontalOverflow)
216
+ case "idle.elapsed":
217
+ case "drag.started":
218
+ return to(state, NONE)
219
+ }
220
+ }
221
+
222
+ function release(state: Dragging, vertical: boolean, horizontal: boolean) {
223
+ if (state.scrolled && (vertical || horizontal))
224
+ return to(resting("scrolling", state.hovered, vertical, horizontal), RELEASE_AND_RESET)
225
+
226
+ return to(resting("idle", state.hovered, vertical, horizontal), RELEASE)
227
+ }
228
+
229
+ function to(state: ScrollThumbState, effects: readonly ScrollThumbEffect[]): ScrollThumbTransition {
230
+ if (effects === NONE) return KEEP.get(state) ?? keep(state)
231
+
232
+ return (effects === RESET ? RESTART.get(state) : undefined) ?? { state, effects }
233
+ }
234
+
235
+ function keep(state: ScrollThumbState) {
236
+ const transition = Object.freeze({ state, effects: NONE })
237
+ KEEP.set(state, transition)
238
+
239
+ return transition
240
+ }
241
+
242
+ function resting(status: Resting["status"], hovered: boolean, vertical: boolean, horizontal: boolean) {
243
+ return RESTING[(status === "scrolling" ? 8 : 0) + (hovered ? 4 : 0) + (vertical ? 2 : 0) + (horizontal ? 1 : 0)]
244
+ }