@astryxdesign/cli 0.7.9-canary.ffa495b → 0.7.10-canary.010db3f

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.
Files changed (91) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +1 -1
  3. package/api/component/__fixtures__/resolutions/modules.txt +5 -6
  4. package/api/search/_candidate-score.mjs +25 -23
  5. package/api/search/_gather-components.mjs +2 -1
  6. package/api/search/_words.d.mts +44 -7
  7. package/api/search/_words.mjs +113 -21
  8. package/api/swizzle/_adapter.d.mts +10 -0
  9. package/api/swizzle/_adapter.mjs +24 -1
  10. package/api/swizzle/copy/copy.d.mts +17 -1
  11. package/api/swizzle/copy/copy.mjs +249 -9
  12. package/api/swizzle/swizzle.doc.mjs +1 -1
  13. package/api/swizzle/swizzle.type.mjs +1 -1
  14. package/api/theme/build/build.mjs +363 -73
  15. package/api/theme/build/core-interception.mjs +92 -29
  16. package/api/theme/build/icon-imports.d.mts +35 -4
  17. package/api/theme/build/icon-imports.mjs +299 -38
  18. package/assets/discover/official-packages.json +16 -0
  19. package/assets/docs/author-a-theme.doc.mjs +3 -1
  20. package/assets/docs/getting-started.doc.mjs +20 -0
  21. package/assets/docs/icons.doc.mjs +29 -0
  22. package/assets/docs/motion.doc.mjs +1 -1
  23. package/assets/docs/tree/calendar-systems.doc.mjs +146 -0
  24. package/assets/docs/tree/calendars.doc.mjs +46 -0
  25. package/assets/docs/tree/i18n-calendars.doc.mjs +51 -0
  26. package/assets/docs/tree/internationalization.doc.mjs +3 -3
  27. package/assets/templates/blocks/components/Calendar/useCalendarDaysHookUsage.doc.mjs +14 -0
  28. package/assets/templates/blocks/components/Calendar/useCalendarDaysHookUsage.tsx +71 -0
  29. package/assets/templates/blocks/components/Carousel/CarouselSnapPaging.doc.mjs +14 -0
  30. package/assets/templates/blocks/components/Carousel/CarouselSnapPaging.tsx +49 -0
  31. package/assets/templates/blocks/components/Hooks/useInputContainerHookUsage.doc.mjs +15 -0
  32. package/assets/templates/blocks/components/Hooks/useInputContainerHookUsage.tsx +93 -0
  33. package/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.doc.mjs +14 -0
  34. package/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.tsx +78 -0
  35. package/assets/templates/blocks/components/Hooks/useScrollLockHookUsage.doc.mjs +14 -0
  36. package/assets/templates/blocks/components/Hooks/useScrollLockHookUsage.tsx +80 -0
  37. package/assets/templates/blocks/components/Hooks/useScrollOverflowHookUsage.doc.mjs +14 -0
  38. package/assets/templates/blocks/components/Hooks/useScrollOverflowHookUsage.tsx +94 -0
  39. package/assets/templates/blocks/components/Icon/useIconHookUsage.doc.mjs +19 -0
  40. package/assets/templates/blocks/components/Icon/useIconHookUsage.tsx +68 -0
  41. package/assets/templates/blocks/components/Link/useLinkComponentHookUsage.doc.mjs +14 -0
  42. package/assets/templates/blocks/components/Link/useLinkComponentHookUsage.tsx +89 -0
  43. package/assets/templates/blocks/components/MultiSelector/useMultiComboboxHookUsage.doc.mjs +23 -0
  44. package/assets/templates/blocks/components/MultiSelector/useMultiComboboxHookUsage.tsx +164 -0
  45. package/assets/templates/blocks/components/Overlay/useOverlayHookUsage.doc.mjs +21 -0
  46. package/assets/templates/blocks/components/Overlay/useOverlayHookUsage.tsx +49 -0
  47. package/assets/templates/blocks/components/Table/useTableSortableStateHookUsage.doc.mjs +14 -0
  48. package/assets/templates/blocks/components/Table/useTableSortableStateHookUsage.tsx +61 -0
  49. package/assets/templates/blocks/components/createDragToSnapPoints/DragToSnapPointsCardVanilla.doc.mjs +14 -0
  50. package/assets/templates/blocks/components/createDragToSnapPoints/DragToSnapPointsCardVanilla.tsx +110 -0
  51. package/assets/templates/blocks/components/useDragToSnapPoints/DragToSnapPointsCard.doc.mjs +21 -0
  52. package/assets/templates/blocks/components/useDragToSnapPoints/DragToSnapPointsCard.tsx +74 -0
  53. package/assets/templates/blocks/components/usePanGesture/PanDraggableBox.doc.mjs +15 -0
  54. package/assets/templates/blocks/components/usePanGesture/PanDraggableBox.tsx +98 -0
  55. package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.doc.mjs +14 -0
  56. package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.tsx +147 -0
  57. package/assets/templates/pages/ai-chat-landing/page.tsx +0 -9
  58. package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +2 -2
  59. package/assets/templates/themes/neutral/neutralTheme.ts +30 -4
  60. package/assets/theme.template.ts +14 -2
  61. package/authoring/doctypes/base/type.ts +41 -0
  62. package/authoring/doctypes/component/component.doc.mjs +7 -1
  63. package/authoring/doctypes/component/type.ts +5 -0
  64. package/authoring/index.d.ts +2 -0
  65. package/clients/cli/commands/ensure-core-built.mjs +399 -43
  66. package/clients/cli/commands/swizzle.mjs +10 -0
  67. package/clients/cli/lib/component-format.mjs +49 -0
  68. package/foundation/config/project.mjs +27 -9
  69. package/foundation/discovery/authoring-self-docs.mjs +2 -2
  70. package/foundation/discovery/cli-self-docs.mjs +2 -1
  71. package/foundation/discovery/component-discovery.mjs +56 -10
  72. package/foundation/discovery/docs-discovery.mjs +6 -4
  73. package/foundation/discovery/icon-definition-discovery.mjs +3 -3
  74. package/foundation/discovery/template-adapter.mjs +5 -7
  75. package/foundation/discovery/theme-discovery.mjs +8 -10
  76. package/foundation/doc-compiler/tree.mjs +2 -2
  77. package/foundation/env/search-path.d.mts +12 -0
  78. package/foundation/env/search-path.mjs +71 -43
  79. package/foundation/fs/listing-cache.d.mts +3 -1
  80. package/foundation/fs/listing-cache.mjs +63 -11
  81. package/foundation/fs/paths.mjs +2 -1
  82. package/foundation/integrations/manifest-writer.d.mts +3 -4
  83. package/foundation/integrations/manifest-writer.mjs +5 -3
  84. package/foundation/release/cache.d.mts +11 -4
  85. package/foundation/release/cache.mjs +15 -7
  86. package/foundation/response/response-types.doc.mjs +1 -1
  87. package/foundation/text/levenshtein.d.mts +4 -1
  88. package/foundation/text/levenshtein.mjs +44 -1
  89. package/package.json +6 -18
  90. package/api/swizzle/_github.d.mts +0 -8
  91. package/api/swizzle/_github.mjs +0 -22
