@sessile/react 0.0.0-stage → 0.1.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,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative `<GlassField>` container component managing a `@sessile/dom`
|
|
3
|
+
* `GlassSurfaceController` lifecycle, 3-layer DOM synchronization, and child primitives.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { createElement, useState, } from 'react';
|
|
8
|
+
import { GlassSurfaceController, resolveElementTheme, } from '@sessile/dom';
|
|
9
|
+
import { GlassFieldContext, assignRef, useIsomorphicLayoutEffect, } from "./GlassFieldContext.js";
|
|
10
|
+
const CONTENT_WRAPPER_STYLE = { display: 'contents' };
|
|
11
|
+
function parseBorderRadiusPx(rawValue, width, height) {
|
|
12
|
+
if (!rawValue)
|
|
13
|
+
return 0;
|
|
14
|
+
const trimmed = rawValue.trim();
|
|
15
|
+
if (!trimmed)
|
|
16
|
+
return 0;
|
|
17
|
+
if (trimmed.endsWith('%')) {
|
|
18
|
+
const pct = Number.parseFloat(trimmed);
|
|
19
|
+
if (!Number.isFinite(pct) || pct <= 0)
|
|
20
|
+
return 0;
|
|
21
|
+
return Math.min(width, height) * (pct / 100);
|
|
22
|
+
}
|
|
23
|
+
const px = Number.parseFloat(trimmed);
|
|
24
|
+
return Number.isFinite(px) && px > 0 ? px : 0;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolves a registered primitive's CSS coordinates in the managed layers' frame.
|
|
28
|
+
* Assumes an untransformed ancestor subtree for `'absolute'` positioning; ancestors
|
|
29
|
+
* with CSS transforms already form a Backdrop Root that alters backdrop sampling.
|
|
30
|
+
*/
|
|
31
|
+
function resolveRegisteredPrimitive(entry, container, positioning = 'absolute') {
|
|
32
|
+
if (entry.active === false) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const el = entry.getElement?.() ?? null;
|
|
36
|
+
const needsOrigin = entry.x === undefined || entry.y === undefined;
|
|
37
|
+
const needsSize = entry.width === undefined || entry.height === undefined;
|
|
38
|
+
let measuredX = 0;
|
|
39
|
+
let measuredY = 0;
|
|
40
|
+
let measuredW = 0;
|
|
41
|
+
let measuredH = 0;
|
|
42
|
+
if (el && (needsOrigin || needsSize)) {
|
|
43
|
+
const elRect = el.getBoundingClientRect();
|
|
44
|
+
measuredW = Math.max(0, elRect.width);
|
|
45
|
+
measuredH = Math.max(0, elRect.height);
|
|
46
|
+
if (container && positioning !== 'fixed' && needsOrigin) {
|
|
47
|
+
const containerRect = container.getBoundingClientRect();
|
|
48
|
+
// Align coordinates against the container's scrollable padding-box origin where
|
|
49
|
+
// `position: absolute; top: 0; left: 0` managed layers sit.
|
|
50
|
+
const originX = containerRect.left + (container.clientLeft || 0) - (container.scrollLeft || 0);
|
|
51
|
+
const originY = containerRect.top + (container.clientTop || 0) - (container.scrollTop || 0);
|
|
52
|
+
measuredX = elRect.left - originX;
|
|
53
|
+
measuredY = elRect.top - originY;
|
|
54
|
+
}
|
|
55
|
+
else if (needsOrigin) {
|
|
56
|
+
// Fixed-positioned surface layers anchor at viewport (0, 0), matching getBoundingClientRect().
|
|
57
|
+
measuredX = elRect.left;
|
|
58
|
+
measuredY = elRect.top;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const x = entry.x ?? measuredX;
|
|
62
|
+
const y = entry.y ?? measuredY;
|
|
63
|
+
const width = Math.max(0, entry.width ?? measuredW);
|
|
64
|
+
const height = Math.max(0, entry.height ?? measuredH);
|
|
65
|
+
let radius = entry.radius;
|
|
66
|
+
if (radius === undefined) {
|
|
67
|
+
if (el && typeof globalThis.getComputedStyle === 'function') {
|
|
68
|
+
const computed = globalThis.getComputedStyle(el);
|
|
69
|
+
radius = parseBorderRadiusPx(computed.borderTopLeftRadius, width, height);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
radius = 0;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
id: entry.id,
|
|
77
|
+
center: {
|
|
78
|
+
x: x + width * 0.5,
|
|
79
|
+
y: y + height * 0.5,
|
|
80
|
+
},
|
|
81
|
+
size: {
|
|
82
|
+
width,
|
|
83
|
+
height,
|
|
84
|
+
},
|
|
85
|
+
radius: Math.max(0, radius),
|
|
86
|
+
weight: entry.weight,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function createFieldStore() {
|
|
90
|
+
const registered = new Map();
|
|
91
|
+
let container = null;
|
|
92
|
+
let controller = null;
|
|
93
|
+
let frameConfig = null;
|
|
94
|
+
let callbacks = {};
|
|
95
|
+
let renderScheduled = false;
|
|
96
|
+
const flushRender = () => {
|
|
97
|
+
renderScheduled = false;
|
|
98
|
+
if (!controller || !frameConfig)
|
|
99
|
+
return;
|
|
100
|
+
const combined = [];
|
|
101
|
+
if (frameConfig.primitives) {
|
|
102
|
+
for (let i = 0; i < frameConfig.primitives.length; i++) {
|
|
103
|
+
combined.push(frameConfig.primitives[i]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const entry of registered.values()) {
|
|
107
|
+
const resolved = resolveRegisteredPrimitive(entry, container, frameConfig.positioning);
|
|
108
|
+
if (resolved) {
|
|
109
|
+
combined.push(resolved);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (combined.length === 0) {
|
|
113
|
+
controller.clear();
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const frame = {
|
|
117
|
+
field: {
|
|
118
|
+
primitives: combined,
|
|
119
|
+
blendK: frameConfig.blendK,
|
|
120
|
+
rimOpacity: frameConfig.rimOpacity,
|
|
121
|
+
styleMix: frameConfig.styleMix,
|
|
122
|
+
},
|
|
123
|
+
bounds: frameConfig.bounds,
|
|
124
|
+
refractBounds: frameConfig.refractBounds,
|
|
125
|
+
material: frameConfig.material,
|
|
126
|
+
veilOptics: frameConfig.veilOptics,
|
|
127
|
+
opacity: frameConfig.opacity,
|
|
128
|
+
blurBumpPx: frameConfig.blurBumpPx,
|
|
129
|
+
dpr: frameConfig.dpr,
|
|
130
|
+
solidBackground: frameConfig.solidBackground,
|
|
131
|
+
frostedBackground: frameConfig.frostedBackground,
|
|
132
|
+
refractTintBackground: frameConfig.refractTintBackground,
|
|
133
|
+
refraction: frameConfig.refraction,
|
|
134
|
+
};
|
|
135
|
+
controller.render(frame);
|
|
136
|
+
};
|
|
137
|
+
// Coalesce sibling primitive updates and ResizeObserver notifications within a single
|
|
138
|
+
// commit or tick so multi-primitive fields avoid interleaved layout reads and duplicate renders.
|
|
139
|
+
const scheduleRender = () => {
|
|
140
|
+
if (renderScheduled || !controller || !frameConfig)
|
|
141
|
+
return;
|
|
142
|
+
renderScheduled = true;
|
|
143
|
+
queueMicrotask(() => {
|
|
144
|
+
if (!renderScheduled)
|
|
145
|
+
return;
|
|
146
|
+
flushRender();
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
return {
|
|
150
|
+
syncFrameConfig(config, nextCallbacks) {
|
|
151
|
+
frameConfig = config;
|
|
152
|
+
callbacks = nextCallbacks;
|
|
153
|
+
},
|
|
154
|
+
setContainer(next) {
|
|
155
|
+
container = next;
|
|
156
|
+
},
|
|
157
|
+
setController(next) {
|
|
158
|
+
controller = next;
|
|
159
|
+
if (!next) {
|
|
160
|
+
renderScheduled = false;
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
hasRegisteredPrimitives() {
|
|
164
|
+
return registered.size > 0;
|
|
165
|
+
},
|
|
166
|
+
getCallbacks() {
|
|
167
|
+
return callbacks;
|
|
168
|
+
},
|
|
169
|
+
flushRender,
|
|
170
|
+
registerPrimitive(entry) {
|
|
171
|
+
registered.set(entry.id, entry);
|
|
172
|
+
},
|
|
173
|
+
updatePrimitive(entry) {
|
|
174
|
+
registered.set(entry.id, entry);
|
|
175
|
+
},
|
|
176
|
+
unregisterPrimitive(id) {
|
|
177
|
+
if (registered.delete(id)) {
|
|
178
|
+
scheduleRender();
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
requestRender() {
|
|
182
|
+
scheduleRender();
|
|
183
|
+
},
|
|
184
|
+
getContainer() {
|
|
185
|
+
return container;
|
|
186
|
+
},
|
|
187
|
+
getController() {
|
|
188
|
+
return controller;
|
|
189
|
+
},
|
|
190
|
+
getResizeObserverCtor() {
|
|
191
|
+
return callbacks.resizeObserverCtor;
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Container component that hosts a 3-layer Sessile `GlassSurfaceController` and collects
|
|
197
|
+
* geometry from `<GlassPrimitive>` descendants and/or controlled `primitives`.
|
|
198
|
+
*/
|
|
199
|
+
export function GlassField(props) {
|
|
200
|
+
const { blendK = 24, primitives, rimOpacity, styleMix, mode, theme, material, veilOptics, opacity, blurBumpPx, refraction, filterId, svgMountTarget, positioning, zIndex, disableRefractOnReducedMotion, observeResize = true, bounds, refractBounds, solidBackground, frostedBackground, refractTintBackground, dpr, onResize, onCapabilitiesChange, controllerRef: controllerRefProp, containerRef: containerRefProp, env, overlayRenderer, scheduler, ResizeObserverCtor, className, style, children, } = props;
|
|
201
|
+
const [containerEl, setContainerEl] = useState(null);
|
|
202
|
+
const [activeController, setActiveController] = useState(null);
|
|
203
|
+
const [store] = useState(createFieldStore);
|
|
204
|
+
useIsomorphicLayoutEffect(() => {
|
|
205
|
+
store.syncFrameConfig({
|
|
206
|
+
blendK,
|
|
207
|
+
primitives,
|
|
208
|
+
rimOpacity,
|
|
209
|
+
styleMix,
|
|
210
|
+
material,
|
|
211
|
+
veilOptics,
|
|
212
|
+
opacity,
|
|
213
|
+
blurBumpPx,
|
|
214
|
+
refraction,
|
|
215
|
+
positioning,
|
|
216
|
+
bounds,
|
|
217
|
+
refractBounds,
|
|
218
|
+
solidBackground,
|
|
219
|
+
frostedBackground,
|
|
220
|
+
refractTintBackground,
|
|
221
|
+
dpr,
|
|
222
|
+
}, {
|
|
223
|
+
onResize,
|
|
224
|
+
onCapabilitiesChange,
|
|
225
|
+
resizeObserverCtor: ResizeObserverCtor,
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
useIsomorphicLayoutEffect(() => {
|
|
229
|
+
store.setContainer(containerEl);
|
|
230
|
+
if (!containerEl)
|
|
231
|
+
return;
|
|
232
|
+
const cleanup = assignRef(containerRefProp, containerEl);
|
|
233
|
+
return () => {
|
|
234
|
+
if (cleanup) {
|
|
235
|
+
cleanup();
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
assignRef(containerRefProp, null);
|
|
239
|
+
}
|
|
240
|
+
if (store.getContainer() === containerEl) {
|
|
241
|
+
store.setContainer(null);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}, [store, containerEl, containerRefProp]);
|
|
245
|
+
useIsomorphicLayoutEffect(() => {
|
|
246
|
+
if (!activeController)
|
|
247
|
+
return;
|
|
248
|
+
const cleanup = assignRef(controllerRefProp, activeController);
|
|
249
|
+
return () => {
|
|
250
|
+
if (cleanup) {
|
|
251
|
+
cleanup();
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
assignRef(controllerRefProp, null);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
}, [activeController, controllerRefProp]);
|
|
258
|
+
const zIndexRefract = zIndex?.refract;
|
|
259
|
+
const zIndexVeil = zIndex?.veil;
|
|
260
|
+
const zIndexOverlay = zIndex?.overlay;
|
|
261
|
+
useIsomorphicLayoutEffect(() => {
|
|
262
|
+
if (!containerEl)
|
|
263
|
+
return;
|
|
264
|
+
store.setContainer(containerEl);
|
|
265
|
+
// Material and refraction overrides flow dynamically via flushRender(); avoid
|
|
266
|
+
// pinning initial overrides as sticky controller defaults if later cleared to undefined.
|
|
267
|
+
const nextController = new GlassSurfaceController({
|
|
268
|
+
container: containerEl,
|
|
269
|
+
mode,
|
|
270
|
+
theme,
|
|
271
|
+
filterId,
|
|
272
|
+
svgMountTarget,
|
|
273
|
+
positioning,
|
|
274
|
+
zIndex: zIndexRefract !== undefined || zIndexVeil !== undefined || zIndexOverlay !== undefined
|
|
275
|
+
? { refract: zIndexRefract, veil: zIndexVeil, overlay: zIndexOverlay }
|
|
276
|
+
: undefined,
|
|
277
|
+
disableRefractOnReducedMotion,
|
|
278
|
+
observeResize,
|
|
279
|
+
onResize: (size) => {
|
|
280
|
+
store.getCallbacks().onResize?.(size);
|
|
281
|
+
if (store.hasRegisteredPrimitives()) {
|
|
282
|
+
store.flushRender();
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
onCapabilitiesChange: (caps) => {
|
|
286
|
+
store.getCallbacks().onCapabilitiesChange?.(caps);
|
|
287
|
+
},
|
|
288
|
+
env,
|
|
289
|
+
overlayRenderer,
|
|
290
|
+
scheduler,
|
|
291
|
+
ResizeObserverCtor,
|
|
292
|
+
});
|
|
293
|
+
store.setController(nextController);
|
|
294
|
+
setActiveController(nextController);
|
|
295
|
+
return () => {
|
|
296
|
+
store.setController(null);
|
|
297
|
+
setActiveController(null);
|
|
298
|
+
nextController.dispose();
|
|
299
|
+
};
|
|
300
|
+
}, [
|
|
301
|
+
store,
|
|
302
|
+
containerEl,
|
|
303
|
+
filterId,
|
|
304
|
+
svgMountTarget,
|
|
305
|
+
positioning,
|
|
306
|
+
zIndexRefract,
|
|
307
|
+
zIndexVeil,
|
|
308
|
+
zIndexOverlay,
|
|
309
|
+
disableRefractOnReducedMotion,
|
|
310
|
+
observeResize,
|
|
311
|
+
env,
|
|
312
|
+
overlayRenderer,
|
|
313
|
+
scheduler,
|
|
314
|
+
ResizeObserverCtor,
|
|
315
|
+
]);
|
|
316
|
+
useIsomorphicLayoutEffect(() => {
|
|
317
|
+
const controller = store.getController();
|
|
318
|
+
if (!controller)
|
|
319
|
+
return;
|
|
320
|
+
controller.setMode(mode ?? 'refract');
|
|
321
|
+
}, [store, containerEl, mode]);
|
|
322
|
+
useIsomorphicLayoutEffect(() => {
|
|
323
|
+
const controller = store.getController();
|
|
324
|
+
if (!controller)
|
|
325
|
+
return;
|
|
326
|
+
if (theme !== undefined) {
|
|
327
|
+
controller.setTheme(theme);
|
|
328
|
+
}
|
|
329
|
+
else if (containerEl) {
|
|
330
|
+
const themeOpts = env && 'matchMedia' in env ? { matchMedia: env.matchMedia ?? null } : undefined;
|
|
331
|
+
controller.setTheme(resolveElementTheme(containerEl, themeOpts));
|
|
332
|
+
}
|
|
333
|
+
}, [store, containerEl, theme, env]);
|
|
334
|
+
useIsomorphicLayoutEffect(() => {
|
|
335
|
+
store.flushRender();
|
|
336
|
+
}, [
|
|
337
|
+
store,
|
|
338
|
+
containerEl,
|
|
339
|
+
blendK,
|
|
340
|
+
primitives,
|
|
341
|
+
rimOpacity,
|
|
342
|
+
styleMix,
|
|
343
|
+
material,
|
|
344
|
+
veilOptics,
|
|
345
|
+
opacity,
|
|
346
|
+
blurBumpPx,
|
|
347
|
+
refraction,
|
|
348
|
+
bounds,
|
|
349
|
+
refractBounds,
|
|
350
|
+
solidBackground,
|
|
351
|
+
frostedBackground,
|
|
352
|
+
refractTintBackground,
|
|
353
|
+
dpr,
|
|
354
|
+
filterId,
|
|
355
|
+
svgMountTarget,
|
|
356
|
+
positioning,
|
|
357
|
+
zIndexRefract,
|
|
358
|
+
zIndexVeil,
|
|
359
|
+
zIndexOverlay,
|
|
360
|
+
disableRefractOnReducedMotion,
|
|
361
|
+
observeResize,
|
|
362
|
+
env,
|
|
363
|
+
overlayRenderer,
|
|
364
|
+
scheduler,
|
|
365
|
+
ResizeObserverCtor,
|
|
366
|
+
className,
|
|
367
|
+
style,
|
|
368
|
+
]);
|
|
369
|
+
// Isolate React children inside a layout-transparent `display: contents` node so text/number
|
|
370
|
+
// reconciliation (`node.textContent = ...`) cannot wipe out controller-managed sibling layers.
|
|
371
|
+
return createElement(GlassFieldContext.Provider, { value: store }, createElement('div', { ref: setContainerEl, className, style }, createElement('div', { 'data-sessile-content': '', style: CONTENT_WRAPPER_STYLE }, children)));
|
|
372
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal React context and ref utilities connecting `<GlassPrimitive>` children
|
|
3
|
+
* to their enclosing `<GlassField>` surface controller.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { useLayoutEffect, type Ref } from 'react';
|
|
8
|
+
import type { GlassSurfaceController } from '@sessile/dom';
|
|
9
|
+
export interface RegisteredGlassPrimitive {
|
|
10
|
+
id: string;
|
|
11
|
+
x?: number;
|
|
12
|
+
y?: number;
|
|
13
|
+
width?: number;
|
|
14
|
+
height?: number;
|
|
15
|
+
radius?: number;
|
|
16
|
+
weight?: number;
|
|
17
|
+
active?: boolean;
|
|
18
|
+
getElement?: () => HTMLElement | null;
|
|
19
|
+
}
|
|
20
|
+
export interface GlassFieldContextValue {
|
|
21
|
+
registerPrimitive(entry: RegisteredGlassPrimitive): void;
|
|
22
|
+
updatePrimitive(entry: RegisteredGlassPrimitive): void;
|
|
23
|
+
unregisterPrimitive(id: string): void;
|
|
24
|
+
requestRender(): void;
|
|
25
|
+
getContainer(): HTMLDivElement | null;
|
|
26
|
+
getController(): GlassSurfaceController | null;
|
|
27
|
+
getResizeObserverCtor?: () => typeof ResizeObserver | undefined;
|
|
28
|
+
}
|
|
29
|
+
export declare const GlassFieldContext: import("react").Context<GlassFieldContextValue | null>;
|
|
30
|
+
/** SSR-safe layout effect hook falling back to `useEffect` outside browser environments. */
|
|
31
|
+
export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
|
|
32
|
+
/** Assigns a value to either a callback ref or a mutable React ref object. */
|
|
33
|
+
export declare function assignRef<T>(ref: Ref<T> | undefined, value: T | null): (() => void) | void;
|
|
34
|
+
/** Returns the nearest parent `<GlassField>` context, throwing if mounted outside one. */
|
|
35
|
+
export declare function useGlassField(): GlassFieldContextValue;
|
|
36
|
+
/** Returns the nearest parent `<GlassField>` context, or `null` if none exists. */
|
|
37
|
+
export declare function useOptionalGlassField(): GlassFieldContextValue | null;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal React context and ref utilities connecting `<GlassPrimitive>` children
|
|
3
|
+
* to their enclosing `<GlassField>` surface controller.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { createContext, useContext, useEffect, useLayoutEffect, } from 'react';
|
|
8
|
+
export const GlassFieldContext = createContext(null);
|
|
9
|
+
/** SSR-safe layout effect hook falling back to `useEffect` outside browser environments. */
|
|
10
|
+
export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
11
|
+
/** Assigns a value to either a callback ref or a mutable React ref object. */
|
|
12
|
+
export function assignRef(ref, value) {
|
|
13
|
+
if (!ref)
|
|
14
|
+
return undefined;
|
|
15
|
+
if (typeof ref === 'function') {
|
|
16
|
+
const cleanup = ref(value);
|
|
17
|
+
return typeof cleanup === 'function' ? cleanup : undefined;
|
|
18
|
+
}
|
|
19
|
+
;
|
|
20
|
+
ref.current = value;
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Returns the nearest parent `<GlassField>` context, throwing if mounted outside one. */
|
|
24
|
+
export function useGlassField() {
|
|
25
|
+
const ctx = useContext(GlassFieldContext);
|
|
26
|
+
if (!ctx) {
|
|
27
|
+
throw new Error('useGlassField must be used within a <GlassField> component.');
|
|
28
|
+
}
|
|
29
|
+
return ctx;
|
|
30
|
+
}
|
|
31
|
+
/** Returns the nearest parent `<GlassField>` context, or `null` if none exists. */
|
|
32
|
+
export function useOptionalGlassField() {
|
|
33
|
+
return useContext(GlassFieldContext);
|
|
34
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative `<GlassPrimitive>` component registering either explicit geometry
|
|
3
|
+
* (headless mode) or a DOM-measured wrapper box (wrapper mode) into its parent `<GlassField>`.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { type CSSProperties, type ReactElement, type ReactNode, type Ref } from 'react';
|
|
8
|
+
export interface GlassPrimitiveProps {
|
|
9
|
+
/** Stable identifier within the `<GlassField>` (auto-generated via `useId()` when omitted). */
|
|
10
|
+
id?: string;
|
|
11
|
+
/** Left coordinate relative to the `<GlassField>` padding box in CSS px. */
|
|
12
|
+
x?: number;
|
|
13
|
+
/** Top coordinate relative to the `<GlassField>` padding box in CSS px. */
|
|
14
|
+
y?: number;
|
|
15
|
+
/** Box width in CSS px (measured automatically in wrapper mode when omitted). */
|
|
16
|
+
width?: number;
|
|
17
|
+
/** Box height in CSS px (measured automatically in wrapper mode when omitted). */
|
|
18
|
+
height?: number;
|
|
19
|
+
/** Corner radius in CSS px (resolved from computed `borderTopLeftRadius` in wrapper mode when omitted). */
|
|
20
|
+
radius?: number;
|
|
21
|
+
/** Optional SDF primitive weight. */
|
|
22
|
+
weight?: number;
|
|
23
|
+
/** Whether this primitive participates in the active SDF field (default: `true`). */
|
|
24
|
+
active?: boolean;
|
|
25
|
+
/** Child content; passing children, `className`, `style`, or `elementRef` enables wrapper mode. */
|
|
26
|
+
children?: ReactNode;
|
|
27
|
+
className?: string;
|
|
28
|
+
style?: CSSProperties;
|
|
29
|
+
elementRef?: Ref<HTMLDivElement | null>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Registers a rounded-rectangle or capsule primitive into the enclosing `<GlassField>`.
|
|
33
|
+
* Renders `null` when used headlessly with explicit geometry, or wraps children in a measured `<div>`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function GlassPrimitive(props: GlassPrimitiveProps): ReactElement | null;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative `<GlassPrimitive>` component registering either explicit geometry
|
|
3
|
+
* (headless mode) or a DOM-measured wrapper box (wrapper mode) into its parent `<GlassField>`.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { createElement, useId, useState, } from 'react';
|
|
8
|
+
import { observeElementResize } from '@sessile/dom';
|
|
9
|
+
import { assignRef, useGlassField, useIsomorphicLayoutEffect, } from "./GlassFieldContext.js";
|
|
10
|
+
/**
|
|
11
|
+
* Registers a rounded-rectangle or capsule primitive into the enclosing `<GlassField>`.
|
|
12
|
+
* Renders `null` when used headlessly with explicit geometry, or wraps children in a measured `<div>`.
|
|
13
|
+
*/
|
|
14
|
+
export function GlassPrimitive(props) {
|
|
15
|
+
const { id: explicitId, x, y, width, height, radius, weight, active = true, children, className, style, elementRef: elementRefProp, } = props;
|
|
16
|
+
const generatedId = useId();
|
|
17
|
+
const primitiveId = explicitId ?? generatedId;
|
|
18
|
+
const field = useGlassField();
|
|
19
|
+
const isWrapperMode = children !== undefined ||
|
|
20
|
+
className !== undefined ||
|
|
21
|
+
style !== undefined ||
|
|
22
|
+
elementRefProp !== undefined;
|
|
23
|
+
const [wrapperEl, setWrapperEl] = useState(null);
|
|
24
|
+
useIsomorphicLayoutEffect(() => {
|
|
25
|
+
if (!isWrapperMode || !wrapperEl)
|
|
26
|
+
return;
|
|
27
|
+
const cleanup = assignRef(elementRefProp, wrapperEl);
|
|
28
|
+
return () => {
|
|
29
|
+
if (cleanup) {
|
|
30
|
+
cleanup();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
assignRef(elementRefProp, null);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}, [elementRefProp, isWrapperMode, wrapperEl]);
|
|
37
|
+
useIsomorphicLayoutEffect(() => {
|
|
38
|
+
const entry = {
|
|
39
|
+
id: primitiveId,
|
|
40
|
+
x,
|
|
41
|
+
y,
|
|
42
|
+
width,
|
|
43
|
+
height,
|
|
44
|
+
radius,
|
|
45
|
+
weight,
|
|
46
|
+
active,
|
|
47
|
+
getElement: isWrapperMode ? () => wrapperEl : undefined,
|
|
48
|
+
};
|
|
49
|
+
field.registerPrimitive(entry);
|
|
50
|
+
field.requestRender();
|
|
51
|
+
return () => {
|
|
52
|
+
field.unregisterPrimitive(primitiveId);
|
|
53
|
+
};
|
|
54
|
+
}, [field, primitiveId, isWrapperMode]);
|
|
55
|
+
useIsomorphicLayoutEffect(() => {
|
|
56
|
+
field.updatePrimitive({
|
|
57
|
+
id: primitiveId,
|
|
58
|
+
x,
|
|
59
|
+
y,
|
|
60
|
+
width,
|
|
61
|
+
height,
|
|
62
|
+
radius,
|
|
63
|
+
weight,
|
|
64
|
+
active,
|
|
65
|
+
getElement: isWrapperMode ? () => wrapperEl : undefined,
|
|
66
|
+
});
|
|
67
|
+
field.requestRender();
|
|
68
|
+
}, [
|
|
69
|
+
field,
|
|
70
|
+
primitiveId,
|
|
71
|
+
x,
|
|
72
|
+
y,
|
|
73
|
+
width,
|
|
74
|
+
height,
|
|
75
|
+
radius,
|
|
76
|
+
weight,
|
|
77
|
+
active,
|
|
78
|
+
isWrapperMode,
|
|
79
|
+
wrapperEl,
|
|
80
|
+
className,
|
|
81
|
+
style,
|
|
82
|
+
]);
|
|
83
|
+
const resizeObserverCtor = field.getResizeObserverCtor?.();
|
|
84
|
+
useIsomorphicLayoutEffect(() => {
|
|
85
|
+
if (!isWrapperMode || !wrapperEl)
|
|
86
|
+
return;
|
|
87
|
+
return observeElementResize(wrapperEl, () => {
|
|
88
|
+
field.requestRender();
|
|
89
|
+
}, {
|
|
90
|
+
ResizeObserverCtor: resizeObserverCtor,
|
|
91
|
+
emitInitial: false,
|
|
92
|
+
});
|
|
93
|
+
}, [field, isWrapperMode, wrapperEl, resizeObserverCtor]);
|
|
94
|
+
if (!isWrapperMode) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return createElement('div', { ref: setWrapperEl, className, style }, children);
|
|
98
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for `@sessile/react`.
|
|
3
|
+
* Lightweight React 19 components and hooks for Sessile glass morphing surfaces.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
export { GlassField, type GlassFieldProps } from './GlassField.ts';
|
|
8
|
+
export { GlassPrimitive, type GlassPrimitiveProps } from './GlassPrimitive.ts';
|
|
9
|
+
export { useGlassField, useOptionalGlassField, type GlassFieldContextValue, type RegisteredGlassPrimitive, } from './GlassFieldContext.ts';
|
|
10
|
+
export { useReducedMotion } from './useReducedMotion.ts';
|
|
11
|
+
export { useGlassMorph, type UseGlassMorphOptions, type UseGlassMorphResult, } from './useGlassMorph.ts';
|
|
12
|
+
export { useElasticPress, type UseElasticPressOptions, } from './useElasticPress.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for `@sessile/react`.
|
|
3
|
+
* Lightweight React 19 components and hooks for Sessile glass morphing surfaces.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
export { GlassField } from "./GlassField.js";
|
|
8
|
+
export { GlassPrimitive } from "./GlassPrimitive.js";
|
|
9
|
+
export { useGlassField, useOptionalGlassField, } from "./GlassFieldContext.js";
|
|
10
|
+
export { useReducedMotion } from "./useReducedMotion.js";
|
|
11
|
+
export { useGlassMorph, } from "./useGlassMorph.js";
|
|
12
|
+
export { useElasticPress, } from "./useElasticPress.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
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 { type RefObject } from 'react';
|
|
8
|
+
import { type ElasticPressOptions } from '@sessile/core';
|
|
9
|
+
import { type CapabilityEnv, type FrameScheduler } from '@sessile/dom';
|
|
10
|
+
export interface UseElasticPressOptions extends ElasticPressOptions {
|
|
11
|
+
/** Enables or disables pointer listeners and transform updates (default: `true`). */
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
/** Scale and drag displacement multiplier (default: `1`). */
|
|
14
|
+
intensity?: number;
|
|
15
|
+
/** Attribute on the target element suppressing elastic press while morphing (default: `'data-sessile-morphing'`). */
|
|
16
|
+
ignoreAttribute?: string;
|
|
17
|
+
/** Attribute set on the target element while an elastic press transform is active (default: `'data-sessile-elastic-press'`). */
|
|
18
|
+
activeAttribute?: string;
|
|
19
|
+
/** Capability environment override forwarded to `useReducedMotion`. */
|
|
20
|
+
env?: CapabilityEnv;
|
|
21
|
+
/** Custom frame scheduler (useful for deterministic testing). */
|
|
22
|
+
scheduler?: Partial<FrameScheduler>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Drives spring-based press inflation and rubber-band pointer tracking on `targetRef`
|
|
26
|
+
* using GPU-composited `translate3d`/`scale` transforms only.
|
|
27
|
+
* The hook owns inline `element.style.transform` while active; apply layout positioning
|
|
28
|
+
* transforms on an outer wrapper when combining with static translations or rotations.
|
|
29
|
+
*/
|
|
30
|
+
export declare function useElasticPress(targetRef: RefObject<HTMLElement | null>, options?: UseElasticPressOptions): void;
|