@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
@@ -1,7 +1,8 @@
1
1
  import { Dialog as Kobalte, useDialogContext } from "@kobalte/core/dialog"
2
2
  import { makeEventListener } from "@solid-primitives/event-listener"
3
3
  import { createResizeObserver } from "@solid-primitives/resize-observer"
4
- import { createEffect, createMemo, createSignal, on, onCleanup, Show } from "solid-js"
4
+ import { createEffect, createMemo, createSignal, on, onCleanup, Show, untrack } from "solid-js"
5
+ import { createStore } from "solid-js/store"
5
6
  import { useDialogLayer } from "../context/dialog"
6
7
  import { useI18n } from "../context/i18n"
7
8
  import { Button } from "@opencode/ui/button"
@@ -19,6 +20,20 @@ import {
19
20
  type FlightFrame,
20
21
  } from "./image-flight"
21
22
  import {
23
+ DECODE_TIMEOUT_MS,
24
+ INITIAL_PREVIEW_FLIGHT_STATE,
25
+ selectAwaitingReveal,
26
+ selectDecodeCurrent,
27
+ selectFlightFading,
28
+ selectFlightInteractive,
29
+ selectFlightStage,
30
+ selectVideoAtRest,
31
+ transitionPreviewFlight,
32
+ type PreviewFlightEffect,
33
+ type PreviewFlightEvent,
34
+ } from "./image-flight-machine"
35
+ import {
36
+ clampAxis,
22
37
  clampView,
23
38
  detailScale,
24
39
  FIT_VIEW,
@@ -29,6 +44,15 @@ import {
29
44
  zoomAt,
30
45
  type ZoomView,
31
46
  } from "./image-zoom"
47
+ import {
48
+ INITIAL_PREVIEW_ZOOM_STATE,
49
+ selectDragging,
50
+ selectPanPointer,
51
+ transitionPreviewZoom,
52
+ type PanTarget,
53
+ type PreviewZoomEffect,
54
+ type PreviewZoomEvent,
55
+ } from "./image-zoom-machine"
32
56
 
33
57
  export interface ImagePreviewProps {
34
58
  src: string
@@ -52,13 +76,19 @@ const fade = { duration: 0.15, bounce: 0 }
52
76
 
53
77
  // How long the dialog stays mounted after a close starts, in milliseconds: the flight lands and hands the image back
54
78
  // to the page well within this.
55
- const exitDuration = Math.max(FLIGHT_SPRING.duration, fade.duration) * 3200
79
+ const EXIT_DURATION_MS = Math.max(FLIGHT_SPRING.duration, fade.duration) * 3200
56
80
 
57
81
  const CENTER = { x: 0, y: 0 }
58
82
 
83
+ const SETTLED: PreviewFlightEvent = Object.freeze({ type: "motion.settled" })
84
+
85
+ // Previews closing at once, such as one still flying home under the next, share the body's closing mark.
86
+ let closingPreviews = 0
87
+
59
88
  export function ImagePreview(props: ImagePreviewProps) {
60
89
  const i18n = useI18n()
61
90
  const dialog = useDialogContext()
91
+ const layer = useDialogLayer()
62
92
  let root!: HTMLDivElement
63
93
  let scrim!: HTMLDivElement
64
94
  let content!: HTMLDivElement
@@ -71,13 +101,18 @@ export function ImagePreview(props: ImagePreviewProps) {
71
101
  let surface!: HTMLDivElement
72
102
  let edge!: HTMLDivElement
73
103
  let close: HTMLButtonElement | undefined
104
+
74
105
  // Nothing shows until the image has loaded and the preview knows where it lands; while flying, a stand-in image and
75
106
  // its shadow travel above the dialog in place of the real one.
76
- const [stage, setStage] = createSignal<"pending" | "flying" | "rest">("pending")
107
+ const [flight, setFlight] = createSignal(INITIAL_PREVIEW_FLIGHT_STATE)
108
+ const [zoom, setZoom] = createSignal(INITIAL_PREVIEW_ZOOM_STATE)
109
+ // What the view shows the controls and the stylesheet, written only when it changes rather than on every frame.
110
+ const [shown, setShown] = createStore({ zoomed: false, ownsEscape: false, level: 100 })
77
111
 
78
- // The flight under way: where the stand-in is laid out (the flight only transforms and clips it), the springs, and
79
- // what happens when it lands.
80
- let course: { at: Box; flight: ReturnType<typeof createFlight>; landed: () => void } | undefined
112
+ // The flight under way: where the stand-in is laid out (the flight only transforms and clips it) and the springs.
113
+ let course: { at: Box; flight: ReturnType<typeof createFlight> } | undefined
114
+ let flightJob: (() => void) | undefined
115
+ let decodeTimer: ReturnType<typeof setTimeout> | undefined
81
116
 
82
117
  const dim = Spring.fromDuration(0, FLIGHT_SPRING)
83
118
  // Once a close lands, the stand-in fades out over the image the page shows again, so whatever the page draws over
@@ -86,162 +121,230 @@ export function ImagePreview(props: ImagePreviewProps) {
86
121
 
87
122
  // How far the flight has lifted off the page, as last painted: the origin's outline fades out as it rises.
88
123
  let lift = 0
89
- // Without a flight (reduced motion, or nothing on the page to fly to) the preview only fades with the backdrop.
90
- let plain = false
91
- let stop: (() => void) | undefined
92
- const [view, setView] = createSignal(FIT_VIEW)
93
- const [fitRatio, setFitRatio] = createSignal(1)
94
- const [drag, setDrag] = createSignal<{ id: number; x: number; y: number }>()
95
- // Whether the press that ends in a click began on the backdrop around the image and stayed put. A pan captures the
96
- // pointer, so its click lands on the backdrop wherever it began.
97
- let backdrop = false
98
- // Whether the pan under way has moved, so the click that ends it neither closes the preview nor plays the video.
99
- let panned = false
100
- // Where an eased zoom is heading, so repeated steps build on the destination rather than the frame in flight.
101
- const [goal, setGoal] = createSignal(FIT_VIEW)
102
- const glide = [1, 0, 0].map((value) => Spring.fromDuration(value, FLIGHT_SPRING))
103
- let gliding: (() => void) | undefined
124
+
125
+ // The view: each spring's value is what shows, and its target is where an eased zoom is heading, so repeated steps
126
+ // build on the destination rather than the frame in flight. Scale and offsets ride springs of one shape, so the point
127
+ // under the pointer stays put; a new target mid-glide keeps the velocity it has.
128
+ const scaleSpring = Spring.fromDuration(1, FLIGHT_SPRING)
129
+ const xSpring = Spring.fromDuration(0, FLIGHT_SPRING)
130
+ const ySpring = Spring.fromDuration(0, FLIGHT_SPRING)
131
+ let glideJob: (() => void) | undefined
132
+ // Where the pan under way last saw its pointer.
133
+ const cursor = { x: 0, y: 0 }
134
+
135
+ // Measured when the preview lays out rather than on every move or frame. A video zooms inside its own frame, so its
136
+ // controls stay where they are; an image zooms out across the window.
137
+ const natural = { width: 0, height: 0 }
138
+ const bounds = { width: 0, height: 0, viewportWidth: 0, viewportHeight: 0, maxScale: 1 }
139
+ let fitRatio = 1
140
+
141
+ // Reused on every pointer move of a pan.
142
+ const followEvent = { type: "follow" as const, zoomed: false }
143
+ const movedEvent = { type: "pointer.moved" as const, pointerId: 0 }
104
144
 
105
145
  const percent = createMemo(() => new Intl.NumberFormat(i18n.locale(), { style: "percent", maximumFractionDigits: 0 }))
106
146
 
107
- const natural = () =>
108
- image instanceof HTMLImageElement
109
- ? { width: image.naturalWidth, height: image.naturalHeight }
110
- : { width: image.width, height: image.height }
111
-
112
- // A video zooms inside its own frame, so its controls stay where they are; an image zooms out across the window.
113
- const bounds = () => ({
114
- width: image.offsetWidth,
115
- height: image.offsetHeight,
116
- viewportWidth: props.video ? image.offsetWidth : viewport.clientWidth,
117
- viewportHeight: props.video ? image.offsetHeight : viewport.clientHeight,
118
- maxScale: maxScale(fitRatio()),
119
- })
147
+ const dispatchFlight = (event: PreviewFlightEvent) => {
148
+ const prev = untrack(flight)
149
+ const next = transitionPreviewFlight(prev, event)
120
150
 
121
- // The fitted size, read when the preview lays out rather than on every frame of a glide.
122
- let size = { width: 0, height: 0 }
151
+ if (next.state !== prev) setFlight(next.state)
152
+ next.effects.forEach(runFlight)
153
+ }
123
154
 
124
- const show = (next: ZoomView) => {
125
- setView(next)
155
+ const runFlight = (effect: PreviewFlightEffect) => {
156
+ if (effect.type === "decode.start") return decode(effect.attempt)
126
157
 
127
- if (props.video) {
128
- props.video.style.setProperty("object-view-box", viewBox(next, size, natural()))
158
+ if (effect.type === "decode.cancel") return clearTimeout(decodeTimer)
129
159
 
130
- return
131
- }
160
+ if (effect.type === "video.borrow") return borrow()
132
161
 
133
- image.style.transform = `translate(${next.x}px, ${next.y}px) scale(${next.scale})`
134
- }
162
+ if (effect.type === "video.return") return giveBack()
135
163
 
136
- const settle = () => {
137
- gliding?.()
138
- gliding = undefined
139
- }
164
+ if (effect.type === "origin.hide") return home()?.setAttribute("data-image-preview-origin", "")
140
165
 
141
- // Pinch, wheel and drag follow the hand directly and take over from an eased zoom mid-flight.
142
- const follow = (next: ZoomView) => {
143
- settle()
144
- setGoal(next)
145
- show(next)
146
- }
166
+ if (effect.type === "origin.show") return home()?.removeAttribute("data-image-preview-origin")
147
167
 
148
- // Scale and offsets ride springs of one shape, so the point under the pointer stays put; a new target mid-glide
149
- // keeps the velocity it has.
150
- const ease = (target: ZoomView) => {
151
- setGoal(target)
168
+ if (effect.type === "open.flight") {
169
+ dimTo(1, FLIGHT_SPRING)
170
+ const next = createFlight(effect.from)
171
+ next.to(restFrame(), KICK.open, FLIGHT_SPRING)
172
+ course = { at: place(fitted()), flight: next }
173
+ paint()
152
174
 
153
- if (prefersReducedMotion()) return follow(target)
175
+ return run()
176
+ }
154
177
 
155
- if (!gliding) [view().scale, view().x, view().y].forEach((value, index) => glide[index].snap(value))
156
- ;[target.scale, target.x, target.y].forEach((value, index) => glide[index].set(value))
157
- gliding ??= frameLoop.add((dt) => {
158
- const [scale, x, y] = glide.map((spring) => spring.step(dt))
159
- show({ scale, x, y })
178
+ if (effect.type === "open.fade") {
179
+ dimTo(1, fade)
160
180
 
161
- if (!glide.every((spring) => spring.resting)) return true
162
- gliding = undefined
181
+ if (props.video) place(fitted())
182
+ paint()
163
183
 
164
- return false
165
- })
166
- }
184
+ return run()
185
+ }
167
186
 
168
- const step = (factor: number) => ease(zoomAt(goal(), goal().scale * factor, CENTER, bounds()))
187
+ if (effect.type === "close.flight") {
188
+ dimTo(0, FLIGHT_SPRING)
169
189
 
170
- // Kobalte keeps the content mounted while an exit animation runs; the dialog stack waits for the flight too.
171
- const layer = useDialogLayer()
172
- const [exit, setExit] = createSignal(0)
190
+ if (!effect.turn) course = { at: place(fitted()), flight: createFlight(restFrame()) }
191
+ course?.flight.to(effect.to, KICK.close, FLIGHT_SPRING)
192
+ paint()
173
193
 
174
- const holdExit = () => {
175
- setExit(exitDuration)
176
- layer?.setExitDuration(exit())
177
- }
194
+ return run()
195
+ }
178
196
 
179
- holdExit()
197
+ if (effect.type === "close.veil") {
198
+ veil.omega = (2 * Math.PI) / (FLIGHT_SPRING.duration * 0.75)
199
+ veil.set(0)
180
200
 
181
- const reveal = async () => {
182
- if (image instanceof HTMLImageElement) await Promise.all([image.decode(), standIn?.decode()]).catch(() => undefined)
201
+ return
202
+ }
183
203
 
184
- if (stage() !== "pending" || !dialog.isOpen()) return
185
- const from = prefersReducedMotion() ? undefined : originFrame()
204
+ if (effect.type === "close.fade") {
205
+ course = undefined
206
+ dimTo(0, fade)
186
207
 
187
- if (props.video) borrow(props.video)
188
- holdExit()
189
- dimTo(1, from ? FLIGHT_SPRING : fade)
208
+ return run()
209
+ }
190
210
 
191
- if (!from) {
192
- plain = true
211
+ if (effect.type === "zoom.fit") return ease(FIT_VIEW)
193
212
 
194
- if (props.video) place(fitted())
195
- paint()
196
- setStage("rest")
213
+ if (effect.type === "zoom.settle") return follow(scaleSpring.value, xSpring.value, ySpring.value)
197
214
 
198
- return run()
215
+ // See image-preview.css: the body takes clicks again while the closing flight holds the dialog mounted.
216
+ if (effect.type === "body.closing.acquire") {
217
+ closingPreviews++
218
+ document.body.setAttribute("data-image-preview-closing", "")
219
+
220
+ return
221
+ }
222
+
223
+ if (effect.type === "body.closing.release") {
224
+ closingPreviews--
225
+
226
+ if (!closingPreviews) document.body.removeAttribute("data-image-preview-closing")
227
+
228
+ return
199
229
  }
200
230
 
201
- const flight = createFlight(from)
202
- const rest = restFrame()
203
- flight.to(rest, KICK.open, FLIGHT_SPRING)
204
- course = { at: place(fitted()), flight, landed: () => setStage("rest") }
205
- hideOrigin()
206
- paint()
207
- setStage("flying")
208
- run()
231
+ flightJob?.()
232
+ flightJob = undefined
209
233
  }
210
234
 
211
- const dismiss = () => {
212
- // A video eases back to its whole frame on the way home; an image flies as it is shown.
213
- if (props.video) ease(FIT_VIEW)
214
- else settle()
235
+ const dispatchZoom = (event: PreviewZoomEvent) => {
236
+ const prev = untrack(zoom)
237
+ const next = transitionPreviewZoom(prev, event)
238
+
239
+ if (next.state !== prev) setZoom(next.state)
240
+ next.effects.forEach(runZoom)
241
+ }
215
242
 
216
- if (stage() === "pending") return
217
- const to = prefersReducedMotion() ? undefined : originFrame()
218
- dimTo(0, to ? FLIGHT_SPRING : fade)
243
+ const runZoom = (effect: PreviewZoomEffect) => {
244
+ if (effect.type === "pointer.capture") return panElement(effect.target).setPointerCapture(effect.pointerId)
219
245
 
220
- if (!to) {
221
- plain = true
246
+ if (effect.type === "pointer.release") {
247
+ const element = panElement(effect.target)
222
248
 
223
- return run()
249
+ if (element.hasPointerCapture(effect.pointerId)) element.releasePointerCapture(effect.pointerId)
250
+
251
+ return
224
252
  }
225
253
 
226
- // A close mid-open turns the flight around where it is, keeping its velocity.
227
- if (!course) {
228
- const rest = restFrame()
229
- course = { at: place(fitted()), flight: createFlight(rest), landed: showOrigin }
254
+ if (effect.type === "glide.loop.start") {
255
+ glideJob = frameLoop.add((dt) => {
256
+ scaleSpring.step(dt)
257
+ xSpring.step(dt)
258
+ ySpring.step(dt)
259
+ paintZoom()
260
+
261
+ if (!scaleSpring.resting || !xSpring.resting || !ySpring.resting) return true
262
+ dispatchZoom({ type: "glide.settled", zoomed: scaleSpring.value > 1 })
263
+
264
+ return false
265
+ })
266
+
267
+ return
230
268
  }
231
269
 
232
- course.flight.to(to, KICK.close, FLIGHT_SPRING)
233
- course.landed = () => {
234
- showOrigin()
270
+ if (effect.type === "glide.loop.stop") {
271
+ glideJob?.()
272
+ glideJob = undefined
235
273
 
236
- if (props.video) return giveBack()
237
- veil.omega = (2 * Math.PI) / (FLIGHT_SPRING.duration * 0.75)
238
- veil.set(0)
274
+ return
275
+ }
276
+
277
+ if (effect.type === "video.suppress-click") return suppressVideoClick()
278
+
279
+ dialog.close()
280
+ }
281
+
282
+ // Writes the view to the DOM. A frame allocates nothing but the style string, and touches Solid state only when
283
+ // the zoom level's rounded percent or whether the image is zoomed changes.
284
+ const paintZoom = () => {
285
+ const scale = scaleSpring.value
286
+
287
+ if (props.video) {
288
+ props.video.style.setProperty("object-view-box", viewBox(scale, xSpring.value, ySpring.value, bounds, natural))
239
289
  }
240
290
 
241
- hideOrigin()
242
- paint()
243
- setStage("flying")
244
- run()
291
+ if (!props.video) image.style.transform = `translate(${xSpring.value}px, ${ySpring.value}px) scale(${scale})`
292
+ const level = Math.round(scale * fitRatio * 100)
293
+
294
+ if (shown.zoomed !== scale > 1) setShown("zoomed", scale > 1)
295
+
296
+ if (shown.level !== level) setShown("level", level)
297
+ }
298
+
299
+ const view = (): ZoomView => ({ scale: scaleSpring.value, x: xSpring.value, y: ySpring.value })
300
+ const goal = (): ZoomView => ({ scale: scaleSpring.target, x: xSpring.target, y: ySpring.target })
301
+
302
+ const own = (scale: number) => {
303
+ if (shown.ownsEscape !== scale > 1) setShown("ownsEscape", scale > 1)
304
+ }
305
+
306
+ // Pinch, wheel and drag follow the hand directly and take over from an eased zoom mid-flight.
307
+ const follow = (scale: number, x: number, y: number) => {
308
+ followEvent.zoomed = scale > 1
309
+ dispatchZoom(followEvent)
310
+ scaleSpring.snap(scale)
311
+ xSpring.snap(x)
312
+ ySpring.snap(y)
313
+ own(scale)
314
+ paintZoom()
315
+ }
316
+
317
+ const ease = (target: ZoomView) => {
318
+ if (prefersReducedMotion()) return follow(target.scale, target.x, target.y)
319
+ scaleSpring.set(target.scale)
320
+ xSpring.set(target.x)
321
+ ySpring.set(target.y)
322
+ own(target.scale)
323
+ dispatchZoom({ type: "glide.start" })
324
+ }
325
+
326
+ const step = (factor: number) => {
327
+ const target = goal()
328
+ ease(zoomAt(target, target.scale * factor, CENTER, bounds))
329
+ }
330
+
331
+ // Kobalte keeps the content mounted while an exit animation runs; the dialog stack waits for the flight too.
332
+ layer?.setExitDuration(EXIT_DURATION_MS)
333
+
334
+ const decode = (attempt: number) => {
335
+ clearTimeout(decodeTimer)
336
+ decodeTimer = setTimeout(() => decoded(attempt), DECODE_TIMEOUT_MS)
337
+
338
+ if (!(image instanceof HTMLImageElement)) return
339
+ void Promise.all([image.decode(), standIn?.decode()])
340
+ .catch(() => undefined)
341
+ .then(() => decoded(attempt))
342
+ }
343
+
344
+ // Measuring where the origin shows is not free, so a decode the preview no longer waits for skips it.
345
+ const decoded = (attempt: number) => {
346
+ if (!selectDecodeCurrent(untrack(flight), attempt)) return
347
+ dispatchFlight({ type: "decode.ready", attempt, origin: originFrame() })
245
348
  }
246
349
 
247
350
  const dimTo = (target: number, spring: DurationOptions) => {
@@ -252,7 +355,7 @@ export function ImagePreview(props: ImagePreviewProps) {
252
355
 
253
356
  // One job on the shared frame loop steps the flight and the backdrop together and writes them straight to the DOM.
254
357
  const run = () => {
255
- stop ??= frameLoop.add((dt) => {
358
+ flightJob ??= frameLoop.add((dt) => {
256
359
  const flying = course?.flight.step(dt)
257
360
  dim.step(dt)
258
361
  veil.step(dt)
@@ -260,16 +363,13 @@ export function ImagePreview(props: ImagePreviewProps) {
260
363
 
261
364
  // The real image takes over only once the panel holding it is fully opaque, so its brightness doesn't step.
262
365
  if (course && !flying && dim.resting) {
263
- const landed = course.landed
264
366
  course = undefined
265
- landed()
367
+ dispatchFlight(SETTLED)
266
368
  }
267
369
 
268
370
  if (course || !dim.resting || !veil.resting) return true
269
- stop = undefined
270
-
271
- // A video that faded out rather than flying home goes back once it has.
272
- if (!dialog.isOpen()) giveBack()
371
+ flightJob = undefined
372
+ dispatchFlight(SETTLED)
273
373
 
274
374
  return false
275
375
  })
@@ -280,6 +380,8 @@ export function ImagePreview(props: ImagePreviewProps) {
280
380
 
281
381
  const paint = () => {
282
382
  const flying = flyer()
383
+ // Without a flight (reduced motion, or nothing on the page to fly to) the preview only fades with the backdrop.
384
+ const plain = selectFlightFading(untrack(flight))
283
385
  scrim.style.opacity = String(dim.value)
284
386
  surface.style.opacity = course ? "1" : "0"
285
387
 
@@ -310,19 +412,20 @@ export function ImagePreview(props: ImagePreviewProps) {
310
412
  const place = (box: Box) => {
311
413
  const flying = flyer()
312
414
 
313
- if (flying)
314
- Object.assign(flying.style, {
315
- left: `${box.x}px`,
316
- top: `${box.y}px`,
317
- width: `${box.width}px`,
318
- height: `${box.height}px`,
319
- })
415
+ if (!flying) return box
416
+ flying.style.left = `${box.x}px`
417
+ flying.style.top = `${box.y}px`
418
+ flying.style.width = `${box.width}px`
419
+ flying.style.height = `${box.height}px`
320
420
 
321
421
  return box
322
422
  }
323
423
 
324
424
  // Moves the video into the preview, leaving a box of its size in its place to fly back to.
325
- const borrow = (video: HTMLVideoElement) => {
425
+ const borrow = () => {
426
+ const video = props.video
427
+
428
+ if (!video) return
326
429
  const rect = video.getBoundingClientRect()
327
430
  holder = document.createElement("div")
328
431
  Object.assign(holder.style, {
@@ -352,19 +455,23 @@ export function ImagePreview(props: ImagePreviewProps) {
352
455
 
353
456
  // The image as shown now, zoomed and panned and cropped to the viewport. There is no panel: the image is the surface.
354
457
  const restFrame = (): FlightFrame => {
355
- const shown = image.getBoundingClientRect()
356
- const clip = intersect(shown, viewport.getBoundingClientRect())
357
- const cropped = clip.width < shown.width - 0.5 || clip.height < shown.height - 0.5
358
- const radius = cropped ? 0 : parseFloat(getComputedStyle(image).borderTopLeftRadius) * scale()
458
+ const shownBox = image.getBoundingClientRect()
459
+ const clip = intersect(shownBox, viewport.getBoundingClientRect())
460
+ const cropped = clip.width < shownBox.width - 0.5 || clip.height < shownBox.height - 0.5
461
+
462
+ const radius = cropped
463
+ ? 0
464
+ : parseFloat(getComputedStyle(image).borderTopLeftRadius) * (props.video ? 1 : scaleSpring.value)
359
465
 
360
- return { image: shown, clip, radius, surface: clip, surfaceRadius: radius, lift: 1 }
466
+ return { image: shownBox, clip, radius, surface: clip, surfaceRadius: radius, lift: 1 }
361
467
  }
362
468
 
363
- // The origin's frame, cropped to the part the page shows, or nothing when it is gone or out of sight.
469
+ // The origin's frame, cropped to the part the page shows, or nothing when it is gone, out of sight, or motion is
470
+ // reduced.
364
471
  const originFrame = () => {
365
472
  const origin = home()
366
473
 
367
- if (!origin?.isConnected || !natural().width) return
474
+ if (prefersReducedMotion() || !origin?.isConnected || !natural.width) return
368
475
  const rect = origin.getBoundingClientRect()
369
476
 
370
477
  if (!rect.width || !rect.height) return
@@ -379,46 +486,52 @@ export function ImagePreview(props: ImagePreviewProps) {
379
486
  ? "none"
380
487
  : ([style.boxShadow, getComputedStyle(origin, "::after").boxShadow].find((shadow) => shadow !== "none") ?? "none")
381
488
 
382
- return elementFrame(rect, natural(), style.objectFit, parseFloat(style.borderTopLeftRadius) || 0, visible)
489
+ return elementFrame(rect, natural, style.objectFit, parseFloat(style.borderTopLeftRadius) || 0, visible)
383
490
  }
384
491
 
385
492
  // The image's fitted layout box, wherever zoom has moved it. A video zooms inside its box, which stays put.
386
493
  const fitted = (): Box => {
387
- const shown = image.getBoundingClientRect()
388
- const offset = props.video ? CENTER : view()
494
+ const shownBox = image.getBoundingClientRect()
495
+ const x = props.video ? 0 : xSpring.value
496
+ const y = props.video ? 0 : ySpring.value
389
497
 
390
498
  return {
391
- x: shown.x + shown.width / 2 - offset.x - image.offsetWidth / 2,
392
- y: shown.y + shown.height / 2 - offset.y - image.offsetHeight / 2,
499
+ x: shownBox.x + shownBox.width / 2 - x - image.offsetWidth / 2,
500
+ y: shownBox.y + shownBox.height / 2 - y - image.offsetHeight / 2,
393
501
  width: image.offsetWidth,
394
502
  height: image.offsetHeight,
395
503
  }
396
504
  }
397
505
 
398
- const scale = () => (props.video ? 1 : view().scale)
399
506
  const home = () => holder ?? props.origin ?? props.video
400
- const hideOrigin = () => home()?.setAttribute("data-image-preview-origin", "")
401
- const showOrigin = () => home()?.removeAttribute("data-image-preview-origin")
507
+ const panElement = (target: PanTarget) => (target === "video" && props.video ? props.video : viewport)
508
+
509
+ const suppressVideoClick = () => {
510
+ const video = props.video
511
+
512
+ if (!video) return
513
+
514
+ const swallow = (event: MouseEvent) => {
515
+ event.preventDefault()
516
+ event.stopImmediatePropagation()
517
+ }
518
+
519
+ // The click follows its pointerup in the same task; one that never comes must not swallow a later one.
520
+ video.addEventListener("click", swallow, { capture: true, once: true })
521
+ setTimeout(() => video.removeEventListener("click", swallow, { capture: true }))
522
+ }
402
523
 
403
524
  onCleanup(() => {
404
- stop?.()
405
- settle()
406
- showOrigin()
407
- giveBack()
408
- document.body.removeAttribute("data-image-preview-closing")
525
+ dispatchFlight({ type: "disposed" })
526
+ dispatchZoom({ type: "disposed" })
409
527
  })
410
528
 
411
- createEffect(
412
- on(
413
- () => dialog.isOpen(),
414
- (open) => {
415
- // See image-preview.css: the body takes clicks again while the closing flight holds the dialog mounted.
416
- document.body.toggleAttribute("data-image-preview-closing", !open)
529
+ const closing = () => (layer ? layer.closing() : !dialog.isOpen())
417
530
 
418
- if (!open) dismiss()
419
- },
420
- { defer: true },
421
- ),
531
+ createEffect(
532
+ on(closing, (value) => {
533
+ if (value) dispatchFlight({ type: "close.started", origin: originFrame() })
534
+ }),
422
535
  )
423
536
 
424
537
  // From the center of the box zoom is measured in: the window's for an image, the frame's for a video.
@@ -429,17 +542,25 @@ export function ImagePreview(props: ImagePreviewProps) {
429
542
  }
430
543
 
431
544
  const measure = () => {
432
- if (!natural().width || !image.offsetWidth) return
433
- size = { width: image.offsetWidth, height: image.offsetHeight }
434
- setFitRatio(size.width / natural().width)
435
- follow(clampView(view(), bounds()))
545
+ natural.width = image instanceof HTMLImageElement ? image.naturalWidth : image.width
546
+ natural.height = image instanceof HTMLImageElement ? image.naturalHeight : image.height
547
+
548
+ if (!natural.width || !image.offsetWidth) return
549
+ bounds.width = image.offsetWidth
550
+ bounds.height = image.offsetHeight
551
+ bounds.viewportWidth = props.video ? image.offsetWidth : viewport.clientWidth
552
+ bounds.viewportHeight = props.video ? image.offsetHeight : viewport.clientHeight
553
+ fitRatio = bounds.width / natural.width
554
+ bounds.maxScale = maxScale(fitRatio)
555
+ const next = clampView(view(), bounds)
556
+ follow(next.scale, next.x, next.y)
436
557
 
437
558
  if (!props.video) return
438
559
 
439
560
  // A video is ready as soon as the preview has laid out its box.
440
- if (stage() === "pending") return void reveal()
561
+ if (selectAwaitingReveal(untrack(flight))) return dispatchFlight({ type: "video.ready", origin: originFrame() })
441
562
 
442
- if (holder && !course) place(fitted())
563
+ if (selectVideoAtRest(untrack(flight))) place(fitted())
443
564
  }
444
565
 
445
566
  createResizeObserver(() => [viewport, image], measure)
@@ -447,59 +568,75 @@ export function ImagePreview(props: ImagePreviewProps) {
447
568
  createEffect(
448
569
  on(
449
570
  () => props.src,
450
- () => follow(FIT_VIEW),
571
+ () => {
572
+ dispatchFlight({ type: "src.changed" })
573
+ follow(FIT_VIEW.scale, FIT_VIEW.x, FIT_VIEW.y)
574
+ },
451
575
  { defer: true },
452
576
  ),
453
577
  )
454
578
 
579
+ const interactive = () => selectFlightInteractive(untrack(flight))
580
+
455
581
  const detail = (event: MouseEvent) => {
456
- if (goal().scale > 1) return ease(FIT_VIEW)
457
- ease(zoomAt(goal(), detailScale(fitRatio()), point(event), bounds()))
582
+ if (!interactive()) return
583
+
584
+ if (scaleSpring.target > 1) return ease(FIT_VIEW)
585
+ ease(zoomAt(goal(), detailScale(fitRatio), point(event), bounds))
458
586
  }
459
587
 
460
- const grab = (event: PointerEvent, target: HTMLElement) => {
461
- if (event.button !== 0 || view().scale <= 1) return
588
+ const press = (event: PointerEvent, target: PanTarget | "content", onBackdrop: boolean) => {
589
+ if (!interactive()) return
590
+ dispatchZoom({
591
+ type: "pointer.down",
592
+ pointerId: event.pointerId,
593
+ button: event.button,
594
+ target,
595
+ onBackdrop,
596
+ zoomed: scaleSpring.value > 1,
597
+ })
598
+
599
+ if (!selectPanPointer(untrack(zoom), event.pointerId)) return
462
600
  event.preventDefault()
463
- target.setPointerCapture(event.pointerId)
464
- setDrag({ id: event.pointerId, x: event.clientX, y: event.clientY })
601
+ cursor.x = event.clientX
602
+ cursor.y = event.clientY
465
603
  }
466
604
 
605
+ // The hot path of a pan: no layout reads, and no Solid writes once the pan has moved.
467
606
  const pan = (event: PointerEvent) => {
468
- const current = drag()
469
-
470
- if (current?.id !== event.pointerId) return
471
- backdrop = false
472
- panned = true
473
- setDrag({ id: current.id, x: event.clientX, y: event.clientY })
474
- follow(panBy(view(), { x: event.clientX - current.x, y: event.clientY - current.y }, bounds()))
607
+ if (!selectPanPointer(untrack(zoom), event.pointerId)) return
608
+
609
+ // The button came up without a pointerup reaching the preview, so this is a hover, not a drag.
610
+ if ((event.buttons & 1) === 0) return dispatchZoom({ type: "pointer.cancel", pointerId: event.pointerId })
611
+ const dx = event.clientX - cursor.x
612
+ const dy = event.clientY - cursor.y
613
+
614
+ if (dx === 0 && dy === 0) return
615
+ cursor.x = event.clientX
616
+ cursor.y = event.clientY
617
+ movedEvent.pointerId = event.pointerId
618
+ dispatchZoom(movedEvent)
619
+ const scale = scaleSpring.value
620
+ follow(
621
+ scale,
622
+ clampAxis(xSpring.value + dx, bounds.width * scale, bounds.viewportWidth),
623
+ clampAxis(ySpring.value + dy, bounds.height * scale, bounds.viewportHeight),
624
+ )
475
625
  }
476
626
 
477
- const release = (event: PointerEvent) => {
478
- if (drag()?.id === event.pointerId) setDrag(undefined)
479
- }
627
+ const release = (event: PointerEvent) => dispatchZoom({ type: "pointer.up", pointerId: event.pointerId })
628
+ const cancel = (event: PointerEvent) => dispatchZoom({ type: "pointer.cancel", pointerId: event.pointerId })
480
629
 
481
630
  if (props.video) {
482
631
  const video = props.video
483
632
  makeEventListener(video, "pointerdown", (event: PointerEvent) => {
484
- panned = false
485
-
486
- if (holder && event.clientY < video.getBoundingClientRect().bottom - CONTROLS) grab(event, video)
633
+ if (event.clientY < video.getBoundingClientRect().bottom - CONTROLS) press(event, "video", false)
487
634
  })
488
635
  makeEventListener(video, "pointermove", pan)
489
636
  makeEventListener(video, "pointerup", release)
490
- makeEventListener(video, "pointercancel", release)
491
- makeEventListener(video, "dblclick", (event: MouseEvent) => holder && detail(event))
492
- makeEventListener(
493
- video,
494
- "click",
495
- (event: MouseEvent) => {
496
- if (!panned) return
497
- panned = false
498
- event.preventDefault()
499
- event.stopImmediatePropagation()
500
- },
501
- { capture: true },
502
- )
637
+ makeEventListener(video, "pointercancel", cancel)
638
+ makeEventListener(video, "lostpointercapture", cancel)
639
+ makeEventListener(video, "dblclick", detail)
503
640
  }
504
641
 
505
642
  // The preview owns zoom gestures while it is open: the page behind must neither scroll nor zoom the whole window.
@@ -507,35 +644,23 @@ export function ImagePreview(props: ImagePreviewProps) {
507
644
  window,
508
645
  "wheel",
509
646
  (event: WheelEvent) => {
510
- const inside =
511
- event.target instanceof Node && (viewport.contains(event.target) || (!!holder && event.target === props.video))
647
+ if (!interactive()) return
648
+ const inside = event.target instanceof Node && (viewport.contains(event.target) || event.target === props.video)
512
649
 
513
- const zoom = event.ctrlKey || event.metaKey
650
+ const pinch = event.ctrlKey || event.metaKey
514
651
 
515
- if (!inside && !zoom) return
652
+ if (!inside && !pinch) return
516
653
  event.preventDefault()
517
654
  event.stopPropagation()
518
655
 
519
656
  if (!inside) return
520
657
  const current = view()
521
658
 
522
- if (zoom) {
523
- follow(zoomAt(current, current.scale * wheelZoomFactor(pixels(event, event.deltaY)), point(event), bounds()))
659
+ const next = pinch
660
+ ? zoomAt(current, current.scale * wheelZoomFactor(pixels(event, event.deltaY)), point(event), bounds)
661
+ : panBy(current, wheelPan(event), bounds)
524
662
 
525
- return
526
- }
527
-
528
- const horizontal = event.shiftKey && event.deltaX === 0
529
- follow(
530
- panBy(
531
- current,
532
- {
533
- x: -pixels(event, horizontal ? event.deltaY : event.deltaX),
534
- y: horizontal ? 0 : -pixels(event, event.deltaY),
535
- },
536
- bounds(),
537
- ),
538
- )
663
+ follow(next.scale, next.x, next.y)
539
664
  },
540
665
  { passive: false, capture: true },
541
666
  )
@@ -545,9 +670,10 @@ export function ImagePreview(props: ImagePreviewProps) {
545
670
  window,
546
671
  "keydown",
547
672
  (event: KeyboardEvent) => {
673
+ if (!interactive()) return
548
674
  const bare = !event.metaKey && !event.ctrlKey && !event.altKey
549
675
 
550
- if (bare && event.key === " " && props.video && holder) {
676
+ if (bare && event.key === " " && props.video) {
551
677
  event.preventDefault()
552
678
  event.stopPropagation()
553
679
 
@@ -572,10 +698,10 @@ export function ImagePreview(props: ImagePreviewProps) {
572
698
  <div
573
699
  ref={root}
574
700
  data-component="image-preview"
575
- data-stage={stage()}
701
+ data-stage={selectFlightStage(flight())}
576
702
  // Zoomed in, the first Escape zooms back out; the dialog stack leaves it to the preview.
577
- data-owns-escape={goal().scale > 1 ? "" : undefined}
578
- style={{ "--image-preview-exit": `${exit()}ms` }}
703
+ data-owns-escape={shown.ownsEscape ? "" : undefined}
704
+ style={{ "--image-preview-exit": `${EXIT_DURATION_MS}ms` }}
579
705
  >
580
706
  <div ref={scrim} data-slot="image-preview-scrim" />
581
707
  <Kobalte.Content
@@ -587,7 +713,7 @@ export function ImagePreview(props: ImagePreviewProps) {
587
713
  close?.focus()
588
714
  }}
589
715
  onEscapeKeyDown={(event) => {
590
- if (goal().scale <= 1) return
716
+ if (scaleSpring.target <= 1) return
591
717
  event.preventDefault()
592
718
  ease(FIT_VIEW)
593
719
  }}
@@ -596,24 +722,28 @@ export function ImagePreview(props: ImagePreviewProps) {
596
722
  if (props.video && event.target === props.video) event.preventDefault()
597
723
  }}
598
724
  onPointerDown={(event: PointerEvent) => {
599
- backdrop = event.target === content || event.target === viewport
600
- }}
601
- onClick={(event: MouseEvent) => {
602
- // The preview fills the window, so a click on the backdrop around the image lands here rather than outside.
603
- if (!backdrop || (event.target !== content && event.target !== viewport)) return
604
- dialog.close()
725
+ const target = event.target
726
+ const body = target instanceof Node && viewport.contains(target)
727
+ press(event, body ? "viewport" : "content", target === content || target === viewport)
605
728
  }}
729
+ // The preview fills the window, so a click on the backdrop around the image lands here rather than outside.
730
+ onClick={(event: MouseEvent) =>
731
+ dispatchZoom({
732
+ type: "backdrop.click",
733
+ onBackdrop: event.target === content || event.target === viewport,
734
+ })
735
+ }
606
736
  >
607
737
  <div
608
738
  ref={viewport}
609
739
  data-slot="image-preview-body"
610
- data-zoomed={view().scale > 1 ? "" : undefined}
611
- data-dragging={drag() ? "" : undefined}
740
+ data-zoomed={shown.zoomed ? "" : undefined}
741
+ data-dragging={selectDragging(zoom()) ? "" : undefined}
612
742
  onDblClick={detail}
613
- onPointerDown={(event) => grab(event, viewport)}
614
743
  onPointerMove={pan}
615
744
  onPointerUp={release}
616
- onPointerCancel={release}
745
+ onPointerCancel={cancel}
746
+ onLostPointerCapture={cancel}
617
747
  >
618
748
  <Show
619
749
  when={props.video}
@@ -626,9 +756,9 @@ export function ImagePreview(props: ImagePreviewProps) {
626
756
  draggable={false}
627
757
  onLoad={() => {
628
758
  measure()
629
- void reveal()
759
+ dispatchFlight({ type: "image.loaded" })
630
760
  }}
631
- onError={() => void reveal()}
761
+ onError={() => dispatchFlight({ type: "image.failed" })}
632
762
  />
633
763
  }
634
764
  >
@@ -658,7 +788,7 @@ export function ImagePreview(props: ImagePreviewProps) {
658
788
  title={i18n.t("ui.imagePreview.zoomToFit")}
659
789
  onClick={() => ease(FIT_VIEW)}
660
790
  >
661
- {percent().format(view().scale * fitRatio())}
791
+ {percent().format(shown.level / 100)}
662
792
  </Button>
663
793
  <IconButton
664
794
  data-slot="image-preview-zoom-in"
@@ -758,13 +888,11 @@ function dispose(video: HTMLVideoElement) {
758
888
  }
759
889
 
760
890
  function layout(element: HTMLElement, frame: Box, radius: number) {
761
- Object.assign(element.style, {
762
- left: `${frame.x}px`,
763
- top: `${frame.y}px`,
764
- width: `${frame.width}px`,
765
- height: `${frame.height}px`,
766
- borderRadius: `${radius}px`,
767
- })
891
+ element.style.left = `${frame.x}px`
892
+ element.style.top = `${frame.y}px`
893
+ element.style.width = `${frame.width}px`
894
+ element.style.height = `${frame.height}px`
895
+ element.style.borderRadius = `${radius}px`
768
896
  }
769
897
 
770
898
  function shortcut(key: string) {
@@ -775,6 +903,16 @@ function shortcut(key: string) {
775
903
  if (key === "0") return "fit"
776
904
  }
777
905
 
906
+ // A plain wheel pans; Shift turns a vertical wheel sideways.
907
+ function wheelPan(event: WheelEvent) {
908
+ const horizontal = event.shiftKey && event.deltaX === 0
909
+
910
+ return {
911
+ x: -pixels(event, horizontal ? event.deltaY : event.deltaX),
912
+ y: horizontal ? 0 : -pixels(event, event.deltaY),
913
+ }
914
+ }
915
+
778
916
  function pixels(event: WheelEvent, delta: number) {
779
917
  if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) return delta * 16
780
918