@@ -0,0 +1,61 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use client';
4
+
5
+ import {VStack} from '@astryxdesign/core/Layout';
6
+ import {
7
+ Table,
8
+ useTableSortable,
9
+ useTableSortableState,
10
+ } from '@astryxdesign/core/Table';
11
+ import type {TableColumn} from '@astryxdesign/core/Table';
12
+ import {Text} from '@astryxdesign/core/Text';
13
+
14
+ interface Order extends Record<string, unknown> {
15
+ id: string;
16
+ customer: string;
17
+ total: number;
18
+ }
19
+
20
+ const orders: Order[] = [
21
+ {id: '1042', customer: 'Chen', total: 85},
22
+ {id: '1043', customer: 'Ana', total: 120},
23
+ {id: '1044', customer: 'Ben', total: 40},
24
+ {id: '1045', customer: 'Ana', total: 35},
25
+ {id: '1046', customer: 'Chen', total: 15},
26
+ {id: '1047', customer: 'Ben', total: 210},
27
+ {id: '1048', customer: 'Ana', total: 60},
28
+ ];
29
+
30
+ const columns: TableColumn<Order>[] = [
31
+ {key: 'id', header: 'Order'},
32
+ {key: 'customer', header: 'Customer', sortable: true},
33
+ {key: 'total', header: 'Total ($)', sortable: true, align: 'end'},
34
+ ];
35
+
36
+ export default function UseTableSortableStateHookUsage() {
37
+ const {sortedData, sort, sortConfig} = useTableSortableState<Order>({
38
+ data: orders,
39
+ isMultiSortEnabled: true,
40
+ });
41
+ const sortablePlugin = useTableSortable<Order>(sortConfig);
42
+
43
+ return (
44
+ <VStack gap={3}>
45
+ <Text type="supporting" color="secondary">
46
+ Sorted by:{' '}
47
+ {sort.length === 0
48
+ ? 'none'
49
+ : sort
50
+ .map(({sortKey, direction}) => `${sortKey} ${direction}`)
51
+ .join(', then ')}
52
+ </Text>
53
+ <Table
54
+ data={sortedData}
55
+ columns={columns}
56
+ idKey="id"
57
+ plugins={{sortable: sortablePlugin}}
58
+ />
59
+ </VStack>
60
+ );
61
+ }
@@ -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
+ };
@@ -0,0 +1,110 @@
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
+ // The Astryx reset removes a native button's own look, so these take the
54
+ // theme's secondary button look.
55
+ Object.assign(button.style, {
56
+ height: 'var(--size-element-md)',
57
+ paddingInline: 'var(--spacing-3)',
58
+ border: 'none',
59
+ borderRadius: 'var(--radius-element)',
60
+ backgroundColor: 'var(--color-neutral)',
61
+ color: 'var(--color-text-primary)',
62
+ font: 'inherit',
63
+ fontSize: 'var(--text-label-size)',
64
+ fontWeight: 'var(--font-weight-medium)',
65
+ cursor: 'pointer',
66
+ });
67
+ button.addEventListener('click', () => snap.settleTo(placeAt(index)));
68
+ buttons.append(button);
69
+ return button;
70
+ });
71
+ return () => {
72
+ snap.destroy();
73
+ for (const button of controls) {
74
+ button.remove();
75
+ }
76
+ card.remove();
77
+ status.remove();
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Mounts the card when the wrapper attaches, and takes it down when it
83
+ * detaches: its cleanup is the callback ref's.
84
+ */
85
+ function mountTable(wrapper: HTMLDivElement | null) {
86
+ if (wrapper == null) {
87
+ return undefined;
88
+ }
89
+ const table = wrapper.firstElementChild as HTMLElement;
90
+ const buttons = wrapper.lastElementChild as HTMLElement;
91
+ return mountCard(table, buttons);
92
+ }
93
+
94
+ export default function DragToSnapPointsCardVanilla() {
95
+ return (
96
+ <div
97
+ ref={mountTable}
98
+ style={{display: 'grid', gap: 16, padding: 16, maxWidth: 380}}>
99
+ <div
100
+ style={{
101
+ position: 'relative',
102
+ height: 150,
103
+ display: 'flex',
104
+ alignItems: 'center',
105
+ }}
106
+ />
107
+ <div style={{display: 'flex', gap: 8}} />
108
+ </div>
109
+ );
110
+ }
@@ -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
+ };
@@ -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
+ }
@@ -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
+ }
@@ -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: 'usePanGesture',
7
+ name: 'usePanGesture — A playing card (advanced)',
8
+ displayName: 'usePanGesture — A playing card (advanced)',
9
+ description:
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
+ isReady: true,
12
+ aspectRatio: 4 / 3,
13
+ componentsUsed: ['Button', 'Card', 'Center', 'Text', 'VStack'],
14
+ };
@@ -0,0 +1,147 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use client';
4
+
5
+ import {useRef, useState} from 'react';
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';
19
+
20
+ const DECK = [
21
+ {face: 'A\u2660', name: 'Ace of spades'},
22
+ {face: 'K\u2665', name: 'King of hearts'},
23
+ {face: 'Q\u2663', name: 'Queen of clubs'},
24
+ {face: 'J\u2666', name: 'Jack of diamonds'},
25
+ ];
26
+ /** A release faster than this (px/s) throws the card away. */
27
+ const THROW_SPEED = 1000;
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'};
59
+
60
+ export default function PanPlayingCard() {
61
+ const card = useRef<HTMLDivElement>(null);
62
+ const [top, setTop] = useState(0);
63
+ const [pose, setPose] = useState<Pose>({x: 0, y: 0, motion: 'rest'});
64
+ const announce = useAnnounce();
65
+ /** A throw is in flight: the next card is dealt when it ends. */
66
+ const throwing = useRef(false);
67
+ /** The drag in progress met a throw: the rest of it moves nothing. */
68
+ const ignoringDrag = useRef(false);
69
+ const shown = DECK[top % DECK.length];
70
+
71
+ function discard(direction: number) {
72
+ const element = card.current;
73
+ // One throw at a time: a second Discard during it would deal twice.
74
+ if (element == null || throwing.current) {
75
+ return;
76
+ }
77
+ throwing.current = true;
78
+ ignoringDrag.current = true;
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 = () => {
82
+ throwing.current = false;
83
+ announce(`${shown.name} discarded`);
84
+ setTop(value => value + 1);
85
+ setPose({x: 0, y: 0, motion: 'rest'});
86
+ };
87
+ if (matchMedia('(prefers-reduced-motion: reduce)').matches) {
88
+ dealNext();
89
+ return;
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
+ });
97
+ }
98
+
99
+ usePanGesture(card, {
100
+ onPan: ({state, translation, velocity}) => {
101
+ // A drag that meets a throw is ignored to its end, or the next card
102
+ // would jump to it.
103
+ if (state === 'began') {
104
+ ignoringDrag.current = throwing.current;
105
+ }
106
+ if (ignoringDrag.current) {
107
+ return;
108
+ }
109
+ if (state === 'began' || state === 'changed') {
110
+ setPose({...translation, motion: 'rest'});
111
+ } else if (state === 'ended' && Math.abs(velocity.x) > THROW_SPEED) {
112
+ discard(Math.sign(velocity.x));
113
+ } else {
114
+ setPose({x: 0, y: 0, motion: 'settle'});
115
+ }
116
+ },
117
+ });
118
+
119
+ return (
120
+ <VStack gap={4} padding={4} width="min(360px, 80vw)">
121
+ <Center height={280} xstyle={styles.stage}>
122
+ <Card
123
+ ref={card}
124
+ // An image's name is all a screen reader announces, so it names the
125
+ // card.
126
+ role="img"
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>
143
+ {/* The drag's keyboard and assistive-technology path (WCAG 2.5.7). */}
144
+ <Button label="Discard the top card" onClick={() => discard(1)} />
145
+ </VStack>
146
+ );
147
+ }
@@ -252,12 +252,6 @@ export default function AIChatTemplate() {
252
252
  const activeMode = MODE_OPTIONS.find(m => m.key === mode) ?? MODE_OPTIONS[0];
253
253
  const suggestions = category ? CATEGORY_SUGGESTIONS[category] : null;
254
254
 
255
- // The composer's imperative insert methods mutate the DOM without emitting a
256
- // change, so dispatch an input event to sync its value and clear the placeholder.
257
- const syncComposerValue = () => {
258
- document.activeElement?.dispatchEvent(new Event('input', {bubbles: true}));
259
- };
260
-
261
255
  const applySuggestion = (prompt: string) => {
262
256
  const input = composerInputRef.current;
263
257
  if (!input) {
@@ -268,7 +262,6 @@ export default function AIChatTemplate() {
268
262
  window.getSelection()?.selectAllChildren(document.activeElement);
269
263
  }
270
264
  input.insertText(prompt);
271
- syncComposerValue();
272
265
  };
273
266
 
274
267
  const insertMention = (item: (typeof MENTION_ITEMS)[number]) => {
@@ -287,7 +280,6 @@ export default function AIChatTemplate() {
287
280
  label: item.label,
288
281
  variant: 'blue',
289
282
  });
290
- syncComposerValue();
291
283
  };
292
284
 
293
285
  const insertModeToken = (label: string) => {
@@ -297,7 +289,6 @@ export default function AIChatTemplate() {
297
289
  label,
298
290
  variant: 'orange',
299
291
  });
300
- syncComposerValue();
301
292
  };
