@astryxdesign/cli 0.7.9-canary.b2659fe → 0.7.9-canary.b779b26
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/api/theme/build/build.mjs +29 -1
- package/assets/docs/author-a-theme.doc.mjs +2 -0
- package/assets/docs/icons.doc.mjs +29 -0
- package/assets/templates/blocks/components/usePanGesture/PanDraggableBox.doc.mjs +15 -0
- package/assets/templates/blocks/components/usePanGesture/PanDraggableBox.tsx +98 -0
- package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.doc.mjs +4 -4
- package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.tsx +87 -83
- package/assets/theme.template.ts +9 -0
- package/authoring/doctypes/base/type.ts +41 -0
- package/authoring/doctypes/component/component.doc.mjs +7 -1
- package/authoring/doctypes/component/type.ts +5 -0
- package/authoring/index.d.ts +2 -0
- package/clients/cli/lib/component-format.mjs +49 -0
- package/package.json +9 -9
- package/assets/templates/blocks/components/createPanGesture/PanPlayingCardVanilla.doc.mjs +0 -14
- package/assets/templates/blocks/components/createPanGesture/PanPlayingCardVanilla.tsx +0 -156
|
@@ -2198,6 +2198,17 @@ function iconNodesExpression(entries, entriesName, nodesName) {
|
|
|
2198
2198
|
})()`;
|
|
2199
2199
|
}
|
|
2200
2200
|
|
|
2201
|
+
/**
|
|
2202
|
+
* A theme's component icon slot values: a theme from defineTheme keeps them
|
|
2203
|
+
* in `__componentIcons` when any is a function (its `componentIcons` is the
|
|
2204
|
+
* static view); otherwise `componentIcons` holds them.
|
|
2205
|
+
* @param {any} theme
|
|
2206
|
+
* @returns {Record<string, unknown> | undefined}
|
|
2207
|
+
*/
|
|
2208
|
+
function themeComponentIcons(theme) {
|
|
2209
|
+
return theme?.__componentIcons ?? theme?.componentIcons;
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2201
2212
|
/**
|
|
2202
2213
|
* Generate a minimal JS module for a built theme.
|
|
2203
2214
|
* Includes the theme name, marker, and re-exports the icon registry.
|
|
@@ -2224,6 +2235,9 @@ function iconNodesExpression(entries, entriesName, nodesName) {
|
|
|
2224
2235
|
* renders, and the theme's `icons` and the re-exported registry are the node
|
|
2225
2236
|
* view, each function read at `{size: 'md'}` as `DefinedTheme['icons']` is.
|
|
2226
2237
|
*
|
|
2238
|
+
* Component icon slot values are names, `null`, and requests, written as
|
|
2239
|
+
* JSON. A slot function cannot be serialized, so it fails the build.
|
|
2240
|
+
*
|
|
2227
2241
|
* @param {any} themeDef
|
|
2228
2242
|
* @param {import('./icon-imports.mjs').IconImports | null} iconInfo
|
|
2229
2243
|
* @param {string} [iconsSpecifier] - Overrides the selected registry specifier.
|
|
@@ -2295,6 +2309,19 @@ function generateBuiltModule(
|
|
|
2295
2309
|
: `\nexport { ${iconInfo.exportName} };\n`
|
|
2296
2310
|
: '';
|
|
2297
2311
|
|
|
2312
|
+
const componentIcons = themeComponentIcons(themeDef);
|
|
2313
|
+
const slotFunction = Object.entries(componentIcons ?? {}).find(
|
|
2314
|
+
([, value]) => typeof value === 'function',
|
|
2315
|
+
);
|
|
2316
|
+
if (slotFunction) {
|
|
2317
|
+
throw new AstryxError(
|
|
2318
|
+
`Theme "${themeDef.name}" sets componentIcons["${slotFunction[0]}"] to a slot function, which a built theme cannot carry. ` +
|
|
2319
|
+
'A built theme keeps names, null, and requests. Use one of those, or apply the source theme at runtime.',
|
|
2320
|
+
undefined,
|
|
2321
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
2322
|
+
);
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2298
2325
|
// Resolve token values — tuples become light-dark() strings
|
|
2299
2326
|
/** @type {Record<string, unknown>} */
|
|
2300
2327
|
const resolvedTokens = {};
|
|
@@ -2365,7 +2392,8 @@ function generateBuiltModule(
|
|
|
2365
2392
|
list ?? [],
|
|
2366
2393
|
]),
|
|
2367
2394
|
),
|
|
2368
|
-
)
|
|
2395
|
+
) +
|
|
2396
|
+
serializeField('componentIcons', componentIcons, true);
|
|
2369
2397
|
|
|
2370
2398
|
return `${iconImport}${iconDeclaration}${iconNodesDeclaration}/**
|
|
2371
2399
|
* ${themeDef.name} theme — built by \`${getCliInvocation()} theme build\`
|
|
@@ -56,6 +56,7 @@ export const docs = {
|
|
|
56
56
|
['components', 'Deep-merged: child component rules override matching keys from the base.'],
|
|
57
57
|
['icons', 'Shallow-merged: child icons override matching names from the base.'],
|
|
58
58
|
['iconVariants', 'Replaced, never merged: a child that sets iconVariants replaces the base list; a child that omits it inherits the list.'],
|
|
59
|
+
['componentIcons', 'Merged by slot: a child entry replaces the base entry for that slot whole; an omitted or undefined entry keeps the inherited one.'],
|
|
59
60
|
['indicators', 'Shallow-merged: child indicators override matching names from the base.'],
|
|
60
61
|
['onDark, onLight', "Deep-merged per surface: the base's resolved surface first, then the child's overrides."],
|
|
61
62
|
['motion', "Field by field: the child's `fast`, `medium` and `ratio` regenerate the fast and medium duration tokens; `slow` and `easing` left unset keep the base theme's tokens, so an inherited slow band stays at the base's ratio. Each knob (`tempo`, `character`) the child leaves unset keeps the base's."],
|
|
@@ -138,6 +139,7 @@ export const docs = {
|
|
|
138
139
|
{type: 'prose', text: 'Keep `react` and the icon library external so the registry does not bundle its own copies of those dependencies.'},
|
|
139
140
|
{type: 'prose', text: "A registry entry can be a function of `{size, variant}`. The built theme keeps the function, so `Icon` asks it for the size and variant it renders, as the runtime theme does, including for entries inherited from a built or package theme. The built theme's `icons` field and the registry its module re-exports are React nodes, with each function read at size `md`. A function written in the theme file itself cannot be carried, so the build fails; move it into the registry module."},
|
|
140
141
|
{type: 'prose', text: "A theme that lists `iconVariants` also writes an `IconVariantMap` augmentation into `<theme>.variants.d.ts`, so `Icon`'s `variant` accepts those names wherever the theme's types load. Its declaration carries the list too, so a theme that extends the built theme types `variant` in its icon entries by that list."},
|
|
142
|
+
{type: 'prose', text: "A built theme keeps its `componentIcons` names, `null` values, and static requests, so component slots resolve the same for the built theme as for the runtime theme. A slot function cannot be written into the module, so a theme with one fails the build: use a static value, or apply the source theme at runtime."},
|
|
141
143
|
],
|
|
142
144
|
},
|
|
143
145
|
{
|
|
@@ -214,5 +214,34 @@ export const brandTheme = defineTheme({
|
|
|
214
214
|
},
|
|
215
215
|
],
|
|
216
216
|
},
|
|
217
|
+
{
|
|
218
|
+
title: 'Component Icon Slots',
|
|
219
|
+
category: 'foundations',
|
|
220
|
+
content: [
|
|
221
|
+
{
|
|
222
|
+
type: 'prose',
|
|
223
|
+
text: "A component icon slot names why an icon appears in one component, such as `code-block-copy` for CodeBlock's copy button. `defineTheme({componentIcons})` maps a slot to one of the shared icon names. A theme can then change the icon one component uses without changing that name's artwork everywhere; `icons` still chooses the artwork for each name. Each component's docs list its slots, their state, and their fallbacks (`astryx component CodeBlock`).",
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
type: 'code',
|
|
227
|
+
lang: 'tsx',
|
|
228
|
+
label: 'Mapping a slot, with its state',
|
|
229
|
+
code: `export const brandTheme = defineTheme({
|
|
230
|
+
name: 'brand',
|
|
231
|
+
componentIcons: {
|
|
232
|
+
'code-block-copy': ({copied}) => ({icon: copied ? 'checkDouble' : 'copy'}),
|
|
233
|
+
},
|
|
234
|
+
});`,
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
type: 'prose',
|
|
238
|
+
text: "A slot value is a shared icon name, `null` to hide the icon, or a partial request of `icon`, `variant`, and `size`. A field the request leaves out keeps the component's own value. A slot with state also accepts a pure, synchronous function of that state that returns a request: returning `null` hides the icon, and returning `undefined` changes nothing. A request `size` changes only the icon, never the control around it. Icon content a component's caller passes wins over the slot.",
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
type: 'prose',
|
|
242
|
+
text: "An unknown icon name, a variant the theme does not list, or a size other than `xsm`, `sm`, `md`, or `lg` is ignored for that field, with one development warning. A theme that extends another merges `componentIcons` by slot ({@link generic:author-a-theme}). `theme.componentIcons` reads back names, `null` values, and static requests, and leaves out the slots a function resolves.",
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
},
|
|
217
246
|
],
|
|
218
247
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
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: 'usePanGesture',
|
|
7
|
+
alsoExampleFor: ['createPanGesture'],
|
|
8
|
+
name: 'usePanGesture — A draggable box',
|
|
9
|
+
displayName: 'usePanGesture — A draggable box',
|
|
10
|
+
description:
|
|
11
|
+
'The smallest pan example, in two forms side by side: the usePanGesture hook and the createPanGesture factory. The factory takes any element, React or not. Each card names its API, follows a drag from a finger, a mouse or a pen, and springs back, with the same StyleX styles. A starting point for a draggable tile, a swipe or any drag-and-release gesture.',
|
|
12
|
+
isReady: true,
|
|
13
|
+
aspectRatio: 4 / 3,
|
|
14
|
+
componentsUsed: ['Card', 'Center', 'HStack'],
|
|
15
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {useCallback, useRef, useState, type Ref} from 'react';
|
|
6
|
+
import * as stylex from '@stylexjs/stylex';
|
|
7
|
+
import {createPanGesture} from '@astryxdesign/core';
|
|
8
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
9
|
+
import {Center} from '@astryxdesign/core/Center';
|
|
10
|
+
import {HStack} from '@astryxdesign/core/HStack';
|
|
11
|
+
import {usePanGesture, type PanGestureEvent} from '@astryxdesign/core/hooks';
|
|
12
|
+
import {
|
|
13
|
+
colorVars,
|
|
14
|
+
durationVars,
|
|
15
|
+
easeVars,
|
|
16
|
+
radiusVars,
|
|
17
|
+
} from '@astryxdesign/core/theme/tokens.stylex';
|
|
18
|
+
|
|
19
|
+
const styles = stylex.create({
|
|
20
|
+
// A wash under the stage, so the box on top reads in light and dark themes.
|
|
21
|
+
stage: {
|
|
22
|
+
backgroundColor: colorVars['--color-neutral'],
|
|
23
|
+
borderRadius: radiusVars['--radius-container'],
|
|
24
|
+
},
|
|
25
|
+
box: {cursor: 'grab', userSelect: 'none'},
|
|
26
|
+
// Let go, the box springs back, unless the person reduces motion.
|
|
27
|
+
springBack: {
|
|
28
|
+
transition: {
|
|
29
|
+
default: `translate ${durationVars['--duration-medium']} ${easeVars['--ease-move']}`,
|
|
30
|
+
'@media (prefers-reduced-motion: reduce)': 'none',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
drag: (x: number, y: number) => ({translate: `${x}px ${y}px`}),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
type Drag = {x: number; y: number; held: boolean};
|
|
37
|
+
const REST: Drag = {x: 0, y: 0, held: false};
|
|
38
|
+
|
|
39
|
+
/** The box follows a held drag and goes back to rest when it ends. */
|
|
40
|
+
function nextDrag({state, translation}: PanGestureEvent): Drag {
|
|
41
|
+
const held = state === 'began' || state === 'changed';
|
|
42
|
+
return held ? {...translation, held} : REST;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The same stage and card for both forms; the card names its API. */
|
|
46
|
+
function DraggableBox(props: {
|
|
47
|
+
name: string;
|
|
48
|
+
ref: Ref<HTMLDivElement>;
|
|
49
|
+
drag: Drag;
|
|
50
|
+
}) {
|
|
51
|
+
const {name, ref, drag} = props;
|
|
52
|
+
return (
|
|
53
|
+
<Center width="min(280px, 80vw)" height={180} xstyle={styles.stage}>
|
|
54
|
+
<Card
|
|
55
|
+
ref={ref}
|
|
56
|
+
padding={6}
|
|
57
|
+
elevation="med"
|
|
58
|
+
xstyle={[
|
|
59
|
+
styles.box,
|
|
60
|
+
styles.drag(drag.x, drag.y),
|
|
61
|
+
!drag.held && styles.springBack,
|
|
62
|
+
]}>
|
|
63
|
+
{name}
|
|
64
|
+
</Card>
|
|
65
|
+
</Center>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function WithHook() {
|
|
70
|
+
const box = useRef<HTMLDivElement>(null);
|
|
71
|
+
const [drag, setDrag] = useState(REST);
|
|
72
|
+
usePanGesture(box, {onPan: event => setDrag(nextDrag(event))});
|
|
73
|
+
return <DraggableBox name="usePanGesture" ref={box} drag={drag} />;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function WithFactory() {
|
|
77
|
+
const [drag, setDrag] = useState(REST);
|
|
78
|
+
// A callback ref: its cleanup takes the pan down with the box.
|
|
79
|
+
const draggable = useCallback((box: HTMLDivElement | null) => {
|
|
80
|
+
if (box == null) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const pan = createPanGesture(box, {
|
|
84
|
+
onPan: event => setDrag(nextDrag(event)),
|
|
85
|
+
});
|
|
86
|
+
return () => pan.destroy();
|
|
87
|
+
}, []);
|
|
88
|
+
return <DraggableBox name="createPanGesture" ref={draggable} drag={drag} />;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default function PanDraggableBox() {
|
|
92
|
+
return (
|
|
93
|
+
<HStack gap={6} wrap="wrap" justify="center">
|
|
94
|
+
<WithHook />
|
|
95
|
+
<WithFactory />
|
|
96
|
+
</HStack>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
export const doc = {
|
|
5
5
|
type: 'block',
|
|
6
6
|
exampleFor: 'usePanGesture',
|
|
7
|
-
name: 'usePanGesture — A playing card',
|
|
8
|
-
displayName: 'usePanGesture — A playing card',
|
|
7
|
+
name: 'usePanGesture — A playing card (advanced)',
|
|
8
|
+
displayName: 'usePanGesture — A playing card (advanced)',
|
|
9
9
|
description:
|
|
10
|
-
'
|
|
10
|
+
'An advanced usePanGesture example: a playing card follows a drag, settles back on a gentle release and flies off on a quick flick, while a Discard button deals the next card from the keyboard and announces it. Drag, settle and throw are StyleX styles driven by React state. A pattern for swipe-to-dismiss, card decks and flick gestures.',
|
|
11
11
|
isReady: true,
|
|
12
12
|
aspectRatio: 4 / 3,
|
|
13
|
-
componentsUsed: [],
|
|
13
|
+
componentsUsed: ['Button', 'Card', 'Center', 'Text', 'VStack'],
|
|
14
14
|
};
|
|
@@ -3,8 +3,19 @@
|
|
|
3
3
|
'use client';
|
|
4
4
|
|
|
5
5
|
import {useRef, useState} from 'react';
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
6
|
+
import * as stylex from '@stylexjs/stylex';
|
|
7
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
8
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
9
|
+
import {Center} from '@astryxdesign/core/Center';
|
|
10
|
+
import {useAnnounce, usePanGesture} from '@astryxdesign/core/hooks';
|
|
11
|
+
import {Text} from '@astryxdesign/core/Text';
|
|
12
|
+
import {VStack} from '@astryxdesign/core/VStack';
|
|
13
|
+
import {
|
|
14
|
+
colorVars,
|
|
15
|
+
durationVars,
|
|
16
|
+
easeVars,
|
|
17
|
+
radiusVars,
|
|
18
|
+
} from '@astryxdesign/core/theme/tokens.stylex';
|
|
8
19
|
|
|
9
20
|
const DECK = [
|
|
10
21
|
{face: 'A\u2660', name: 'Ace of spades'},
|
|
@@ -14,32 +25,48 @@ const DECK = [
|
|
|
14
25
|
];
|
|
15
26
|
/** A release faster than this (px/s) throws the card away. */
|
|
16
27
|
const THROW_SPEED = 1000;
|
|
17
|
-
/** How far a thrown card travels
|
|
18
|
-
const THROW_DISTANCE =
|
|
19
|
-
const
|
|
20
|
-
|
|
28
|
+
/** How far a thrown card travels while it fades, px. */
|
|
29
|
+
const THROW_DISTANCE = 160;
|
|
30
|
+
const EXIT = `${durationVars['--duration-exit']} ${easeVars['--ease-exit']}`;
|
|
31
|
+
|
|
32
|
+
const styles = stylex.create({
|
|
33
|
+
// A wash under the stage, so the card on top reads in light and dark themes.
|
|
34
|
+
stage: {
|
|
35
|
+
backgroundColor: colorVars['--color-neutral'],
|
|
36
|
+
borderRadius: radiusVars['--radius-container'],
|
|
37
|
+
overflow: 'hidden',
|
|
38
|
+
},
|
|
39
|
+
card: {cursor: 'grab', userSelect: 'none'},
|
|
40
|
+
// A gentle release settles the card back, unless the person reduces motion.
|
|
41
|
+
settle: {
|
|
42
|
+
transition: {
|
|
43
|
+
default: `translate ${durationVars['--duration-medium']} ${easeVars['--ease-move']}`,
|
|
44
|
+
'@media (prefers-reduced-motion: reduce)': 'none',
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
// A flick throws it off to one side as it fades.
|
|
48
|
+
thrown: {
|
|
49
|
+
opacity: 0,
|
|
50
|
+
transition: {
|
|
51
|
+
default: `translate ${EXIT}, opacity ${EXIT}`,
|
|
52
|
+
'@media (prefers-reduced-motion: reduce)': 'none',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
drag: (x: number, y: number) => ({translate: `${x}px ${y}px`}),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
type Pose = {x: number; y: number; motion: 'rest' | 'settle' | 'thrown'};
|
|
21
59
|
|
|
22
60
|
export default function PanPlayingCard() {
|
|
23
61
|
const card = useRef<HTMLDivElement>(null);
|
|
24
62
|
const [top, setTop] = useState(0);
|
|
25
|
-
const [
|
|
63
|
+
const [pose, setPose] = useState<Pose>({x: 0, y: 0, motion: 'rest'});
|
|
64
|
+
const announce = useAnnounce();
|
|
26
65
|
/** A throw is in flight: the next card is dealt when it ends. */
|
|
27
66
|
const throwing = useRef(false);
|
|
28
67
|
/** The drag in progress met a throw: the rest of it moves nothing. */
|
|
29
68
|
const ignoringDrag = useRef(false);
|
|
30
|
-
|
|
31
|
-
/** Move the card, with a transition unless the person reduces motion. */
|
|
32
|
-
function moveTo(x: number, y: number, transition: string) {
|
|
33
|
-
const element = card.current;
|
|
34
|
-
if (element == null) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const reduced = window.matchMedia(
|
|
38
|
-
'(prefers-reduced-motion: reduce)',
|
|
39
|
-
).matches;
|
|
40
|
-
element.style.transition = reduced ? 'none' : transition;
|
|
41
|
-
element.style.translate = `${x}px ${y}px`;
|
|
42
|
-
}
|
|
69
|
+
const shown = DECK[top % DECK.length];
|
|
43
70
|
|
|
44
71
|
function discard(direction: number) {
|
|
45
72
|
const element = card.current;
|
|
@@ -48,33 +75,31 @@ export default function PanPlayingCard() {
|
|
|
48
75
|
return;
|
|
49
76
|
}
|
|
50
77
|
throwing.current = true;
|
|
51
|
-
// The drag in progress, if any, moves nothing more.
|
|
52
78
|
ignoringDrag.current = true;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
79
|
+
setPose({x: direction * THROW_DISTANCE, y: 0, motion: 'thrown'});
|
|
80
|
+
// The next card's face and its place at the center commit together.
|
|
81
|
+
const dealNext = () => {
|
|
56
82
|
throwing.current = false;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
setAnnouncement(`${DECK[top % DECK.length].name} discarded`);
|
|
61
|
-
setTop(value => value + 1);
|
|
62
|
-
});
|
|
63
|
-
moveTo(0, 0, 'none');
|
|
64
|
-
element.style.opacity = '1';
|
|
83
|
+
announce(`${shown.name} discarded`);
|
|
84
|
+
setTop(value => value + 1);
|
|
85
|
+
setPose({x: 0, y: 0, motion: 'rest'});
|
|
65
86
|
};
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
element.addEventListener('transitionend', deal, {once: true});
|
|
87
|
+
if (matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
88
|
+
dealNext();
|
|
89
|
+
return;
|
|
70
90
|
}
|
|
91
|
+
// Once the throw's style applies, deal when its transitions settle: at
|
|
92
|
+
// the theme's duration, or at once when a stylesheet stops them.
|
|
93
|
+
requestAnimationFrame(() => {
|
|
94
|
+
const ends = element.getAnimations().map(animation => animation.finished);
|
|
95
|
+
void Promise.allSettled(ends).then(dealNext);
|
|
96
|
+
});
|
|
71
97
|
}
|
|
72
98
|
|
|
73
|
-
// The drag writes the card's style in onPan: no render per move.
|
|
74
99
|
usePanGesture(card, {
|
|
75
100
|
onPan: ({state, translation, velocity}) => {
|
|
76
|
-
// A drag that meets a throw is ignored to its end
|
|
77
|
-
//
|
|
101
|
+
// A drag that meets a throw is ignored to its end, or the next card
|
|
102
|
+
// would jump to it.
|
|
78
103
|
if (state === 'began') {
|
|
79
104
|
ignoringDrag.current = throwing.current;
|
|
80
105
|
}
|
|
@@ -82,62 +107,41 @@ export default function PanPlayingCard() {
|
|
|
82
107
|
return;
|
|
83
108
|
}
|
|
84
109
|
if (state === 'began' || state === 'changed') {
|
|
85
|
-
|
|
110
|
+
setPose({...translation, motion: 'rest'});
|
|
86
111
|
} else if (state === 'ended' && Math.abs(velocity.x) > THROW_SPEED) {
|
|
87
112
|
discard(Math.sign(velocity.x));
|
|
88
113
|
} else {
|
|
89
|
-
|
|
114
|
+
setPose({x: 0, y: 0, motion: 'settle'});
|
|
90
115
|
}
|
|
91
116
|
},
|
|
92
117
|
});
|
|
93
118
|
|
|
94
119
|
return (
|
|
95
|
-
<
|
|
96
|
-
<
|
|
97
|
-
|
|
98
|
-
height: 200,
|
|
99
|
-
display: 'grid',
|
|
100
|
-
placeItems: 'center',
|
|
101
|
-
overflow: 'hidden',
|
|
102
|
-
}}>
|
|
103
|
-
<div
|
|
120
|
+
<VStack gap={4} padding={4} width="min(360px, 80vw)">
|
|
121
|
+
<Center height={280} xstyle={styles.stage}>
|
|
122
|
+
<Card
|
|
104
123
|
ref={card}
|
|
105
124
|
// An image's name is all a screen reader announces, so it names the
|
|
106
125
|
// card.
|
|
107
126
|
role="img"
|
|
108
|
-
aria-label={
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
</div>
|
|
127
|
+
aria-label={shown.name}
|
|
128
|
+
width={80}
|
|
129
|
+
height={112}
|
|
130
|
+
elevation="med"
|
|
131
|
+
xstyle={[
|
|
132
|
+
styles.card,
|
|
133
|
+
styles.drag(pose.x, pose.y),
|
|
134
|
+
pose.motion !== 'rest' && styles[pose.motion],
|
|
135
|
+
]}>
|
|
136
|
+
<Center height="100%">
|
|
137
|
+
<Text size="3xl" weight="bold">
|
|
138
|
+
{shown.face}
|
|
139
|
+
</Text>
|
|
140
|
+
</Center>
|
|
141
|
+
</Card>
|
|
142
|
+
</Center>
|
|
125
143
|
{/* The drag's keyboard and assistive-technology path (WCAG 2.5.7). */}
|
|
126
|
-
<
|
|
127
|
-
|
|
128
|
-
</button>
|
|
129
|
-
<div
|
|
130
|
-
aria-live="polite"
|
|
131
|
-
style={{
|
|
132
|
-
position: 'absolute',
|
|
133
|
-
width: 1,
|
|
134
|
-
height: 1,
|
|
135
|
-
overflow: 'hidden',
|
|
136
|
-
clipPath: 'inset(50%)',
|
|
137
|
-
whiteSpace: 'nowrap',
|
|
138
|
-
}}>
|
|
139
|
-
{announcement}
|
|
140
|
-
</div>
|
|
141
|
-
</div>
|
|
144
|
+
<Button label="Discard the top card" onClick={() => discard(1)} />
|
|
145
|
+
</VStack>
|
|
142
146
|
);
|
|
143
147
|
}
|
package/assets/theme.template.ts
CHANGED
|
@@ -346,6 +346,15 @@ export const myTheme = defineTheme({
|
|
|
346
346
|
*/
|
|
347
347
|
// iconVariants: ['outline', 'filled'],
|
|
348
348
|
|
|
349
|
+
/**
|
|
350
|
+
* Choose which shared icon a component's own icon role uses, by slot, such
|
|
351
|
+
* as `code-block-copy` for CodeBlock's copy button. A value is an icon name,
|
|
352
|
+
* `null` to hide the icon, a partial `{icon, variant, size}` request, or a
|
|
353
|
+
* pure function of the slot's state that returns one.
|
|
354
|
+
* Reference: `astryx docs icons`; each component's docs list its slots.
|
|
355
|
+
*/
|
|
356
|
+
// componentIcons: {'code-block-copy': 'clock'},
|
|
357
|
+
|
|
349
358
|
/**
|
|
350
359
|
* Replace the small components that DRAW control state — the checkbox box,
|
|
351
360
|
* the radio dot, the mark on a chosen option — by indicator name rather than
|
|
@@ -549,6 +549,47 @@ export interface ComponentThemingVar {
|
|
|
549
549
|
private?: boolean;
|
|
550
550
|
}
|
|
551
551
|
|
|
552
|
+
/**
|
|
553
|
+
* A component icon slot: a themeable icon role the component owns, which a
|
|
554
|
+
* theme maps to a shared icon name through `defineTheme({componentIcons})`.
|
|
555
|
+
* Each entry corresponds to a slot in `ComponentIconSlotMap`.
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* ```
|
|
559
|
+
* {
|
|
560
|
+
* slot: 'code-block-copy',
|
|
561
|
+
* description: 'The copy button icon.',
|
|
562
|
+
* fallback: '`copy`; `check` while copied',
|
|
563
|
+
* state: [{name: 'copied', type: 'boolean', description: 'True while the copied feedback shows.'}],
|
|
564
|
+
* whenNull: 'Hides the glyph. The button keeps its size, name, and tooltip.',
|
|
565
|
+
* }
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
// SYNC: When adding a field here, add it to the docsite's generated-registry
|
|
569
|
+
// copy too — apps/docsite/scripts/generate-data.mjs (interface ComponentIconSlot).
|
|
570
|
+
export interface ComponentIconSlotDoc {
|
|
571
|
+
/** The slot name, `<component-kebab>-<semantic-role>`. */
|
|
572
|
+
slot: string;
|
|
573
|
+
/** The icon's role in the component. */
|
|
574
|
+
description: string;
|
|
575
|
+
/** The icon the component renders when the theme maps nothing, per state. */
|
|
576
|
+
fallback: string;
|
|
577
|
+
/** The state a slot function receives. Omit for a stateless slot. */
|
|
578
|
+
state?: ComponentIconSlotStateDoc[];
|
|
579
|
+
/** What a slot value of `null` does for this component. */
|
|
580
|
+
whenNull: string;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** One field of the state a component passes to a slot function. */
|
|
584
|
+
export interface ComponentIconSlotStateDoc {
|
|
585
|
+
/** The field name. */
|
|
586
|
+
name: string;
|
|
587
|
+
/** The field's TypeScript type. */
|
|
588
|
+
type: string;
|
|
589
|
+
/** When the field has each value. */
|
|
590
|
+
description: string;
|
|
591
|
+
}
|
|
592
|
+
|
|
552
593
|
/**
|
|
553
594
|
* Documents a hook parameter/option. Similar to ComponentPropDoc but for hook
|
|
554
595
|
* arguments and options object fields.
|
|
@@ -102,7 +102,7 @@ export const doc = {
|
|
|
102
102
|
},
|
|
103
103
|
{
|
|
104
104
|
name: 'theming',
|
|
105
|
-
type: '{ container?: boolean; targets: ComponentThemingTarget[]; vars?: ComponentThemingVar[]; derived?: ComponentThemingDerivedVar[] }',
|
|
105
|
+
type: '{ container?: boolean; targets: ComponentThemingTarget[]; vars?: ComponentThemingVar[]; derived?: ComponentThemingDerivedVar[]; componentIcons?: ComponentIconSlotDoc[] }',
|
|
106
106
|
description:
|
|
107
107
|
'Theming configuration: the stable selector surface (astryx-* classes + data-attribute reflections) that themes target via @scope selectors in defineTheme.',
|
|
108
108
|
fields: [
|
|
@@ -130,6 +130,12 @@ export const doc = {
|
|
|
130
130
|
description:
|
|
131
131
|
"Maps standard CSS properties to internal vars for theme-pipeline expansion. Ordered by priority: earlier entries emit first. The pipeline reads only Core components' entries, so an integration's have no effect.",
|
|
132
132
|
},
|
|
133
|
+
{
|
|
134
|
+
name: 'theming.componentIcons',
|
|
135
|
+
type: 'ComponentIconSlotDoc[]',
|
|
136
|
+
description:
|
|
137
|
+
"The component's icon slots, which a theme maps through `defineTheme({componentIcons})`: each slot's name, role, fallback per state, the state a slot function receives, and what `null` does. Each entry corresponds to a slot in `ComponentIconSlotMap`.",
|
|
138
|
+
},
|
|
133
139
|
],
|
|
134
140
|
},
|
|
135
141
|
{
|
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
ComponentAnatomyElement,
|
|
11
11
|
ComponentBestPractice,
|
|
12
12
|
ComponentExampleDoc,
|
|
13
|
+
ComponentIconSlotDoc,
|
|
13
14
|
ComponentPlaygroundConfig,
|
|
14
15
|
ComponentPropDoc,
|
|
15
16
|
ComponentThemingDerivedVar,
|
|
@@ -162,6 +163,10 @@ export interface ComponentBaseDoc extends AuthoredDocGraphFields {
|
|
|
162
163
|
* on this component, also emit the internal var.
|
|
163
164
|
* @see ComponentThemingDerivedVar */
|
|
164
165
|
derived?: ComponentThemingDerivedVar[];
|
|
166
|
+
/** Component icon slots this component resolves through the theme's
|
|
167
|
+
* `componentIcons`. Each entry corresponds to a slot in
|
|
168
|
+
* `ComponentIconSlotMap`. */
|
|
169
|
+
componentIcons?: ComponentIconSlotDoc[];
|
|
165
170
|
};
|
|
166
171
|
/** Component usage documentation — concise summary, best practices,
|
|
167
172
|
* and optional visual anatomy. */
|
package/authoring/index.d.ts
CHANGED
|
@@ -163,6 +163,34 @@ function getTargetStates(target) {
|
|
|
163
163
|
return target.states || [];
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Format a component's icon slots: the `componentIcons` keys a theme sets,
|
|
168
|
+
* with each slot's state, fallback, and what `null` does.
|
|
169
|
+
* @param {any[]} slots - `theming.componentIcons` entries.
|
|
170
|
+
* @returns {string}
|
|
171
|
+
*/
|
|
172
|
+
function formatComponentIconSlots(slots) {
|
|
173
|
+
const lines = [
|
|
174
|
+
'**Component icon slots** - set in `defineTheme({componentIcons})` to a shared icon name, `null`, a partial `{icon, variant, size}` request, or a function of the slot state.\n',
|
|
175
|
+
'| Slot | State | Fallback | Role | `null` |',
|
|
176
|
+
'|------|-------|----------|------|--------|',
|
|
177
|
+
];
|
|
178
|
+
for (const slot of slots) {
|
|
179
|
+
const state = slot.state?.length
|
|
180
|
+
? slot.state
|
|
181
|
+
.map(
|
|
182
|
+
(/** @type {any} */ field) =>
|
|
183
|
+
`\`${mdCell(field.name)}: ${mdCell(field.type)}\` ${mdCell(field.description)}`,
|
|
184
|
+
)
|
|
185
|
+
.join('<br>')
|
|
186
|
+
: 'none';
|
|
187
|
+
lines.push(
|
|
188
|
+
`| \`${mdCell(slot.slot)}\` | ${state} | ${mdCell(slot.fallback)} | ${mdCell(slot.description)} | ${mdCell(slot.whenNull)} |`,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return lines.join('\n') + '\n';
|
|
192
|
+
}
|
|
193
|
+
|
|
166
194
|
/**
|
|
167
195
|
* Format the theming targets table, merging in theme variants if available.
|
|
168
196
|
*
|
|
@@ -351,6 +379,10 @@ export function formatFull(docs, options = {}) {
|
|
|
351
379
|
}
|
|
352
380
|
}
|
|
353
381
|
|
|
382
|
+
if (docs.theming.componentIcons?.length) {
|
|
383
|
+
sections.push(formatComponentIconSlots(docs.theming.componentIcons));
|
|
384
|
+
}
|
|
385
|
+
|
|
354
386
|
// Legacy componentKey (for backward compatibility)
|
|
355
387
|
if (docs.theming.componentKey) {
|
|
356
388
|
sections.push(`Component key: \`${docs.theming.componentKey}\`\n`);
|
|
@@ -507,6 +539,11 @@ export function formatCompact(docs, componentName, importHint) {
|
|
|
507
539
|
sections.push(propLines.join('\n') + '\n');
|
|
508
540
|
}
|
|
509
541
|
|
|
542
|
+
if (docs.theming?.componentIcons?.length) {
|
|
543
|
+
sections.push('## Component Icon Slots\n');
|
|
544
|
+
sections.push(formatComponentIconSlots(docs.theming.componentIcons));
|
|
545
|
+
}
|
|
546
|
+
|
|
510
547
|
return sections.join('\n');
|
|
511
548
|
}
|
|
512
549
|
|
|
@@ -626,6 +663,18 @@ export function formatBrief(docs, componentName, importHint, options = {}) {
|
|
|
626
663
|
output.push(` Derived: ${derivedNames}`);
|
|
627
664
|
}
|
|
628
665
|
|
|
666
|
+
// Component icon slots, with the state each slot function receives
|
|
667
|
+
if (docs.theming?.componentIcons?.length) {
|
|
668
|
+
const slots = docs.theming.componentIcons
|
|
669
|
+
.map((/** @type {any} */ slot) =>
|
|
670
|
+
slot.state?.length
|
|
671
|
+
? `${slot.slot} {${slot.state.map((/** @type {any} */ field) => `${field.name}: ${field.type}`).join(', ')}}`
|
|
672
|
+
: slot.slot,
|
|
673
|
+
)
|
|
674
|
+
.join('; ');
|
|
675
|
+
output.push(` Icon slots: ${slots}`);
|
|
676
|
+
}
|
|
677
|
+
|
|
629
678
|
// Theme targets (component class, preferred data attrs, props, states) with theme variant merging
|
|
630
679
|
if (docs.theming?.targets?.length) {
|
|
631
680
|
const {themeData = null} = options;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/cli",
|
|
3
|
-
"version": "0.7.9-canary.
|
|
3
|
+
"version": "0.7.9-canary.b779b26",
|
|
4
4
|
"displayName": "CLI",
|
|
5
5
|
"description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
@@ -124,10 +124,10 @@
|
|
|
124
124
|
"zod": "^4.4.3"
|
|
125
125
|
},
|
|
126
126
|
"peerDependencies": {
|
|
127
|
-
"@astryxdesign/charts": "0.7.9-canary.
|
|
128
|
-
"@astryxdesign/core": "0.7.9-canary.
|
|
129
|
-
"@astryxdesign/lab": "0.7.9-canary.
|
|
130
|
-
"@astryxdesign/theme-neutral": "0.7.9-canary.
|
|
127
|
+
"@astryxdesign/charts": "0.7.9-canary.b779b26",
|
|
128
|
+
"@astryxdesign/core": "0.7.9-canary.b779b26",
|
|
129
|
+
"@astryxdesign/lab": "0.7.9-canary.b779b26",
|
|
130
|
+
"@astryxdesign/theme-neutral": "0.7.9-canary.b779b26"
|
|
131
131
|
},
|
|
132
132
|
"peerDependenciesMeta": {
|
|
133
133
|
"@astryxdesign/charts": {
|
|
@@ -144,10 +144,10 @@
|
|
|
144
144
|
}
|
|
145
145
|
},
|
|
146
146
|
"devDependencies": {
|
|
147
|
-
"@astryxdesign/charts": "0.7.9-canary.
|
|
148
|
-
"@astryxdesign/core": "0.7.9-canary.
|
|
149
|
-
"@astryxdesign/lab": "0.7.9-canary.
|
|
150
|
-
"@astryxdesign/theme-neutral": "0.7.9-canary.
|
|
147
|
+
"@astryxdesign/charts": "0.7.9-canary.b779b26",
|
|
148
|
+
"@astryxdesign/core": "0.7.9-canary.b779b26",
|
|
149
|
+
"@astryxdesign/lab": "0.7.9-canary.b779b26",
|
|
150
|
+
"@astryxdesign/theme-neutral": "0.7.9-canary.b779b26",
|
|
151
151
|
"@heroicons/react": "^2.2.0",
|
|
152
152
|
"@stylexjs/stylex": "^0.19.0",
|
|
153
153
|
"@types/babel__core": "^7.20.5",
|
|
@@ -1,14 +0,0 @@
|
|
|
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: 'createPanGesture',
|
|
7
|
-
name: 'createPanGesture — A playing card without React',
|
|
8
|
-
displayName: 'createPanGesture — A playing card without React',
|
|
9
|
-
description:
|
|
10
|
-
'A playing card built in plain JavaScript with createPanGesture: it follows a drag from a finger, a mouse or a pen, snaps back on a gentle release, flies off on a quick flick, and a button discards it from the keyboard. React only mounts it.',
|
|
11
|
-
isReady: true,
|
|
12
|
-
aspectRatio: 4 / 3,
|
|
13
|
-
componentsUsed: [],
|
|
14
|
-
};
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
-
|
|
3
|
-
'use client';
|
|
4
|
-
|
|
5
|
-
import {createPanGesture} from '@astryxdesign/core';
|
|
6
|
-
|
|
7
|
-
const DECK = [
|
|
8
|
-
{face: 'A\u2660', name: 'Ace of spades'},
|
|
9
|
-
{face: 'K\u2665', name: 'King of hearts'},
|
|
10
|
-
{face: 'Q\u2663', name: 'Queen of clubs'},
|
|
11
|
-
{face: 'J\u2666', name: 'Jack of diamonds'},
|
|
12
|
-
];
|
|
13
|
-
/** A release faster than this (px/s) throws the card away. */
|
|
14
|
-
const THROW_SPEED = 1000;
|
|
15
|
-
/** How far a thrown card travels before the next one is dealt, px. */
|
|
16
|
-
const THROW_DISTANCE = 480;
|
|
17
|
-
const SETTLE = 'translate 300ms cubic-bezier(0.2, 0.9, 0.3, 1.2)';
|
|
18
|
-
const THROW = 'translate 250ms ease-in, opacity 250ms ease-in';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A deck on a table, in plain JavaScript: no React below this line. The pan
|
|
22
|
-
* reports the drag; the card's own style moves it. Returns a function that
|
|
23
|
-
* takes it down again.
|
|
24
|
-
*/
|
|
25
|
-
function mountDeck(table: HTMLElement, discardButton: HTMLButtonElement) {
|
|
26
|
-
let top = 0;
|
|
27
|
-
/** A throw is in flight: the next card is dealt when it ends. */
|
|
28
|
-
let isThrowing = false;
|
|
29
|
-
/** The drag in progress met a throw: the rest of it moves nothing. */
|
|
30
|
-
let isIgnoringDrag = false;
|
|
31
|
-
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
32
|
-
const card = document.createElement('div');
|
|
33
|
-
card.textContent = DECK[0].face;
|
|
34
|
-
// An image's name is all a screen reader announces, so it names the card.
|
|
35
|
-
card.setAttribute('role', 'img');
|
|
36
|
-
card.setAttribute('aria-label', DECK[0].name);
|
|
37
|
-
Object.assign(card.style, {
|
|
38
|
-
width: '120px',
|
|
39
|
-
height: '170px',
|
|
40
|
-
display: 'grid',
|
|
41
|
-
placeItems: 'center',
|
|
42
|
-
font: 'bold 40px system-ui, sans-serif',
|
|
43
|
-
borderRadius: '12px',
|
|
44
|
-
backgroundColor: 'var(--color-background-surface)',
|
|
45
|
-
color: 'var(--color-text-primary)',
|
|
46
|
-
boxShadow: '0 2px 8px rgb(0 0 0 / 0.2)',
|
|
47
|
-
cursor: 'grab',
|
|
48
|
-
userSelect: 'none',
|
|
49
|
-
});
|
|
50
|
-
// Announces each discard to screen readers.
|
|
51
|
-
const status = document.createElement('div');
|
|
52
|
-
status.setAttribute('aria-live', 'polite');
|
|
53
|
-
Object.assign(status.style, {
|
|
54
|
-
position: 'absolute',
|
|
55
|
-
width: '1px',
|
|
56
|
-
height: '1px',
|
|
57
|
-
overflow: 'hidden',
|
|
58
|
-
clipPath: 'inset(50%)',
|
|
59
|
-
whiteSpace: 'nowrap',
|
|
60
|
-
});
|
|
61
|
-
table.append(card, status);
|
|
62
|
-
|
|
63
|
-
/** Move the card, with a transition unless the person reduces motion. */
|
|
64
|
-
function moveTo(x: number, y: number, transition: string) {
|
|
65
|
-
card.style.transition = reduceMotion.matches ? 'none' : transition;
|
|
66
|
-
card.style.translate = `${x}px ${y}px`;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function discard(direction: number) {
|
|
70
|
-
// One throw at a time: a second Discard during it would deal twice.
|
|
71
|
-
if (isThrowing) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
isThrowing = true;
|
|
75
|
-
// The drag in progress, if any, moves nothing more.
|
|
76
|
-
isIgnoringDrag = true;
|
|
77
|
-
moveTo(direction * THROW_DISTANCE, 0, THROW);
|
|
78
|
-
card.style.opacity = '0';
|
|
79
|
-
const deal = () => {
|
|
80
|
-
isThrowing = false;
|
|
81
|
-
status.textContent = `${DECK[top % DECK.length].name} discarded`;
|
|
82
|
-
top += 1;
|
|
83
|
-
card.textContent = DECK[top % DECK.length].face;
|
|
84
|
-
card.setAttribute('aria-label', DECK[top % DECK.length].name);
|
|
85
|
-
moveTo(0, 0, 'none');
|
|
86
|
-
card.style.opacity = '1';
|
|
87
|
-
};
|
|
88
|
-
if (reduceMotion.matches) {
|
|
89
|
-
deal();
|
|
90
|
-
} else {
|
|
91
|
-
card.addEventListener('transitionend', deal, {once: true});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const pan = createPanGesture(card, {
|
|
96
|
-
onPan: ({state, translation, velocity}) => {
|
|
97
|
-
// A drag that meets a throw is ignored to its end. Its moves would
|
|
98
|
-
// cancel the throw's transitions, and the next card would jump to it.
|
|
99
|
-
if (state === 'began') {
|
|
100
|
-
isIgnoringDrag = isThrowing;
|
|
101
|
-
}
|
|
102
|
-
if (isIgnoringDrag) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
if (state === 'began' || state === 'changed') {
|
|
106
|
-
// Follows the finger: translation leaves out the travel the drag
|
|
107
|
-
// needed to begin, so the card does not jump.
|
|
108
|
-
moveTo(translation.x, translation.y, 'none');
|
|
109
|
-
} else if (state === 'ended' && Math.abs(velocity.x) > THROW_SPEED) {
|
|
110
|
-
discard(Math.sign(velocity.x));
|
|
111
|
-
} else {
|
|
112
|
-
moveTo(0, 0, SETTLE);
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
});
|
|
116
|
-
// The drag's keyboard and assistive-technology path (WCAG 2.5.7).
|
|
117
|
-
const onDiscard = () => discard(1);
|
|
118
|
-
discardButton.addEventListener('click', onDiscard);
|
|
119
|
-
return () => {
|
|
120
|
-
discardButton.removeEventListener('click', onDiscard);
|
|
121
|
-
pan.destroy();
|
|
122
|
-
card.remove();
|
|
123
|
-
status.remove();
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Mounts the deck when the wrapper attaches, and takes it down when it
|
|
129
|
-
* detaches: its cleanup is the callback ref's.
|
|
130
|
-
*/
|
|
131
|
-
function mountTable(wrapper: HTMLDivElement | null) {
|
|
132
|
-
if (wrapper == null) {
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
const table = wrapper.firstElementChild as HTMLElement;
|
|
136
|
-
const button = wrapper.querySelector('button') as HTMLButtonElement;
|
|
137
|
-
return mountDeck(table, button);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export default function PanPlayingCardVanilla() {
|
|
141
|
-
return (
|
|
142
|
-
<div
|
|
143
|
-
ref={mountTable}
|
|
144
|
-
style={{display: 'grid', gap: 16, padding: 16, maxWidth: 360}}>
|
|
145
|
-
<div
|
|
146
|
-
style={{
|
|
147
|
-
height: 200,
|
|
148
|
-
display: 'grid',
|
|
149
|
-
placeItems: 'center',
|
|
150
|
-
overflow: 'hidden',
|
|
151
|
-
}}
|
|
152
|
-
/>
|
|
153
|
-
<button type="button">Discard the top card</button>
|
|
154
|
-
</div>
|
|
155
|
-
);
|
|
156
|
-
}
|