Skip to content

Commit 83c69ca

Browse files
committed
feat(core): createDragToSnapPoints and useDragToSnapPoints settle an element on snap points
`createDragToSnapPoints(element, {axis, snapPoints, flickThreshold?, onSettle}) → {settleTo, destroy}` and `useDragToSnapPoints(ref, …)` over it (spec:AST-107 FR2, FR6): a drag moves the element along its axis under every pointer, and the release settles it where Core's one release policy says (the nearest point; the next one past the flick speed, 1008 px/s by default; back to where the drag started when the release moves back toward it), on the settle spring from the release velocity. Under Reduce Motion the release cross-fades after a touch drag or on a coarse primary pointer, and lands at once elsewhere. Docs pages and playing-card examples for both, the route lock and the sandbox's routes, and the core API report's rows for both. The plain-JavaScript example mounts its card from a callback ref on its wrapper, whose cleanup takes it down.
1 parent d007fc2 commit 83c69ca

25 files changed

Lines changed: 2469 additions & 0 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@astryxdesign/core': patch
3+
'@astryxdesign/cli': patch
4+
---
5+
6+
[feat] `createDragToSnapPoints` and its hook `useDragToSnapPoints` let a drag move an element along one axis and settle it on snap points (px offsets from its resting position) at the release: the nearest one, the next one in its direction after a flick faster than `flickThreshold` (default 1008 px/s), or the one it started from when the release moves back. The settle continues the release velocity, `settleTo(index)` settles without a drag, and `onSettle(index)` reports where it landed. `createDragToSnapPoints` is exported from the package root and `useDragToSnapPoints` from `@astryxdesign/core/hooks`; the CLI's templates gain a playing-card example of each.
7+
8+
@vjeux

‎apps/sandbox/scripts/next-export-routes.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@
294294
"/templates/DividerShowcase/",
295295
"/templates/DividerVariants/",
296296
"/templates/DividerVertical/",
297+
"/templates/DragToSnapPointsCard/",
298+
"/templates/DragToSnapPointsCardVanilla/",
297299
"/templates/DrawerForm/",
298300
"/templates/DrawerHeaderShowcase/",
299301
"/templates/DrawerHeaderSubtitleEndContent/",

