@tremolo-ui/dom 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,10 @@
1
1
  export interface WheelOptions {
2
- /** Replace the callback through {@link WheelInstance.update}. */
3
- onWheel?: (event: WheelEvent) => void
2
+ /**
3
+ * Called for every wheel event that `requireFocus` lets through. The
4
+ * listener is not passive, so this may call `preventDefault()` to keep the
5
+ * page from scrolling.
6
+ */
7
+ onWheel: (event: WheelEvent) => void
4
8
  /**
5
9
  * Only report events while the focus is inside the element.
6
10
  *
@@ -19,7 +23,7 @@ export interface WheelOptions {
19
23
 
20
24
  export interface WheelInstance {
21
25
  /** Replace the given options, keeping the listener in place. */
22
- update: (options: WheelOptions) => void
26
+ update: (options: Partial<WheelOptions>) => void
23
27
  destroy: () => void
24
28
  }
25
29
 
@@ -31,10 +35,9 @@ export interface WheelInstance {
31
35
  */
32
36
  export function createWheel(
33
37
  element: Element,
34
- onWheel: (event: WheelEvent) => void,
35
- options: WheelOptions = {},
38
+ options: WheelOptions,
36
39
  ): WheelInstance {
37
- let opts = { ...options, onWheel }
40
+ let opts = options
38
41
 
39
42
  function hasFocus() {
40
43
  const active = element.ownerDocument?.activeElement
@@ -43,7 +46,7 @@ export function createWheel(
43
46
 
44
47
  const handler = (event: Event) => {
45
48
  if (opts.requireFocus && !hasFocus()) return
46
- opts.onWheel?.(event as WheelEvent)
49
+ opts.onWheel(event as WheelEvent)
47
50
  }
48
51
 
49
52
  element.addEventListener('wheel', handler, { passive: false })
@@ -68,6 +68,11 @@ export interface PointsEditorPoint {
68
68
  element?: Element | null
69
69
  /** The wheel option the point resolved, `null` for no wheel. */
70
70
  wheel?: ModifierValue<InputEventOption> | null
71
+ /**
72
+ * Called just before the wheel moves this point as the focused one, so that
73
+ * it can report its change as started before the value arrives.
74
+ */
75
+ beforeWheel?: () => void
71
76
  }
72
77
 
73
78
  export interface PointsEditorOptions {
@@ -326,7 +331,7 @@ export function createPointsEditor(
326
331
  nudgePoint,
327
332
  nudgeFocusedPoint: (axis, direction, modifiers) => {
328
333
  for (const [id, read] of points) {
329
- const { element, wheel, readonly, onChange } = read()
334
+ const { element, wheel, readonly, onChange, beforeWheel } = read()
330
335
  // The document the point is in, which need not be the one this script
331
336
  // runs in — an editor rendered into an iframe has its own focus.
332
337
  const active = element?.ownerDocument.activeElement
@@ -334,6 +339,7 @@ export function createPointsEditor(
334
339
  // axes stay part of the same interaction.
335
340
  if (!active || !element?.contains(active)) continue
336
341
  if (!wheel || readonly || !onChange) return false
342
+ beforeWheel?.()
337
343
  nudgePoint(id, axis, direction, wheel, modifiers)
338
344
  return true
339
345
  }