Skip to content

JSWidget JSX 元素完整文档

本文档描述 JSWidget 支持的所有 JSX 元素、属性及其用法。

各组件专有属性均与 JSWidgetCommonAttributes 交叉组合(row 除外,见下文)。除另有说明外,均可使用通用属性


布局容器

row(水平堆叠)

水平排列子元素。继承 JSWidgetCommonAttributes,但不包含 align

属性类型说明
spacingnumber子元素水平间距
justify"start" | "center" | "end"主轴分布(需配合 size 使用)
size通用属性尺寸
其他通用属性不含 align
jsx
<row justify="center" spacing={8} size={{ width: "fill" }}>
  <text>Left</text>
  <text>Right</text>
</row>

col(垂直堆叠)

垂直排列子元素。继承 JSWidgetCommonAttributes

属性类型说明
spacingnumber子元素垂直间距
align"start" | "center" | "end"子元素水平对齐(交叉轴)
justify"start" | "center" | "end"主轴分布(需配合 size 使用)
jsx
<col align="center" spacing={10}>
  <text>Hello</text>
  <text>World</text>
</col>

stack(层叠容器)

按 z 轴顺序层叠子元素。仅继承 JSWidgetCommonAttributes,无额外专有属性。

jsx
<stack>
  <roundedrect radius={8} color="#f1f5f9" size={{ width: 100, height: 60 }} />
  <text>覆盖在上面</text>
</stack>

grid(网格)

网格排列子元素。继承 JSWidgetCommonAttributes

属性类型说明
rowsstring | number | boolean行配置 JSON 字符串
columnsstring | number | boolean列数 / 列配置
spacingnumber间距
jsx
<grid
  columns='[{"type":"adaptive","min":"60"},{"type":"fixed","value":"80"}]'
  spacing={10}
>
  <text>Item 1</text>
  <text>Item 2</text>
</grid>

grid-row(网格行)

网格行布局。继承 JSWidgetCommonAttributes,但不包含通用 align(使用下方专用 align)。

属性类型说明
align"start" | "end" | "center" | "firstBaseline" | "lastBaseline"垂直对齐
rowsstring | number | boolean行数
columnsstring | number | boolean列配置 JSON 字符串
spacingnumber间距

文本

text

显示文本。继承 JSWidgetCommonAttributes

属性类型说明
fontJSWidgetFont字体:语义名 / 数字字号 / 对象
colorstring文字颜色
textAlign"start" | "center" | "end"文本对齐
lineLimitnumber行数限制

JSWidgetFont 支持三种形式:

  • 语义名"largeTitle" | "title" | "title2" | "title3" | "headline" | "subheadline" | "body" | "callout" | "footnote" | "caption" | "caption2"
  • 数字:直接指定字号
  • 对象:精细控制
字段类型说明
name语义名基础字体名
weight"ultraLight" | "thin" | "light" | "regular" | "medium" | "semibold" | "bold" | "heavy" | "black"字重
design"monospaced" | "rounded" | "serif" | "default"字体设计
sizenumber字号
customstring自定义字体名
jsx
<text font="title" color="#333">Hello World</text>
<text font={{ name: "body", weight: "bold" }} lineLimit={1}>Single line</text>

date

动态显示日期和时间。继承 JSWidgetCommonAttributes

属性类型说明
style"date" | "time" | "relative" | "offset" | "timer"显示样式
fontJSWidgetFont字体
colorstring文字颜色
textAlign"start" | "center" | "end"文本对齐
lineLimitnumber行数限制
jsx
<date style="time" />
<date style="relative" color="#64748b" />

图片与媒体

image

显示图片。继承 JSWidgetCommonAttributes