302
293
 
303
294
  return (
@@ -15,8 +15,8 @@ export const neutralPaletteRefs = {
15
15
  dark: {25: '#2b422b', 80: '#aad4a9', 85: '#bce0bb'},
16
16
  },
17
17
  neutral: {
18
- light: {0: '#000000', 5: '#111111', 10: '#1b1b1b', 30: '#474747', 45: '#6a6a6a', 60: '#919191', 85: '#d4d4d4', 90: '#e2e2e2', 95: '#f1f1f1', 100: '#ffffff'},
19
- dark: {0: '#000000', 5: '#111111', 10: '#1b1b1b', 15: '#262626', 20: '#303030', 35: '#525252', 65: '#9e9e9e', 85: '#d4d4d4', 90: '#e2e2e2', 95: '#f1f1f1', 100: '#ffffff'},
18
+ light: {0: '#000000', 5: '#111111', 10: '#1b1b1b', 30: '#474747', 35: '#525252', 45: '#6a6a6a', 60: '#919191', 85: '#d4d4d4', 90: '#e2e2e2', 95: '#f1f1f1', 100: '#ffffff'},
19
+ dark: {0: '#000000', 5: '#111111', 10: '#1b1b1b', 15: '#262626', 20: '#303030', 30: '#474747', 35: '#525252', 65: '#9e9e9e', 75: '#b9b9b9', 80: '#c6c6c6', 85: '#d4d4d4', 90: '#e2e2e2', 95: '#f1f1f1', 100: '#ffffff'},
20
20
  },
21
21
  orange: {
22
22
  light: {25: '#622700', 30: '#733100', 75: '#ff9e55', 85: '#ffc7a1'},
@@ -17,17 +17,17 @@ const neutralSyntax = defineSyntaxTheme({
17
17
  tokens: {
18
18
  keyword: [purple.light[30], purple.light[80]],
19
19
  string: [green.light[30], green.light[80]],
20
- comment: [neutral.light[45], neutral.dark[65]],
20
+ comment: [neutral.light[35], neutral.dark[75]],
21
21
  number: [orange.light[30], orange.dark[80]],
22
22
  function: [blue.light[30], blue.dark[80]],
23
23
  type: [purple.light[30], purple.light[80]],
24
24
  variable: [neutral.light[5], neutral.dark[90]],
25
- operator: [neutral.light[45], neutral.dark[65]],
25
+ operator: [neutral.light[35], neutral.dark[75]],
26
26
  constant: [orange.light[30], orange.dark[80]],
27
27
  tag: [red.light[30], red.dark[80]],
28
28
  attribute: [yellow.light[30], yellow.light[80]],
29
29
  property: [teal.light[30], teal.light[80]],
30
- punctuation: [neutral.light[45], neutral.dark[65]],
30
+ punctuation: [neutral.light[35], neutral.dark[75]],
31
31
  background: [neutral.light[100], neutral.dark[5]],
32
32
  },
33
33
  });
@@ -94,7 +94,7 @@ export const neutralTheme = defineTheme({
94
94
  '--color-background-muted': [neutral.light[95], neutral.dark[10]],
95
95
 
96
96
  '--color-accent': [neutral.light[10], neutral.dark[95]],
97
- '--color-accent-muted': [neutral.light[95], neutral.dark[15]],
97
+ '--color-accent-muted': [neutral.light[85], neutral.dark[30]],
98
98
  '--color-neutral': [
99
99
  withAlpha(neutral.light[0], '0F'),
100
100
  withAlpha(neutral.dark[100], '1A'),
@@ -273,6 +273,32 @@ export const neutralTheme = defineTheme({
273
273
  },
274
274
 
275
275
  components: {
276
+ // In dark mode, secondary text falls under 4.5:1 on the selected fill
277
+ // and under its hover. Selected rows use the next stop that clears both.
278
+ item: {
279
+ selected: {
280
+ '--color-text-secondary': `light-dark(${neutral.light[30]}, ${neutral.dark[80]})`,
281
+ },
282
+ },
283
+ 'tree-list-item': {
284
+ selected: {
285
+ '--color-text-secondary': `light-dark(${neutral.light[30]}, ${neutral.dark[80]})`,
286
+ },
287
+ },
288
+ // A dropzone's hint sits on the same fill while a file is dragged over.
289
+ // That state has no target, so the dropzone takes the lift at rest too.
290
+ 'file-input': {
291
+ 'mode:dropzone': {
292
+ '--color-text-secondary': `light-dark(${neutral.light[30]}, ${neutral.dark[80]})`,
293
+ },
294
+ },
295
+ // A numbered citation is a pill on the same fill.
296
+ citation: {
297
+ 'variant:number': {
298
+ '--color-text-secondary': `light-dark(${neutral.light[30]}, ${neutral.dark[80]})`,
299
+ },
300
+ },
301
+
276
302
  button: {
277
303
  'variant:destructive': {
278
304
  backgroundColor: 'var(--color-error-muted)',