@cognitap/radiant 0.0.5 → 0.0.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.
- package/CHANGELOG.md +17 -0
- package/README.md +8 -0
- package/dist/library/Button.d.ts +3 -1
- package/dist/library/Button.d.ts.map +1 -1
- package/dist/library/Button.js +43 -2
- package/dist/library/Button.js.map +1 -1
- package/dist/library/Collapsible.d.ts +2 -1
- package/dist/library/Collapsible.d.ts.map +1 -1
- package/dist/library/Collapsible.js +5 -2
- package/dist/library/Collapsible.js.map +1 -1
- package/dist/library/Dialog.d.ts +2 -1
- package/dist/library/Dialog.d.ts.map +1 -1
- package/dist/library/Dialog.js +5 -2
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/ThemeScope.d.ts.map +1 -1
- package/dist/library/ThemeScope.js +10 -9
- package/dist/library/ThemeScope.js.map +1 -1
- package/dist/library/index.d.ts +3 -1
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +1 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/motion/MotionLink.d.ts +7 -0
- package/dist/library/motion/MotionLink.d.ts.map +1 -0
- package/dist/library/motion/MotionLink.js +45 -0
- package/dist/library/motion/MotionLink.js.map +1 -0
- package/dist/library/motion/browser.d.ts +5 -0
- package/dist/library/motion/browser.d.ts.map +1 -0
- package/dist/library/motion/browser.js +35 -0
- package/dist/library/motion/browser.js.map +1 -0
- package/dist/library/motion/config.d.ts +26 -0
- package/dist/library/motion/config.d.ts.map +1 -0
- package/dist/library/motion/config.js +58 -0
- package/dist/library/motion/config.js.map +1 -0
- package/dist/library/motion/context.d.ts +2 -0
- package/dist/library/motion/context.d.ts.map +1 -0
- package/dist/library/motion/context.js +5 -0
- package/dist/library/motion/context.js.map +1 -0
- package/dist/library/motion/drag.d.ts +20 -0
- package/dist/library/motion/drag.d.ts.map +1 -0
- package/dist/library/motion/drag.js +138 -0
- package/dist/library/motion/drag.js.map +1 -0
- package/dist/library/motion/dragBrowser.d.ts +4 -0
- package/dist/library/motion/dragBrowser.d.ts.map +1 -0
- package/dist/library/motion/dragBrowser.js +156 -0
- package/dist/library/motion/dragBrowser.js.map +1 -0
- package/dist/library/motion/follow.d.ts +9 -0
- package/dist/library/motion/follow.d.ts.map +1 -0
- package/dist/library/motion/follow.js +26 -0
- package/dist/library/motion/follow.js.map +1 -0
- package/dist/library/motion/host.d.ts +27 -0
- package/dist/library/motion/host.d.ts.map +1 -0
- package/dist/library/motion/host.js +62 -0
- package/dist/library/motion/host.js.map +1 -0
- package/dist/library/motion/index.d.ts +16 -0
- package/dist/library/motion/index.d.ts.map +1 -0
- package/dist/library/motion/index.js +9 -0
- package/dist/library/motion/index.js.map +1 -0
- package/dist/library/motion/layout.d.ts +20 -0
- package/dist/library/motion/layout.d.ts.map +1 -0
- package/dist/library/motion/layout.js +67 -0
- package/dist/library/motion/layout.js.map +1 -0
- package/dist/library/motion/layoutDom.d.ts +17 -0
- package/dist/library/motion/layoutDom.d.ts.map +1 -0
- package/dist/library/motion/layoutDom.js +272 -0
- package/dist/library/motion/layoutDom.js.map +1 -0
- package/dist/library/motion/press.d.ts +5 -0
- package/dist/library/motion/press.d.ts.map +1 -0
- package/dist/library/motion/press.js +72 -0
- package/dist/library/motion/press.js.map +1 -0
- package/dist/library/motion/pressPolicy.d.ts +5 -0
- package/dist/library/motion/pressPolicy.d.ts.map +1 -0
- package/dist/library/motion/pressPolicy.js +8 -0
- package/dist/library/motion/pressPolicy.js.map +1 -0
- package/dist/library/motion/react.d.ts +35 -0
- package/dist/library/motion/react.d.ts.map +1 -0
- package/dist/library/motion/react.js +224 -0
- package/dist/library/motion/react.js.map +1 -0
- package/dist/library/motion/runtime.d.ts +22 -0
- package/dist/library/motion/runtime.d.ts.map +1 -0
- package/dist/library/motion/runtime.js +117 -0
- package/dist/library/motion/runtime.js.map +1 -0
- package/dist/library/motion/transitions.d.ts +5 -0
- package/dist/library/motion/transitions.d.ts.map +1 -0
- package/dist/library/motion/transitions.js +211 -0
- package/dist/library/motion/transitions.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/defineTheme.d.ts +3 -0
- package/dist/library/themes/defineTheme.d.ts.map +1 -1
- package/dist/library/themes/defineTheme.js +4 -2
- package/dist/library/themes/defineTheme.js.map +1 -1
- package/dist/library/themes/index.d.ts +1 -1
- package/dist/library/themes/index.d.ts.map +1 -1
- package/dist/library/themes/index.js.map +1 -1
- package/dist/library/themes/themeDocument.d.ts.map +1 -1
- package/dist/library/themes/themeDocument.js +11 -5
- package/dist/library/themes/themeDocument.js.map +1 -1
- package/dist/library/themes/themeDocumentTypes.d.ts +5 -1
- package/dist/library/themes/themeDocumentTypes.d.ts.map +1 -1
- package/dist/library/themes/themeDocumentTypes.js.map +1 -1
- package/docs/motion.md +434 -0
- package/docs/system.md +26 -0
- package/package.json +14 -3
- package/src/Button.tsx +49 -1
- package/src/Collapsible.tsx +13 -1
- package/src/Dialog.tsx +10 -1
- package/src/ThemeScope.tsx +21 -17
- package/src/index.ts +9 -0
- package/src/motion/MotionLink.tsx +64 -0
- package/src/motion/browser.ts +45 -0
- package/src/motion/config.ts +79 -0
- package/src/motion/context.ts +4 -0
- package/src/motion/drag.ts +151 -0
- package/src/motion/dragBrowser.ts +159 -0
- package/src/motion/follow.ts +28 -0
- package/src/motion/host.ts +88 -0
- package/src/motion/index.ts +28 -0
- package/src/motion/layout.ts +78 -0
- package/src/motion/layoutDom.ts +293 -0
- package/src/motion/press.ts +77 -0
- package/src/motion/pressPolicy.ts +12 -0
- package/src/motion/react.ts +272 -0
- package/src/motion/runtime.ts +122 -0
- package/src/motion/transitions.ts +206 -0
- package/src/themes/defineTheme.ts +10 -2
- package/src/themes/index.ts +1 -0
- package/src/themes/themeDocument.ts +14 -6
- package/src/themes/themeDocumentTypes.ts +12 -5
package/src/Dialog.tsx
CHANGED
|
@@ -4,6 +4,8 @@ import type { ComponentProps, MouseEvent, ReactNode } from 'react'
|
|
|
4
4
|
import { createContext, useContext, useEffect, useId, useRef, useState } from 'react'
|
|
5
5
|
import { cva } from 'class-variance-authority'
|
|
6
6
|
import { Button, type ButtonProps, type ButtonRenderProps } from './Button.js'
|
|
7
|
+
import { callerOwnsPress } from './motion/pressPolicy.js'
|
|
8
|
+
import { useTransitionHost } from './motion/host.js'
|
|
7
9
|
import { cn } from './cn.js'
|
|
8
10
|
import { composeAction } from './internal/events.js'
|
|
9
11
|
import { joinIds } from './internal/ids.js'
|
|
@@ -93,6 +95,7 @@ export type DialogTriggerProps = ButtonProps
|
|
|
93
95
|
|
|
94
96
|
export type DialogContentProps = Omit<ComponentProps<'dialog'>, 'open' | 'style'> & {
|
|
95
97
|
style?: ThemeStyle
|
|
98
|
+
motion?: 'inherit' | 'off'
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
export type DialogTitleProps = Omit<ComponentProps<'h2'>, 'style'> & { style?: ThemeStyle }
|
|
@@ -184,6 +187,7 @@ export function DialogTrigger(props: DialogTriggerProps) {
|
|
|
184
187
|
}
|
|
185
188
|
|
|
186
189
|
export function DialogContent({
|
|
190
|
+
motion = 'inherit',
|
|
187
191
|
className,
|
|
188
192
|
onClick,
|
|
189
193
|
onCancel,
|
|
@@ -193,7 +197,12 @@ export function DialogContent({
|
|
|
193
197
|
}: DialogContentProps) {
|
|
194
198
|
const dialog = useDialogContext()
|
|
195
199
|
const nodeRef = useRef<HTMLDialogElement>(null)
|
|
196
|
-
const
|
|
200
|
+
const transition = useTransitionHost<HTMLDialogElement>(
|
|
201
|
+
'dialog',
|
|
202
|
+
ref,
|
|
203
|
+
motion === 'off' || callerOwnsPress(props.style, className)
|
|
204
|
+
)
|
|
205
|
+
const hostRef = useComposedRefs(nodeRef, transition.active ? transition.ref : ref)
|
|
197
206
|
const unmounting = useRef(false)
|
|
198
207
|
const previousModal = useRef(dialog.modal)
|
|
199
208
|
// Bumps after the browser closes a dialog whose controlled `open` value is still true.
|
package/src/ThemeScope.tsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
+
import { MotionProfileContext } from './motion/context.js'
|
|
4
|
+
|
|
3
5
|
import { createContext, useContext } from 'react'
|
|
4
6
|
import type { ComponentProps } from 'react'
|
|
5
7
|
import { cn } from './cn.js'
|
|
@@ -42,23 +44,25 @@ export function ThemeScope({ theme, mode, style, className, children, ...props }
|
|
|
42
44
|
|
|
43
45
|
return (
|
|
44
46
|
<ThemeContext.Provider value={{ theme: selected, overrides }}>
|
|
45
|
-
<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
?
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
47
|
+
<MotionProfileContext.Provider value={selected.motion ?? defaultTheme.motion}>
|
|
48
|
+
<div
|
|
49
|
+
{...props}
|
|
50
|
+
data-rad-theme={theme?.id}
|
|
51
|
+
data-rad-mode={mode}
|
|
52
|
+
className={cn(scopeClass, className)}
|
|
53
|
+
style={{
|
|
54
|
+
...resolved,
|
|
55
|
+
...(mode === undefined
|
|
56
|
+
? parent === null
|
|
57
|
+
? { colorScheme: 'light dark' }
|
|
58
|
+
: {}
|
|
59
|
+
: { colorScheme: mode === 'system' ? 'light dark' : mode }),
|
|
60
|
+
...style
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
{children}
|
|
64
|
+
</div>
|
|
65
|
+
</MotionProfileContext.Provider>
|
|
62
66
|
</ThemeContext.Provider>
|
|
63
67
|
)
|
|
64
68
|
}
|
package/src/index.ts
CHANGED
|
@@ -116,6 +116,7 @@ export type {
|
|
|
116
116
|
ThemeColorStop,
|
|
117
117
|
ThemeBrandSource,
|
|
118
118
|
ThemePalette,
|
|
119
|
+
ThemeMotionSection,
|
|
119
120
|
ThemeTypographySection,
|
|
120
121
|
ThemeFontAxis,
|
|
121
122
|
ThemeFontMetadata
|
|
@@ -132,3 +133,11 @@ export type {
|
|
|
132
133
|
PaletteContrastCheck,
|
|
133
134
|
WorkingPaletteRole
|
|
134
135
|
} from './color/index.js'
|
|
136
|
+
|
|
137
|
+
export { defineMotionProfile, defaultMotionProfile } from './motion/config.js'
|
|
138
|
+
export type {
|
|
139
|
+
MotionProfile,
|
|
140
|
+
MotionProfileInput,
|
|
141
|
+
MotionInteraction,
|
|
142
|
+
SpringParameters
|
|
143
|
+
} from './motion/config.js'
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { useContext } from 'react'
|
|
3
|
+
import { Link, type LinkProps } from '../Link.js'
|
|
4
|
+
import { cn } from '../cn.js'
|
|
5
|
+
import { MotionHostContext } from './host.js'
|
|
6
|
+
import { usePressHost } from './host.js'
|
|
7
|
+
import { callerOwnsPress, springPressClass, offPressClass } from './pressPolicy.js'
|
|
8
|
+
export type MotionLinkProps = LinkProps & { motion?: 'inherit' | 'off' }
|
|
9
|
+
/** Optional client adapter for action-shaped Link; ordinary Link stays server-renderable. */
|
|
10
|
+
export function MotionLink({ ref, motion = 'inherit', className, ...props }: MotionLinkProps) {
|
|
11
|
+
const owns = callerOwnsPress(props.style, className)
|
|
12
|
+
const disabled =
|
|
13
|
+
owns ||
|
|
14
|
+
motion === 'off' ||
|
|
15
|
+
props.variant === undefined ||
|
|
16
|
+
props.variant === 'inline' ||
|
|
17
|
+
props.variant === 'subtle'
|
|
18
|
+
const runtime = useContext(MotionHostContext)
|
|
19
|
+
const active = runtime !== null && !disabled
|
|
20
|
+
const {
|
|
21
|
+
ref: motionRef,
|
|
22
|
+
press: startPress,
|
|
23
|
+
release
|
|
24
|
+
} = usePressHost<HTMLAnchorElement>(ref, disabled)
|
|
25
|
+
return (
|
|
26
|
+
<Link
|
|
27
|
+
{...props}
|
|
28
|
+
ref={active ? motionRef : ref}
|
|
29
|
+
className={cn(
|
|
30
|
+
active && springPressClass,
|
|
31
|
+
motion === 'off' && !owns && offPressClass,
|
|
32
|
+
className
|
|
33
|
+
)}
|
|
34
|
+
onPointerDown={event => {
|
|
35
|
+
props.onPointerDown?.(event)
|
|
36
|
+
if (!event.defaultPrevented) startPress(event.nativeEvent)
|
|
37
|
+
}}
|
|
38
|
+
onPointerUp={event => {
|
|
39
|
+
props.onPointerUp?.(event)
|
|
40
|
+
release()
|
|
41
|
+
}}
|
|
42
|
+
onPointerCancel={event => {
|
|
43
|
+
props.onPointerCancel?.(event)
|
|
44
|
+
release()
|
|
45
|
+
}}
|
|
46
|
+
onPointerLeave={event => {
|
|
47
|
+
props.onPointerLeave?.(event)
|
|
48
|
+
release()
|
|
49
|
+
}}
|
|
50
|
+
onBlur={event => {
|
|
51
|
+
props.onBlur?.(event)
|
|
52
|
+
release()
|
|
53
|
+
}}
|
|
54
|
+
onKeyDown={event => {
|
|
55
|
+
props.onKeyDown?.(event)
|
|
56
|
+
if (!event.defaultPrevented) startPress(event.nativeEvent)
|
|
57
|
+
}}
|
|
58
|
+
onKeyUp={event => {
|
|
59
|
+
props.onKeyUp?.(event)
|
|
60
|
+
release()
|
|
61
|
+
}}
|
|
62
|
+
/>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { SpringController } from './runtime.js'
|
|
2
|
+
|
|
3
|
+
export type SpringStyleProperty =
|
|
4
|
+
| 'scale'
|
|
5
|
+
| 'opacity'
|
|
6
|
+
| 'width'
|
|
7
|
+
| 'height'
|
|
8
|
+
| 'translateX'
|
|
9
|
+
| 'translateY'
|
|
10
|
+
/** Bind one explicitly reserved individual property; never overwrite transform or events. */
|
|
11
|
+
export function bindSpringStyle(
|
|
12
|
+
node: HTMLElement,
|
|
13
|
+
spring: SpringController,
|
|
14
|
+
property: SpringStyleProperty
|
|
15
|
+
): () => void {
|
|
16
|
+
if (!['scale', 'opacity', 'width', 'height', 'translateX', 'translateY'].includes(property))
|
|
17
|
+
throw new TypeError('Unsupported spring style property.')
|
|
18
|
+
const key = property === 'translateX' || property === 'translateY' ? 'translate' : property
|
|
19
|
+
const previous = node.style.getPropertyValue(key)
|
|
20
|
+
const priority = node.style.getPropertyPriority(key)
|
|
21
|
+
let owned = previous
|
|
22
|
+
const apply = (next: number) => {
|
|
23
|
+
const formatted =
|
|
24
|
+
property === 'width' || property === 'height'
|
|
25
|
+
? `${Math.max(0, next)}px`
|
|
26
|
+
: property === 'translateX'
|
|
27
|
+
? `${next}px 0px`
|
|
28
|
+
: property === 'translateY'
|
|
29
|
+
? `0px ${next}px`
|
|
30
|
+
: property === 'opacity'
|
|
31
|
+
? String(Math.max(0, Math.min(1, next)))
|
|
32
|
+
: String(Math.max(0, next))
|
|
33
|
+
node.style.setProperty(key, formatted)
|
|
34
|
+
owned = node.style.getPropertyValue(key)
|
|
35
|
+
}
|
|
36
|
+
apply(spring.get())
|
|
37
|
+
const off = spring.subscribe(apply)
|
|
38
|
+
return () => {
|
|
39
|
+
off()
|
|
40
|
+
// A caller may have reclaimed the property since the last spring frame.
|
|
41
|
+
if (node.style.getPropertyValue(key) !== owned) return
|
|
42
|
+
if (previous) node.style.setProperty(key, previous, priority)
|
|
43
|
+
else node.style.removeProperty(key)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface SpringParameters {
|
|
2
|
+
readonly stiffness: number
|
|
3
|
+
readonly damping: number
|
|
4
|
+
readonly mass: number
|
|
5
|
+
}
|
|
6
|
+
export const motionInteractions = [
|
|
7
|
+
'press',
|
|
8
|
+
'disclosure',
|
|
9
|
+
'dialog',
|
|
10
|
+
'follow',
|
|
11
|
+
'drag',
|
|
12
|
+
'layout'
|
|
13
|
+
] as const
|
|
14
|
+
export type MotionInteraction = (typeof motionInteractions)[number]
|
|
15
|
+
export interface MotionProfileInput {
|
|
16
|
+
readonly mode?: 'system' | 'off'
|
|
17
|
+
readonly spring?: Partial<SpringParameters>
|
|
18
|
+
readonly interactions?: Partial<Record<MotionInteraction, Partial<SpringParameters>>>
|
|
19
|
+
}
|
|
20
|
+
export interface MotionProfile {
|
|
21
|
+
readonly mode: 'system' | 'off'
|
|
22
|
+
readonly spring: SpringParameters
|
|
23
|
+
readonly interactions: Readonly<Record<MotionInteraction, SpringParameters>>
|
|
24
|
+
}
|
|
25
|
+
const baseline: SpringParameters = Object.freeze({ stiffness: 320, damping: 30, mass: 1 })
|
|
26
|
+
function record(value: unknown, allowed: readonly string[], path: string): void {
|
|
27
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
28
|
+
throw new TypeError(`${path} must be an object.`)
|
|
29
|
+
for (const key of Object.keys(value))
|
|
30
|
+
if (!allowed.includes(key)) throw new TypeError(`Unknown key: ${path}.${key}.`)
|
|
31
|
+
}
|
|
32
|
+
export function resolveSpringParameters(
|
|
33
|
+
input: Partial<SpringParameters> = {},
|
|
34
|
+
base: SpringParameters = baseline
|
|
35
|
+
): SpringParameters {
|
|
36
|
+
record(input, ['stiffness', 'damping', 'mass'], 'spring')
|
|
37
|
+
const result = { ...base }
|
|
38
|
+
for (const key of ['stiffness', 'damping', 'mass'] as const) {
|
|
39
|
+
const value = input[key]
|
|
40
|
+
if (value === undefined) continue
|
|
41
|
+
const max = key === 'mass' ? 100 : 10000
|
|
42
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0 || value > max)
|
|
43
|
+
throw new TypeError(`spring.${key} must be greater than zero and at most ${max}.`)
|
|
44
|
+
result[key] = value
|
|
45
|
+
}
|
|
46
|
+
return Object.freeze(result)
|
|
47
|
+
}
|
|
48
|
+
/** Plain immutable theme data; importing configuration never loads an animation engine. */
|
|
49
|
+
export function defineMotionProfile(input: MotionProfileInput = {}): MotionProfile {
|
|
50
|
+
record(input, ['mode', 'spring', 'interactions'], 'motion')
|
|
51
|
+
if (input.mode !== undefined && input.mode !== 'system' && input.mode !== 'off')
|
|
52
|
+
throw new TypeError('motion.mode must be system or off.')
|
|
53
|
+
const spring = resolveSpringParameters(input.spring)
|
|
54
|
+
if (input.interactions !== undefined)
|
|
55
|
+
record(input.interactions, motionInteractions, 'motion.interactions')
|
|
56
|
+
const interactions = Object.fromEntries(
|
|
57
|
+
motionInteractions.map(name => [
|
|
58
|
+
name,
|
|
59
|
+
resolveSpringParameters(input.interactions?.[name], spring)
|
|
60
|
+
])
|
|
61
|
+
) as Record<MotionInteraction, SpringParameters>
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
mode: input.mode ?? 'system',
|
|
64
|
+
spring,
|
|
65
|
+
interactions: Object.freeze(interactions)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
export const defaultMotionProfile = defineMotionProfile()
|
|
69
|
+
export function resolveMotion(
|
|
70
|
+
profile: MotionProfile,
|
|
71
|
+
interaction: MotionInteraction,
|
|
72
|
+
reducedMotion: boolean
|
|
73
|
+
) {
|
|
74
|
+
if (!motionInteractions.includes(interaction)) throw new TypeError('Unknown motion interaction.')
|
|
75
|
+
return {
|
|
76
|
+
enabled: profile.mode !== 'off' && !reducedMotion,
|
|
77
|
+
parameters: profile.interactions[interaction]
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { createSpring, type SpringController, type SpringOptions } from './runtime.js'
|
|
2
|
+
export interface SpringDragOptions extends SpringOptions {
|
|
3
|
+
readonly min: number
|
|
4
|
+
readonly max: number
|
|
5
|
+
readonly snapPoints?: readonly number[]
|
|
6
|
+
}
|
|
7
|
+
export interface SpringDragController extends SpringController {
|
|
8
|
+
begin(): void
|
|
9
|
+
move(position: number, time: number): void
|
|
10
|
+
release(time: number): { target: number; velocity: number }
|
|
11
|
+
cancel(): void
|
|
12
|
+
step(direction: -1 | 1 | 'first' | 'last'): void
|
|
13
|
+
configure(options: Partial<SpringDragOptions>): void
|
|
14
|
+
}
|
|
15
|
+
function validated(input: SpringDragOptions) {
|
|
16
|
+
if (!Number.isFinite(input.min) || !Number.isFinite(input.max) || input.min >= input.max)
|
|
17
|
+
throw new TypeError('Drag bounds must be finite and min < max.')
|
|
18
|
+
if (input.reducedValue !== undefined)
|
|
19
|
+
throw new TypeError('Drag off snaps to its functional goal, not a decorative reducedValue.')
|
|
20
|
+
const points = input.snapPoints ?? [input.min, input.max]
|
|
21
|
+
if (
|
|
22
|
+
!Array.isArray(points) ||
|
|
23
|
+
!points.length ||
|
|
24
|
+
points.some(value => !Number.isFinite(value) || value < input.min || value > input.max)
|
|
25
|
+
)
|
|
26
|
+
throw new TypeError('Snap points must be a nonempty array within bounds.')
|
|
27
|
+
return { ...input, snapPoints: [...new Set(points)].sort((a, b) => a - b) }
|
|
28
|
+
}
|
|
29
|
+
/** One axis, local pixels; Motion supplies all spring physics. */
|
|
30
|
+
export function createSpringDrag(initial: number, input: SpringDragOptions): SpringDragController {
|
|
31
|
+
let options = validated(input)
|
|
32
|
+
const clamp = (value: number) => Math.max(options.min, Math.min(options.max, value))
|
|
33
|
+
if (!Number.isFinite(initial) || initial < options.min || initial > options.max)
|
|
34
|
+
throw new TypeError('Initial drag position must be within bounds.')
|
|
35
|
+
const spring = createSpring(initial, options)
|
|
36
|
+
let goal = initial,
|
|
37
|
+
cancelGoal = initial,
|
|
38
|
+
dragging = false,
|
|
39
|
+
velocity = 0,
|
|
40
|
+
lastTime: number | undefined,
|
|
41
|
+
lastPosition = initial,
|
|
42
|
+
destroyed = false
|
|
43
|
+
const choose = (position: number) =>
|
|
44
|
+
options.snapPoints.reduce(
|
|
45
|
+
(best, point) => (Math.abs(point - position) < Math.abs(best - position) ? point : best),
|
|
46
|
+
options.snapPoints[0]
|
|
47
|
+
)
|
|
48
|
+
const to = (target: number, speed?: number) => {
|
|
49
|
+
if (!Number.isFinite(target)) throw new TypeError('Drag target must be finite.')
|
|
50
|
+
if (destroyed) return
|
|
51
|
+
goal = clamp(target)
|
|
52
|
+
spring.to(goal, speed)
|
|
53
|
+
}
|
|
54
|
+
const controller: SpringDragController = {
|
|
55
|
+
get: () => clamp(spring.get()),
|
|
56
|
+
getVelocity: () =>
|
|
57
|
+
spring.get() < options.min || spring.get() > options.max ? 0 : spring.getVelocity(),
|
|
58
|
+
isAnimating: () => spring.isAnimating(),
|
|
59
|
+
to,
|
|
60
|
+
jump(target) {
|
|
61
|
+
if (!Number.isFinite(target)) throw new TypeError('Drag target must be finite.')
|
|
62
|
+
if (destroyed) return
|
|
63
|
+
goal = clamp(target)
|
|
64
|
+
spring.jump(goal)
|
|
65
|
+
},
|
|
66
|
+
stop() {
|
|
67
|
+
if (destroyed) return
|
|
68
|
+
spring.stop()
|
|
69
|
+
goal = controller.get()
|
|
70
|
+
},
|
|
71
|
+
subscribe(listener) {
|
|
72
|
+
return spring.subscribe(() => listener(controller.get()))
|
|
73
|
+
},
|
|
74
|
+
configure(next) {
|
|
75
|
+
if (destroyed) return
|
|
76
|
+
const resolved = validated({ ...options, ...next })
|
|
77
|
+
spring.configure(resolved)
|
|
78
|
+
options = resolved
|
|
79
|
+
goal = clamp(goal)
|
|
80
|
+
cancelGoal = clamp(cancelGoal)
|
|
81
|
+
if (dragging) spring.jump(controller.get())
|
|
82
|
+
else spring.to(goal)
|
|
83
|
+
},
|
|
84
|
+
begin() {
|
|
85
|
+
if (destroyed) return
|
|
86
|
+
cancelGoal = goal
|
|
87
|
+
dragging = true
|
|
88
|
+
lastTime = undefined
|
|
89
|
+
velocity = 0
|
|
90
|
+
lastPosition = controller.get()
|
|
91
|
+
spring.jump(lastPosition)
|
|
92
|
+
},
|
|
93
|
+
move(position, time) {
|
|
94
|
+
if (!Number.isFinite(position) || !Number.isFinite(time))
|
|
95
|
+
throw new TypeError('Drag samples need finite pixel positions and millisecond times.')
|
|
96
|
+
if (!dragging || destroyed) return
|
|
97
|
+
const current = clamp(position)
|
|
98
|
+
if (lastTime !== undefined) {
|
|
99
|
+
const dt = time - lastTime
|
|
100
|
+
velocity =
|
|
101
|
+
dt > 0 && dt <= 80
|
|
102
|
+
? Math.max(-3000, Math.min(3000, ((current - lastPosition) * 1000) / dt))
|
|
103
|
+
: 0
|
|
104
|
+
}
|
|
105
|
+
lastTime = time
|
|
106
|
+
lastPosition = current
|
|
107
|
+
spring.jump(current)
|
|
108
|
+
},
|
|
109
|
+
release(time) {
|
|
110
|
+
if (!Number.isFinite(time)) throw new TypeError('Release time must be finite.')
|
|
111
|
+
if (!dragging || destroyed) return { target: goal, velocity: 0 }
|
|
112
|
+
dragging = false
|
|
113
|
+
const speed =
|
|
114
|
+
options.enabled === false ||
|
|
115
|
+
lastTime === undefined ||
|
|
116
|
+
time - lastTime > 80 ||
|
|
117
|
+
time < lastTime
|
|
118
|
+
? 0
|
|
119
|
+
: velocity
|
|
120
|
+
const target = choose(clamp(controller.get() + speed * 0.18))
|
|
121
|
+
to(target, speed)
|
|
122
|
+
return { target, velocity: speed }
|
|
123
|
+
},
|
|
124
|
+
cancel() {
|
|
125
|
+
if (!dragging || destroyed) return
|
|
126
|
+
dragging = false
|
|
127
|
+
to(cancelGoal, 0)
|
|
128
|
+
},
|
|
129
|
+
step(direction) {
|
|
130
|
+
if (destroyed) return
|
|
131
|
+
dragging = false
|
|
132
|
+
const current = goal
|
|
133
|
+
const points = options.snapPoints
|
|
134
|
+
const target =
|
|
135
|
+
direction === 'first'
|
|
136
|
+
? points[0]
|
|
137
|
+
: direction === 'last'
|
|
138
|
+
? points.at(-1)!
|
|
139
|
+
: direction === 1
|
|
140
|
+
? (points.find(point => point > current + 0.01) ?? points.at(-1)!)
|
|
141
|
+
: ([...points].reverse().find(point => point < current - 0.01) ?? points[0])
|
|
142
|
+
to(target)
|
|
143
|
+
},
|
|
144
|
+
destroy() {
|
|
145
|
+
destroyed = true
|
|
146
|
+
dragging = false
|
|
147
|
+
spring.destroy()
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return controller
|
|
151
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { SpringDragController } from './drag.js'
|
|
2
|
+
import { bindSpringStyle } from './browser.js'
|
|
3
|
+
/** Bind one dedicated handle. Document bubble handlers let React/native consumers cancel first. */
|
|
4
|
+
export function bindSpringDrag(
|
|
5
|
+
node: HTMLElement,
|
|
6
|
+
controller: SpringDragController,
|
|
7
|
+
axis: 'x' | 'y' = 'x'
|
|
8
|
+
) {
|
|
9
|
+
const document = node.ownerDocument
|
|
10
|
+
const unbind = bindSpringStyle(node, controller, axis === 'x' ? 'translateX' : 'translateY')
|
|
11
|
+
const previousTouch = node.style.touchAction
|
|
12
|
+
const ownsTouch = !previousTouch
|
|
13
|
+
if (ownsTouch) node.style.touchAction = axis === 'x' ? 'pan-y' : 'pan-x'
|
|
14
|
+
const ownedTouch = node.style.touchAction
|
|
15
|
+
let active: number | undefined,
|
|
16
|
+
start = 0,
|
|
17
|
+
origin = 0,
|
|
18
|
+
cross = 0,
|
|
19
|
+
moved = false,
|
|
20
|
+
suppress = false
|
|
21
|
+
const coordinate = (event: PointerEvent) => (axis === 'x' ? event.clientX : event.clientY)
|
|
22
|
+
const other = (event: PointerEvent) => (axis === 'x' ? event.clientY : event.clientX)
|
|
23
|
+
const blocked = () =>
|
|
24
|
+
node.matches(':disabled') ||
|
|
25
|
+
node.getAttribute('aria-busy') === 'true' ||
|
|
26
|
+
node.getAttribute('aria-disabled') === 'true'
|
|
27
|
+
const target = (event: Event) => event.target instanceof Node && node.contains(event.target)
|
|
28
|
+
function free() {
|
|
29
|
+
if (active !== undefined && node.hasPointerCapture(active)) node.releasePointerCapture(active)
|
|
30
|
+
active = undefined
|
|
31
|
+
}
|
|
32
|
+
function down(event: PointerEvent) {
|
|
33
|
+
if (
|
|
34
|
+
event.defaultPrevented ||
|
|
35
|
+
!target(event) ||
|
|
36
|
+
event.button !== 0 ||
|
|
37
|
+
event.isPrimary === false ||
|
|
38
|
+
active !== undefined ||
|
|
39
|
+
blocked()
|
|
40
|
+
)
|
|
41
|
+
return
|
|
42
|
+
suppress = false
|
|
43
|
+
active = event.pointerId
|
|
44
|
+
start = coordinate(event)
|
|
45
|
+
cross = other(event)
|
|
46
|
+
origin = controller.get()
|
|
47
|
+
moved = false
|
|
48
|
+
controller.begin()
|
|
49
|
+
controller.move(origin, event.timeStamp)
|
|
50
|
+
}
|
|
51
|
+
function move(event: PointerEvent) {
|
|
52
|
+
if (active !== event.pointerId) return
|
|
53
|
+
if (event.defaultPrevented || blocked()) {
|
|
54
|
+
cancel()
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
const delta = coordinate(event) - start
|
|
58
|
+
if (!moved && Math.abs(other(event) - cross) > Math.max(4, Math.abs(delta))) {
|
|
59
|
+
cancel()
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
if (!moved && Math.abs(delta) < 3) return
|
|
63
|
+
if (!moved) {
|
|
64
|
+
moved = true
|
|
65
|
+
try {
|
|
66
|
+
node.setPointerCapture(event.pointerId)
|
|
67
|
+
} catch {
|
|
68
|
+
/* Document events still provide outside release for synthetic input. */
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
controller.move(origin + delta, event.timeStamp)
|
|
72
|
+
}
|
|
73
|
+
function up(event: PointerEvent) {
|
|
74
|
+
if (active !== event.pointerId) return
|
|
75
|
+
if (event.defaultPrevented || blocked()) {
|
|
76
|
+
cancel()
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
suppress = moved
|
|
80
|
+
controller.release(event.timeStamp)
|
|
81
|
+
free()
|
|
82
|
+
moved = false
|
|
83
|
+
}
|
|
84
|
+
function cancel() {
|
|
85
|
+
if (active === undefined) return
|
|
86
|
+
controller.cancel()
|
|
87
|
+
free()
|
|
88
|
+
suppress = moved
|
|
89
|
+
}
|
|
90
|
+
function cancelled(event: PointerEvent) {
|
|
91
|
+
if (active === event.pointerId) cancel()
|
|
92
|
+
}
|
|
93
|
+
function key(event: KeyboardEvent) {
|
|
94
|
+
if (!target(event) || event.defaultPrevented || blocked()) return
|
|
95
|
+
const backward = axis === 'x' ? 'ArrowLeft' : 'ArrowUp',
|
|
96
|
+
forward = axis === 'x' ? 'ArrowRight' : 'ArrowDown'
|
|
97
|
+
if (event.key === 'Escape') {
|
|
98
|
+
if (active !== undefined) {
|
|
99
|
+
event.preventDefault()
|
|
100
|
+
cancel()
|
|
101
|
+
}
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
if ([backward, forward, 'Home', 'End'].includes(event.key)) {
|
|
105
|
+
event.preventDefault()
|
|
106
|
+
if (active !== undefined) cancel()
|
|
107
|
+
controller.step(
|
|
108
|
+
event.key === 'Home'
|
|
109
|
+
? 'first'
|
|
110
|
+
: event.key === 'End'
|
|
111
|
+
? 'last'
|
|
112
|
+
: event.key === forward
|
|
113
|
+
? 1
|
|
114
|
+
: -1
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function click(event: MouseEvent) {
|
|
119
|
+
if (suppress) {
|
|
120
|
+
suppress = false
|
|
121
|
+
event.preventDefault()
|
|
122
|
+
event.stopPropagation()
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
document.addEventListener('pointerdown', down)
|
|
126
|
+
document.addEventListener('pointermove', move)
|
|
127
|
+
document.addEventListener('pointerup', up)
|
|
128
|
+
document.addEventListener('pointercancel', cancelled)
|
|
129
|
+
document.addEventListener('keydown', key)
|
|
130
|
+
node.addEventListener('lostpointercapture', cancelled)
|
|
131
|
+
node.addEventListener('click', click, true)
|
|
132
|
+
document.defaultView!.addEventListener('blur', cancel)
|
|
133
|
+
node.addEventListener('blur', cancel)
|
|
134
|
+
const observer = new MutationObserver(() => {
|
|
135
|
+
if (blocked()) cancel()
|
|
136
|
+
})
|
|
137
|
+
observer.observe(node, {
|
|
138
|
+
attributes: true,
|
|
139
|
+
attributeFilter: ['disabled', 'aria-busy', 'aria-disabled']
|
|
140
|
+
})
|
|
141
|
+
for (let parent = node.parentElement; parent; parent = parent.parentElement)
|
|
142
|
+
if (parent.tagName === 'FIELDSET')
|
|
143
|
+
observer.observe(parent, { attributes: true, attributeFilter: ['disabled'] })
|
|
144
|
+
return () => {
|
|
145
|
+
cancel()
|
|
146
|
+
unbind()
|
|
147
|
+
if (ownsTouch && node.style.touchAction === ownedTouch) node.style.touchAction = previousTouch
|
|
148
|
+
document.removeEventListener('pointerdown', down)
|
|
149
|
+
document.removeEventListener('pointermove', move)
|
|
150
|
+
document.removeEventListener('pointerup', up)
|
|
151
|
+
document.removeEventListener('pointercancel', cancelled)
|
|
152
|
+
document.removeEventListener('keydown', key)
|
|
153
|
+
node.removeEventListener('lostpointercapture', cancelled)
|
|
154
|
+
node.removeEventListener('click', click, true)
|
|
155
|
+
document.defaultView!.removeEventListener('blur', cancel)
|
|
156
|
+
node.removeEventListener('blur', cancel)
|
|
157
|
+
observer.disconnect()
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SpringController } from './runtime.js'
|
|
2
|
+
|
|
3
|
+
/** Synchronous numeric snapshot with notifications in the same units as the follower. */
|
|
4
|
+
export interface SpringSource {
|
|
5
|
+
get(): number
|
|
6
|
+
subscribe(listener: (value: number) => void): () => void
|
|
7
|
+
}
|
|
8
|
+
/** The source owns destinations; the existing spring owns position, momentum and physics. */
|
|
9
|
+
export function bindSpringSource(spring: SpringController, source: SpringSource): () => void {
|
|
10
|
+
if (source === spring) throw new TypeError('A spring cannot follow itself.')
|
|
11
|
+
let active = true
|
|
12
|
+
const off = source.subscribe(value => {
|
|
13
|
+
if (active) spring.to(value)
|
|
14
|
+
})
|
|
15
|
+
try {
|
|
16
|
+
// Read after subscribing so a synchronous subscription cannot leave a stale goal.
|
|
17
|
+
spring.to(source.get())
|
|
18
|
+
} catch (error) {
|
|
19
|
+
active = false
|
|
20
|
+
off()
|
|
21
|
+
throw error
|
|
22
|
+
}
|
|
23
|
+
return () => {
|
|
24
|
+
if (!active) return
|
|
25
|
+
active = false
|
|
26
|
+
off()
|
|
27
|
+
}
|
|
28
|
+
}
|