@astryxdesign/cli 0.7.7 → 0.7.8-canary.15a98a1
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/case-01.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-02.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-03.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-04.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-05.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-06.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-07.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-08.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-09.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-10.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-11.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-12.txt +30 -0
- package/api/component/__fixtures__/resolutions/case-13.txt +25 -0
- package/api/component/__fixtures__/resolutions/modules.txt +309 -0
- package/api/component/component.mjs +11 -7
- package/api/gap-report/gap-report.mjs +9 -4
- package/api/integration/authoring-checks.mjs +31 -0
- package/api/integration/check/check.mjs +8 -3
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/promote-tokenizer-presentation.mjs +174 -0
- package/assets/discover/official-packages.json +4 -8
- package/assets/templates/blocks/components/Hooks/useAnnounceHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useAnnounceHookUsage.tsx +90 -0
- package/assets/templates/blocks/components/Hooks/useFocusTrapHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useFocusTrapHookUsage.tsx +70 -0
- package/assets/templates/blocks/components/Tokenizer/TokenizerClear.tsx +1 -0
- package/assets/templates/blocks/components/Tokenizer/TokenizerCreatable.tsx +1 -0
- package/assets/templates/blocks/components/Tokenizer/TokenizerEndContent.tsx +1 -0
- package/assets/templates/blocks/components/Tokenizer/TokenizerOverflow.tsx +2 -0
- package/assets/templates/blocks/components/createAnimator/AnimatorPlayingCardsVanilla.doc.mjs +14 -0
- package/assets/templates/blocks/components/createAnimator/AnimatorPlayingCardsVanilla.tsx +199 -0
- package/assets/templates/blocks/components/useAnimator/AnimatorPlayingCards.doc.mjs +21 -0
- package/assets/templates/blocks/components/useAnimator/AnimatorPlayingCards.tsx +210 -0
- package/assets/templates/pages/table-inbox/page.tsx +6 -0
- package/assets/templates/pages/work-item-detail/page.tsx +1 -0
- package/clients/cli/commands/component.mjs +18 -8
- package/foundation/discovery/component-discovery.mjs +111 -16
- package/foundation/fs/listing-cache.d.mts +25 -0
- package/foundation/fs/listing-cache.mjs +69 -2
- package/foundation/fs/paths.mjs +50 -5
- package/foundation/integrations/public-imports.d.mts +24 -0
- package/foundation/integrations/public-imports.mjs +2 -2
- package/foundation/xle/registry.d.mts +1 -1
- package/foundation/xle/registry.mjs +11 -1
- package/package.json +9 -9
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Codemod: move Tokenizer's experimental presentation to the stable prop
|
|
5
|
+
*
|
|
6
|
+
* `experimental={{presentation: x}}` on a Tokenizer becomes
|
|
7
|
+
* `presentation={x}`, or `presentation="x"` for a string. Other keys stay in
|
|
8
|
+
* the `experimental` object, and an object left empty is removed. The
|
|
9
|
+
* deprecated spelling selects the same surface, so the result renders the
|
|
10
|
+
* same. Built on the shared `transformProp` helper, so it does not follow
|
|
11
|
+
* variables, helpers, wrappers, or types. Where it cannot prove the result it
|
|
12
|
+
* keeps the deprecated spelling, which still works, and leaves a TODO: an
|
|
13
|
+
* `experimental` value that is not an object literal, a spread or computed
|
|
14
|
+
* key in it, a Tokenizer that already passes `presentation`, and a spread of
|
|
15
|
+
* props after `experimental`, which could set `presentation` over the moved
|
|
16
|
+
* value. A spread before `experimental` is safe: the moved attribute takes
|
|
17
|
+
* `experimental`'s place, so it still wins over the spread, as the
|
|
18
|
+
* experimental value did, and before this release a spread's `presentation`
|
|
19
|
+
* did nothing. Each TODO is added once, so a second run changes
|
|
20
|
+
* nothing.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import {transformProp} from '../../transform-prop.mjs';
|
|
24
|
+
|
|
25
|
+
export const meta = {
|
|
26
|
+
title: "Move Tokenizer's experimental presentation to presentation",
|
|
27
|
+
description:
|
|
28
|
+
'Rewrites `experimental={{presentation: x}}` on Tokenizer to the stable ' +
|
|
29
|
+
'`presentation={x}`. Keeps the deprecated spelling, which still works, ' +
|
|
30
|
+
'with a TODO where the experimental value is not an object literal, ' +
|
|
31
|
+
'where the Tokenizer already passes presentation, and where it spreads ' +
|
|
32
|
+
'props after experimental.',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const CORE_IMPORT_RE = /^@(astryxdesign|xds)\/core(?:\/|$)/;
|
|
36
|
+
const COMPONENT_PROPS = new Map([['Tokenizer', new Set(['experimental'])]]);
|
|
37
|
+
|
|
38
|
+
const TODO_COMMENT =
|
|
39
|
+
' TODO(astryx upgrade): experimental.presentation is deprecated. Pass ' +
|
|
40
|
+
'presentation instead; this one was left unchanged because the codemod ' +
|
|
41
|
+
'could not prove the rewrite. ';
|
|
42
|
+
|
|
43
|
+
const TYPE_WRAPPERS = new Set([
|
|
44
|
+
'TSAsExpression',
|
|
45
|
+
'TSSatisfiesExpression',
|
|
46
|
+
'TSTypeAssertion',
|
|
47
|
+
'TSNonNullExpression',
|
|
48
|
+
'TypeCastExpression',
|
|
49
|
+
'ParenthesizedExpression',
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/** @param {any} node */
|
|
53
|
+
function unwrap(node) {
|
|
54
|
+
let current = node;
|
|
55
|
+
while (current && TYPE_WRAPPERS.has(current.type)) {
|
|
56
|
+
current = current.expression;
|
|
57
|
+
}
|
|
58
|
+
return current;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The static key of an object entry, or null. @param {any} entry */
|
|
62
|
+
function keyName(entry) {
|
|
63
|
+
if (
|
|
64
|
+
(entry.type !== 'ObjectProperty' && entry.type !== 'Property') ||
|
|
65
|
+
entry.computed
|
|
66
|
+
) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const key = entry.key;
|
|
70
|
+
if (key.type === 'Identifier') {
|
|
71
|
+
return key.name;
|
|
72
|
+
}
|
|
73
|
+
if (
|
|
74
|
+
(key.type === 'StringLiteral' || key.type === 'Literal') &&
|
|
75
|
+
typeof key.value === 'string'
|
|
76
|
+
) {
|
|
77
|
+
return key.value;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {any} j @param {any} node @param {string} text */
|
|
83
|
+
function addComment(j, node, text) {
|
|
84
|
+
node.comments ??= [];
|
|
85
|
+
if (node.comments.some((/** @type {any} */ c) => c.value === text)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
node.comments.push(j.commentBlock(text, true, false));
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
|
|
94
|
+
* @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
|
|
95
|
+
* @returns {string | null | undefined}
|
|
96
|
+
*/
|
|
97
|
+
export default function transformer(file, api) {
|
|
98
|
+
const j = api.jscodeshift;
|
|
99
|
+
const root = j(file.source);
|
|
100
|
+
let hasChanges = false;
|
|
101
|
+
|
|
102
|
+
transformProp(
|
|
103
|
+
root,
|
|
104
|
+
j,
|
|
105
|
+
{
|
|
106
|
+
matchesImport: source => CORE_IMPORT_RE.test(source),
|
|
107
|
+
components: COMPONENT_PROPS,
|
|
108
|
+
},
|
|
109
|
+
propPath => {
|
|
110
|
+
const opening = propPath.parent?.node;
|
|
111
|
+
const attributes = opening?.attributes ?? [];
|
|
112
|
+
const prop = propPath.node;
|
|
113
|
+
const value =
|
|
114
|
+
prop.value?.type === 'JSXExpressionContainer'
|
|
115
|
+
? unwrap(prop.value.expression)
|
|
116
|
+
: null;
|
|
117
|
+
const entries =
|
|
118
|
+
value?.type === 'ObjectExpression' ? value.properties : [];
|
|
119
|
+
const presentationEntries = entries.filter(
|
|
120
|
+
(/** @type {any} */ entry) => keyName(entry) === 'presentation',
|
|
121
|
+
);
|
|
122
|
+
const hasUnprovenEntry = entries.some(
|
|
123
|
+
(/** @type {any} */ entry) => keyName(entry) == null,
|
|
124
|
+
);
|
|
125
|
+
if (
|
|
126
|
+
value?.type === 'ObjectExpression' &&
|
|
127
|
+
presentationEntries.length === 0 &&
|
|
128
|
+
!hasUnprovenEntry
|
|
129
|
+
) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const isProvable =
|
|
133
|
+
value?.type === 'ObjectExpression' &&
|
|
134
|
+
presentationEntries.length === 1 &&
|
|
135
|
+
!hasUnprovenEntry &&
|
|
136
|
+
!attributes.some(
|
|
137
|
+
(/** @type {any} */ a, /** @type {number} */ index) =>
|
|
138
|
+
(a.type === 'JSXSpreadAttribute' &&
|
|
139
|
+
index > attributes.indexOf(prop)) ||
|
|
140
|
+
(a.type === 'JSXAttribute' && a.name?.name === 'presentation'),
|
|
141
|
+
);
|
|
142
|
+
if (!isProvable) {
|
|
143
|
+
hasChanges = addComment(j, prop, TODO_COMMENT) || hasChanges;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const entry = presentationEntries[0];
|
|
148
|
+
const entryValue = entry.value;
|
|
149
|
+
const literal = unwrap(entryValue);
|
|
150
|
+
const attributeValue =
|
|
151
|
+
(literal?.type === 'StringLiteral' || literal?.type === 'Literal') &&
|
|
152
|
+
typeof literal.value === 'string'
|
|
153
|
+
? j.stringLiteral(literal.value)
|
|
154
|
+
: j.jsxExpressionContainer(entryValue);
|
|
155
|
+
const presentation = j.jsxAttribute(
|
|
156
|
+
j.jsxIdentifier('presentation'),
|
|
157
|
+
attributeValue,
|
|
158
|
+
);
|
|
159
|
+
value.properties = entries.filter((/** @type {any} */ e) => e !== entry);
|
|
160
|
+
const index = opening.attributes.indexOf(prop);
|
|
161
|
+
if (value.properties.length === 0) {
|
|
162
|
+
opening.attributes.splice(index, 1, presentation);
|
|
163
|
+
} else {
|
|
164
|
+
opening.attributes.splice(index + 1, 0, presentation);
|
|
165
|
+
}
|
|
166
|
+
hasChanges = true;
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
if (!hasChanges) return undefined;
|
|
171
|
+
// `auto` keeps every reprinted string's own quotes and writes the new
|
|
172
|
+
// attribute string in double quotes, as JSX attributes are written.
|
|
173
|
+
return root.toSource({quote: 'auto'});
|
|
174
|
+
}
|
|
@@ -140,14 +140,6 @@
|
|
|
140
140
|
"kind": "component",
|
|
141
141
|
"name": "LogStream"
|
|
142
142
|
},
|
|
143
|
-
{
|
|
144
|
-
"kind": "component",
|
|
145
|
-
"name": "MobilePowerSearch"
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
"kind": "component",
|
|
149
|
-
"name": "MobileTokenizer"
|
|
150
|
-
},
|
|
151
143
|
{
|
|
152
144
|
"kind": "component",
|
|
153
145
|
"name": "RadialChart"
|
|
@@ -160,6 +152,10 @@
|
|
|
160
152
|
"kind": "component",
|
|
161
153
|
"name": "Schedule"
|
|
162
154
|
},
|
|
155
|
+
{
|
|
156
|
+
"kind": "component",
|
|
157
|
+
"name": "SelectableCardGroup"
|
|
158
|
+
},
|
|
163
159
|
{
|
|
164
160
|
"kind": "component",
|
|
165
161
|
"name": "Stat"
|
|
@@ -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: 'useAnnounce',
|
|
7
|
+
name: 'useAnnounce — Filter Result Count',
|
|
8
|
+
displayName: 'useAnnounce — Filter Result Count',
|
|
9
|
+
description:
|
|
10
|
+
'A product filter that announces its result count with useAnnounce: once typing pauses for 500 ms, a screen reader hears "3 results" or "No results". The count has its own hasReplace instance, as the hook\'s docs advise for a changing value.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Card', 'TextInput', 'List', 'Text', 'Layout'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {useEffect, useRef, useState} from 'react';
|
|
6
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
7
|
+
import {useAnnounce} from '@astryxdesign/core/hooks';
|
|
8
|
+
import {VStack} from '@astryxdesign/core/Layout';
|
|
9
|
+
import {List, ListItem} from '@astryxdesign/core/List';
|
|
10
|
+
import {Text} from '@astryxdesign/core/Text';
|
|
11
|
+
import {TextInput} from '@astryxdesign/core/TextInput';
|
|
12
|
+
|
|
13
|
+
const PRODUCTS = [
|
|
14
|
+
'Desk lamp',
|
|
15
|
+
'Desk organizer',
|
|
16
|
+
'Standing desk',
|
|
17
|
+
'Office chair',
|
|
18
|
+
'Chair mat',
|
|
19
|
+
'Monitor arm',
|
|
20
|
+
'Monitor stand',
|
|
21
|
+
'Keyboard tray',
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const ANNOUNCE_DELAY_MS = 500;
|
|
25
|
+
|
|
26
|
+
function filterProducts(query: string): string[] {
|
|
27
|
+
const needle = query.trim().toLowerCase();
|
|
28
|
+
return PRODUCTS.filter(product => product.toLowerCase().includes(needle));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default function UseAnnounceHookUsage() {
|
|
32
|
+
const announce = useAnnounce({hasReplace: true});
|
|
33
|
+
const [query, setQuery] = useState('');
|
|
34
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
35
|
+
const matches = filterProducts(query);
|
|
36
|
+
const count = matches.length;
|
|
37
|
+
|
|
38
|
+
const handleChange = (value: string) => {
|
|
39
|
+
setQuery(value);
|
|
40
|
+
if (timerRef.current != null) {
|
|
41
|
+
clearTimeout(timerRef.current);
|
|
42
|
+
timerRef.current = null;
|
|
43
|
+
}
|
|
44
|
+
if (value === '') {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const nextCount = filterProducts(value).length;
|
|
48
|
+
timerRef.current = setTimeout(() => {
|
|
49
|
+
timerRef.current = null;
|
|
50
|
+
announce(
|
|
51
|
+
nextCount === 0
|
|
52
|
+
? 'No results'
|
|
53
|
+
: `${nextCount} ${nextCount === 1 ? 'result' : 'results'}`,
|
|
54
|
+
);
|
|
55
|
+
}, ANNOUNCE_DELAY_MS);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// A count still pending at unmount would be spoken for a filter that is gone.
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
return () => {
|
|
61
|
+
if (timerRef.current != null) {
|
|
62
|
+
clearTimeout(timerRef.current);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}, []);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Card width={360} padding={4}>
|
|
69
|
+
<VStack gap={3}>
|
|
70
|
+
<TextInput
|
|
71
|
+
label="Filter products"
|
|
72
|
+
value={query}
|
|
73
|
+
onChange={handleChange}
|
|
74
|
+
hasClear
|
|
75
|
+
/>
|
|
76
|
+
{count === 0 ? (
|
|
77
|
+
<Text type="body" color="secondary">
|
|
78
|
+
No results
|
|
79
|
+
</Text>
|
|
80
|
+
) : (
|
|
81
|
+
<List density="compact">
|
|
82
|
+
{matches.map(product => (
|
|
83
|
+
<ListItem key={product} label={product} />
|
|
84
|
+
))}
|
|
85
|
+
</List>
|
|
86
|
+
)}
|
|
87
|
+
</VStack>
|
|
88
|
+
</Card>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
@@ -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: 'useFocusTrap',
|
|
7
|
+
name: 'useFocusTrap — Quick Edit Panel',
|
|
8
|
+
displayName: 'useFocusTrap — Quick Edit Panel',
|
|
9
|
+
description:
|
|
10
|
+
'A custom quick-edit panel, not built on Dialog, that traps focus with useFocusTrap: focusFirst moves focus to its field on open, Tab and Shift+Tab stay inside it, Escape closes it through onEscape, and closing returns focus to the Quick edit button.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Card', 'Button', 'TextInput', 'Text', 'Layout'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {useEffect, useState} from 'react';
|
|
6
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
7
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
8
|
+
import {useFocusTrap} from '@astryxdesign/core/hooks';
|
|
9
|
+
import {HStack, VStack} from '@astryxdesign/core/Layout';
|
|
10
|
+
import {Text} from '@astryxdesign/core/Text';
|
|
11
|
+
import {TextInput} from '@astryxdesign/core/TextInput';
|
|
12
|
+
|
|
13
|
+
export default function UseFocusTrapHookUsage() {
|
|
14
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
15
|
+
const [title, setTitle] = useState('Update onboarding guide');
|
|
16
|
+
const [draft, setDraft] = useState(title);
|
|
17
|
+
const close = () => setIsOpen(false);
|
|
18
|
+
const {containerRef, focusFirst} = useFocusTrap<HTMLDivElement>({
|
|
19
|
+
isActive: isOpen,
|
|
20
|
+
onEscape: close,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Runs after commit, once the panel is mounted and useFocusTrap has recorded
|
|
24
|
+
// the opener, so closing the panel returns focus to it.
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (isOpen) {
|
|
27
|
+
focusFirst();
|
|
28
|
+
}
|
|
29
|
+
}, [isOpen, focusFirst]);
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<HStack gap={4} vAlign="start">
|
|
33
|
+
<VStack gap={2}>
|
|
34
|
+
<Text type="body" weight="bold">
|
|
35
|
+
{title}
|
|
36
|
+
</Text>
|
|
37
|
+
<Button
|
|
38
|
+
label="Quick edit"
|
|
39
|
+
onClick={() => {
|
|
40
|
+
setDraft(title);
|
|
41
|
+
setIsOpen(true);
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
</VStack>
|
|
45
|
+
{isOpen && (
|
|
46
|
+
<Card
|
|
47
|
+
ref={containerRef}
|
|
48
|
+
role="dialog"
|
|
49
|
+
aria-label="Quick edit"
|
|
50
|
+
width={280}
|
|
51
|
+
padding={4}>
|
|
52
|
+
<VStack gap={3}>
|
|
53
|
+
<TextInput label="Title" value={draft} onChange={setDraft} />
|
|
54
|
+
<HStack gap={2} hAlign="end">
|
|
55
|
+
<Button label="Cancel" variant="secondary" onClick={close} />
|
|
56
|
+
<Button
|
|
57
|
+
label="Save"
|
|
58
|
+
variant="primary"
|
|
59
|
+
onClick={() => {
|
|
60
|
+
setTitle(draft);
|
|
61
|
+
close();
|
|
62
|
+
}}
|
|
63
|
+
/>
|
|
64
|
+
</HStack>
|
|
65
|
+
</VStack>
|
|
66
|
+
</Card>
|
|
67
|
+
)}
|
|
68
|
+
</HStack>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
@@ -41,6 +41,7 @@ export default function TokenizerOverflow() {
|
|
|
41
41
|
value={inlineValue}
|
|
42
42
|
onChange={items => setInlineValue(items)}
|
|
43
43
|
tokenOverflowBehavior="unfocusedInline"
|
|
44
|
+
presentation="popover"
|
|
44
45
|
/>
|
|
45
46
|
</Stack>
|
|
46
47
|
<Stack direction="vertical" gap={1}>
|
|
@@ -54,6 +55,7 @@ export default function TokenizerOverflow() {
|
|
|
54
55
|
value={layerValue}
|
|
55
56
|
onChange={items => setLayerValue(items)}
|
|
56
57
|
tokenOverflowBehavior="unfocusedLayer"
|
|
58
|
+
presentation="popover"
|
|
57
59
|
/>
|
|
58
60
|
</Stack>
|
|
59
61
|
</Stack>
|
|
@@ -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: 'createAnimator',
|
|
7
|
+
name: 'createAnimator — Playing cards in plain JavaScript',
|
|
8
|
+
displayName: 'createAnimator — Playing cards in plain JavaScript',
|
|
9
|
+
description:
|
|
10
|
+
'The playing card from the useAnimator example, built in plain JavaScript with createAnimator and pointer events: it follows a drag, springs back on a gentle release, flies off with momentum 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
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {createAnimator} 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
|
+
/** The slowest throw, so a thrown card always leaves the table. */
|
|
16
|
+
const MIN_THROW = 1600;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A deck on a table, in plain JavaScript: no React below this line. It
|
|
20
|
+
* returns a function that takes it down again.
|
|
21
|
+
*/
|
|
22
|
+
function mountDeck(table: HTMLElement, discardButton: HTMLButtonElement) {
|
|
23
|
+
let top = 0;
|
|
24
|
+
const card = document.createElement('div');
|
|
25
|
+
card.textContent = DECK[0].face;
|
|
26
|
+
// An image's name is all a screen reader announces, so it names the card.
|
|
27
|
+
card.setAttribute('role', 'img');
|
|
28
|
+
card.setAttribute('aria-label', DECK[0].name);
|
|
29
|
+
Object.assign(card.style, {
|
|
30
|
+
width: '120px',
|
|
31
|
+
height: '170px',
|
|
32
|
+
display: 'grid',
|
|
33
|
+
placeItems: 'center',
|
|
34
|
+
font: 'bold 40px system-ui, sans-serif',
|
|
35
|
+
// The theme's surface and text, so the rank reads in light and dark.
|
|
36
|
+
borderRadius: 'var(--radius-container)',
|
|
37
|
+
backgroundColor: 'var(--color-background-surface)',
|
|
38
|
+
color: 'var(--color-text-primary)',
|
|
39
|
+
boxShadow: '0 2px 8px var(--color-shadow)',
|
|
40
|
+
touchAction: 'none',
|
|
41
|
+
cursor: 'grab',
|
|
42
|
+
userSelect: 'none',
|
|
43
|
+
});
|
|
44
|
+
// Announces each discard to screen readers.
|
|
45
|
+
const status = document.createElement('div');
|
|
46
|
+
status.setAttribute('aria-live', 'polite');
|
|
47
|
+
Object.assign(status.style, {
|
|
48
|
+
position: 'absolute',
|
|
49
|
+
width: '1px',
|
|
50
|
+
height: '1px',
|
|
51
|
+
overflow: 'hidden',
|
|
52
|
+
clipPath: 'inset(50%)',
|
|
53
|
+
whiteSpace: 'nowrap',
|
|
54
|
+
});
|
|
55
|
+
table.append(card, status);
|
|
56
|
+
const animator = createAnimator(card);
|
|
57
|
+
// One discard at a time: a press while a card leaves deals no second card.
|
|
58
|
+
let isDiscarding = false;
|
|
59
|
+
|
|
60
|
+
// Announces the discard and deals the next card in place of the old one.
|
|
61
|
+
async function dealNext() {
|
|
62
|
+
status.textContent = `${DECK[top % DECK.length].name} discarded`;
|
|
63
|
+
top += 1;
|
|
64
|
+
const next = DECK[top % DECK.length];
|
|
65
|
+
card.textContent = next.face;
|
|
66
|
+
card.setAttribute('aria-label', next.name);
|
|
67
|
+
animator.set({x: 0, y: 0, rotate: 0, scale: 0.9, opacity: 0});
|
|
68
|
+
isDiscarding = false;
|
|
69
|
+
await animator.animate({scale: 1, opacity: 1});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Starts a discard unless one runs. It also ends a drag in progress: the
|
|
73
|
+
// drag's next move would take the discard's motion over and drag the dealt
|
|
74
|
+
// card.
|
|
75
|
+
function startDiscard() {
|
|
76
|
+
if (isDiscarding) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
isDiscarding = true;
|
|
80
|
+
origin = null;
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function discard(vx: number, vy: number) {
|
|
85
|
+
if (!startDiscard()) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const speed = Math.hypot(vx, vy);
|
|
89
|
+
const boost = speed < MIN_THROW ? MIN_THROW / Math.max(speed, 1) : 1;
|
|
90
|
+
// The card coasts on while it fades; once it is gone, the next card is
|
|
91
|
+
// dealt in place, which ends the coast. A coast continues the person's
|
|
92
|
+
// throw, so Reduce Motion keeps it.
|
|
93
|
+
void animator.decay({x: speed < 1 ? MIN_THROW : vx * boost, y: vy * boost});
|
|
94
|
+
await animator.animate({opacity: 0});
|
|
95
|
+
await dealNext();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// The drag: follow the pointer, then read the 80 ms before the release, so
|
|
99
|
+
// a pointer that held still before letting go releases at rest.
|
|
100
|
+
let origin: {x: number; y: number} | null = null;
|
|
101
|
+
let samples: {t: number; x: number; y: number}[] = [];
|
|
102
|
+
function onDown(event: PointerEvent) {
|
|
103
|
+
// A leaving card takes no press. Pinning it would end its discard and deal
|
|
104
|
+
// the next card under the finger.
|
|
105
|
+
if (isDiscarding) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
card.setPointerCapture(event.pointerId);
|
|
109
|
+
// Pins the card where it shows, so the drag starts there. Only the
|
|
110
|
+
// position stops, so a card fading in keeps fading in.
|
|
111
|
+
const {x, y, rotate} = animator.get();
|
|
112
|
+
animator.set({x, y, rotate});
|
|
113
|
+
origin = {x: event.clientX - x, y: event.clientY - y};
|
|
114
|
+
samples = [];
|
|
115
|
+
}
|
|
116
|
+
function onMove(event: PointerEvent) {
|
|
117
|
+
if (origin == null) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const x = event.clientX - origin.x;
|
|
121
|
+
const y = event.clientY - origin.y;
|
|
122
|
+
samples.push({t: event.timeStamp, x, y});
|
|
123
|
+
animator.set({x, y, rotate: x / 18});
|
|
124
|
+
}
|
|
125
|
+
// A cancelled pointer was not let go, so the card springs back.
|
|
126
|
+
function onCancel() {
|
|
127
|
+
if (origin == null) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
origin = null;
|
|
131
|
+
void animator.animate({x: 0, y: 0, rotate: 0});
|
|
132
|
+
}
|
|
133
|
+
function onUp(event: PointerEvent) {
|
|
134
|
+
if (origin == null) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
origin = null;
|
|
138
|
+
const recent = samples.filter(s => event.timeStamp - s.t <= 80);
|
|
139
|
+
const first = recent[0];
|
|
140
|
+
const last = recent.at(-1);
|
|
141
|
+
const seconds = last && first ? (last.t - first.t) / 1000 : 0;
|
|
142
|
+
const vx = seconds > 0 && last && first ? (last.x - first.x) / seconds : 0;
|
|
143
|
+
const vy = seconds > 0 && last && first ? (last.y - first.y) / seconds : 0;
|
|
144
|
+
if (Math.abs(vx) > THROW_SPEED) {
|
|
145
|
+
void discard(vx, vy);
|
|
146
|
+
} else {
|
|
147
|
+
void animator.animate(
|
|
148
|
+
{x: 0, y: 0, rotate: 0},
|
|
149
|
+
{velocity: {x: vx, y: vy}},
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// The button moves the card off on the state-change motion: a planned
|
|
154
|
+
// move, which Reduce Motion turns into a fade where the card is.
|
|
155
|
+
async function discardWithButton() {
|
|
156
|
+
if (!startDiscard()) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
await animator.animate({x: 400, opacity: 0});
|
|
160
|
+
await dealNext();
|
|
161
|
+
}
|
|
162
|
+
const onDiscard = () => void discardWithButton();
|
|
163
|
+
|
|
164
|
+
card.addEventListener('pointerdown', onDown);
|
|
165
|
+
card.addEventListener('pointermove', onMove);
|
|
166
|
+
card.addEventListener('pointerup', onUp);
|
|
167
|
+
card.addEventListener('pointercancel', onCancel);
|
|
168
|
+
discardButton.addEventListener('click', onDiscard);
|
|
169
|
+
return () => {
|
|
170
|
+
discardButton.removeEventListener('click', onDiscard);
|
|
171
|
+
animator.destroy();
|
|
172
|
+
card.remove();
|
|
173
|
+
status.remove();
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Mounts the deck when the wrapper attaches, and takes it down when it
|
|
179
|
+
* detaches: its cleanup is the callback ref's.
|
|
180
|
+
*/
|
|
181
|
+
function mountTable(wrapper: HTMLDivElement | null) {
|
|
182
|
+
if (wrapper == null) {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
const table = wrapper.firstElementChild as HTMLElement;
|
|
186
|
+
const button = wrapper.querySelector('button') as HTMLButtonElement;
|
|
187
|
+
return mountDeck(table, button);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export default function AnimatorPlayingCardsVanilla() {
|
|
191
|
+
return (
|
|
192
|
+
<div
|
|
193
|
+
ref={mountTable}
|
|
194
|
+
style={{display: 'grid', gap: 16, padding: 16, maxWidth: 360}}>
|
|
195
|
+
<div style={{height: 200, display: 'grid', placeItems: 'center'}} />
|
|
196
|
+
<button type="button">Discard the top card</button>
|
|
197
|
+
</div>
|
|
198
|
+
);
|
|
199
|
+
}
|
|
@@ -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: 'useAnimator',
|
|
7
|
+
name: 'useAnimator — Playing cards',
|
|
8
|
+
displayName: 'useAnimator — Playing cards',
|
|
9
|
+
description:
|
|
10
|
+
'A playing card that follows a drag: a gentle release springs it back on its release velocity, and a quick flick throws it away with momentum before the next card is dealt. A Discard button moves it off from the keyboard, fading it in place under Reduce Motion, and the discard is announced. Built only on the public useAnimator hook.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: [
|
|
14
|
+
'Card',
|
|
15
|
+
'Button',
|
|
16
|
+
'Layout',
|
|
17
|
+
'Text',
|
|
18
|
+
'VisuallyHidden',
|
|
19
|
+
'useAnimator',
|
|
20
|
+
],
|
|
21
|
+
};
|