@sessile/presets 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.
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Segmented Pill continuous-flow preset for Sessile.
3
+ *
4
+ * Drives a spring-coupled active indicator pill that smoothly stretches into a
5
+ * two-lobe smin liquid bridge during segment transitions and settles back into
6
+ * a crisp single rounded capsule at rest.
7
+ *
8
+ * SPDX-License-Identifier: MPL-2.0
9
+ */
10
+ import { type GlassFieldState, type GlassPrimitive, type Rect, type SpringParams } from '@sessile/core';
11
+ /** Layout descriptor for a single selectable segment in the control. */
12
+ export interface SegmentItem {
13
+ id: string;
14
+ rect: Rect;
15
+ radius?: number;
16
+ }
17
+ /** Configuration options for `createSegmentedPillController`. */
18
+ export interface SegmentedPillOptions {
19
+ segments: readonly SegmentItem[];
20
+ initialActiveId?: string;
21
+ defaultRadius?: number;
22
+ leadSpring?: SpringParams;
23
+ trailSpring?: SpringParams;
24
+ blendK?: number;
25
+ squeezeRatio?: number;
26
+ maxStretchPx?: number;
27
+ /** Enforce exact 2D area conservation (`W' * H' === W * H`) on stretched lobes (defaults to `true`). */
28
+ preserveArea?: boolean;
29
+ reducedMotion?: boolean;
30
+ }
31
+ /** Instantaneous snapshot of the segmented indicator pill and its SDF field. */
32
+ export interface SegmentedPillState {
33
+ activeId: string | null;
34
+ primitive: GlassPrimitive | null;
35
+ field: GlassFieldState;
36
+ stretchRatio: number;
37
+ isAnimating: boolean;
38
+ reducedMotion: boolean;
39
+ }
40
+ /** Interruptible controller driving a continuous-flow segmented glass pill. */
41
+ export interface SegmentedPillController {
42
+ setActive(id: string): void;
43
+ snapTo(id: string): void;
44
+ setSegments(segments: readonly SegmentItem[]): void;
45
+ setReducedMotion(reduced: boolean): void;
46
+ step(dtSec: number): SegmentedPillState;
47
+ getState(): SegmentedPillState;
48
+ subscribe(listener: (state: SegmentedPillState) => void): () => void;
49
+ dispose(): void;
50
+ }
51
+ /** Default springs and deformation limits for the segmented pill preset. */
52
+ export declare const SEGMENTED_PILL_DEFAULTS: {
53
+ readonly leadSpring: {
54
+ readonly mass: 1;
55
+ readonly stiffness: 380;
56
+ readonly dampingRatio: 0.78;
57
+ };
58
+ readonly trailSpring: {
59
+ readonly mass: 1;
60
+ readonly stiffness: 220;
61
+ readonly dampingRatio: 0.82;
62
+ };
63
+ readonly blendK: 18;
64
+ readonly squeezeRatio: 0.12;
65
+ readonly maxStretchPx: 14;
66
+ };
67
+ /**
68
+ * Creates an interruptible continuous-flow segmented pill controller.
69
+ */
70
+ export declare function createSegmentedPillController(options: SegmentedPillOptions): SegmentedPillController;
@@ -0,0 +1,396 @@
1
+ /**
2
+ * Segmented Pill continuous-flow preset for Sessile.
3
+ *
4
+ * Drives a spring-coupled active indicator pill that smoothly stretches into a
5
+ * two-lobe smin liquid bridge during segment transitions and settles back into
6
+ * a crisp single rounded capsule at rest.
7
+ *
8
+ * SPDX-License-Identifier: MPL-2.0
9
+ */
10
+ import { clamp, computeAreaPreservingScale, createSpringChannel, lerp, } from '@sessile/core';
11
+ /** Default springs and deformation limits for the segmented pill preset. */
12
+ export const SEGMENTED_PILL_DEFAULTS = {
13
+ leadSpring: { mass: 1, stiffness: 380, dampingRatio: 0.78 },
14
+ trailSpring: { mass: 1, stiffness: 220, dampingRatio: 0.82 },
15
+ blendK: 18,
16
+ squeezeRatio: 0.12,
17
+ maxStretchPx: 14,
18
+ };
19
+ const TRAVEL_EPSILON_PX = 0.25;
20
+ function resolveSegment(item, fallbackRadius) {
21
+ const width = Math.max(0, item.rect.width);
22
+ const height = Math.max(0, item.rect.height);
23
+ const defaultPillRadius = Math.min(width, height) * 0.5;
24
+ const rawRadius = item.radius ?? fallbackRadius ?? defaultPillRadius;
25
+ const radius = Math.max(0, Math.min(rawRadius, width * 0.5, height * 0.5));
26
+ return {
27
+ id: item.id,
28
+ cx: item.rect.x + width * 0.5,
29
+ cy: item.rect.y + height * 0.5,
30
+ width,
31
+ height,
32
+ radius,
33
+ };
34
+ }
35
+ function resolveAllSegments(items, fallbackRadius) {
36
+ return items.map((item) => resolveSegment(item, fallbackRadius));
37
+ }
38
+ function snapChannel(channel, value) {
39
+ channel.setTarget(value);
40
+ channel.snap(value, 0);
41
+ }
42
+ function evalSegmentedBoxesGap(sep, ux, uy, leadW, leadH, trailW, trailH, radius) {
43
+ const rLead = Math.max(0, Math.min(radius, leadW * 0.5, leadH * 0.5));
44
+ const rTrail = Math.max(0, Math.min(radius, trailW * 0.5, trailH * 0.5));
45
+ const innerSumW = Math.max(0, 0.5 * (leadW + trailW) - rLead - rTrail);
46
+ const innerSumH = Math.max(0, 0.5 * (leadH + trailH) - rLead - rTrail);
47
+ const ox = Math.max(0, Math.abs(ux) * sep - innerSumW);
48
+ const oy = Math.max(0, Math.abs(uy) * sep - innerSumH);
49
+ return Math.hypot(ox, oy) - rLead - rTrail;
50
+ }
51
+ function computeLobeScale(stretchRatio, squeezeRatio, ux, uy, preserveArea, isLead) {
52
+ if (preserveArea) {
53
+ const stretchAmount = isLead
54
+ ? squeezeRatio * 0.5 * stretchRatio
55
+ : squeezeRatio * stretchRatio;
56
+ return computeAreaPreservingScale(1 + stretchAmount, ux, uy);
57
+ }
58
+ if (isLead) {
59
+ return {
60
+ scaleX: (1 + squeezeRatio * 0.35 * stretchRatio * Math.abs(ux)) *
61
+ (1 - squeezeRatio * 0.5 * stretchRatio * Math.abs(uy)),
62
+ scaleY: (1 + squeezeRatio * 0.35 * stretchRatio * Math.abs(uy)) *
63
+ (1 - squeezeRatio * 0.5 * stretchRatio * Math.abs(ux)),
64
+ };
65
+ }
66
+ return {
67
+ scaleX: 1 - squeezeRatio * stretchRatio * Math.abs(uy),
68
+ scaleY: 1 - squeezeRatio * stretchRatio * Math.abs(ux),
69
+ };
70
+ }
71
+ function computeConnectedStretchCap(maxStretchPx, ux, uy, lw, lh, tw, th, lr, baseBlendK, squeezeRatio, preserveArea) {
72
+ if (maxStretchPx <= 0)
73
+ return 0;
74
+ const leadScale = computeLobeScale(1, squeezeRatio, ux, uy, preserveArea, true);
75
+ const trailScale = computeLobeScale(1, squeezeRatio, ux, uy, preserveArea, false);
76
+ const minLeadW = Math.max(0, lw * leadScale.scaleX);
77
+ const minLeadH = Math.max(0, lh * leadScale.scaleY);
78
+ const minTrailW = Math.max(0, tw * trailScale.scaleX);
79
+ const minTrailH = Math.max(0, th * trailScale.scaleY);
80
+ const maxBridgeGap = baseBlendK * 0.4;
81
+ if (evalSegmentedBoxesGap(maxStretchPx, ux, uy, minLeadW, minLeadH, minTrailW, minTrailH, lr) <= maxBridgeGap) {
82
+ return maxStretchPx;
83
+ }
84
+ let lo = 0;
85
+ let hi = maxStretchPx;
86
+ for (let i = 0; i < 10; i += 1) {
87
+ const mid = 0.5 * (lo + hi);
88
+ const gap = evalSegmentedBoxesGap(mid, ux, uy, minLeadW, minLeadH, minTrailW, minTrailH, lr);
89
+ if (gap <= maxBridgeGap) {
90
+ lo = mid;
91
+ }
92
+ else {
93
+ hi = mid;
94
+ }
95
+ }
96
+ return lo;
97
+ }
98
+ function createZeroChannel(params, initialValue) {
99
+ return createSpringChannel({
100
+ params,
101
+ initialValue,
102
+ initialTarget: initialValue,
103
+ settlePosEps: 0.05,
104
+ settleVelEps: 0.25,
105
+ });
106
+ }
107
+ /**
108
+ * Creates an interruptible continuous-flow segmented pill controller.
109
+ */
110
+ export function createSegmentedPillController(options) {
111
+ const leadSpring = options.leadSpring ?? SEGMENTED_PILL_DEFAULTS.leadSpring;
112
+ const trailSpring = options.trailSpring ?? SEGMENTED_PILL_DEFAULTS.trailSpring;
113
+ const baseBlendK = Math.max(0, options.blendK ?? SEGMENTED_PILL_DEFAULTS.blendK);
114
+ const squeezeRatio = clamp(options.squeezeRatio ?? SEGMENTED_PILL_DEFAULTS.squeezeRatio, 0, 0.45);
115
+ const maxStretchPx = Math.max(0, options.maxStretchPx ?? SEGMENTED_PILL_DEFAULTS.maxStretchPx);
116
+ const preserveArea = options.preserveArea ?? true;
117
+ const defaultRadius = options.defaultRadius;
118
+ let reducedMotion = options.reducedMotion ?? false;
119
+ let disposed = false;
120
+ let segments = resolveAllSegments(options.segments, defaultRadius);
121
+ function findSegment(id) {
122
+ if (!id)
123
+ return null;
124
+ for (const seg of segments) {
125
+ if (seg.id === id)
126
+ return seg;
127
+ }
128
+ return null;
129
+ }
130
+ let activeId = findSegment(options.initialActiveId ?? null)?.id ?? segments[0]?.id ?? null;
131
+ const initialSeg = findSegment(activeId) ?? {
132
+ id: '',
133
+ cx: 0,
134
+ cy: 0,
135
+ width: 0,
136
+ height: 0,
137
+ radius: 0,
138
+ };
139
+ const leadX = createZeroChannel(leadSpring, initialSeg.cx);
140
+ const leadY = createZeroChannel(leadSpring, initialSeg.cy);
141
+ const leadW = createZeroChannel(leadSpring, initialSeg.width);
142
+ const leadH = createZeroChannel(leadSpring, initialSeg.height);
143
+ const leadR = createZeroChannel(leadSpring, initialSeg.radius);
144
+ const trailX = createZeroChannel(trailSpring, initialSeg.cx);
145
+ const trailY = createZeroChannel(trailSpring, initialSeg.cy);
146
+ const trailW = createZeroChannel(trailSpring, initialSeg.width);
147
+ const trailH = createZeroChannel(trailSpring, initialSeg.height);
148
+ const listeners = new Set();
149
+ function snapAllChannels(seg) {
150
+ snapChannel(leadX, seg.cx);
151
+ snapChannel(leadY, seg.cy);
152
+ snapChannel(leadW, seg.width);
153
+ snapChannel(leadH, seg.height);
154
+ snapChannel(leadR, seg.radius);
155
+ snapChannel(trailX, seg.cx);
156
+ snapChannel(trailY, seg.cy);
157
+ snapChannel(trailW, seg.width);
158
+ snapChannel(trailH, seg.height);
159
+ }
160
+ function targetAllChannels(seg) {
161
+ leadX.setTarget(seg.cx);
162
+ leadY.setTarget(seg.cy);
163
+ leadW.setTarget(seg.width);
164
+ leadH.setTarget(seg.height);
165
+ leadR.setTarget(seg.radius);
166
+ trailX.setTarget(seg.cx);
167
+ trailY.setTarget(seg.cy);
168
+ trailW.setTarget(seg.width);
169
+ trailH.setTarget(seg.height);
170
+ }
171
+ function areAllChannelsSettled() {
172
+ return (leadX.getState().settled &&
173
+ leadY.getState().settled &&
174
+ leadW.getState().settled &&
175
+ leadH.getState().settled &&
176
+ leadR.getState().settled &&
177
+ trailX.getState().settled &&
178
+ trailY.getState().settled &&
179
+ trailW.getState().settled &&
180
+ trailH.getState().settled);
181
+ }
182
+ function buildState() {
183
+ if (!activeId || segments.length === 0) {
184
+ return {
185
+ activeId: null,
186
+ primitive: null,
187
+ field: { primitives: [], blendK: 0 },
188
+ stretchRatio: 0,
189
+ isAnimating: false,
190
+ reducedMotion,
191
+ };
192
+ }
193
+ const lx = leadX.getState().value;
194
+ const ly = leadY.getState().value;
195
+ const lw = Math.max(0, leadW.getState().value);
196
+ const lh = Math.max(0, leadH.getState().value);
197
+ const lr = Math.max(0, Math.min(leadR.getState().value, lw * 0.5, lh * 0.5));
198
+ const tx = trailX.getState().value;
199
+ const ty = trailY.getState().value;
200
+ const tw = Math.max(0, trailW.getState().value);
201
+ const th = Math.max(0, trailH.getState().value);
202
+ const dx = lx - tx;
203
+ const dy = ly - ty;
204
+ const rawLagDist = Math.hypot(dx, dy);
205
+ const animating = !reducedMotion && !areAllChannelsSettled();
206
+ if (reducedMotion || (!animating && rawLagDist <= TRAVEL_EPSILON_PX) || rawLagDist <= TRAVEL_EPSILON_PX) {
207
+ const restPrimitive = {
208
+ id: 'segmented-pill',
209
+ center: { x: lx, y: ly },
210
+ size: { width: lw, height: lh },
211
+ radius: lr,
212
+ };
213
+ return {
214
+ activeId,
215
+ primitive: restPrimitive,
216
+ field: {
217
+ primitives: [restPrimitive],
218
+ blendK: 0,
219
+ },
220
+ stretchRatio: 0,
221
+ isAnimating: animating,
222
+ reducedMotion,
223
+ };
224
+ }
225
+ const ux = dx / rawLagDist;
226
+ const uy = dy / rawLagDist;
227
+ // Constrain maximum center separation by the actual rounded-box bridge gap so diagonal jumps never split into two lobes.
228
+ const stretchCap = computeConnectedStretchCap(maxStretchPx, ux, uy, lw, lh, tw, th, lr, baseBlendK, squeezeRatio, preserveArea);
229
+ let clampedSep = stretchCap > 0 ? (stretchCap * rawLagDist) / (stretchCap + rawLagDist) : 0;
230
+ const stretchRatio = stretchCap > 0 ? clamp(clampedSep / stretchCap, 0, 1) : 0;
231
+ const leadScale = computeLobeScale(stretchRatio, squeezeRatio, ux, uy, preserveArea, true);
232
+ const trailScale = computeLobeScale(stretchRatio, squeezeRatio, ux, uy, preserveArea, false);
233
+ const outLeadW = Math.max(0, lw * leadScale.scaleX);
234
+ const outLeadH = Math.max(0, lh * leadScale.scaleY);
235
+ const outTrailW = Math.max(0, tw * trailScale.scaleX);
236
+ const outTrailH = Math.max(0, th * trailScale.scaleY);
237
+ const activeBlendK = baseBlendK * clamp(lerp(0.25, 1, stretchRatio), 0, 1);
238
+ const activeMaxGap = activeBlendK * 0.45;
239
+ if (clampedSep > 0 &&
240
+ evalSegmentedBoxesGap(clampedSep, ux, uy, outLeadW, outLeadH, outTrailW, outTrailH, lr) >
241
+ activeMaxGap) {
242
+ let lo = 0;
243
+ let hi = clampedSep;
244
+ for (let i = 0; i < 8; i += 1) {
245
+ const mid = 0.5 * (lo + hi);
246
+ if (evalSegmentedBoxesGap(mid, ux, uy, outLeadW, outLeadH, outTrailW, outTrailH, lr) <=
247
+ activeMaxGap) {
248
+ lo = mid;
249
+ }
250
+ else {
251
+ hi = mid;
252
+ }
253
+ }
254
+ clampedSep = lo;
255
+ }
256
+ const leadPrimitive = {
257
+ id: 'segmented-pill-lead',
258
+ center: { x: lx, y: ly },
259
+ size: { width: outLeadW, height: outLeadH },
260
+ radius: Math.max(0, Math.min(lr, outLeadW * 0.5, outLeadH * 0.5)),
261
+ };
262
+ const trailPrimitive = {
263
+ id: 'segmented-pill-trail',
264
+ center: {
265
+ x: lx - ux * clampedSep,
266
+ y: ly - uy * clampedSep,
267
+ },
268
+ size: { width: outTrailW, height: outTrailH },
269
+ radius: Math.max(0, Math.min(lr, outTrailW * 0.5, outTrailH * 0.5)),
270
+ };
271
+ return {
272
+ activeId,
273
+ primitive: leadPrimitive,
274
+ field: {
275
+ primitives: [leadPrimitive, trailPrimitive],
276
+ blendK: activeBlendK,
277
+ },
278
+ stretchRatio,
279
+ isAnimating: true,
280
+ reducedMotion: false,
281
+ };
282
+ }
283
+ function notify() {
284
+ if (disposed || listeners.size === 0)
285
+ return;
286
+ const snapshot = buildState();
287
+ for (const listener of [...listeners]) {
288
+ listener(snapshot);
289
+ }
290
+ }
291
+ return {
292
+ getState() {
293
+ return buildState();
294
+ },
295
+ setActive(id) {
296
+ if (disposed)
297
+ return;
298
+ const targetSeg = findSegment(id);
299
+ if (!targetSeg)
300
+ return;
301
+ if (activeId === targetSeg.id && areAllChannelsSettled())
302
+ return;
303
+ activeId = targetSeg.id;
304
+ if (reducedMotion) {
305
+ snapAllChannels(targetSeg);
306
+ }
307
+ else {
308
+ targetAllChannels(targetSeg);
309
+ }
310
+ notify();
311
+ },
312
+ snapTo(id) {
313
+ if (disposed)
314
+ return;
315
+ const targetSeg = findSegment(id);
316
+ if (!targetSeg)
317
+ return;
318
+ activeId = targetSeg.id;
319
+ snapAllChannels(targetSeg);
320
+ notify();
321
+ },
322
+ setSegments(nextSegments) {
323
+ if (disposed)
324
+ return;
325
+ const hadActive = activeId !== null;
326
+ segments = resolveAllSegments(nextSegments, defaultRadius);
327
+ if (segments.length === 0) {
328
+ activeId = null;
329
+ snapAllChannels({
330
+ id: '',
331
+ cx: 0,
332
+ cy: 0,
333
+ width: 0,
334
+ height: 0,
335
+ radius: 0,
336
+ });
337
+ notify();
338
+ return;
339
+ }
340
+ const matched = findSegment(activeId) ?? segments[0];
341
+ activeId = matched.id;
342
+ if (!hadActive || reducedMotion) {
343
+ snapAllChannels(matched);
344
+ }
345
+ else {
346
+ targetAllChannels(matched);
347
+ }
348
+ notify();
349
+ },
350
+ setReducedMotion(reduced) {
351
+ if (disposed || reducedMotion === reduced)
352
+ return;
353
+ reducedMotion = reduced;
354
+ if (reducedMotion && activeId) {
355
+ const currentSeg = findSegment(activeId);
356
+ if (currentSeg) {
357
+ snapAllChannels(currentSeg);
358
+ }
359
+ }
360
+ notify();
361
+ },
362
+ step(dtSec) {
363
+ if (disposed || !Number.isFinite(dtSec) || dtSec <= 0) {
364
+ return buildState();
365
+ }
366
+ if (reducedMotion || areAllChannelsSettled()) {
367
+ return buildState();
368
+ }
369
+ leadX.step(dtSec);
370
+ leadY.step(dtSec);
371
+ leadW.step(dtSec);
372
+ leadH.step(dtSec);
373
+ leadR.step(dtSec);
374
+ trailX.step(dtSec);
375
+ trailY.step(dtSec);
376
+ trailW.step(dtSec);
377
+ trailH.step(dtSec);
378
+ const next = buildState();
379
+ notify();
380
+ return next;
381
+ },
382
+ subscribe(listener) {
383
+ if (disposed) {
384
+ return () => { };
385
+ }
386
+ listeners.add(listener);
387
+ return () => {
388
+ listeners.delete(listener);
389
+ };
390
+ },
391
+ dispose() {
392
+ disposed = true;
393
+ listeners.clear();
394
+ },
395
+ };
396
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@sessile/presets",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "license": "MPL-2.0",
6
+ "description": "Optional interaction presets (morphing menu, segmented pill, drag-merge) built on Sessile.",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/MERURUXD/sessile.git",
10
+ "directory": "packages/presets"
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/core": "0.2.0"
30
+ },
31
+ "scripts": {
32
+ "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit",
33
+ "test": "node --test test/*.test.ts",
34
+ "build": "tsc -p tsconfig.build.json"
35
+ }
6
36
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.