@sessile/react 0.0.0-stage → 0.2.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.
- package/LICENSE +373 -0
- package/dist/GlassField.d.ts +71 -0
- package/dist/GlassField.js +372 -0
- package/dist/GlassFieldContext.d.ts +37 -0
- package/dist/GlassFieldContext.js +34 -0
- package/dist/GlassPrimitive.d.ts +35 -0
- package/dist/GlassPrimitive.js +98 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/useElasticPress.d.ts +30 -0
- package/dist/useElasticPress.js +219 -0
- package/dist/useGlassMorph.d.ts +42 -0
- package/dist/useGlassMorph.js +140 -0
- package/dist/useReducedMotion.d.ts +13 -0
- package/dist/useReducedMotion.js +33 -0
- package/package.json +45 -3
- package/README.md +0 -3
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React hook attaching `@sessile/core`'s elastic press and rubber-band drag physics
|
|
3
|
+
* to an HTML element via pointer listeners and CSS `transform`.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useRef } from 'react';
|
|
8
|
+
import { createElasticPressController, } from '@sessile/core';
|
|
9
|
+
import { createFrameLoop, } from '@sessile/dom';
|
|
10
|
+
import { useReducedMotion } from "./useReducedMotion.js";
|
|
11
|
+
const TRANSFORM_EPS = 1e-4;
|
|
12
|
+
const OFFSET_EPS = 0.05;
|
|
13
|
+
const DEFAULT_IGNORE_ATTR = 'data-sessile-morphing';
|
|
14
|
+
const DEFAULT_ACTIVE_ATTR = 'data-sessile-elastic-press';
|
|
15
|
+
function isSameAttachmentConfig(a, b) {
|
|
16
|
+
return (a.el === b.el &&
|
|
17
|
+
a.enabled === b.enabled &&
|
|
18
|
+
a.effectiveReduced === b.effectiveReduced &&
|
|
19
|
+
a.ignoreAttribute === b.ignoreAttribute &&
|
|
20
|
+
a.activeAttribute === b.activeAttribute &&
|
|
21
|
+
a.spring === b.spring &&
|
|
22
|
+
a.dragTrackSec === b.dragTrackSec &&
|
|
23
|
+
a.maxGrowthRatio === b.maxGrowthRatio &&
|
|
24
|
+
a.maxGrowthPx === b.maxGrowthPx &&
|
|
25
|
+
a.dragMaxRatio === b.dragMaxRatio &&
|
|
26
|
+
a.scheduler === b.scheduler);
|
|
27
|
+
}
|
|
28
|
+
function attachElasticPressToElement(el, config, intensityRef) {
|
|
29
|
+
const { enabled, effectiveReduced, ignoreAttribute, activeAttribute, spring, dragTrackSec, maxGrowthRatio, maxGrowthPx, dragMaxRatio, scheduler, } = config;
|
|
30
|
+
const clearTransform = () => {
|
|
31
|
+
if (el.hasAttribute(activeAttribute)) {
|
|
32
|
+
el.style.transform = '';
|
|
33
|
+
el.removeAttribute(activeAttribute);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
if (!enabled || effectiveReduced) {
|
|
37
|
+
clearTransform();
|
|
38
|
+
return () => { };
|
|
39
|
+
}
|
|
40
|
+
const win = el.ownerDocument?.defaultView ?? (typeof window !== 'undefined' ? window : null);
|
|
41
|
+
const ctrl = createElasticPressController({
|
|
42
|
+
spring,
|
|
43
|
+
dragTrackSec,
|
|
44
|
+
maxGrowthRatio,
|
|
45
|
+
maxGrowthPx,
|
|
46
|
+
dragMaxRatio,
|
|
47
|
+
reducedMotion: effectiveReduced,
|
|
48
|
+
});
|
|
49
|
+
let activePointerId = null;
|
|
50
|
+
let listenersAttached = false;
|
|
51
|
+
let startX = 0;
|
|
52
|
+
let startY = 0;
|
|
53
|
+
let targetSize = { width: 44, height: 44 };
|
|
54
|
+
let disposed = false;
|
|
55
|
+
const applyFrame = () => {
|
|
56
|
+
if (disposed)
|
|
57
|
+
return false;
|
|
58
|
+
if (el.hasAttribute(ignoreAttribute)) {
|
|
59
|
+
clearTransform();
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
const s = ctrl.getState();
|
|
63
|
+
const scale = intensityRef.current;
|
|
64
|
+
const sx = 1 + (s.scaleX - 1) * scale;
|
|
65
|
+
const sy = 1 + (s.scaleY - 1) * scale;
|
|
66
|
+
const ox = s.offsetX * scale;
|
|
67
|
+
const oy = s.offsetY * scale;
|
|
68
|
+
const isIdentity = Math.abs(sx - 1) < TRANSFORM_EPS &&
|
|
69
|
+
Math.abs(sy - 1) < TRANSFORM_EPS &&
|
|
70
|
+
Math.abs(ox) < OFFSET_EPS &&
|
|
71
|
+
Math.abs(oy) < OFFSET_EPS;
|
|
72
|
+
if (isIdentity && !s.isAnimating && activePointerId === null) {
|
|
73
|
+
clearTransform();
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
el.setAttribute(activeAttribute, 'true');
|
|
77
|
+
el.style.transform = `translate3d(${ox.toFixed(2)}px, ${oy.toFixed(2)}px, 0) scale(${sx.toFixed(4)}, ${sy.toFixed(4)})`;
|
|
78
|
+
return s.isAnimating || activePointerId !== null;
|
|
79
|
+
};
|
|
80
|
+
const loop = createFrameLoop((dtSec) => {
|
|
81
|
+
if (disposed)
|
|
82
|
+
return false;
|
|
83
|
+
if (dtSec > 0) {
|
|
84
|
+
ctrl.step(dtSec);
|
|
85
|
+
}
|
|
86
|
+
return applyFrame();
|
|
87
|
+
}, { scheduler });
|
|
88
|
+
const detachWindowListeners = () => {
|
|
89
|
+
if (!listenersAttached || !win)
|
|
90
|
+
return;
|
|
91
|
+
listenersAttached = false;
|
|
92
|
+
win.removeEventListener('pointermove', onWindowMove);
|
|
93
|
+
win.removeEventListener('pointerup', onWindowEnd);
|
|
94
|
+
win.removeEventListener('pointercancel', onWindowEnd);
|
|
95
|
+
win.removeEventListener('blur', onWindowEnd);
|
|
96
|
+
};
|
|
97
|
+
const releasePointer = () => {
|
|
98
|
+
if (activePointerId === null)
|
|
99
|
+
return;
|
|
100
|
+
activePointerId = null;
|
|
101
|
+
detachWindowListeners();
|
|
102
|
+
ctrl.pressRelease();
|
|
103
|
+
loop.start();
|
|
104
|
+
if (!loop.isRunning()) {
|
|
105
|
+
clearTransform();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const onWindowMove = (e) => {
|
|
109
|
+
const pe = e;
|
|
110
|
+
if (pe.pointerId !== activePointerId)
|
|
111
|
+
return;
|
|
112
|
+
ctrl.pressMove({ x: pe.clientX - startX, y: pe.clientY - startY }, targetSize);
|
|
113
|
+
if (!loop.isRunning()) {
|
|
114
|
+
loop.start();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const onWindowEnd = (e) => {
|
|
118
|
+
if ('pointerId' in e && e.pointerId !== activePointerId) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
releasePointer();
|
|
122
|
+
};
|
|
123
|
+
const onPointerDown = (e) => {
|
|
124
|
+
const pe = e;
|
|
125
|
+
if (disposed)
|
|
126
|
+
return;
|
|
127
|
+
if (pe.pointerType === 'mouse' && pe.button !== 0)
|
|
128
|
+
return;
|
|
129
|
+
if (el.hasAttribute('disabled') || el.getAttribute('aria-disabled') === 'true')
|
|
130
|
+
return;
|
|
131
|
+
if (el.hasAttribute(ignoreAttribute))
|
|
132
|
+
return;
|
|
133
|
+
// Release any previous stuck pointer before tracking the new active pointer.
|
|
134
|
+
if (activePointerId !== null) {
|
|
135
|
+
releasePointer();
|
|
136
|
+
}
|
|
137
|
+
// Measure unscaled layout box when available to prevent compound magnification across clicks.
|
|
138
|
+
const rect = el.getBoundingClientRect();
|
|
139
|
+
targetSize = {
|
|
140
|
+
width: Math.max(1, el.offsetWidth || rect.width),
|
|
141
|
+
height: Math.max(1, el.offsetHeight || rect.height),
|
|
142
|
+
};
|
|
143
|
+
startX = pe.clientX;
|
|
144
|
+
startY = pe.clientY;
|
|
145
|
+
activePointerId = pe.pointerId;
|
|
146
|
+
if (win && !listenersAttached) {
|
|
147
|
+
listenersAttached = true;
|
|
148
|
+
win.addEventListener('pointermove', onWindowMove, { passive: true });
|
|
149
|
+
win.addEventListener('pointerup', onWindowEnd);
|
|
150
|
+
win.addEventListener('pointercancel', onWindowEnd);
|
|
151
|
+
win.addEventListener('blur', onWindowEnd);
|
|
152
|
+
}
|
|
153
|
+
ctrl.pressStart(targetSize);
|
|
154
|
+
applyFrame();
|
|
155
|
+
if (!loop.isRunning()) {
|
|
156
|
+
loop.start();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
el.addEventListener('pointerdown', onPointerDown);
|
|
160
|
+
return () => {
|
|
161
|
+
disposed = true;
|
|
162
|
+
el.removeEventListener('pointerdown', onPointerDown);
|
|
163
|
+
activePointerId = null;
|
|
164
|
+
detachWindowListeners();
|
|
165
|
+
loop.stop();
|
|
166
|
+
clearTransform();
|
|
167
|
+
ctrl.reset();
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Drives spring-based press inflation and rubber-band pointer tracking on `targetRef`
|
|
172
|
+
* using GPU-composited `translate3d`/`scale` transforms only.
|
|
173
|
+
* The hook owns inline `element.style.transform` while active; apply layout positioning
|
|
174
|
+
* transforms on an outer wrapper when combining with static translations or rotations.
|
|
175
|
+
*/
|
|
176
|
+
export function useElasticPress(targetRef, options = {}) {
|
|
177
|
+
const { enabled = true, intensity = 1, spring, dragTrackSec, maxGrowthRatio, maxGrowthPx, dragMaxRatio, reducedMotion: reducedMotionOverride, ignoreAttribute = DEFAULT_IGNORE_ATTR, activeAttribute = DEFAULT_ACTIVE_ATTR, env, scheduler, } = options;
|
|
178
|
+
const systemReduced = useReducedMotion(env);
|
|
179
|
+
const effectiveReduced = reducedMotionOverride ?? systemReduced;
|
|
180
|
+
const intensityRef = useRef(intensity);
|
|
181
|
+
const attachmentRef = useRef(null);
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
intensityRef.current = intensity;
|
|
184
|
+
}, [intensity]);
|
|
185
|
+
// Inspect targetRef.current on each commit so conditional mounting or node replacement attaches cleanly.
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
const nextConfig = {
|
|
188
|
+
el: targetRef.current,
|
|
189
|
+
enabled,
|
|
190
|
+
effectiveReduced,
|
|
191
|
+
ignoreAttribute,
|
|
192
|
+
activeAttribute,
|
|
193
|
+
spring,
|
|
194
|
+
dragTrackSec,
|
|
195
|
+
maxGrowthRatio,
|
|
196
|
+
maxGrowthPx,
|
|
197
|
+
dragMaxRatio,
|
|
198
|
+
scheduler,
|
|
199
|
+
};
|
|
200
|
+
const current = attachmentRef.current;
|
|
201
|
+
if (current && isSameAttachmentConfig(current, nextConfig)) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
current?.cleanup?.();
|
|
205
|
+
const cleanup = nextConfig.el
|
|
206
|
+
? attachElasticPressToElement(nextConfig.el, nextConfig, intensityRef)
|
|
207
|
+
: null;
|
|
208
|
+
attachmentRef.current = {
|
|
209
|
+
...nextConfig,
|
|
210
|
+
cleanup,
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
return () => {
|
|
215
|
+
attachmentRef.current?.cleanup?.();
|
|
216
|
+
attachmentRef.current = null;
|
|
217
|
+
};
|
|
218
|
+
}, []);
|
|
219
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React hook driving `@sessile/core`'s interruptible 0<->1 morph timeline with
|
|
3
|
+
* `@sessile/dom`'s clamped rAF loop and native reduced-motion detection.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { type RefObject } from 'react';
|
|
8
|
+
import { type MorphTimeline, type MorphTimelineOptions, type MorphTimelinePhase, type MorphTimelineState } from '@sessile/core';
|
|
9
|
+
import { type CapabilityEnv, type FrameScheduler } from '@sessile/dom';
|
|
10
|
+
export interface UseGlassMorphOptions extends MorphTimelineOptions {
|
|
11
|
+
/** Controlled open/closed target. When toggled, calls `open()` / `close()` on transition. */
|
|
12
|
+
open?: boolean;
|
|
13
|
+
/** Capability environment override forwarded to `useReducedMotion`. */
|
|
14
|
+
env?: CapabilityEnv;
|
|
15
|
+
/** Custom frame scheduler (useful for deterministic testing). */
|
|
16
|
+
scheduler?: Partial<FrameScheduler>;
|
|
17
|
+
/** Maximum per-frame delta clamp in seconds passed to `createFrameLoop`. */
|
|
18
|
+
maxDtSec?: number;
|
|
19
|
+
/** High-frequency frame callback invoked on every animation tick with `(state, dtSec)`. */
|
|
20
|
+
onFrame?: (state: MorphTimelineState, dtSec: number) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Whether to sync every intermediate animation frame into React state (`true` by default).
|
|
23
|
+
* Set to `false` for imperative 60/120fps DOM/canvas consumers using `onFrame` / `stateRef`,
|
|
24
|
+
* while still syncing phase transitions and settled states.
|
|
25
|
+
*/
|
|
26
|
+
syncState?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface UseGlassMorphResult {
|
|
29
|
+
state: MorphTimelineState;
|
|
30
|
+
stateRef: RefObject<MorphTimelineState>;
|
|
31
|
+
open: () => void;
|
|
32
|
+
close: () => void;
|
|
33
|
+
toggle: () => void;
|
|
34
|
+
reset: (toPhase?: 'closed' | 'open') => void;
|
|
35
|
+
seek: (phase: MorphTimelinePhase, normalizedT: number) => MorphTimelineState;
|
|
36
|
+
getTimeline: () => MorphTimeline;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Manages an interruptible glass morph timeline inside React 19 with StrictMode-safe
|
|
40
|
+
* frame loop scheduling and optional low-overhead imperative mode (`syncState: false`).
|
|
41
|
+
*/
|
|
42
|
+
export declare function useGlassMorph(options?: UseGlassMorphOptions): UseGlassMorphResult;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React hook driving `@sessile/core`'s interruptible 0<->1 morph timeline with
|
|
3
|
+
* `@sessile/dom`'s clamped rAF loop and native reduced-motion detection.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
8
|
+
import { createMorphTimeline, } from '@sessile/core';
|
|
9
|
+
import { createFrameLoop, } from '@sessile/dom';
|
|
10
|
+
import { useReducedMotion } from "./useReducedMotion.js";
|
|
11
|
+
/**
|
|
12
|
+
* Manages an interruptible glass morph timeline inside React 19 with StrictMode-safe
|
|
13
|
+
* frame loop scheduling and optional low-overhead imperative mode (`syncState: false`).
|
|
14
|
+
*/
|
|
15
|
+
export function useGlassMorph(options = {}) {
|
|
16
|
+
const { open: controlledOpen, initialPhase, openDurationSec, closeDurationSec, cornerDelaySec, reducedMotionDurationSec, spring, cornerSpring, reducedMotion: reducedMotionOverride, env, scheduler, maxDtSec, onFrame, syncState = true, } = options;
|
|
17
|
+
const systemReduced = useReducedMotion(env);
|
|
18
|
+
const effectiveReduced = reducedMotionOverride ?? systemReduced;
|
|
19
|
+
const [timeline] = useState(() => createMorphTimeline({
|
|
20
|
+
initialPhase: initialPhase ?? (controlledOpen ? 'open' : 'closed'),
|
|
21
|
+
openDurationSec,
|
|
22
|
+
closeDurationSec,
|
|
23
|
+
cornerDelaySec,
|
|
24
|
+
reducedMotionDurationSec,
|
|
25
|
+
spring,
|
|
26
|
+
cornerSpring,
|
|
27
|
+
reducedMotion: reducedMotionOverride ?? systemReduced,
|
|
28
|
+
}));
|
|
29
|
+
const [state, setState] = useState(() => timeline.getState());
|
|
30
|
+
const stateRef = useRef(state);
|
|
31
|
+
const lastSyncedRef = useRef(state);
|
|
32
|
+
const onFrameRef = useRef(onFrame);
|
|
33
|
+
const syncStateRef = useRef(syncState);
|
|
34
|
+
const loopRef = useRef(null);
|
|
35
|
+
const mountedRef = useRef(false);
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
onFrameRef.current = onFrame;
|
|
38
|
+
syncStateRef.current = syncState;
|
|
39
|
+
}, [onFrame, syncState]);
|
|
40
|
+
const syncSnapshot = useCallback((next) => {
|
|
41
|
+
stateRef.current = next;
|
|
42
|
+
const last = lastSyncedRef.current;
|
|
43
|
+
if (syncStateRef.current ||
|
|
44
|
+
last.phase !== next.phase ||
|
|
45
|
+
last.isAnimating !== next.isAnimating ||
|
|
46
|
+
last.reducedMotion !== next.reducedMotion) {
|
|
47
|
+
lastSyncedRef.current = next;
|
|
48
|
+
setState(next);
|
|
49
|
+
}
|
|
50
|
+
}, []);
|
|
51
|
+
const ensureLoopRunning = useCallback(() => {
|
|
52
|
+
if (!mountedRef.current)
|
|
53
|
+
return;
|
|
54
|
+
const loop = loopRef.current;
|
|
55
|
+
if (loop && !loop.isRunning()) {
|
|
56
|
+
loop.start();
|
|
57
|
+
}
|
|
58
|
+
}, []);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
mountedRef.current = true;
|
|
61
|
+
const loop = createFrameLoop((dtSec) => {
|
|
62
|
+
const next = timeline.step(dtSec);
|
|
63
|
+
syncSnapshot(next);
|
|
64
|
+
onFrameRef.current?.(next, dtSec);
|
|
65
|
+
return next.isAnimating;
|
|
66
|
+
}, { maxDtSec, scheduler });
|
|
67
|
+
loopRef.current = loop;
|
|
68
|
+
const unsubscribe = timeline.subscribe((next) => {
|
|
69
|
+
syncSnapshot(next);
|
|
70
|
+
if (next.isAnimating) {
|
|
71
|
+
ensureLoopRunning();
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
loop.stop();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const current = timeline.getState();
|
|
78
|
+
syncSnapshot(current);
|
|
79
|
+
if (current.isAnimating) {
|
|
80
|
+
loop.start();
|
|
81
|
+
}
|
|
82
|
+
return () => {
|
|
83
|
+
mountedRef.current = false;
|
|
84
|
+
unsubscribe();
|
|
85
|
+
loop.stop();
|
|
86
|
+
if (loopRef.current === loop) {
|
|
87
|
+
loopRef.current = null;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}, [timeline, maxDtSec, scheduler, syncSnapshot, ensureLoopRunning]);
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
timeline.setReducedMotion(effectiveReduced);
|
|
93
|
+
}, [timeline, effectiveReduced]);
|
|
94
|
+
const prevControlledOpenRef = useRef(controlledOpen);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const prev = prevControlledOpenRef.current;
|
|
97
|
+
prevControlledOpenRef.current = controlledOpen;
|
|
98
|
+
if (controlledOpen === undefined || prev === controlledOpen) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (controlledOpen) {
|
|
102
|
+
timeline.open();
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
timeline.close();
|
|
106
|
+
}
|
|
107
|
+
}, [controlledOpen, timeline]);
|
|
108
|
+
const open = useCallback(() => {
|
|
109
|
+
timeline.open();
|
|
110
|
+
}, [timeline]);
|
|
111
|
+
const close = useCallback(() => {
|
|
112
|
+
timeline.close();
|
|
113
|
+
}, [timeline]);
|
|
114
|
+
const toggle = useCallback(() => {
|
|
115
|
+
const phase = timeline.getState().phase;
|
|
116
|
+
if (phase === 'open' || phase === 'opening') {
|
|
117
|
+
timeline.close();
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
timeline.open();
|
|
121
|
+
}
|
|
122
|
+
}, [timeline]);
|
|
123
|
+
const reset = useCallback((toPhase) => {
|
|
124
|
+
timeline.reset(toPhase);
|
|
125
|
+
}, [timeline]);
|
|
126
|
+
const seek = useCallback((phase, normalizedT) => {
|
|
127
|
+
return timeline.seek(phase, normalizedT);
|
|
128
|
+
}, [timeline]);
|
|
129
|
+
const getTimeline = useCallback(() => timeline, [timeline]);
|
|
130
|
+
return useMemo(() => ({
|
|
131
|
+
state,
|
|
132
|
+
stateRef,
|
|
133
|
+
open,
|
|
134
|
+
close,
|
|
135
|
+
toggle,
|
|
136
|
+
reset,
|
|
137
|
+
seek,
|
|
138
|
+
getTimeline,
|
|
139
|
+
}), [state, open, close, toggle, reset, seek, getTimeline]);
|
|
140
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive React hook subscribing to `(prefers-reduced-motion: reduce)` via `@sessile/dom`.
|
|
3
|
+
* Uses `useSyncExternalStore` so SSR, hydration, and live OS toggles stay tear-free
|
|
4
|
+
* without external animation libraries.
|
|
5
|
+
*
|
|
6
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
7
|
+
*/
|
|
8
|
+
import { type CapabilityEnv } from '@sessile/dom';
|
|
9
|
+
/**
|
|
10
|
+
* Returns whether reduced motion is currently preferred and stays synchronized
|
|
11
|
+
* with live OS/browser media query changes. Safely returns `false` during SSR.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useReducedMotion(env?: CapabilityEnv): boolean;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive React hook subscribing to `(prefers-reduced-motion: reduce)` via `@sessile/dom`.
|
|
3
|
+
* Uses `useSyncExternalStore` so SSR, hydration, and live OS toggles stay tear-free
|
|
4
|
+
* without external animation libraries.
|
|
5
|
+
*
|
|
6
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
7
|
+
*/
|
|
8
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
9
|
+
import { observeReducedMotion, prefersReducedMotion, } from '@sessile/dom';
|
|
10
|
+
const getServerSnapshot = () => false;
|
|
11
|
+
/**
|
|
12
|
+
* Returns whether reduced motion is currently preferred and stays synchronized
|
|
13
|
+
* with live OS/browser media query changes. Safely returns `false` during SSR.
|
|
14
|
+
*/
|
|
15
|
+
export function useReducedMotion(env) {
|
|
16
|
+
const hasCustomMatchMedia = env !== undefined && 'matchMedia' in env;
|
|
17
|
+
const customMatchMedia = env?.matchMedia;
|
|
18
|
+
const subscribe = useCallback((onStoreChange) => {
|
|
19
|
+
const activeEnv = hasCustomMatchMedia
|
|
20
|
+
? { matchMedia: customMatchMedia }
|
|
21
|
+
: undefined;
|
|
22
|
+
return observeReducedMotion(() => {
|
|
23
|
+
onStoreChange();
|
|
24
|
+
}, activeEnv);
|
|
25
|
+
}, [hasCustomMatchMedia, customMatchMedia]);
|
|
26
|
+
const getSnapshot = useCallback(() => {
|
|
27
|
+
const activeEnv = hasCustomMatchMedia
|
|
28
|
+
? { matchMedia: customMatchMedia }
|
|
29
|
+
: undefined;
|
|
30
|
+
return prefersReducedMotion(activeEnv);
|
|
31
|
+
}, [hasCustomMatchMedia, customMatchMedia]);
|
|
32
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sessile/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MPL-2.0",
|
|
6
|
+
"description": "Lightweight React bindings for Sessile glass morphing surfaces.",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/MERURUXD/sessile.git",
|
|
10
|
+
"directory": "packages/react"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/MERURUXD/sessile#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/MERURUXD/sessile/issues"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@sessile/dom": "0.2.0",
|
|
30
|
+
"@sessile/core": "0.2.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": ">=19.0.0",
|
|
34
|
+
"react-dom": ">=19.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/react": "^19.3.0",
|
|
38
|
+
"@types/react-dom": "^19.3.0",
|
|
39
|
+
"happy-dom": "^21.0.4",
|
|
40
|
+
"react": "^19.3.1",
|
|
41
|
+
"react-dom": "^19.3.1"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",
|
|
45
|
+
"test": "node --test test/*.test.ts",
|
|
46
|
+
"build": "tsc -p tsconfig.build.json"
|
|
47
|
+
}
|
|
6
48
|
}
|
package/README.md
DELETED