@astryxdesign/cli 0.7.7-canary.6690e33 → 0.7.7-canary.6cb8b4a
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/assets/discover/official-packages.json +0 -4
- package/assets/templates/blocks/components/Hooks/useGridFocusHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useGridFocusHookUsage.tsx +86 -0
- package/assets/templates/blocks/components/Hooks/useOverflowHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useOverflowHookUsage.tsx +89 -0
- package/assets/templates/blocks/components/Hooks/usePressFeedbackHookUsage.tsx +24 -21
- package/assets/templates/blocks/components/Outline/useOutlineFromMarkdownHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Outline/useOutlineFromMarkdownHookUsage.tsx +69 -0
- package/foundation/discovery/template-adapter.d.mts +7 -3
- package/foundation/discovery/template-adapter.mjs +150 -10
- package/package.json +9 -9
|
@@ -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: 'useGridFocus',
|
|
7
|
+
name: 'useGridFocus — Color Swatch Grid',
|
|
8
|
+
displayName: 'useGridFocus — Color Swatch Grid',
|
|
9
|
+
description:
|
|
10
|
+
'A color swatch picker in rows of eight with one Tab stop. useGridFocus moves focus with the arrow keys, Home and End, and Ctrl+Home and Ctrl+End. Each swatch is a button, so Enter or a click picks it.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 4 / 3,
|
|
13
|
+
componentsUsed: ['Card', 'Text', 'Layout'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {useState} from 'react';
|
|
6
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
7
|
+
import {useGridFocus} from '@astryxdesign/core/hooks';
|
|
8
|
+
import {VStack} from '@astryxdesign/core/Layout';
|
|
9
|
+
import {Text} from '@astryxdesign/core/Text';
|
|
10
|
+
|
|
11
|
+
const COLUMNS = 8;
|
|
12
|
+
|
|
13
|
+
const SWATCHES = [
|
|
14
|
+
{name: 'Red', value: '#e5484d'},
|
|
15
|
+
{name: 'Tomato', value: '#f76b15'},
|
|
16
|
+
{name: 'Amber', value: '#ffc53d'},
|
|
17
|
+
{name: 'Lime', value: '#bdee63'},
|
|
18
|
+
{name: 'Green', value: '#30a46c'},
|
|
19
|
+
{name: 'Teal', value: '#12a594'},
|
|
20
|
+
{name: 'Cyan', value: '#00a2c7'},
|
|
21
|
+
{name: 'Blue', value: '#0090ff'},
|
|
22
|
+
{name: 'Indigo', value: '#3e63dd'},
|
|
23
|
+
{name: 'Violet', value: '#6e56cf'},
|
|
24
|
+
{name: 'Purple', value: '#8e4ec6'},
|
|
25
|
+
{name: 'Plum', value: '#ab4aba'},
|
|
26
|
+
{name: 'Pink', value: '#d6409f'},
|
|
27
|
+
{name: 'Brown', value: '#ad7f58'},
|
|
28
|
+
{name: 'Gray', value: '#8d8d8d'},
|
|
29
|
+
{name: 'Black', value: '#111111'},
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const ROWS = Array.from({length: SWATCHES.length / COLUMNS}, (_, row) =>
|
|
33
|
+
SWATCHES.slice(row * COLUMNS, (row + 1) * COLUMNS),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
export default function UseGridFocusHookUsage() {
|
|
37
|
+
const [selected, setSelected] = useState(SWATCHES[0].name);
|
|
38
|
+
const {gridRef, handleKeyDown, handleFocus} = useGridFocus<HTMLDivElement>({
|
|
39
|
+
columns: COLUMNS,
|
|
40
|
+
cellSelector: '[role="gridcell"]',
|
|
41
|
+
getFocusTarget: cell => cell.querySelector('button'),
|
|
42
|
+
hasRovingTabIndex: true,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<Card padding={4}>
|
|
47
|
+
<VStack gap={3}>
|
|
48
|
+
<Text type="body">Selected: {selected}</Text>
|
|
49
|
+
<div
|
|
50
|
+
ref={gridRef}
|
|
51
|
+
role="grid"
|
|
52
|
+
aria-label="Colors"
|
|
53
|
+
onKeyDown={handleKeyDown}
|
|
54
|
+
onFocus={handleFocus}
|
|
55
|
+
style={{display: 'flex', flexDirection: 'column', gap: 4}}>
|
|
56
|
+
{ROWS.map((row, index) => (
|
|
57
|
+
<div key={index} role="row" style={{display: 'flex', gap: 4}}>
|
|
58
|
+
{row.map(swatch => (
|
|
59
|
+
<div
|
|
60
|
+
key={swatch.name}
|
|
61
|
+
role="gridcell"
|
|
62
|
+
aria-selected={swatch.name === selected}>
|
|
63
|
+
<button
|
|
64
|
+
type="button"
|
|
65
|
+
aria-label={swatch.name}
|
|
66
|
+
aria-pressed={swatch.name === selected}
|
|
67
|
+
onClick={() => setSelected(swatch.name)}
|
|
68
|
+
style={{
|
|
69
|
+
width: 28,
|
|
70
|
+
height: 28,
|
|
71
|
+
border: 'none',
|
|
72
|
+
boxShadow:
|
|
73
|
+
'inset 0 0 0 1px var(--color-border-emphasized)',
|
|
74
|
+
borderRadius: 4,
|
|
75
|
+
background: swatch.value,
|
|
76
|
+
}}
|
|
77
|
+
/>
|
|
78
|
+
</div>
|
|
79
|
+
))}
|
|
80
|
+
</div>
|
|
81
|
+
))}
|
|
82
|
+
</div>
|
|
83
|
+
</VStack>
|
|
84
|
+
</Card>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -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: 'useOverflow',
|
|
7
|
+
name: 'useOverflow — Editor Toolbar',
|
|
8
|
+
displayName: 'useOverflow — Editor Toolbar',
|
|
9
|
+
description:
|
|
10
|
+
'Resize the card: useOverflow moves the toolbar actions that do not fit into a More menu, and shows them again when the card gets wider.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 16 / 9,
|
|
13
|
+
componentsUsed: ['Card', 'Button', 'DropdownMenu'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import * as stylex from '@stylexjs/stylex';
|
|
6
|
+
import {useOverflow} from '@astryxdesign/core/hooks';
|
|
7
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
8
|
+
import {Card} from '@astryxdesign/core/Card';
|
|
9
|
+
import {DropdownMenu} from '@astryxdesign/core/DropdownMenu';
|
|
10
|
+
|
|
11
|
+
const actions = [
|
|
12
|
+
'Bold',
|
|
13
|
+
'Italic',
|
|
14
|
+
'Underline',
|
|
15
|
+
'Strikethrough',
|
|
16
|
+
'Link',
|
|
17
|
+
'Quote',
|
|
18
|
+
'Code',
|
|
19
|
+
'Clear formatting',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const GAP = 4;
|
|
23
|
+
|
|
24
|
+
const styles = stylex.create({
|
|
25
|
+
box: {
|
|
26
|
+
resize: 'horizontal',
|
|
27
|
+
overflow: 'hidden',
|
|
28
|
+
width: 480,
|
|
29
|
+
minWidth: 120,
|
|
30
|
+
maxWidth: '100%',
|
|
31
|
+
},
|
|
32
|
+
row: {
|
|
33
|
+
display: 'flex',
|
|
34
|
+
gap: GAP,
|
|
35
|
+
},
|
|
36
|
+
// Takes no space and paints nothing, so the hook measures every item
|
|
37
|
+
// without a flicker. Do not use display: none, which stops measurement.
|
|
38
|
+
measure: {
|
|
39
|
+
position: 'absolute',
|
|
40
|
+
visibility: 'hidden',
|
|
41
|
+
height: 0,
|
|
42
|
+
overflow: 'hidden',
|
|
43
|
+
whiteSpace: 'nowrap',
|
|
44
|
+
pointerEvents: 'none',
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
function MoreMenu({items}: {items: string[]}) {
|
|
49
|
+
return (
|
|
50
|
+
<DropdownMenu
|
|
51
|
+
button={{label: 'More', variant: 'ghost', size: 'sm'}}
|
|
52
|
+
items={items.map(label => ({label}))}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default function UseOverflowHookUsage() {
|
|
58
|
+
const {containerRef, measureRef, visibleCount, hasOverflow} = useOverflow(
|
|
59
|
+
actions.length,
|
|
60
|
+
{gap: GAP},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<Card padding={2} xstyle={styles.box}>
|
|
65
|
+
<div
|
|
66
|
+
ref={measureRef}
|
|
67
|
+
aria-hidden="true"
|
|
68
|
+
inert
|
|
69
|
+
{...stylex.props(styles.row, styles.measure)}>
|
|
70
|
+
{actions.map(label => (
|
|
71
|
+
<Button key={label} label={label} variant="ghost" size="sm" />
|
|
72
|
+
))}
|
|
73
|
+
<div data-overflow-indicator>
|
|
74
|
+
<MoreMenu items={actions} />
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
<div
|
|
78
|
+
ref={containerRef}
|
|
79
|
+
role="group"
|
|
80
|
+
aria-label="Text formatting"
|
|
81
|
+
{...stylex.props(styles.row)}>
|
|
82
|
+
{actions.slice(0, visibleCount).map(label => (
|
|
83
|
+
<Button key={label} label={label} variant="ghost" size="sm" />
|
|
84
|
+
))}
|
|
85
|
+
{hasOverflow && <MoreMenu items={actions.slice(visibleCount)} />}
|
|
86
|
+
</div>
|
|
87
|
+
</Card>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
focusOutlineStyles,
|
|
10
10
|
interactionOverlayStyles,
|
|
11
11
|
} from '@astryxdesign/core/utils';
|
|
12
|
-
import {PlayingCardContents} from '@astryx-docs/demo-content';
|
|
12
|
+
import {DemoHint, PlayingCardContents} from '@astryx-docs/demo-content';
|
|
13
13
|
|
|
14
14
|
export default function UsePressFeedbackHookUsage() {
|
|
15
15
|
const pressable = usePressFeedback();
|
|
@@ -17,25 +17,28 @@ export default function UsePressFeedbackHookUsage() {
|
|
|
17
17
|
const flip = () => setIsFaceUp(faceUp => !faceUp);
|
|
18
18
|
|
|
19
19
|
return (
|
|
20
|
-
<
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
20
|
+
<DemoHint text="Press and hold the card to see the press; tap, click, Enter or Space to flip it.">
|
|
21
|
+
<Card
|
|
22
|
+
{...pressable}
|
|
23
|
+
xstyle={[
|
|
24
|
+
interactionOverlayStyles.backgroundImage,
|
|
25
|
+
focusOutlineStyles.focusVisible,
|
|
26
|
+
]}
|
|
27
|
+
width={120}
|
|
28
|
+
padding={1}
|
|
29
|
+
role="button"
|
|
30
|
+
tabIndex={0}
|
|
31
|
+
aria-label="Flip card"
|
|
32
|
+
aria-pressed={!isFaceUp}
|
|
33
|
+
onClick={flip}
|
|
34
|
+
onKeyDown={event => {
|
|
35
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
flip();
|
|
38
|
+
}
|
|
39
|
+
}}>
|
|
40
|
+
<PlayingCardContents rank="Q" suit="hearts" faceDown={!isFaceUp} />
|
|
41
|
+
</Card>
|
|
42
|
+
</DemoHint>
|
|
40
43
|
);
|
|
41
44
|
}
|
|
@@ -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: 'useOutlineFromMarkdown',
|
|
7
|
+
name: 'useOutlineFromMarkdown — Docs Article',
|
|
8
|
+
displayName: 'useOutlineFromMarkdown — Docs Article',
|
|
9
|
+
description:
|
|
10
|
+
'A docs-style article with an Outline next to it, built from the same Markdown string through useOutlineFromMarkdown. "Add section" appends a heading to the string, and a new entry that links to it appears in the outline.',
|
|
11
|
+
isReady: true,
|
|
12
|
+
aspectRatio: 16 / 9,
|
|
13
|
+
componentsUsed: ['Markdown', 'Outline', 'ScrollableArea', 'Button', 'Layout'],
|
|
14
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
import {useRef, useState} from 'react';
|
|
6
|
+
import {Button} from '@astryxdesign/core/Button';
|
|
7
|
+
import {HStack, VStack} from '@astryxdesign/core/Layout';
|
|
8
|
+
import {Markdown} from '@astryxdesign/core/Markdown';
|
|
9
|
+
import {Outline, useOutlineFromMarkdown} from '@astryxdesign/core/Outline';
|
|
10
|
+
import {ScrollableArea} from '@astryxdesign/core/ScrollableArea';
|
|
11
|
+
|
|
12
|
+
const ARTICLE = `## Before you start
|
|
13
|
+
|
|
14
|
+
Check that your project builds on the current release. Commit your work, so
|
|
15
|
+
that you can compare the result of the upgrade.
|
|
16
|
+
|
|
17
|
+
## Run the upgrade
|
|
18
|
+
|
|
19
|
+
Update the packages, then rebuild the project. The upgrade changes no source
|
|
20
|
+
files.
|
|
21
|
+
|
|
22
|
+
### Update the packages
|
|
23
|
+
|
|
24
|
+
Install the new version of every package at the same time. Mixed versions fail
|
|
25
|
+
the build.
|
|
26
|
+
|
|
27
|
+
### Rebuild the project
|
|
28
|
+
|
|
29
|
+
Delete the build output first. A stale cache can hide a real error.
|
|
30
|
+
|
|
31
|
+
## Check the result
|
|
32
|
+
|
|
33
|
+
Open the pages that you use most, and compare them with the earlier build.
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
const NEW_SECTION = `
|
|
37
|
+
## New section
|
|
38
|
+
|
|
39
|
+
The button added this heading to the Markdown string.
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
export default function UseOutlineFromMarkdownHookUsage() {
|
|
43
|
+
const [markdown, setMarkdown] = useState(ARTICLE);
|
|
44
|
+
const items = useOutlineFromMarkdown(markdown);
|
|
45
|
+
const articleRef = useRef<HTMLDivElement>(null);
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<HStack gap={6} align="start" width="100%">
|
|
49
|
+
<ScrollableArea
|
|
50
|
+
ref={articleRef}
|
|
51
|
+
label="Article"
|
|
52
|
+
height={320}
|
|
53
|
+
width="100%">
|
|
54
|
+
<Markdown>{markdown}</Markdown>
|
|
55
|
+
</ScrollableArea>
|
|
56
|
+
<VStack gap={3} align="start" width={220}>
|
|
57
|
+
{/* A non-zero offset moves only the article. With 0, a click also moves
|
|
58
|
+
the page around the article. */}
|
|
59
|
+
<Outline items={items} scrollContainerRef={articleRef} offset={8} />
|
|
60
|
+
<Button
|
|
61
|
+
label="Add section"
|
|
62
|
+
variant="secondary"
|
|
63
|
+
size="sm"
|
|
64
|
+
onClick={() => setMarkdown(current => current + NEW_SECTION)}
|
|
65
|
+
/>
|
|
66
|
+
</VStack>
|
|
67
|
+
</HStack>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -69,7 +69,11 @@ export function replaceDemoMedia(source: string, { videoPlaceholder }?: {
|
|
|
69
69
|
/**
|
|
70
70
|
* Replace each JSX element imported from {@link DEMO_CONTENT_MODULE} with a
|
|
71
71
|
* generic `<Text>` placeholder and remove that import, so emitted source never
|
|
72
|
-
* depends on content only Astryx's previews provide.
|
|
72
|
+
* depends on content only Astryx's previews provide. An element that wraps one
|
|
73
|
+
* element, such as a hint shown under a demo, is removed instead: its child
|
|
74
|
+
* takes its place, with the indentation the child would have had without it.
|
|
75
|
+
* `demoContentReplaced` counts the placeholders, because only they need the
|
|
76
|
+
* consumer's own content.
|
|
73
77
|
*
|
|
74
78
|
* Every check resolves a name to its binding through the module's scope, so a
|
|
75
79
|
* local declaration or parameter with the same name never counts as the same
|
|
@@ -79,12 +83,12 @@ export function replaceDemoMedia(source: string, { videoPlaceholder }?: {
|
|
|
79
83
|
* writes the name `Text`.
|
|
80
84
|
*
|
|
81
85
|
* Only a plain static use is replaced: a named import used only as a JSX tag
|
|
82
|
-
* whose children
|
|
86
|
+
* whose children are whitespace or one element, and whose props are literal values or
|
|
83
87
|
* read only bindings that code outside the replaced elements still reads. So
|
|
84
88
|
* `faceDown={!isOpen}` is safe when the same `isOpen` is also read elsewhere.
|
|
85
89
|
* Anything else (a namespace or type import, a re-export, a dynamic import, the
|
|
86
90
|
* element used as a value or a type, a spread or computed prop, a prop whose
|
|
87
|
-
* binding nothing else reads, children) throws, naming the element, rather than
|
|
91
|
+
* binding nothing else reads, other children) throws, naming the element, rather than
|
|
88
92
|
* leaving a reference behind, leaving a binding unused, or dropping code the
|
|
89
93
|
* rest of the file relies on. Source that never names the module is returned
|
|
90
94
|
* as is.
|
|
@@ -409,7 +409,11 @@ const TEXT_ALIAS = 'AstryxText';
|
|
|
409
409
|
/**
|
|
410
410
|
* Replace each JSX element imported from {@link DEMO_CONTENT_MODULE} with a
|
|
411
411
|
* generic `<Text>` placeholder and remove that import, so emitted source never
|
|
412
|
-
* depends on content only Astryx's previews provide.
|
|
412
|
+
* depends on content only Astryx's previews provide. An element that wraps one
|
|
413
|
+
* element, such as a hint shown under a demo, is removed instead: its child
|
|
414
|
+
* takes its place, with the indentation the child would have had without it.
|
|
415
|
+
* `demoContentReplaced` counts the placeholders, because only they need the
|
|
416
|
+
* consumer's own content.
|
|
413
417
|
*
|
|
414
418
|
* Every check resolves a name to its binding through the module's scope, so a
|
|
415
419
|
* local declaration or parameter with the same name never counts as the same
|
|
@@ -419,12 +423,12 @@ const TEXT_ALIAS = 'AstryxText';
|
|
|
419
423
|
* writes the name `Text`.
|
|
420
424
|
*
|
|
421
425
|
* Only a plain static use is replaced: a named import used only as a JSX tag
|
|
422
|
-
* whose children
|
|
426
|
+
* whose children are whitespace or one element, and whose props are literal values or
|
|
423
427
|
* read only bindings that code outside the replaced elements still reads. So
|
|
424
428
|
* `faceDown={!isOpen}` is safe when the same `isOpen` is also read elsewhere.
|
|
425
429
|
* Anything else (a namespace or type import, a re-export, a dynamic import, the
|
|
426
430
|
* element used as a value or a type, a spread or computed prop, a prop whose
|
|
427
|
-
* binding nothing else reads, children) throws, naming the element, rather than
|
|
431
|
+
* binding nothing else reads, other children) throws, naming the element, rather than
|
|
428
432
|
* leaving a reference behind, leaving a binding unused, or dropping code the
|
|
429
433
|
* rest of the file relies on. Source that never names the module is returned
|
|
430
434
|
* as is.
|
|
@@ -520,6 +524,13 @@ export function replaceDemoContent(source) {
|
|
|
520
524
|
},
|
|
521
525
|
});
|
|
522
526
|
|
|
527
|
+
const wrappers = elements.filter(
|
|
528
|
+
element => wrappedElement(element.node) !== null,
|
|
529
|
+
);
|
|
530
|
+
if (wrappers.length > 0) {
|
|
531
|
+
return replaceDemoContent(unwrapDemoWrappers(source, wrappers));
|
|
532
|
+
}
|
|
533
|
+
|
|
523
534
|
/** @param {any} node */
|
|
524
535
|
const isReplaced = node =>
|
|
525
536
|
elements.some(
|
|
@@ -638,14 +649,147 @@ function uniqueName(base, written) {
|
|
|
638
649
|
return name;
|
|
639
650
|
}
|
|
640
651
|
|
|
652
|
+
/**
|
|
653
|
+
* Replace each outermost demo-content wrapper with the element it wraps.
|
|
654
|
+
* Wrappers inside a kept child go on the next pass, when they are outermost.
|
|
655
|
+
* Throws when a wrapper's prop reads a binding that nothing outside the
|
|
656
|
+
* wrappers' opening tags reads, so the removal would leave it unused.
|
|
657
|
+
* @param {string} source
|
|
658
|
+
* @param {any[]} wrappers - Element paths whose element wraps one element.
|
|
659
|
+
* @returns {string}
|
|
660
|
+
*/
|
|
661
|
+
function unwrapDemoWrappers(source, wrappers) {
|
|
662
|
+
/** @param {any} node */
|
|
663
|
+
const isInWrapperTag = node =>
|
|
664
|
+
wrappers.some(
|
|
665
|
+
wrapper =>
|
|
666
|
+
wrapper.node.openingElement.start <= node.start &&
|
|
667
|
+
node.end <= wrapper.node.openingElement.end,
|
|
668
|
+
);
|
|
669
|
+
for (const wrapper of wrappers) {
|
|
670
|
+
for (const variable of staticPropReads(wrapper.node)) {
|
|
671
|
+
const binding = wrapper.scope.getBinding(variable);
|
|
672
|
+
if (!binding || isExportedDeclaration(binding)) continue;
|
|
673
|
+
if (
|
|
674
|
+
binding.referencePaths.every((/** @type {any} */ ref) =>
|
|
675
|
+
isInWrapperTag(ref.node),
|
|
676
|
+
)
|
|
677
|
+
) {
|
|
678
|
+
throw unsafeDemoContent(
|
|
679
|
+
`<${wrapper.node.openingElement.name.name}> reads ${variable}, and nothing else in the file reads it, so the copy would leave it unused`,
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const outermost = wrappers.filter(
|
|
685
|
+
wrapper =>
|
|
686
|
+
!wrappers.some(
|
|
687
|
+
other =>
|
|
688
|
+
other !== wrapper &&
|
|
689
|
+
other.node.start <= wrapper.node.start &&
|
|
690
|
+
wrapper.node.end <= other.node.end,
|
|
691
|
+
),
|
|
692
|
+
);
|
|
693
|
+
let output = source;
|
|
694
|
+
for (const wrapper of outermost.sort((a, b) => b.node.start - a.node.start)) {
|
|
695
|
+
output =
|
|
696
|
+
output.slice(0, wrapper.node.start) +
|
|
697
|
+
dedentedChild(source, wrapper) +
|
|
698
|
+
output.slice(wrapper.node.end);
|
|
699
|
+
}
|
|
700
|
+
return output;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* The source of the element a wrapper holds, with each of its later lines
|
|
705
|
+
* moved left by the indent the wrapper added. A line that starts inside a
|
|
706
|
+
* string keeps its whitespace, because that whitespace is part of the value.
|
|
707
|
+
* @param {string} source
|
|
708
|
+
* @param {any} wrapper - An element path whose element wraps one element.
|
|
709
|
+
* @returns {string}
|
|
710
|
+
*/
|
|
711
|
+
function dedentedChild(source, wrapper) {
|
|
712
|
+
const child = /** @type {any} */ (wrappedElement(wrapper.node));
|
|
713
|
+
/** @param {number} offset */
|
|
714
|
+
const indentOfLine = offset => {
|
|
715
|
+
const lineStart = source.lastIndexOf('\n', offset - 1) + 1;
|
|
716
|
+
return /^[ \t]*/u.exec(source.slice(lineStart))?.[0].length ?? 0;
|
|
717
|
+
};
|
|
718
|
+
const depth = indentOfLine(child.start) - indentOfLine(wrapper.node.start);
|
|
719
|
+
/** @type {any[]} */
|
|
720
|
+
const strings = [];
|
|
721
|
+
wrapper.traverse({
|
|
722
|
+
'StringLiteral|TemplateLiteral'(/** @type {any} */ path) {
|
|
723
|
+
strings.push(path.node);
|
|
724
|
+
},
|
|
725
|
+
});
|
|
726
|
+
const leadingIndent = new RegExp(`^[ \\t]{0,${Math.max(depth, 0)}}`, 'u');
|
|
727
|
+
let lineStart = child.start;
|
|
728
|
+
return source
|
|
729
|
+
.slice(child.start, child.end)
|
|
730
|
+
.split('\n')
|
|
731
|
+
.map((line, index) => {
|
|
732
|
+
const start = lineStart;
|
|
733
|
+
lineStart += line.length + 1;
|
|
734
|
+
if (
|
|
735
|
+
index === 0 ||
|
|
736
|
+
strings.some(string => string.start < start && start < string.end)
|
|
737
|
+
) {
|
|
738
|
+
return line;
|
|
739
|
+
}
|
|
740
|
+
return line.replace(leadingIndent, '');
|
|
741
|
+
})
|
|
742
|
+
.join('\n');
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* The one element a demo-content element wraps, or null when its children are
|
|
747
|
+
* only whitespace. Throws for any other children.
|
|
748
|
+
* @param {any} element
|
|
749
|
+
* @returns {any}
|
|
750
|
+
*/
|
|
751
|
+
function wrappedElement(element) {
|
|
752
|
+
const content = element.children.filter(
|
|
753
|
+
(/** @type {any} */ child) =>
|
|
754
|
+
!(child.type === 'JSXText' && child.value.trim() === ''),
|
|
755
|
+
);
|
|
756
|
+
if (content.length === 0) return null;
|
|
757
|
+
if (
|
|
758
|
+
content.length === 1 &&
|
|
759
|
+
(content[0].type === 'JSXElement' || content[0].type === 'JSXFragment')
|
|
760
|
+
) {
|
|
761
|
+
return content[0];
|
|
762
|
+
}
|
|
763
|
+
throw unsafeDemoContent(
|
|
764
|
+
`<${element.openingElement.name.name}> has children other than one element`,
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
|
|
641
768
|
/**
|
|
642
769
|
* Throw unless a demo-content element can be replaced without dropping code:
|
|
643
|
-
* each prop a literal or a read of variables (see {@link
|
|
644
|
-
* no children but whitespace.
|
|
770
|
+
* each prop a literal or a read of variables (see {@link staticPropReads}),
|
|
771
|
+
* and no children but whitespace.
|
|
645
772
|
* @param {any} element
|
|
646
773
|
* @returns {string[]} The variables its props read.
|
|
647
774
|
*/
|
|
648
775
|
function assertStaticDemoElement(element) {
|
|
776
|
+
const variables = staticPropReads(element);
|
|
777
|
+
for (const child of element.children) {
|
|
778
|
+
if (child.type === 'JSXText' && child.value.trim() === '') continue;
|
|
779
|
+
throw unsafeDemoContent(
|
|
780
|
+
`<${element.openingElement.name.name}> has children`,
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
return variables;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Throw unless each prop of a demo-content element is a literal or a read of
|
|
788
|
+
* variables (see {@link isVariableRead}).
|
|
789
|
+
* @param {any} element
|
|
790
|
+
* @returns {string[]} The variables its props read.
|
|
791
|
+
*/
|
|
792
|
+
function staticPropReads(element) {
|
|
649
793
|
const name = element.openingElement.name.name;
|
|
650
794
|
/** @type {string[]} */
|
|
651
795
|
const variables = [];
|
|
@@ -666,10 +810,6 @@ function assertStaticDemoElement(element) {
|
|
|
666
810
|
`<${name}> prop ${attribute.name.name ?? ''} is not a literal value or a variable read`,
|
|
667
811
|
);
|
|
668
812
|
}
|
|
669
|
-
for (const child of element.children) {
|
|
670
|
-
if (child.type === 'JSXText' && child.value.trim() === '') continue;
|
|
671
|
-
throw unsafeDemoContent(`<${name}> has children`);
|
|
672
|
-
}
|
|
673
813
|
return variables;
|
|
674
814
|
}
|
|
675
815
|
|
|
@@ -715,7 +855,7 @@ function isLiteralExpression(node) {
|
|
|
715
855
|
/** @param {string} reason */
|
|
716
856
|
function unsafeDemoContent(reason) {
|
|
717
857
|
return new Error(
|
|
718
|
-
`Demo content from ${DEMO_CONTENT_MODULE} cannot be replaced safely: ${reason}. Use each element as a JSX tag with no children, and give each prop a literal or a variable the file also reads elsewhere.`,
|
|
858
|
+
`Demo content from ${DEMO_CONTENT_MODULE} cannot be replaced safely: ${reason}. Use each element as a JSX tag with no children or with one element child, and give each prop a literal or a variable the file also reads elsewhere.`,
|
|
719
859
|
);
|
|
720
860
|
}
|
|
721
861
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/cli",
|
|
3
|
-
"version": "0.7.7-canary.
|
|
3
|
+
"version": "0.7.7-canary.6cb8b4a",
|
|
4
4
|
"displayName": "CLI",
|
|
5
5
|
"description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
@@ -124,10 +124,10 @@
|
|
|
124
124
|
"zod": "^4.4.3"
|
|
125
125
|
},
|
|
126
126
|
"peerDependencies": {
|
|
127
|
-
"@astryxdesign/charts": "0.7.7-canary.
|
|
128
|
-
"@astryxdesign/core": "0.7.7-canary.
|
|
129
|
-
"@astryxdesign/lab": "0.7.7-canary.
|
|
130
|
-
"@astryxdesign/theme-neutral": "0.7.7-canary.
|
|
127
|
+
"@astryxdesign/charts": "0.7.7-canary.6cb8b4a",
|
|
128
|
+
"@astryxdesign/core": "0.7.7-canary.6cb8b4a",
|
|
129
|
+
"@astryxdesign/lab": "0.7.7-canary.6cb8b4a",
|
|
130
|
+
"@astryxdesign/theme-neutral": "0.7.7-canary.6cb8b4a"
|
|
131
131
|
},
|
|
132
132
|
"peerDependenciesMeta": {
|
|
133
133
|
"@astryxdesign/charts": {
|
|
@@ -144,10 +144,10 @@
|
|
|
144
144
|
}
|
|
145
145
|
},
|
|
146
146
|
"devDependencies": {
|
|
147
|
-
"@astryxdesign/charts": "0.7.7-canary.
|
|
148
|
-
"@astryxdesign/core": "0.7.7-canary.
|
|
149
|
-
"@astryxdesign/lab": "0.7.7-canary.
|
|
150
|
-
"@astryxdesign/theme-neutral": "0.7.7-canary.
|
|
147
|
+
"@astryxdesign/charts": "0.7.7-canary.6cb8b4a",
|
|
148
|
+
"@astryxdesign/core": "0.7.7-canary.6cb8b4a",
|
|
149
|
+
"@astryxdesign/lab": "0.7.7-canary.6cb8b4a",
|
|
150
|
+
"@astryxdesign/theme-neutral": "0.7.7-canary.6cb8b4a",
|
|
151
151
|
"@heroicons/react": "^2.2.0",
|
|
152
152
|
"@stylexjs/stylex": "^0.19.0",
|
|
153
153
|
"@types/babel__core": "^7.20.5",
|