‎internal/shadcn-registry/routes.lock.json‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,11 @@
19611961
"path": "examples/create-animator/playing-cards-in-plain-java-script",
19621962
"aliases": []
19631963
},
1964+
{
1965+
"name": "example-create-drag-to-snap-points-playing-card-in-plain-java-script",
1966+
"path": "examples/create-drag-to-snap-points/playing-card-in-plain-java-script",
1967+
"aliases": []
1968+
},
19641969
{
19651970
"name": "example-create-pan-gesture-a-playing-card-without-react",
19661971
"path": "examples/create-pan-gesture/a-playing-card-without-react",
@@ -3676,6 +3681,11 @@
36763681
"path": "examples/use-container-reveal/reveal-on-hover-row-actions",
36773682
"aliases": []
36783683
},
3684+
{
3685+
"name": "example-use-drag-to-snap-points-playing-card-on-three-places",
3686+
"path": "examples/use-drag-to-snap-points/playing-card-on-three-places",
3687+
"aliases": []
3688+
},
36793689
{
36803690
"name": "example-use-focus-trap-quick-edit-panel",
36813691
"path": "examples/use-focus-trap/quick-edit-panel",
@@ -3846,6 +3856,11 @@
38463856
"path": "hooks/create-animator",
38473857
"aliases": []
38483858
},
3859+
{
3860+
"name": "hook-create-drag-to-snap-points",
3861+
"path": "hooks/create-drag-to-snap-points",
3862+
"aliases": []
3863+
},
38493864
{
38503865
"name": "hook-create-pan-gesture",
38513866
"path": "hooks/create-pan-gesture",
@@ -3906,6 +3921,11 @@
39063921
"path": "hooks/use-dev-warning",
39073922
"aliases": []
39083923
},
3924+
{
3925+
"name": "hook-use-drag-to-snap-points",
3926+
"path": "hooks/use-drag-to-snap-points",
3927+
"aliases": []
3928+
},
39093929
{
39103930
"name": "hook-use-entry-animation",
39113931
"path": "hooks/use-entry-animation",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
4+
export const doc = {
5+
type: 'block',
6+
exampleFor: 'createDragToSnapPoints',
7+
name: 'createDragToSnapPoints — Playing card in plain JavaScript',
8+
displayName: 'createDragToSnapPoints — Playing card in plain JavaScript',
9+
description:
10+
'The three-place playing card from the useDragToSnapPoints example, built in plain JavaScript with createDragToSnapPoints: a sideways drag settles it on the nearest place or the next after a flick, buttons settle it from the keyboard, and a live region announces the place. React only mounts it.',
11+
isReady: true,
12+
aspectRatio: 4 / 3,
13+
componentsUsed: [],
14+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
import {createDragToSnapPoints} from '@astryxdesign/core';
6+
7+
const PLACES = ['Hand', 'Table', 'Discard pile'];
8+
9+
/**
10+
* A card that snaps between three places, in plain JavaScript: no React
11+
* below this line. It returns a function that takes it down again.
12+
*/
13+
function mountCard(table: HTMLElement, buttons: HTMLElement) {
14+
const card = document.createElement('div');
15+
card.textContent = 'Q\u2665';
16+
Object.assign(card.style, {
17+
width: '96px',
18+
height: '136px',
19+
display: 'grid',
20+
placeItems: 'center',
21+
font: 'bold 36px system-ui, sans-serif',
22+
// The theme's surface and text, so the card reads in light and dark.
23+
borderRadius: 'var(--radius-container)',
24+
backgroundColor: 'var(--color-background-surface)',
25+
color: 'var(--color-text-primary)',
26+
boxShadow: '0 2px 8px var(--color-shadow)',
27+
touchAction: 'none',
28+
cursor: 'grab',
29+
userSelect: 'none',
30+
});
31+
table.append(card);
32+
const status = document.createElement('div');
33+
status.setAttribute('aria-live', 'polite');
34+
Object.assign(status.style, {position: 'absolute', clip: 'rect(0 0 0 0)'});
35+
table.append(status);
36+
37+
// The places run in reading order, so leftward in a right-to-left layout.
38+
// There the snap points are negative, so they sort the other way round.
39+
const steps = [0, 110, 220];
40+
const isRtl = getComputedStyle(table).direction === 'rtl';
41+
const placeAt = (index: number) => (isRtl ? steps.length - 1 - index : index);
42+
const snap = createDragToSnapPoints(card, {
43+
axis: 'x',
44+
snapPoints: isRtl ? steps.map(step => -step) : steps,
45+
onSettle: index => {
46+
status.textContent = `The card is on the ${PLACES[placeAt(index)].toLowerCase()}.`;
47+
},
48+
});
49+
const controls = PLACES.map((name, index) => {
50+
const button = document.createElement('button');
51+
button.type = 'button';
52+
button.textContent = name;
53+
button.addEventListener('click', () => snap.settleTo(placeAt(index)));
54+
buttons.append(button);
55+
return button;
56+
});
57+
return () => {
58+
snap.destroy();
59+
for (const button of controls) {
60+
button.remove();
61+
}
62+
card.remove();
63+
status.remove();
64+
};
65+
}
66+
67+
/**
68+
* Mounts the card when the wrapper attaches, and takes it down when it
69+
* detaches: its cleanup is the callback ref's.
70+
*/
71+
function mountTable(wrapper: HTMLDivElement | null) {
72+
if (wrapper == null) {
73+
return undefined;
74+
}
75+
const table = wrapper.firstElementChild as HTMLElement;
76+
const buttons = wrapper.lastElementChild as HTMLElement;
77+
return mountCard(table, buttons);
78+
}
79+
80+
export default function DragToSnapPointsCardVanilla() {
81+
return (
82+
<div
83+
ref={mountTable}
84+
style={{display: 'grid', gap: 16, padding: 16, maxWidth: 380}}>
85+
<div
86+
style={{
87+
position: 'relative',
88+
height: 150,
89+
display: 'flex',
90+
alignItems: 'center',
91+
}}
92+
/>
93+
<div style={{display: 'flex', gap: 8}} />
94+
</div>
95+
);
96+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
4+
export const doc = {
5+
type: 'block',
6+
exampleFor: 'useDragToSnapPoints',
7+
name: 'useDragToSnapPoints — Playing card on three places',
8+
displayName: 'useDragToSnapPoints — Playing card on three places',
9+
description:
10+
'A playing card that a sideways drag moves between three places: a slow release settles on the nearest, a quick flick carries to the next, and a release moving back returns where it started. Buttons settle it from the keyboard, and the new place is announced. Built only on the public useDragToSnapPoints hook.',
11+
isReady: true,
12+
aspectRatio: 4 / 3,
13+
componentsUsed: [
14+
'Card',
15+
'Button',
16+
'Layout',
17+
'Text',
18+
'VisuallyHidden',
19+
'useDragToSnapPoints',
20+
],
21+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
import {useLayoutEffect, useRef, useState} from 'react';
6+
import {Button} from '@astryxdesign/core/Button';
7+
import {Card} from '@astryxdesign/core/Card';
8+
import {useDragToSnapPoints} from '@astryxdesign/core/hooks';
9+
import {HStack, VStack} from '@astryxdesign/core/Layout';
10+
import {Text} from '@astryxdesign/core/Text';
11+
import {VisuallyHidden} from '@astryxdesign/core/VisuallyHidden';
12+
13+
const PLACES = ['Hand', 'Table', 'Discard pile'];
14+
/** How far along the row each place sits, in px from where the card rests. */
15+
const STEPS = [0, 110, 220];
16+
17+
export default function DragToSnapPointsCard() {
18+
const ref = useRef<HTMLDivElement>(null);
19+
const [place, setPlace] = useState(0);
20+
// The places run in reading order, so leftward in a right-to-left layout.
21+
// There the snap points are negative, so they sort the other way round.
22+
// The direction is the one the card renders in.
23+
const [isRtl, setIsRtl] = useState(false);
24+
// Reads the direction the browser computed for the card once it is laid
25+
// out.
26+
useLayoutEffect(() => {
27+
if (ref.current != null) {
28+
setIsRtl(getComputedStyle(ref.current).direction === 'rtl');
29+
}
30+
}, []);
31+
const placeAt = (index: number) => (isRtl ? STEPS.length - 1 - index : index);
32+
const {settleTo} = useDragToSnapPoints(ref, {
33+
axis: 'x',
34+
snapPoints: isRtl ? STEPS.map(step => -step) : STEPS,
35+
onSettle: index => setPlace(placeAt(index)),
36+
});
37+
38+
return (
39+
<VStack gap={4} style={{maxWidth: 380, padding: 16}}>
40+
<Text type="body">
41+
Drag the card sideways and let go: it settles on the nearest place, or
42+
the next one after a quick flick.
43+
</Text>
44+
<HStack gap={2} style={{height: 150, alignItems: 'center'}}>
45+
<Card
46+
ref={ref}
47+
width={96}
48+
minHeight={136}
49+
style={{touchAction: 'none', cursor: 'grab', userSelect: 'none'}}
50+
aria-label={`Queen of hearts, on the ${PLACES[place].toLowerCase()}`}>
51+
<VStack gap={1} hAlign="center" style={{paddingBlock: 32}}>
52+
<Text type="display-2" weight="bold">
53+
Q
54+
</Text>
55+
<Text type="display-3">{'\u2665'}</Text>
56+
</VStack>
57+
</Card>
58+
</HStack>
59+
<HStack gap={2}>
60+
{PLACES.map((name, index) => (
61+
<Button
62+
key={name}
63+
label={name}
64+
variant={index === place ? 'primary' : 'secondary'}
65+
onClick={() => settleTo(placeAt(index))}
66+
/>
67+
))}
68+
</HStack>
69+
<VisuallyHidden as="div" aria-live="polite">
70+
{`The card is on the ${PLACES[place].toLowerCase()}.`}
71+
</VisuallyHidden>
72+
</VStack>
73+
);
74+
}

‎packages/cli/foundation/doc-compiler/inputs.test.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('collectDocInputs over what this repo ships', () => {
9898
'Resizable',
9999
'SyntaxTheme',
100100
'createAnimator',
101+
'createDragToSnapPoints',
101102
'createPanGesture',
102103
]);
103104
});

‎packages/core/api-report.md‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)