@astryxdesign/cli 0.7.9-canary.784dc5e → 0.7.9-canary.7aaf2a1
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/README.md +1 -1
- package/api/component/__fixtures__/resolutions/modules.txt +1 -1
- package/api/search/_candidate-score.mjs +20 -17
- package/api/search/_words.d.mts +34 -3
- package/api/search/_words.mjs +87 -21
- package/api/swizzle/copy/copy.d.mts +17 -1
- package/api/swizzle/copy/copy.mjs +240 -5
- package/api/swizzle/swizzle.doc.mjs +1 -1
- package/api/swizzle/swizzle.type.mjs +1 -1
- package/assets/discover/official-packages.json +4 -0
- package/assets/templates/blocks/components/Icon/useIconHookUsage.doc.mjs +19 -0
- package/assets/templates/blocks/components/Icon/useIconHookUsage.tsx +68 -0
- package/assets/templates/blocks/components/createPanGesture/PanPlayingCardVanilla.doc.mjs +14 -0
- package/assets/templates/blocks/components/createPanGesture/PanPlayingCardVanilla.tsx +156 -0
- package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.doc.mjs +14 -0
- package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.tsx +143 -0
- package/clients/cli/commands/swizzle.mjs +10 -0
- package/foundation/discovery/component-discovery.mjs +17 -6
- package/foundation/integrations/manifest-writer.d.mts +3 -4
- package/foundation/integrations/manifest-writer.mjs +5 -3
- package/foundation/response/response-types.doc.mjs +1 -1
- package/package.json +9 -9
|
@@ -59,10 +59,16 @@ const DEFAULT_ISSUES_URL = 'https://github.com/meta/astryx/issues/new';
|
|
|
59
59
|
*
|
|
60
60
|
* @param {string} content
|
|
61
61
|
* @param {string} [ownerPackage]
|
|
62
|
+
* @param {(importPath: string) => boolean} [keep] Imports to leave relative:
|
|
63
|
+
* the modules the copy carries in the same layout (see `sharedModulesOf`).
|
|
62
64
|
*/
|
|
63
|
-
export function rewriteImports(content, ownerPackage = CORE_PACKAGE) {
|
|
65
|
+
export function rewriteImports(content, ownerPackage = CORE_PACKAGE, keep = () => false) {
|
|
64
66
|
/** @param {string} importPath */
|
|
65
67
|
const mapTarget = importPath => {
|
|
68
|
+
// A module the copy carries beside the component keeps its relative path.
|
|
69
|
+
if (keep(importPath)) {
|
|
70
|
+
return importPath;
|
|
71
|
+
}
|
|
66
72
|
// Strip ALL leading `../` so a two-levels-up path never yields `<pkg>/..`.
|
|
67
73
|
const rest = importPath.replace(/^(?:\.\.\/)+/, '');
|
|
68
74
|
const parts = rest.split('/');
|
|
@@ -90,6 +96,153 @@ export function rewriteImports(content, ownerPackage = CORE_PACKAGE) {
|
|
|
90
96
|
);
|
|
91
97
|
}
|
|
92
98
|
|
|
99
|
+
/** A private data attribute's owner module (`architecture:public-component-api` INV23). */
|
|
100
|
+
const OWNER_MODULE = /^privateAttributes\/[^/]+\.stylex$/;
|
|
101
|
+
/** A StyleX module, with or without its file extension. */
|
|
102
|
+
const STYLEX_MODULE = /\.stylex(?:\.[cm]?[jt]sx?)?$/;
|
|
103
|
+
/** The extensions an extensionless import may name, in resolution order. */
|
|
104
|
+
const SOURCE_EXTENSIONS = ['.ts', '.tsx', '.mjs', '.js'];
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The relative import specifiers in a module's source.
|
|
108
|
+
* @param {string} content
|
|
109
|
+
* @returns {string[]}
|
|
110
|
+
*/
|
|
111
|
+
function relativeImportsOf(content) {
|
|
112
|
+
const found = [];
|
|
113
|
+
for (const re of [
|
|
114
|
+
/from\s+['"](\.\.?\/[^'"]+)['"]/g,
|
|
115
|
+
/import\(\s*['"](\.\.?\/[^'"]+)['"]/g,
|
|
116
|
+
]) {
|
|
117
|
+
for (const m of content.matchAll(re)) found.push(m[1]);
|
|
118
|
+
}
|
|
119
|
+
return found;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The source file an import resolves to under `srcRoot`, as a path relative to
|
|
124
|
+
* `srcRoot` without its extension (`privateAttributes/press.stylex`), and the
|
|
125
|
+
* file's own relative path; null when it resolves outside `srcRoot` or to no
|
|
126
|
+
* file.
|
|
127
|
+
* @param {string} srcRoot
|
|
128
|
+
* @param {string} fromDir directory of the importing file, relative to srcRoot
|
|
129
|
+
* @param {string} importPath
|
|
130
|
+
* @returns {{module: string, file: string} | null}
|
|
131
|
+
*/
|
|
132
|
+
function resolveSourceModule(srcRoot, fromDir, importPath) {
|
|
133
|
+
const module = path.posix.normalize(path.posix.join(fromDir, importPath));
|
|
134
|
+
if (module.startsWith('..') || path.posix.isAbsolute(module)) return null;
|
|
135
|
+
for (const ext of ['', ...SOURCE_EXTENSIONS]) {
|
|
136
|
+
const file = module + ext;
|
|
137
|
+
const abs = path.join(srcRoot, file);
|
|
138
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isFile()) {
|
|
139
|
+
return {
|
|
140
|
+
module: ext ? module : module.replace(/\.[cm]?[jt]sx?$/, ''),
|
|
141
|
+
file,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The subpaths the owner package exports (`./theme/tokens.stylex`), read from
|
|
150
|
+
* the nearest package.json above its source root.
|
|
151
|
+
* @param {string} srcRoot
|
|
152
|
+
* @returns {Set<string>}
|
|
153
|
+
*/
|
|
154
|
+
function exportedSubpathsOf(srcRoot) {
|
|
155
|
+
let dir = srcRoot;
|
|
156
|
+
for (;;) {
|
|
157
|
+
const manifest = path.join(dir, 'package.json');
|
|
158
|
+
if (fs.existsSync(manifest)) {
|
|
159
|
+
try {
|
|
160
|
+
const {exports} = JSON.parse(fs.readFileSync(manifest, 'utf-8'));
|
|
161
|
+
return new Set(
|
|
162
|
+
exports && typeof exports === 'object' ? Object.keys(exports) : [],
|
|
163
|
+
);
|
|
164
|
+
} catch {
|
|
165
|
+
return new Set();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const parent = path.dirname(dir);
|
|
169
|
+
if (parent === dir) return new Set();
|
|
170
|
+
dir = parent;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The modules a copy carries beside the component, in the package's own
|
|
176
|
+
* layout under the output directory (`spec:AST-110` FR4): each private
|
|
177
|
+
* attribute owner module the component imports, and every unexported StyleX
|
|
178
|
+
* module such a carried module imports, transitively. The component's own
|
|
179
|
+
* imports of them, and theirs of each other, stay relative.
|
|
180
|
+
*
|
|
181
|
+
* @param {string} srcRoot the owner package's source root
|
|
182
|
+
* @param {string} componentDir
|
|
183
|
+
* @param {string[]} componentFiles files of the component that are copied
|
|
184
|
+
* @returns {Map<string, string>} module path (relative to srcRoot, no
|
|
185
|
+
* extension) -> its file path relative to srcRoot
|
|
186
|
+
*/
|
|
187
|
+
export function sharedModulesOf(srcRoot, componentDir, componentFiles) {
|
|
188
|
+
const exported = exportedSubpathsOf(srcRoot);
|
|
189
|
+
const componentRel = path
|
|
190
|
+
.relative(srcRoot, componentDir)
|
|
191
|
+
.split(path.sep)
|
|
192
|
+
.join('/');
|
|
193
|
+
/** @type {Map<string, string>} */
|
|
194
|
+
const carried = new Map();
|
|
195
|
+
/** @type {Array<{fromDir: string, content: string, owner: boolean}>} */
|
|
196
|
+
const queue = componentFiles
|
|
197
|
+
.filter(f => /\.[cm]?[jt]sx?$/.test(f))
|
|
198
|
+
.map(f => ({
|
|
199
|
+
fromDir: componentRel,
|
|
200
|
+
content: fs.readFileSync(path.join(componentDir, f), 'utf-8'),
|
|
201
|
+
owner: false,
|
|
202
|
+
}));
|
|
203
|
+
while (queue.length > 0) {
|
|
204
|
+
const {fromDir, content, owner} = /** @type {typeof queue[number]} */ (
|
|
205
|
+
queue.shift()
|
|
206
|
+
);
|
|
207
|
+
for (const importPath of relativeImportsOf(content)) {
|
|
208
|
+
const target = resolveSourceModule(srcRoot, fromDir, importPath);
|
|
209
|
+
if (target == null || carried.has(target.module)) continue;
|
|
210
|
+
if (target.module.startsWith(componentRel + '/')) continue;
|
|
211
|
+
const isOwner = OWNER_MODULE.test(target.module);
|
|
212
|
+
// A carried module's unexported StyleX dependency travels with it; a
|
|
213
|
+
// component's own imports of other modules are rewritten as before.
|
|
214
|
+
const isPrivateStyle =
|
|
215
|
+
owner &&
|
|
216
|
+
STYLEX_MODULE.test(target.module) &&
|
|
217
|
+
!exported.has(`./${target.module}`);
|
|
218
|
+
if (!isOwner && !isPrivateStyle) continue;
|
|
219
|
+
carried.set(target.module, target.file);
|
|
220
|
+
queue.push({
|
|
221
|
+
fromDir: path.posix.dirname(target.module),
|
|
222
|
+
content: fs.readFileSync(path.join(srcRoot, target.file), 'utf-8'),
|
|
223
|
+
owner: true,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return carried;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A `keep` predicate for `rewriteImports`: true for an import that names one
|
|
232
|
+
* of the carried modules from a file in `fromDir`.
|
|
233
|
+
* @param {Map<string, string>} carried
|
|
234
|
+
* @param {string} fromDir relative to the source root
|
|
235
|
+
* @returns {(importPath: string) => boolean}
|
|
236
|
+
*/
|
|
237
|
+
function keepCarried(carried, fromDir) {
|
|
238
|
+
return importPath => {
|
|
239
|
+
const module = path.posix
|
|
240
|
+
.normalize(path.posix.join(fromDir, importPath))
|
|
241
|
+
.replace(/\.[cm]?[jt]sx?$/, '');
|
|
242
|
+
return carried.has(module);
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
93
246
|
/**
|
|
94
247
|
* Build the maintainer feedback note for a swizzled component.
|
|
95
248
|
* @param {string} component
|
|
@@ -317,19 +470,64 @@ export async function swizzleCopy(component, options = {}) {
|
|
|
317
470
|
label: 'output file',
|
|
318
471
|
});
|
|
319
472
|
}
|
|
473
|
+
// The owner modules the component imports, and their unexported StyleX
|
|
474
|
+
// dependencies, travel with it in the package's layout (spec:AST-110 FR4).
|
|
475
|
+
const srcRoot = path.dirname(componentDir);
|
|
476
|
+
const componentRel = path
|
|
477
|
+
.relative(srcRoot, componentDir)
|
|
478
|
+
.split(path.sep)
|
|
479
|
+
.join('/');
|
|
480
|
+
const carried = sharedModulesOf(srcRoot, componentDir, sourceFiles);
|
|
481
|
+
const shared = [...carried.values()].map(file => {
|
|
482
|
+
const content = rewriteImports(
|
|
483
|
+
fs.readFileSync(path.join(srcRoot, file), 'utf-8'),
|
|
484
|
+
owner.ownerPackage,
|
|
485
|
+
keepCarried(carried, path.posix.dirname(file)),
|
|
486
|
+
);
|
|
487
|
+
const dest = confineWrite(path.join(outputBase, file), cwd, {
|
|
488
|
+
allowAbsolute: true,
|
|
489
|
+
label: 'output file',
|
|
490
|
+
});
|
|
491
|
+
// Something that is not a readable file in its place counts as changed.
|
|
492
|
+
let existing = null;
|
|
493
|
+
if (fs.existsSync(dest)) {
|
|
494
|
+
try {
|
|
495
|
+
existing = fs.readFileSync(dest, 'utf-8');
|
|
496
|
+
} catch {
|
|
497
|
+
existing = '';
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
// A copy another component's swizzle already wrote is reused when it is
|
|
501
|
+
// the same; a changed one is a file this run would overwrite.
|
|
502
|
+
return {
|
|
503
|
+
file,
|
|
504
|
+
dest,
|
|
505
|
+
content,
|
|
506
|
+
unchanged: existing === content,
|
|
507
|
+
exists: existing != null,
|
|
508
|
+
};
|
|
509
|
+
});
|
|
320
510
|
const existingFiles = sourceFiles.filter(f =>
|
|
321
511
|
fs.existsSync(path.join(outputDir, f)),
|
|
322
512
|
);
|
|
323
|
-
|
|
513
|
+
const changedShared = shared.filter(s => s.exists && !s.unchanged);
|
|
514
|
+
if ((existingFiles.length > 0 || changedShared.length > 0) && !overwrite) {
|
|
324
515
|
const relOutputForMsg = path.relative(cwd, outputDir) || '.';
|
|
516
|
+
const sharedNote =
|
|
517
|
+
changedShared.length > 0
|
|
518
|
+
? ` and ${changedShared.length} changed shared module(s) (${changedShared
|
|
519
|
+
.map(s => path.relative(cwd, s.dest))
|
|
520
|
+
.join(', ')})`
|
|
521
|
+
: '';
|
|
325
522
|
throw new AstryxError(
|
|
326
|
-
`Refusing to overwrite ${existingFiles.length} existing file(s) in ${relOutputForMsg}
|
|
523
|
+
`Refusing to overwrite ${existingFiles.length} existing file(s) in ${relOutputForMsg}/${sharedNote}. ` +
|
|
327
524
|
`Re-run with --overwrite (or -f) to replace them.`,
|
|
328
525
|
[],
|
|
329
526
|
ERROR_CODES.ERR_FILE_EXISTS,
|
|
330
527
|
);
|
|
331
528
|
}
|
|
332
529
|
|
|
530
|
+
|
|
333
531
|
const outputDirExisted = fs.existsSync(outputDir);
|
|
334
532
|
try {
|
|
335
533
|
fs.mkdirSync(outputDir, {recursive: true});
|
|
@@ -351,7 +549,7 @@ export async function swizzleCopy(component, options = {}) {
|
|
|
351
549
|
if (!fs.statSync(srcPath).isFile()) continue;
|
|
352
550
|
let content = fs.readFileSync(srcPath, 'utf-8');
|
|
353
551
|
if (file.endsWith('.ts') || file.endsWith('.tsx')) {
|
|
354
|
-
content = rewriteImports(content, owner.ownerPackage);
|
|
552
|
+
content = rewriteImports(content, owner.ownerPackage, keepCarried(carried, componentRel));
|
|
355
553
|
}
|
|
356
554
|
if (
|
|
357
555
|
(file.endsWith('.ts') || file.endsWith('.tsx')) &&
|
|
@@ -381,6 +579,38 @@ export async function swizzleCopy(component, options = {}) {
|
|
|
381
579
|
copied++;
|
|
382
580
|
}
|
|
383
581
|
|
|
582
|
+
/** @type {string[]} folders this run created for carried modules, outermost first */
|
|
583
|
+
const createdDirs = [];
|
|
584
|
+
for (const module of shared) {
|
|
585
|
+
// An identical copy is already in place: it is part of this copy, unwritten.
|
|
586
|
+
if (module.unchanged) {
|
|
587
|
+
copied++;
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
try {
|
|
591
|
+
const original = fs.existsSync(module.dest)
|
|
592
|
+
? fs.readFileSync(module.dest)
|
|
593
|
+
: null;
|
|
594
|
+
const firstCreated = fs.mkdirSync(path.dirname(module.dest), {recursive: true});
|
|
595
|
+
if (firstCreated != null) createdDirs.push(firstCreated);
|
|
596
|
+
written.push({dest: module.dest, original});
|
|
597
|
+
fs.writeFileSync(module.dest, module.content);
|
|
598
|
+
copied++;
|
|
599
|
+
} catch (err) {
|
|
600
|
+
const unrestored = undoCopy(written);
|
|
601
|
+
// Remove the folders this run created, as a failed component-file write
|
|
602
|
+
// does, so a retry finds the output as it was.
|
|
603
|
+
for (const dir of [...createdDirs].reverse()) {
|
|
604
|
+
fs.rmSync(dir, {recursive: true, force: true});
|
|
605
|
+
}
|
|
606
|
+
if (!outputDirExisted) {
|
|
607
|
+
fs.rmSync(outputDir, {recursive: true, force: true});
|
|
608
|
+
}
|
|
609
|
+
throw writeFailed(module.dest, cwd, err, unrestored);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (shared.length > 0) usesStyleX = true;
|
|
613
|
+
|
|
384
614
|
const relOutput = path.relative(cwd, outputDir);
|
|
385
615
|
const copiedFiles = files.filter(
|
|
386
616
|
f =>
|
|
@@ -393,7 +623,12 @@ export async function swizzleCopy(component, options = {}) {
|
|
|
393
623
|
package: owner.package,
|
|
394
624
|
outputDir: relOutput,
|
|
395
625
|
filesCopied: copied,
|
|
396
|
-
|
|
626
|
+
// The owner modules and their unexported style dependencies the copy
|
|
627
|
+
// carries beside the component (spec:AST-110 FR4) are its files too.
|
|
628
|
+
files: [
|
|
629
|
+
...copiedFiles.map(f => f),
|
|
630
|
+
...shared.map(m => path.relative(outputDir, m.dest).split(path.sep).join('/')),
|
|
631
|
+
],
|
|
397
632
|
usesStyleX,
|
|
398
633
|
};
|
|
399
634
|
if (feedback) data.feedback = feedback;
|
|
@@ -76,7 +76,7 @@ export const doc = {
|
|
|
76
76
|
{
|
|
77
77
|
type: 'swizzle.copy',
|
|
78
78
|
description:
|
|
79
|
-
'A receipt after copying the component into the project: the component name, owning package, output directory, files-copied count, the
|
|
79
|
+
'A receipt after copying the component into the project: the component name, owning package, output directory, files-copied count, the copied file names (relative to the component folder, including the private attribute owner modules and their unexported style modules copied beside it, e.g. `../privateAttributes/press.stylex.ts`), whether any file uses StyleX, and, when the owner has an issues URL, feedback ({issuesUrl, ghCommand?}): where to report the gap that led to swizzling.',
|
|
80
80
|
},
|
|
81
81
|
],
|
|
82
82
|
throws: [
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
* @property {string} data.package Owner package the component source was copied from.
|
|
35
35
|
* @property {string} data.outputDir
|
|
36
36
|
* @property {number} data.filesCopied
|
|
37
|
-
* @property {string[]} data.files
|
|
37
|
+
* @property {string[]} data.files Copied file names, relative to the component folder; owner modules and their unexported style modules copied beside it start with `../` (spec:AST-110 FR4).
|
|
38
38
|
* @property {boolean} data.usesStyleX Whether any copied file uses StyleX (requires build-time setup).
|
|
39
39
|
* @property {SwizzleFeedback} [data.feedback]
|
|
40
40
|
*/
|
|
@@ -0,0 +1,19 @@
|
|
|
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: 'useIcon',
|
|
7
|
+
name: 'useIcon — Status Row',
|
|
8
|
+
displayName: 'useIcon — Status Row',
|
|
9
|
+
description:
|
|
10
|
+
'A custom status row that draws the semantic success, warning and error icons with useIcon. The same row renders under the current theme and under a Theme whose icons override those three, and its icons change with no change to the component.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Theme', 'Layout', 'Text'],
|
|
14
|
+
icons: {
|
|
15
|
+
CheckBadgeIcon: 'success',
|
|
16
|
+
ExclamationCircleIcon: 'warning',
|
|
17
|
+
NoSymbolIcon: 'error',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
CheckBadgeIcon,
|
|
7
|
+
ExclamationCircleIcon,
|
|
8
|
+
NoSymbolIcon,
|
|
9
|
+
} from '@heroicons/react/24/solid';
|
|
10
|
+
import {useIcon} from '@astryxdesign/core/Icon';
|
|
11
|
+
import {HStack, VStack} from '@astryxdesign/core/Layout';
|
|
12
|
+
import {Text} from '@astryxdesign/core/Text';
|
|
13
|
+
import {Theme, defineTheme, useTheme} from '@astryxdesign/core/theme';
|
|
14
|
+
|
|
15
|
+
const statusIconTheme = defineTheme({
|
|
16
|
+
name: 'status-icons-docs',
|
|
17
|
+
icons: {
|
|
18
|
+
success: <CheckBadgeIcon width="1em" height="1em" />,
|
|
19
|
+
warning: <ExclamationCircleIcon width="1em" height="1em" />,
|
|
20
|
+
error: <NoSymbolIcon width="1em" height="1em" />,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function StatusRow() {
|
|
25
|
+
const success = useIcon('success');
|
|
26
|
+
const warning = useIcon('warning');
|
|
27
|
+
const error = useIcon('error');
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<HStack gap={4}>
|
|
31
|
+
<HStack gap={1} vAlign="center">
|
|
32
|
+
{success}
|
|
33
|
+
<Text type="body">Passed</Text>
|
|
34
|
+
</HStack>
|
|
35
|
+
<HStack gap={1} vAlign="center">
|
|
36
|
+
{warning}
|
|
37
|
+
<Text type="body">Flaky</Text>
|
|
38
|
+
</HStack>
|
|
39
|
+
<HStack gap={1} vAlign="center">
|
|
40
|
+
{error}
|
|
41
|
+
<Text type="body">Failed</Text>
|
|
42
|
+
</HStack>
|
|
43
|
+
</HStack>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default function UseIconHookUsage() {
|
|
48
|
+
const {mode} = useTheme();
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<VStack gap={4}>
|
|
52
|
+
<VStack gap={2}>
|
|
53
|
+
<Text type="supporting" color="secondary">
|
|
54
|
+
Current theme
|
|
55
|
+
</Text>
|
|
56
|
+
<StatusRow />
|
|
57
|
+
</VStack>
|
|
58
|
+
<Theme theme={statusIconTheme} mode={mode}>
|
|
59
|
+
<VStack gap={2}>
|
|
60
|
+
<Text type="supporting" color="secondary">
|
|
61
|
+
Theme with its own status icons
|
|
62
|
+
</Text>
|
|
63
|
+
<StatusRow />
|
|
64
|
+
</VStack>
|
|
65
|
+
</Theme>
|
|
66
|
+
</VStack>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -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: '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
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
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
|
+
}
|
|
@@ -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',
|
|
8
|
+
displayName: 'usePanGesture — A playing card',
|
|
9
|
+
description:
|
|
10
|
+
'A playing card built with usePanGesture: it follows a drag from a finger, a mouse or a pen with no render per move, snaps back on a gentle release, flies off on a quick flick, and a button discards it from the keyboard.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: [],
|
|
14
|
+
};
|