@astryxdesign/cli 0.7.9-canary.e423ffa → 0.7.9-canary.e895770
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/component/__fixtures__/resolutions/modules.txt +3 -3
- package/api/theme/build/build.mjs +342 -72
- package/api/theme/build/core-interception.mjs +92 -29
- package/api/theme/build/icon-imports.d.mts +35 -4
- package/api/theme/build/icon-imports.mjs +299 -38
- package/assets/discover/official-packages.json +8 -0
- package/assets/docs/author-a-theme.doc.mjs +3 -1
- package/assets/docs/icons.doc.mjs +29 -0
- package/assets/docs/motion.doc.mjs +1 -1
- package/assets/templates/blocks/components/Calendar/useCalendarDaysHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Calendar/useCalendarDaysHookUsage.tsx +71 -0
- package/assets/templates/blocks/components/Hooks/useInputContainerHookUsage.doc.mjs +15 -0
- package/assets/templates/blocks/components/Hooks/useInputContainerHookUsage.tsx +93 -0
- package/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.tsx +78 -0
- package/assets/templates/blocks/components/Hooks/useScrollLockHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useScrollLockHookUsage.tsx +80 -0
- package/assets/templates/blocks/components/Overlay/useOverlayHookUsage.doc.mjs +21 -0
- package/assets/templates/blocks/components/Overlay/useOverlayHookUsage.tsx +49 -0
- package/assets/templates/blocks/components/Table/useTableSortableStateHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/useTableSortableStateHookUsage.tsx +61 -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/templates/themes/neutral/neutralPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/neutral/neutralTheme.ts +30 -4
- package/assets/theme.template.ts +14 -2
- 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
|
@@ -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: 'useInputContainer',
|
|
7
|
+
name: 'useInputContainer — Search Field Wrapper',
|
|
8
|
+
displayName: 'useInputContainer — Search Field Wrapper',
|
|
9
|
+
description:
|
|
10
|
+
'A custom search field wrapper with a leading search icon, padding and a trailing Clear button. useInputContainer puts focus in the input when the icon or the padding is clicked, while a click on Clear empties the field without the wrapper taking it over.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Icon', 'InputClearButton'],
|
|
14
|
+
icons: {MagnifyingGlassIcon: 'search'},
|
|
15
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
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 {InputClearButton} from '@astryxdesign/core/Field';
|
|
8
|
+
import {useInputContainer} from '@astryxdesign/core/hooks';
|
|
9
|
+
import {Icon} from '@astryxdesign/core/Icon';
|
|
10
|
+
import {
|
|
11
|
+
colorVars,
|
|
12
|
+
focusVars,
|
|
13
|
+
radiusVars,
|
|
14
|
+
spacingVars,
|
|
15
|
+
} from '@astryxdesign/core/theme/tokens.stylex';
|
|
16
|
+
import {MagnifyingGlassIcon} from '@heroicons/react/24/outline';
|
|
17
|
+
|
|
18
|
+
const styles = stylex.create({
|
|
19
|
+
field: {
|
|
20
|
+
display: 'flex',
|
|
21
|
+
alignItems: 'center',
|
|
22
|
+
gap: spacingVars['--spacing-2'],
|
|
23
|
+
width: 320,
|
|
24
|
+
minHeight: 40,
|
|
25
|
+
paddingInline: spacingVars['--spacing-3'],
|
|
26
|
+
borderWidth: 1,
|
|
27
|
+
borderStyle: 'solid',
|
|
28
|
+
borderColor: colorVars['--color-border-emphasized'],
|
|
29
|
+
borderRadius: radiusVars['--radius-element'],
|
|
30
|
+
cursor: 'text',
|
|
31
|
+
outlineWidth: {
|
|
32
|
+
default: 0,
|
|
33
|
+
':has(> input:focus-visible)': focusVars['--focus-outline-width'],
|
|
34
|
+
},
|
|
35
|
+
outlineStyle: {
|
|
36
|
+
default: 'none',
|
|
37
|
+
':has(> input:focus-visible)': focusVars['--focus-outline-style'],
|
|
38
|
+
},
|
|
39
|
+
outlineColor: {
|
|
40
|
+
default: null,
|
|
41
|
+
':has(> input:focus-visible)': focusVars['--focus-outline-color'],
|
|
42
|
+
},
|
|
43
|
+
outlineOffset: {
|
|
44
|
+
default: 0,
|
|
45
|
+
':has(> input:focus-visible)': focusVars['--focus-outline-offset'],
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
input: {
|
|
49
|
+
flexGrow: 1,
|
|
50
|
+
minWidth: 0,
|
|
51
|
+
borderStyle: 'none',
|
|
52
|
+
outlineStyle: 'none',
|
|
53
|
+
backgroundColor: 'transparent',
|
|
54
|
+
color: 'inherit',
|
|
55
|
+
fontFamily: 'inherit',
|
|
56
|
+
fontSize: 'inherit',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export default function UseInputContainerHookUsage() {
|
|
61
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
62
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
63
|
+
const [query, setQuery] = useState('');
|
|
64
|
+
const {onClick, onMouseUp} = useInputContainer({containerRef, inputRef});
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div
|
|
68
|
+
ref={containerRef}
|
|
69
|
+
onClick={onClick}
|
|
70
|
+
onMouseUp={onMouseUp}
|
|
71
|
+
{...stylex.props(styles.field)}>
|
|
72
|
+
<Icon icon={MagnifyingGlassIcon} color="secondary" />
|
|
73
|
+
<input
|
|
74
|
+
ref={inputRef}
|
|
75
|
+
type="text"
|
|
76
|
+
aria-label="Search"
|
|
77
|
+
placeholder="Search"
|
|
78
|
+
value={query}
|
|
79
|
+
onChange={event => setQuery(event.target.value)}
|
|
80
|
+
{...stylex.props(styles.input)}
|
|
81
|
+
/>
|
|
82
|
+
{query !== '' && (
|
|
83
|
+
<InputClearButton
|
|
84
|
+
label="Clear search"
|
|
85
|
+
onClick={() => {
|
|
86
|
+
setQuery('');
|
|
87
|
+
inputRef.current?.focus();
|
|
88
|
+
}}
|
|
89
|
+
/>
|
|
90
|
+
)}
|
|
91
|
+
</div>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
@@ -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: 'useMergedRefs',
|
|
7
|
+
name: 'useMergedRefs — AutoGrowTextArea',
|
|
8
|
+
displayName: 'useMergedRefs — AutoGrowTextArea',
|
|
9
|
+
description:
|
|
10
|
+
"An AutoGrowTextArea that forwards the caller's ref and also keeps its own ref to measure scrollHeight. The merged ref attaches both to the same textarea: it resizes itself while a parent button focuses it through the forwarded ref.",
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Card', 'Button', 'Layout'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {forwardRef, useEffect, useRef, useState} from 'react';
|
|
6
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
7
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
8
|
+
import {useMergedRefs} from '@astryxdesign/core/hooks';
|
|
9
|
+
import {VStack} from '@astryxdesign/core/Layout';
|
|
10
|
+
|
|
11
|
+
type AutoGrowTextAreaProps = {
|
|
12
|
+
value: string;
|
|
13
|
+
onChange: (value: string) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Forwards the caller's ref and also keeps its own ref to measure
|
|
17
|
+
// scrollHeight; useMergedRefs attaches both to the same textarea.
|
|
18
|
+
const AutoGrowTextArea = forwardRef<HTMLTextAreaElement, AutoGrowTextAreaProps>(
|
|
19
|
+
function AutoGrowTextArea({value, onChange}, forwardedRef) {
|
|
20
|
+
const keptRef = useRef<HTMLTextAreaElement>(null);
|
|
21
|
+
const mergedRef = useMergedRefs(forwardedRef, keptRef);
|
|
22
|
+
|
|
23
|
+
// Syncs the textarea's inline height with the committed value.
|
|
24
|
+
// scrollHeight is right only after React writes the new text.
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const el = keptRef.current;
|
|
27
|
+
if (!el) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
el.style.height = 'auto';
|
|
31
|
+
el.style.height = `${el.scrollHeight}px`;
|
|
32
|
+
}, [value]);
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<textarea
|
|
36
|
+
ref={mergedRef}
|
|
37
|
+
value={value}
|
|
38
|
+
onChange={event => onChange(event.target.value)}
|
|
39
|
+
rows={1}
|
|
40
|
+
aria-label="Auto-growing message"
|
|
41
|
+
placeholder="Type — the box grows itself"
|
|
42
|
+
style={{
|
|
43
|
+
width: '100%',
|
|
44
|
+
padding: 8,
|
|
45
|
+
borderRadius: 8,
|
|
46
|
+
border: '1px solid',
|
|
47
|
+
resize: 'none',
|
|
48
|
+
overflow: 'hidden',
|
|
49
|
+
fontFamily: 'inherit',
|
|
50
|
+
fontSize: 14,
|
|
51
|
+
lineHeight: '20px',
|
|
52
|
+
color: 'inherit',
|
|
53
|
+
background: 'transparent',
|
|
54
|
+
}}
|
|
55
|
+
/>
|
|
56
|
+
);
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export default function UseMergedRefsHookUsage() {
|
|
61
|
+
const areaRef = useRef<HTMLTextAreaElement>(null);
|
|
62
|
+
const [value, setValue] = useState(
|
|
63
|
+
'This box keeps its own ref to measure and grow. The button focuses it through the forwarded ref.',
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<Card width={360} padding={4}>
|
|
68
|
+
<VStack gap={3}>
|
|
69
|
+
<AutoGrowTextArea ref={areaRef} value={value} onChange={setValue} />
|
|
70
|
+
<Button
|
|
71
|
+
label="Focus via forwarded ref"
|
|
72
|
+
size="sm"
|
|
73
|
+
onClick={() => areaRef.current?.focus()}
|
|
74
|
+
/>
|
|
75
|
+
</VStack>
|
|
76
|
+
</Card>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
@@ -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: 'useScrollLock',
|
|
7
|
+
name: 'useScrollLock — Full-screen Mobile Menu',
|
|
8
|
+
displayName: 'useScrollLock — Full-screen Mobile Menu',
|
|
9
|
+
description:
|
|
10
|
+
'A custom full-screen menu opened from a Menu button. useScrollLock pins the page behind it while it is open, so the page does not scroll or shift sideways, and closing the menu returns the reader to the same scroll position.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Button', 'Link', 'Layout'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {useEffect, useState} from 'react';
|
|
6
|
+
import * as stylex from '@stylexjs/stylex';
|
|
7
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
8
|
+
import {useFocusTrap, useScrollLock} from '@astryxdesign/core/hooks';
|
|
9
|
+
import {VStack} from '@astryxdesign/core/Layout';
|
|
10
|
+
import {Link} from '@astryxdesign/core/Link';
|
|
11
|
+
import {colorVars, spacingVars} from '@astryxdesign/core/theme/tokens.stylex';
|
|
12
|
+
|
|
13
|
+
const styles = stylex.create({
|
|
14
|
+
menu: {
|
|
15
|
+
width: '100%',
|
|
16
|
+
height: '100%',
|
|
17
|
+
maxWidth: 'none',
|
|
18
|
+
maxHeight: 'none',
|
|
19
|
+
margin: 0,
|
|
20
|
+
borderWidth: 0,
|
|
21
|
+
overflowY: 'auto',
|
|
22
|
+
padding: spacingVars['--spacing-4'],
|
|
23
|
+
color: colorVars['--color-text-primary'],
|
|
24
|
+
backgroundColor: colorVars['--color-background-surface'],
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const MENU_ITEMS = ['Products', 'Pricing', 'Customers', 'Support'];
|
|
29
|
+
|
|
30
|
+
export default function UseScrollLockHookUsage() {
|
|
31
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
32
|
+
useScrollLock(isOpen);
|
|
33
|
+
const {containerRef, focusFirst} = useFocusTrap<HTMLDialogElement>({
|
|
34
|
+
isActive: isOpen,
|
|
35
|
+
onEscape: close,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// The dialog leaves the top layer before the state change, so the page is
|
|
39
|
+
// no longer inert when the focus trap hands focus back to Menu.
|
|
40
|
+
function close() {
|
|
41
|
+
containerRef.current?.close();
|
|
42
|
+
setIsOpen(false);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
const dialog = containerRef.current;
|
|
47
|
+
if (isOpen && dialog != null && !dialog.open) {
|
|
48
|
+
dialog.showModal();
|
|
49
|
+
focusFirst();
|
|
50
|
+
}
|
|
51
|
+
}, [isOpen, containerRef, focusFirst]);
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<>
|
|
55
|
+
<Button label="Menu" onClick={() => setIsOpen(true)} />
|
|
56
|
+
<dialog
|
|
57
|
+
ref={containerRef}
|
|
58
|
+
aria-label="Menu"
|
|
59
|
+
onCancel={event => {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
close();
|
|
62
|
+
}}
|
|
63
|
+
{...stylex.props(styles.menu)}>
|
|
64
|
+
<nav aria-label="Site menu">
|
|
65
|
+
<VStack gap={4} hAlign="start">
|
|
66
|
+
<Button label="Close menu" variant="secondary" onClick={close} />
|
|
67
|
+
{MENU_ITEMS.map(item => (
|
|
68
|
+
<Link
|
|
69
|
+
key={item}
|
|
70
|
+
href={`#menu-${item.toLowerCase()}`}
|
|
71
|
+
onClick={close}>
|
|
72
|
+
{item}
|
|
73
|
+
</Link>
|
|
74
|
+
))}
|
|
75
|
+
</VStack>
|
|
76
|
+
</nav>
|
|
77
|
+
</dialog>
|
|
78
|
+
</>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -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: 'useOverlay',
|
|
7
|
+
name: 'useOverlay — Product Quick View',
|
|
8
|
+
displayName: 'useOverlay — Product Quick View',
|
|
9
|
+
description:
|
|
10
|
+
'A product card that is its own overlay container: useOverlay returns the ref and props to spread on the Card and a bottom strip to render once over the image. Hovering the card, or moving keyboard focus into it, slides in a Quick view button along the bottom of the image, and leaving hides it again. The image is a link that the strip leaves clickable above it, and Tab lands on it first so the next Tab reaches Quick view.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: [
|
|
14
|
+
'Overlay',
|
|
15
|
+
'Card',
|
|
16
|
+
'AspectRatio',
|
|
17
|
+
'Button',
|
|
18
|
+
'Layout',
|
|
19
|
+
'Text',
|
|
20
|
+
],
|
|
21
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import type {RefObject} from 'react';
|
|
6
|
+
import {AspectRatio} from '@astryxdesign/core/AspectRatio';
|
|
7
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
8
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
9
|
+
import {VStack} from '@astryxdesign/core/Layout';
|
|
10
|
+
import {useOverlay} from '@astryxdesign/core/Overlay';
|
|
11
|
+
import {Text} from '@astryxdesign/core/Text';
|
|
12
|
+
|
|
13
|
+
export default function UseOverlayHookUsage() {
|
|
14
|
+
const overlay = useOverlay({
|
|
15
|
+
showOn: 'hover',
|
|
16
|
+
position: 'bottom',
|
|
17
|
+
align: 'center',
|
|
18
|
+
content: <Button label="Quick view" variant="secondary" size="sm" />,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<Card
|
|
23
|
+
width={280}
|
|
24
|
+
padding={0}
|
|
25
|
+
ref={overlay.containerRef as RefObject<HTMLDivElement | null>}
|
|
26
|
+
{...overlay.containerProps}>
|
|
27
|
+
<div style={{position: 'relative'}}>
|
|
28
|
+
<a href="#ceramic-mug">
|
|
29
|
+
<AspectRatio ratio={1}>
|
|
30
|
+
<img
|
|
31
|
+
src="/template-assets/light-product-1.png"
|
|
32
|
+
alt="Ceramic mug"
|
|
33
|
+
style={{width: '100%', height: '100%', objectFit: 'cover'}}
|
|
34
|
+
/>
|
|
35
|
+
</AspectRatio>
|
|
36
|
+
</a>
|
|
37
|
+
{overlay.element}
|
|
38
|
+
</div>
|
|
39
|
+
<VStack gap={1} padding={3}>
|
|
40
|
+
<Text type="body" weight="bold">
|
|
41
|
+
Ceramic mug
|
|
42
|
+
</Text>
|
|
43
|
+
<Text type="supporting" color="secondary">
|
|
44
|
+
$24
|
|
45
|
+
</Text>
|
|
46
|
+
</VStack>
|
|
47
|
+
</Card>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -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: 'useTableSortableState',
|
|
7
|
+
name: 'useTableSortableState — Multi-Sort Orders',
|
|
8
|
+
displayName: 'useTableSortableState — Multi-Sort Orders',
|
|
9
|
+
description:
|
|
10
|
+
'Orders with no sort code of their own: useTableSortableState owns the sort state, returns the sorted orders, and hands useTableSortable a ready config. Click Customer, then Shift-click Total, to sort by customer and then by total within each customer. A line above the orders reads the current sort state from the hook.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Table', 'Layout', 'Text'],
|
|
14
|
+
};
|
|
@@ -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,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
|
};
|