堆叠布局 · Stack
堆叠布局用于布局场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。
安装与前置条件
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0
需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。
示例与使用方式
- 堆叠布局 · 交互示例 1:首先体验这个示例,再查看代码理解组件的组合方式。
- 堆叠布局 · 综合展示 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 堆叠布局 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 堆叠布局 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 堆叠布局 · 交互示例 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
API 与上游详细参考
优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。
Stack
Stack arranges items in a row or column with consistent spacing. Use the gap prop to control the space between items.
使用建议
- Use the gap prop for spacing between items; don't add margins manually.
- Use StackItem with size="fill" to make one item stretch and fill the leftover space.
- 避免:Nest stacks inside stacks; try wrap="wrap" first to let items flow to the next line.
主题 API
{
"targets": [
{
"className": "astryx-stack",
"visualProps": [
"direction",
"gap",
"wrap"
]
},
{
"className": "astryx-stack-item",
"visualProps": [
"size"
]
}
]
}
Stack
Unified stack layout component with a direction prop. Use direction="horizontal" for left-to-right flow or direction="vertical" (default) for top-to-bottom. For convenience, HStack and VStack are pre-configured wrappers.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| direction | 'horizontal' | 'vertical' | 'vertical' | Direction of the stack layout. 'horizontal' flows items left-to-right (like HStack), 'vertical' flows top-to-bottom (like VStack). Note: the value is 'horizontal', NOT 'row'. |
| gap | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Spacing step (number literal): 0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10. Pass as a JSX number expression e.g. gap={4}, NOT a string like gap="4". | |
| padding | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inner padding on all sides, using the spacing scale. Pass as a JSX number expression e.g. padding={3}. | |
| paddingInline | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline (horizontal) padding. Overrides padding on the inline axis when both are set. | |
| paddingInlineStart | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline-start padding, using the spacing scale (left in LTR, right in RTL). Overrides paddingInline and padding on that edge only. | |
| paddingInlineEnd | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline-end padding, using the spacing scale (right in LTR, left in RTL). Overrides paddingInline and padding on that edge only. | |
| paddingBlock | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block (vertical) padding. Overrides padding on the block axis when both are set. | |
| paddingBlockStart | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block-start (top) padding, using the spacing scale. Overrides paddingBlock and padding on that edge only. | |
| paddingBlockEnd | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block-end (bottom) padding, using the spacing scale. Overrides paddingBlock and padding on that edge only. | |
| isScrollable | boolean | false | Enables scrollable overflow (overflow: auto). |
| width | SizeValue | Width of the stack container. Numbers are treated as pixels, strings are used as-is (e.g., '100%'). | |
| height | SizeValue | Height of the stack container. Numbers are treated as pixels, strings are used as-is (e.g., '100%'). | |
| maxWidth | SizeValue | Maximum width of the stack container. | |
| minHeight | SizeValue | Minimum height of the stack container. | |
| hAlign | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | 'stretch' | Horizontal alignment. When direction='horizontal': main-axis (justify-content). When direction='vertical': cross-axis (align-items). | |
| vAlign | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | 'stretch' | Vertical alignment. When direction='horizontal': cross-axis (align-items). When direction='vertical': main-axis (justify-content). | |
| justify | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | Main-axis alignment alias. Resolves to hAlign (horizontal) or vAlign (vertical). Note: use 'between', NOT 'space-between'. | |
| align | 'start' | 'center' | 'end' | 'stretch' | Cross-axis alignment alias. Mirrors CSS align-items. | |
| wrap | 'nowrap' | 'wrap' | 'wrap-reverse' | 'nowrap' | Flex wrap behavior. |
| as | ElementType | 'div' | HTML element to render as the stack container. |
| children | ReactNode | Stack content. | |
| xstyle | StyleXStyles | StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value, not an inline style object like style={{}}. |
H Stack
水平堆叠组件,将元素从左到右排列。支持多态渲染。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| gap | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | 间距步进(数字字面量):0、0.5、1、1.5、2、3、4、5、6、8、10。在 JSX 中使用数字表达式 e.g. gap={4},不要使用字符串 gap="4"。 | |
| padding | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inner padding on all sides, using the spacing scale (0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10). Matches the padding prop on Card, LayoutContent, and LayoutPanel. Pass as a JSX number expression e.g. padding={3}. | |
| paddingInline | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline (horizontal) padding, using the spacing scale. Overrides padding on the inline axis when both are set. | |
| paddingInlineStart | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline-start padding, using the spacing scale (left in LTR, right in RTL). Overrides paddingInline and padding on that edge only. | |
| paddingInlineEnd | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline-end padding, using the spacing scale (right in LTR, left in RTL). Overrides paddingInline and padding on that edge only. | |
| paddingBlock | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block (vertical) padding, using the spacing scale. Overrides padding on the block axis when both are set. | |
| paddingBlockStart | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block-start (top) padding, using the spacing scale. Overrides paddingBlock and padding on that edge only. | |
| paddingBlockEnd | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block-end (bottom) padding, using the spacing scale. Overrides paddingBlock and padding on that edge only. | |
| isScrollable | boolean | false | Enables scrollable overflow (overflow: auto). Matches isScrollable on LayoutContent and LayoutPanel. |
| width | SizeValue | 堆叠容器的宽度。数字按像素处理,字符串原样使用(如 '100%')。 | |
| height | SizeValue | 堆叠容器的高度。数字按像素处理,字符串原样使用(如 '100%')。 | |
| maxWidth | SizeValue | Maximum width of the stack container. Numbers are treated as pixels, strings are used as-is (e.g., '100%'). | |
| minHeight | SizeValue | Minimum height of the stack container. Numbers are treated as pixels, strings are used as-is (e.g., '100%'). | |
| hAlign | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | 水平(主轴)对齐方式。 | |
| vAlign | 'start' | 'center' | 'end' | 'stretch' | 'stretch' | 元素的垂直(交叉轴)对齐方式。 |
| justify | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | hAlign 的别名。对应 CSS justify-content。 | |
| align | 'start' | 'center' | 'end' | 'stretch' | vAlign 的别名。对应 CSS align-items。 | |
| wrap | 'nowrap' | 'wrap' | 'wrap-reverse' | 'nowrap' | Flex 换行行为。 |
| as | ElementType | 'div' | 作为堆叠容器渲染的 HTML 元素。 |
| children | ReactNode | 堆叠内容。 | |
| xstyle | StyleXStyles | 用于布局自定义的 StyleX 样式(外边距、定位、尺寸)。必须是 stylex.create() 的值,而非内联样式对象如 style={{}}。 |
V Stack
垂直堆叠组件,将元素从上到下排列。支持多态渲染。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| gap | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | 间距步进(数字字面量):0、0.5、1、1.5、2、3、4、5、6、8、10。在 JSX 中使用数字表达式 e.g. gap={4},不要使用字符串 gap="4"。 | |
| padding | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inner padding on all sides, using the spacing scale (0, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 8, 10). Matches the padding prop on Card, LayoutContent, and LayoutPanel. Pass as a JSX number expression e.g. padding={3}. | |
| paddingInline | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline (horizontal) padding, using the spacing scale. Overrides padding on the inline axis when both are set. | |
| paddingInlineStart | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline-start padding, using the spacing scale (left in LTR, right in RTL). Overrides paddingInline and padding on that edge only. | |
| paddingInlineEnd | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Inline-end padding, using the spacing scale (right in LTR, left in RTL). Overrides paddingInline and padding on that edge only. | |
| paddingBlock | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block (vertical) padding, using the spacing scale. Overrides padding on the block axis when both are set. | |
| paddingBlockStart | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block-start (top) padding, using the spacing scale. Overrides paddingBlock and padding on that edge only. | |
| paddingBlockEnd | 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | Block-end (bottom) padding, using the spacing scale. Overrides paddingBlock and padding on that edge only. | |
| isScrollable | boolean | false | Enables scrollable overflow (overflow: auto). Matches isScrollable on LayoutContent and LayoutPanel. |
| width | SizeValue | 堆叠容器的宽度。数字按像素处理,字符串原样使用(如 '100%')。 | |
| height | SizeValue | 堆叠容器的高度。数字按像素处理,字符串原样使用(如 '100%')。 | |
| maxWidth | SizeValue | Maximum width of the stack container. Numbers are treated as pixels, strings are used as-is (e.g., '100%'). | |
| minHeight | SizeValue | Minimum height of the stack container. Numbers are treated as pixels, strings are used as-is (e.g., '100%'). | |
| hAlign | 'start' | 'center' | 'end' | 'stretch' | 'stretch' | 元素的水平(交叉轴)对齐方式。 |
| vAlign | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | 垂直(主轴)对齐方式。 | |
| justify | 'start' | 'center' | 'end' | 'between' | 'around' | 'evenly' | vAlign 的别名。对应 CSS justify-content。 | |
| align | 'start' | 'center' | 'end' | 'stretch' | hAlign 的别名。对应 CSS align-items。 | |
| wrap | 'nowrap' | 'wrap' | 'wrap-reverse' | 'nowrap' | Flex 换行行为。 |
| as | ElementType | 'div' | 作为堆叠容器渲染的 HTML 元素。 |
| children | ReactNode | 堆叠内容。 |
Stack Item
堆叠子元素,用于控制堆叠中单个元素的行为。支持多态渲染。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| size | 'static' | 'fill' | 'static' | Flex 增长行为:static 保持自然尺寸,fill 扩展以占据剩余空间。 |
| isScrollable | boolean | false | Enables scrollable overflow (overflow: auto). StackItem already applies the flex min-height/min-width reset, so <StackItem size="fill" isScrollable> is a complete scroll region. Matches isScrollable on LayoutContent and LayoutPanel. |
| crossAlignSelf | 'start' | 'center' | 'end' | 'stretch' | 覆盖此元素的交叉轴对齐方式,忽略父堆叠的对齐设置。 | |
| as | ElementType | 'div' | 作为子元素包装器渲染的 HTML 元素。 |
| children | ReactNode | 子元素内容。 |
组件结构
- Stack container: Layout container that arranges content along one flex axis.
- Item: Optional StackItem wrapper that controls one item in the stack.
- Content: Caller-supplied content rendered by a Stack or StackItem.