@opencode/ui 2.2.5 → 2.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +128 -111
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +111 -34
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +157 -178
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +59 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/inline-input/inline-input.css +0 -1
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +18 -10
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +74 -32
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/i18n/uk.ts +3 -3
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +133 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +159 -92
- package/src/styles/theme.css +10 -2
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
|
@@ -1,14 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { CloseButton, Content, Description, Portal, Root, Title, Trigger } from "@kobalte/core/popover"
|
|
2
|
+
import {
|
|
3
|
+
ComponentProps,
|
|
4
|
+
JSXElement,
|
|
5
|
+
ParentProps,
|
|
6
|
+
Show,
|
|
7
|
+
createComputed,
|
|
8
|
+
createContext,
|
|
9
|
+
createEffect,
|
|
10
|
+
createSignal,
|
|
11
|
+
onCleanup,
|
|
12
|
+
splitProps,
|
|
13
|
+
untrack,
|
|
14
|
+
useContext,
|
|
15
|
+
ValidComponent,
|
|
16
|
+
} from "solid-js"
|
|
5
17
|
import { useI18n } from "../context/i18n"
|
|
6
18
|
import { Icon } from "@opencode/ui/icon"
|
|
7
19
|
import { IconButton } from "@opencode/ui/icon-button"
|
|
20
|
+
import {
|
|
21
|
+
createPopoverState,
|
|
22
|
+
EMPTY_POPOVER_STACK,
|
|
23
|
+
selectPopoverOpen,
|
|
24
|
+
selectPreventCloseAutoFocus,
|
|
25
|
+
transitionPopover,
|
|
26
|
+
transitionPopoverStack,
|
|
27
|
+
type PopoverEvent,
|
|
28
|
+
type PopoverStackEvent,
|
|
29
|
+
} from "./popover-machine"
|
|
8
30
|
|
|
9
31
|
export interface PopoverProps<T extends ValidComponent = "div">
|
|
10
32
|
extends ParentProps,
|
|
11
|
-
Omit<ComponentProps<typeof
|
|
33
|
+
Omit<ComponentProps<typeof Root>, "children"> {
|
|
12
34
|
trigger?: JSXElement
|
|
13
35
|
triggerAs?: T
|
|
14
36
|
triggerProps?: ComponentProps<T>
|
|
@@ -20,6 +42,68 @@ export interface PopoverProps<T extends ValidComponent = "div">
|
|
|
20
42
|
portal?: boolean
|
|
21
43
|
}
|
|
22
44
|
|
|
45
|
+
type PopoverLayer = {
|
|
46
|
+
trigger: () => HTMLElement | undefined
|
|
47
|
+
content: () => HTMLElement | undefined
|
|
48
|
+
dismiss: (reason: "escape" | "outside") => void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Every open popover shares one set of window listeners, so Escape closes only the newest popover and a press inside a
|
|
52
|
+
// portaled child keeps its parent open.
|
|
53
|
+
let stack = EMPTY_POPOVER_STACK
|
|
54
|
+
|
|
55
|
+
let nextLayerID = 0
|
|
56
|
+
|
|
57
|
+
const layers = new Map<number, PopoverLayer>()
|
|
58
|
+
|
|
59
|
+
const PopoverParentContext = createContext<number | null>(null)
|
|
60
|
+
|
|
61
|
+
function dispatchStack(event: PopoverStackEvent) {
|
|
62
|
+
const previous = stack
|
|
63
|
+
const transition = transitionPopoverStack(previous, event)
|
|
64
|
+
stack = transition.state
|
|
65
|
+
|
|
66
|
+
if (previous.entries.length === 0 && stack.entries.length > 0) {
|
|
67
|
+
window.addEventListener("keydown", onStackKeyDown, true)
|
|
68
|
+
window.addEventListener("pointerdown", onStackInteract, true)
|
|
69
|
+
window.addEventListener("focusin", onStackInteract, true)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (previous.entries.length > 0 && stack.entries.length === 0) {
|
|
73
|
+
window.removeEventListener("keydown", onStackKeyDown, true)
|
|
74
|
+
window.removeEventListener("pointerdown", onStackInteract, true)
|
|
75
|
+
window.removeEventListener("focusin", onStackInteract, true)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
transition.effects.forEach((effect) => layers.get(effect.id)?.dismiss(effect.reason))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function onStackKeyDown(event: KeyboardEvent) {
|
|
82
|
+
// A dialog opened above the popover already took this Escape.
|
|
83
|
+
if (event.key !== "Escape" || event.defaultPrevented) return
|
|
84
|
+
const owner = event.target instanceof Element ? event.target.closest("[data-owns-escape]") : null
|
|
85
|
+
|
|
86
|
+
// A control inside the popover that cancels its own edit on Escape, such as a comment editor, keeps it open.
|
|
87
|
+
if (owner && ![...layers.values()].some((layer) => layer.trigger() === owner || layer.content() === owner)) return
|
|
88
|
+
dispatchStack({ type: "escape.pressed" })
|
|
89
|
+
event.preventDefault()
|
|
90
|
+
event.stopPropagation()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function onStackInteract(event: Event) {
|
|
94
|
+
const target = event.target
|
|
95
|
+
|
|
96
|
+
if (!(target instanceof Node)) return
|
|
97
|
+
|
|
98
|
+
const insideIds = new Set(
|
|
99
|
+
[...layers].flatMap(([id, layer]) =>
|
|
100
|
+
layer.trigger()?.contains(target) || layer.content()?.contains(target) ? [id] : [],
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
dispatchStack({ type: "outside.interacted", insideIds })
|
|
105
|
+
}
|
|
106
|
+
|
|
23
107
|
export function Popover<T extends ValidComponent = "div">(props: PopoverProps<T>) {
|
|
24
108
|
const i18n = useI18n()
|
|
25
109
|
|
|
@@ -40,99 +124,65 @@ export function Popover<T extends ValidComponent = "div">(props: PopoverProps<T>
|
|
|
40
124
|
"modal",
|
|
41
125
|
])
|
|
42
126
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
127
|
+
const id = ++nextLayerID
|
|
128
|
+
const parentId = useContext(PopoverParentContext)
|
|
129
|
+
const [popover, setPopover] = createSignal(createPopoverState(local.open, local.defaultOpen))
|
|
130
|
+
let triggerRef: HTMLElement | undefined
|
|
131
|
+
let contentRef: HTMLElement | undefined
|
|
132
|
+
|
|
133
|
+
const dispatch = (event: PopoverEvent) => {
|
|
134
|
+
const transition = transitionPopover(untrack(popover), event)
|
|
135
|
+
setPopover(transition.state)
|
|
136
|
+
transition.effects.forEach((effect) => {
|
|
137
|
+
local.onOpenChange?.(effect.open)
|
|
138
|
+
// A controlled owner that kept `open` refuses the close, which clears the pending dismiss reason.
|
|
139
|
+
dispatch({ type: "props.synced", open: untrack(() => local.open) })
|
|
140
|
+
})
|
|
56
141
|
}
|
|
57
142
|
|
|
58
|
-
|
|
59
|
-
if (next) setState("dismiss", null)
|
|
60
|
-
|
|
61
|
-
if (local.onOpenChange) local.onOpenChange(next)
|
|
62
|
-
|
|
63
|
-
if (controlled()) return
|
|
64
|
-
setState("uncontrolledOpen", next)
|
|
65
|
-
}
|
|
143
|
+
createComputed(() => dispatch({ type: "props.synced", open: local.open }))
|
|
66
144
|
|
|
67
145
|
createEffect(() => {
|
|
68
|
-
if (!
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return false
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const close = (reason: "escape" | "outside") => {
|
|
83
|
-
setState("dismiss", reason)
|
|
84
|
-
onOpenChange(false)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const onKeyDown = (event: KeyboardEvent) => {
|
|
88
|
-
if (event.key !== "Escape") return
|
|
89
|
-
close("escape")
|
|
90
|
-
event.preventDefault()
|
|
91
|
-
event.stopPropagation()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const onPointerDown = (event: PointerEvent) => {
|
|
95
|
-
const target = event.target
|
|
96
|
-
|
|
97
|
-
if (!(target instanceof Node)) return
|
|
98
|
-
|
|
99
|
-
if (inside(target)) return
|
|
100
|
-
close("outside")
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const onFocusIn = (event: FocusEvent) => {
|
|
104
|
-
const target = event.target
|
|
105
|
-
|
|
106
|
-
if (!(target instanceof Node)) return
|
|
107
|
-
|
|
108
|
-
if (inside(target)) return
|
|
109
|
-
close("outside")
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
makeEventListener(window, "keydown", onKeyDown, { capture: true })
|
|
113
|
-
makeEventListener(window, "pointerdown", onPointerDown, { capture: true })
|
|
114
|
-
makeEventListener(window, "focusin", onFocusIn, { capture: true })
|
|
146
|
+
if (!selectPopoverOpen(popover())) return
|
|
147
|
+
layers.set(id, {
|
|
148
|
+
trigger: () => triggerRef,
|
|
149
|
+
content: () => contentRef,
|
|
150
|
+
dismiss: (reason) => dispatch({ type: "dismiss", reason }),
|
|
151
|
+
})
|
|
152
|
+
dispatchStack({ type: "layer.opened", id, parentId })
|
|
153
|
+
onCleanup(() => {
|
|
154
|
+
dispatchStack({ type: "layer.closed", id })
|
|
155
|
+
layers.delete(id)
|
|
156
|
+
})
|
|
115
157
|
})
|
|
116
158
|
|
|
159
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
160
|
+
|
|
117
161
|
const content = () => (
|
|
118
|
-
<
|
|
119
|
-
ref={(el: HTMLElement | undefined) =>
|
|
162
|
+
<Content
|
|
163
|
+
ref={(el: HTMLElement | undefined) => {
|
|
164
|
+
contentRef = el
|
|
165
|
+
}}
|
|
120
166
|
data-component="popover-content"
|
|
167
|
+
data-owns-escape={selectPopoverOpen(popover()) ? "" : undefined}
|
|
121
168
|
classList={{
|
|
122
169
|
...local.classList,
|
|
123
170
|
[local.class ?? ""]: !!local.class,
|
|
124
171
|
}}
|
|
125
172
|
style={local.style}
|
|
173
|
+
onEscapeKeyDown={preventDismiss}
|
|
174
|
+
onPointerDownOutside={preventDismiss}
|
|
175
|
+
onFocusOutside={preventDismiss}
|
|
126
176
|
onCloseAutoFocus={(event: Event) => {
|
|
127
|
-
if (
|
|
128
|
-
|
|
177
|
+
if (selectPreventCloseAutoFocus(untrack(popover))) event.preventDefault()
|
|
178
|
+
dispatch({ type: "close-auto-focus.handled" })
|
|
129
179
|
}}
|
|
130
180
|
>
|
|
131
|
-
{/* <
|
|
181
|
+
{/* <Arrow data-slot="popover-arrow" /> */}
|
|
132
182
|
<Show when={local.title}>
|
|
133
183
|
<div data-slot="popover-header">
|
|
134
|
-
<
|
|
135
|
-
<
|
|
184
|
+
<Title data-slot="popover-title">{local.title}</Title>
|
|
185
|
+
<CloseButton
|
|
136
186
|
data-slot="popover-close-button"
|
|
137
187
|
as={IconButton}
|
|
138
188
|
icon={<Icon name="close" />}
|
|
@@ -142,25 +192,43 @@ export function Popover<T extends ValidComponent = "div">(props: PopoverProps<T>
|
|
|
142
192
|
</div>
|
|
143
193
|
</Show>
|
|
144
194
|
<Show when={local.description}>
|
|
145
|
-
<
|
|
195
|
+
<Description data-slot="popover-description">{local.description}</Description>
|
|
146
196
|
</Show>
|
|
147
|
-
<div data-slot="popover-body">
|
|
148
|
-
|
|
197
|
+
<div data-slot="popover-body">
|
|
198
|
+
<PopoverParentContext.Provider value={id}>{local.children}</PopoverParentContext.Provider>
|
|
199
|
+
</div>
|
|
200
|
+
</Content>
|
|
149
201
|
)
|
|
150
202
|
|
|
151
203
|
return (
|
|
152
|
-
<
|
|
153
|
-
|
|
154
|
-
|
|
204
|
+
<Root
|
|
205
|
+
gutter={4}
|
|
206
|
+
{...rest}
|
|
207
|
+
open={selectPopoverOpen(popover())}
|
|
208
|
+
onOpenChange={(open) => dispatch({ type: "kobalte.changed", open })}
|
|
209
|
+
modal={local.modal ?? false}
|
|
210
|
+
>
|
|
211
|
+
<Trigger
|
|
212
|
+
ref={(el: HTMLElement) => {
|
|
213
|
+
triggerRef = el
|
|
214
|
+
}}
|
|
155
215
|
as={local.triggerAs ?? "div"}
|
|
156
216
|
data-slot="popover-trigger"
|
|
217
|
+
data-owns-escape={selectPopoverOpen(popover()) ? "" : undefined}
|
|
218
|
+
// SAFETY: `triggerProps` are `ComponentProps<T>` for `triggerAs`, the component Kobalte renders; Kobalte's
|
|
219
|
+
// polymorphic props cannot follow the generic `T`.
|
|
157
220
|
{...(local.triggerProps as any)}
|
|
158
221
|
>
|
|
159
222
|
{local.trigger}
|
|
160
|
-
</
|
|
223
|
+
</Trigger>
|
|
161
224
|
<Show when={local.portal ?? true} fallback={content()}>
|
|
162
|
-
<
|
|
225
|
+
<Portal>{content()}</Portal>
|
|
163
226
|
</Show>
|
|
164
|
-
</
|
|
227
|
+
</Root>
|
|
165
228
|
)
|
|
166
229
|
}
|
|
230
|
+
|
|
231
|
+
// The shared window listeners decide when a popover dismisses; Kobalte only closes it from the trigger or close button.
|
|
232
|
+
function preventDismiss(event: Event) {
|
|
233
|
+
event.preventDefault()
|
|
234
|
+
}
|
|
@@ -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
|
+
}
|