属性类型说明
namestring包内 image/ 目录下的图片名(读取 image/{name}.png
filePathstring相对于脚本目录的图片路径,支持任意图片格式
urlstring网络 URL
rationumber宽高比
mode"fit" | "fill"显示模式
fontJSWidgetFontsystemName 时生效(控制 SF Symbol 大小)
jsx
<image name="logo" />
<image url="https://example.com/image.png" mode="fit" />

gif

显示 GIF 动画。继承 JSWidgetCommonAttributes

属性类型说明
filestring必填。GIF 文件名;可为 "cat.gif""cat"(自动补 .gif)。文件放在脚本包的 images 目录下
rationumber宽高比
mode"fit" | "fill"显示模式
jsx
<gif file="animation.gif" mode="fit" />

交互

button

可点击按钮。继承 JSWidgetCommonAttributes

属性类型说明
action"reload"预定义动作,目前仅支持 "reload"(重载小组件)
onClickstring点击时调用的 JS 函数名(在 main.jsx 顶层声明)

actiononClick 二选一:有 action="reload" 时走系统重载;否则按 onClick 调用脚本函数。

jsx
<button action="reload">
  <text>重新加载</text>
</button>

<button onClick="handleClick">
  <text>点击我</text>
</button>

toggle

开关控件。继承 JSWidgetCommonAttributes

属性类型说明
onboolean开关是否开启,默认 false
onClickstring切换时调用的 JS 函数名
jsx
<toggle on={true} onClick="toggleHandler">
  <text>设置选项</text>
</toggle>

可点击链接。继承 JSWidgetCommonAttributes

属性类型说明
urlstring必填。跳转目标 URL
jsx
<link url="widget-deeplink://action">
  <text>点击跳转</text>
</link>

图表

chart

基于 Swift Charts 的图表。继承 JSWidgetCommonAttributes

属性类型说明
data见下方必填。图表数据数组
type见下方图表类型,默认 "bar"
categoryboolean是否按数据项 category 字段分色,默认 false
hideLegendboolean是否隐藏图例
hideXAxisboolean是否隐藏 X 轴
hideYAxisboolean是否隐藏 Y 轴
colorstring前景色

type 可选值

说明data 项类型
bar柱状图(X 轴为标签)JSWidgetChartDataItem
bar-x水平柱状图JSWidgetChartDataItem
bar-y垂直柱状图JSWidgetChartDataItem
bar-gantt甘特图JSWidgetChartGanttDataItem
line折线图JSWidgetChartDataItem
point点图JSWidgetChartDataItem
line-point带点折线图JSWidgetChartDataItem
area面积图JSWidgetChartDataItem
rect矩形图JSWidgetChartDataItem
rule-xX 轴规则线JSWidgetChartRuleDataItem

数据项类型

ts
// 通用(bar / line / point / area / rect 等)
interface JSWidgetChartDataItem {
  label: string;
  value: number;
  category?: string;
}

// bar-gantt
interface JSWidgetChartGanttDataItem {
  job: string;
  start: number;
  end: number;
  category?: string;
}

// rule-x
interface JSWidgetChartRuleDataItem {
  xstart: number;
  xend: number;
  y: number;
  category?: string;
}
jsx
const data = [
  { label: "A", value: 10 },
  { label: "B", value: 20 },
];

<chart type="bar" data={data} hideXAxis={true} color="red" />

形状与装饰

rect

矩形。继承 JSWidgetCommonAttributes

属性类型说明
cornerRadiusnumber圆角半径
jsx
<rect size={{ width: 50, height: 30 }} backgroundColor="blue" cornerRadius={5} />

capsule

胶囊形。仅继承 JSWidgetCommonAttributes,无专有属性。

jsx
<capsule size={{ width: 50, height: 30 }} backgroundColor="blue" />

ellipse

椭圆。仅继承 JSWidgetCommonAttributes,无专有属性。

jsx
<ellipse size={{ width: 50, height: 30 }} backgroundColor="blue" />

circle

圆形。仅继承 JSWidgetCommonAttributes,无专有属性。

jsx
<circle size={{ width: 40, height: 40 }} backgroundColor="blue" />

roundedrect

圆角矩形色块,常用于背景或占位。继承 JSWidgetCommonAttributes

属性类型说明
radiusnumber圆角半径,默认 12
colorstring填充颜色
jsx
<roundedrect radius={12} color="#38bdf8" size={{ width: 140, height: 60 }} />

line

固定长度线段。继承 JSWidgetCommonAttributes

属性类型说明
thicknessnumber线宽,默认 2
lengthnumber长度,默认 48
axis"horizontal" | "vertical"方向
colorstring颜色
jsx
<line length={120} thickness={2} color="#94a3b8" />
<line axis="vertical" length={24} thickness={2} color="#f59e0b" />

divider

分隔线。继承 JSWidgetCommonAttributes

属性类型说明
thicknessnumber线宽,默认 1
axis"horizontal" | "vertical"方向
colorstring颜色

图标与标签

icon

SF Symbol 图标。继承 JSWidgetCommonAttributes

属性类型说明
systemNamestring必填。SF Symbol 名
sizenumber图标大小
colorstring颜色
fontJSWidgetFont字体(语义名 / 数字 / 对象)
jsx
<icon systemName="star.fill" size={24} color="#f59e0b" />

label

带图标的文字(也可用 icon + text 组合实现)。继承 JSWidgetCommonAttributes

属性类型说明
titlestring标题文本
systemNamestring图标名
colorstring颜色
fontJSWidgetFont字体

进度与数据展示

progress

进度条。继承 JSWidgetCommonAttributes

属性类型说明
valuenumber当前值,默认 0
totalnumber总值,默认 1
labelstring进度条旁/上的说明文字
style"linear" | "circular"样式,默认 linear
colorstring进度条着色
trackColorstring轨道/背景色(设置后使用自定义绘制,默认 #e2e8f0
fontJSWidgetFontlabel 的字体
jsx
<progress value={0.6} total={1} label="Loading" style="circular" />

ring

0–1 的环形进度条(轻量,类似 progresscircular)。继承 JSWidgetCommonAttributes

属性类型说明
valuenumber当前值(0–1)
thicknessnumber圆环粗细,默认 8
colorstring进度颜色,默认 #3b82f6
trackColorstring轨道颜色,默认 #e2e8f0
jsx
<ring value={0.68} thickness={10} color="#22c55e" trackColor="#e2e8f0" size={72} />

badge

小号圆角标签,适合 PRONEW 等。继承 JSWidgetCommonAttributes

属性类型说明
textstring徽章文本
radiusnumber圆角半径,默认 6
backgroundColorstring背景色,默认 #0f172a
colorstring文字颜色,默认 #e2e8f0
jsx
<badge text="PRO" backgroundColor="#22c55e" color="#ffffff" />

chip

带边框的胶囊标签。继承 JSWidgetCommonAttributes

属性类型说明
textstring标签文本
radiusnumber圆角半径,默认 14
backgroundColorstring背景色(未设置则用默认)
borderColorstring边框颜色,默认 #cbd5f5
colorstring文字颜色,默认 #0f172a
jsx
<chip text="Today" />
<chip text="Focus" borderColor="#38bdf8" color="#0f172a" />

stat

标题 + 大数字 + 可选副标题,适合仪表盘。继承 JSWidgetCommonAttributes

属性类型说明
titlestring标题,默认 "Title"
valuestring数值,默认 "0"
subtitlestring副标题
colorstring数值颜色,默认 #0f172a
mutedColorstring辅助文字颜色,默认 #64748b
jsx
<stat title="Downloads" value="12.4k" subtitle="Today" color="#0f172a" />

仪表

gauge

仪表盘。继承 JSWidgetCommonAttributes。按 type 分为两种属性集。

type 省略或 "original"(默认)

属性类型说明
type"original"内部自定义仪表
valuenumber当前值(0–1)
anglenumber弧度
thicknessnumber厚度
needleColorstring指针颜色
labelstring中心标签
titlestring副标题
sectionsstring分段配置 JSON 字符串

type="system"

属性类型说明
type"system"SwiftUI 系统 Gauge
valuenumber当前值(0–1)
style"circular" | "linear" | "automatic"样式
textstring标题文本
currentstring当前值标签文字
minstring最小值端标签文字(非数值范围)
maxstring最大值端标签文字(非数值范围)
jsx
<gauge type="system" value={0.6} style="circular" text="Battery" current="60%" />

<gauge value={0.72} label="72%" title="CPU" needleColor="#ef4444" />

布局辅助

spacer

占用空间,用于布局分隔;在 col 中沿垂直方向扩展,在 row 中沿水平方向扩展。继承 JSWidgetCommonAttributes

属性类型说明
lengthnumber最小占用长度
jsx
<col>
  <text font="title">Title</text>
  <spacer length={8} />
  <text font="caption">Footer</text>
</col>

通用属性(JSWidgetCommonAttributes

多数标签通过交叉类型继承。

属性类型说明
sizestring | { width?, height?, minWidth?, maxWidth?, minHeight?, maxHeight? }"max" 或尺寸对象;width / height 等为 number | "fill"
justify"start" | "center" | "end"水平对齐 / 主轴分布(需配合 size 使用;容器的交叉轴对齐不依赖 size)
align"start" | "center" | "end"垂直对齐(需配合 size 使用;容器的交叉轴对齐不依赖 size)。row 无此项;grid-row 使用专用 align
paddingJSWidgetPadding内边距
backgroundColorstring背景色
foregroundColorstring前景色
cornerRadiusnumber圆角
opacitynumber透明度 0–1
rotationEffectnumber旋转角度
scaleEffectnumber缩放
offsetstring偏移
shadowstring阴影
blurnumber模糊
animationstring动画名

JSWidgetPadding

number(四边统一)或对象,按需指定各方向:

字段类型说明
horizontalnumber左右内边距
verticalnumber上下内边距
topnumber顶部内边距
bottomnumber底部内边距
leadingnumber左内边距
trailingnumber右内边距
leftnumber左内边距(同 leading
rightnumber右内边距(同 trailing
jsx
<text size="max" padding={10} backgroundColor="#1e293b" />
<text size={{ width: "fill", height: 48 }} justify="center" align="center" />
<rect cornerRadius={12} opacity={0.9} rotationEffect={15} />

颜色格式

支持的颜色格式:

  • 颜色名称"red", "blue", "green", "secondary", "white", "black"
  • 十六进制"#ff0000", "#f00", "#ff000080"(RGBA 顺序)
  • RGB 函数"rgb(255, 0, 0)"
  • RGBA 函数"rgba(255, 0, 0, 0.5)"

函数组件

除了内置 JSX 元素外,你还可以编写函数组件来封装和复用 UI 片段。函数组件接收一个 props 对象(包含传入的属性和 children),返回 JSX 元素。

jsx
function Greeting(props) {
  return (
    <col>
      <text font="headline" color={props.color}>Hello, {props.name}!</text>
      {props.children}
    </col>
  );
}

$render(
  <col size="max" padding={16}>
    <Greeting name="World" color="#0f172a">
      <text font="caption">Welcome to JSWidget</text>
    </Greeting>
  </col>
);

注意:函数组件必须是同步函数,不支持 async 函数组件。如果需要异步数据,请在函数组件外部获取数据后通过 props 传入。

jsx
// ✅ 正确:在外部获取数据,通过 props 传入
const data = await $http.get("https://api.example.com/data");

function DataCard(props) {
  return <text>{props.data}</text>;
}

$render(<DataCard data={data} />);
jsx
// ❌ 错误:不支持 async 函数组件
async function DataCard() {
  const data = await $http.get("https://api.example.com/data");
  return <text>{data}</text>;
}

元素汇总表

标签分类说明
row布局水平堆叠(无 align
col布局垂直堆叠
stack布局层叠
grid布局网格
grid-row布局网格行
text文本文本
date文本日期/时间
image媒体图片
gif媒体GIF
button交互按钮
toggle交互开关
link交互链接
chart图表图表
rect形状矩形
capsule形状胶囊
ellipse形状椭圆
circle形状
roundedrect形状圆角矩形色块
line形状线段
divider形状分隔线
gauge仪表仪表盘
icon展示SF Symbol
label展示图标+文字
progress展示进度条
ring展示圆环进度
badge展示徽章
chip展示标签 chip
stat展示统计块
spacer布局间距(仅 length

Released under the MIT License.