组件/Flex 弹性布局

Flex 弹性布局

控制方向、对齐、换行与间距的 Flexbox 容器

已验证自 0.3.4

用法

基础

示例加载中...

可换行工具栏

wrap="wrap" 让工具栏在窄容器中换行。

<template>
  <TxFlex align="center" justify="space-between" wrap="wrap" gap="0.75rem">
    <TxFlex align="center" gap="0.5rem" wrap="wrap">
      <TxTag label="Docs" />
      <TxTag label="Reviewed" color="var(--tx-color-success)" />
    </TxFlex>
    <TxButton size="sm">发布</TxButton>
  </TxFlex>
</template>

反向排列

<template>
  <TxFlex direction="row-reverse" align="center" gap="8px">
    <TxButton variant="primary">Primary</TxButton>
    <TxButton variant="ghost">Secondary</TxButton>
  </TxFlex>
</template>

最佳实践

  • 需要精确的 Flexbox 语义时用 TxFlex,常规的横纵节奏用 TxStack。
  • 间距交给 gap,不要在子元素上写 margin。
  • 容器不添加角色与键盘行为,交互交给语义化的子控件。
  • 重复卡片的网格用 TxGridLayout,不要嵌套多层 flex。

API 参考

属性

属性名类型默认值说明
direction'row' | 'row-reverse' | 'column' | 'column-reverse''row'主轴方向。
gapnumber | string12子元素间距;数字按 px,字符串原样使用。
alignstring'stretch'align-items,接受任意 CSS 值;图文行用 center。
justifystring'flex-start'justify-content,接受任意 CSS 值。
wrap'nowrap' | 'wrap' | 'wrap-reverse''nowrap'flex-wrap。
inlinebooleanfalse使用 inline-flex。

插槽

插槽名Props说明
default-Flex 子元素。

CSS 变量

变量来源说明
--tx-flex-gapgapCSS gap。
--tx-flex-alignalignalign-items。
--tx-flex-justifyjustifyjustify-content。
--tx-flex-directiondirectionflex-direction。
--tx-flex-wrapwrapflex-wrap。
--tx-flex-displayinlineflex 或 inline-flex。

概述

  • 根节点是 div,设置 min-width: 0,子元素可在受限容器内收缩。
  • props 写入根节点的 --tx-flex-* 变量,样式表读取这些变量。
  • align、justify、direction、wrap 传空值时回退到默认值。

技术实现

  • 源码:packages/tuffex/packages/components/src/flex/。
查看源码
packages/tuffex/packages/components/src/flex/index.ts