Files
aurora-admin b59247231a
Regression / regression (push) Canceled after 0s
feat(安全+品牌): 对外产物发布脱敏(3注入点/2465文件0泄露) + Kole Cup 定稿
安全:CHANGELOG.md 是仓库内部变更流水(含服务器目录、镜像回滚标签、内网网段、
部署时序、AI 工作流用语),此前被 build-site.ps1 / precompute.mjs / build-mobile.mjs
原样注入站点,而站点是公网可下载的静态文件 —— 抓一次 /site/m/changelog.html
即可拿到内网地址段与服务器目录布局。

- 新增 tools/lib/redact-publish.mjs(零依赖,构建期过滤,不改 CHANGELOG.md,
  内部可追溯性完整保留)
- 接入 precompute.mjs(PC data.json/changelog.json)与 build-mobile.mjs
  (移动端更新日志页);build-site.ps1 不碰(ASCII-only 铁律)
- 只处理 changelog 字段:components[].sources 是规范实现源码,逐字保真
  (详情页代码区主动高亮注释,剥注释会破坏该功能)
- 实测消除:/opt/aurora-admin.prev-*、kole-ui-showcase:pre-*、docker compose、
  192.168.5.7、16 位产物指纹、1531/1531、并发会话/本会话/派子 agent
- settings.html 演示占位 IP 192.168.5.0/24(= 真实网段)改为 RFC 5737 的 192.0.2.0/24

品牌:Kole Cup 饮料杯标记定稿(几何 K → 圆角杯盖 + 杯身负空间 K,无吸管),
brand-mark.json 升 schemaVersion 3(paths 支持 { d, evenodd }),
verify:brand 增至 25 条(新增 B9b:负空间必须带 fill-rule)。

验证:发布集 2465 文件全量扫描 0 泄露;PC 回归 100%(1464/1464) ·
移动端 100%(807/807);门禁品牌 25 / 隔离 31 / 移动文档 12 / 示例 9 / 版本 40 / i18n 17 全绿;
已按 AGENTS §九 发布公网,2461/2461 逐字节一致,五项验收全过。

已知未处理(既有缺口,ROADMAP S7-P28 已记录):data.mobile.json 的
meta.generated 为墙上时钟,会让 CI 的「生成物可复现」断言在重跑构建后永远非空;
该 CI 流水线本身亦从未通过(无 runner)。
2026-09-23 07:31:06 +08:00

1449 lines
77 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
(function () {
var m = 'auto';
try { var v = localStorage.getItem('kole-mode'); if (v === 'light' || v === 'dark' || v === 'auto') m = v; } catch (e) {}
var dark = m === 'dark' || (m === 'auto' && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
document.documentElement.classList.toggle('kole-dark', !!dark);
document.documentElement.style.colorScheme = dark ? 'dark' : 'light';
})();
</script>
<link rel="icon" href="../../assets/kole-mark.svg">
<meta name="theme-color" content="#FFFFFF" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#1C1F26" media="(prefers-color-scheme: dark)">
<title>开关 Switch — Kole UI Mobile</title>
<meta name="description" content="开关 Switch:移动端组件(6 端实现:CSS / H5 原生 / React / Vue 2 / Vue 3 / uni-app)。与 PC 端物理隔离:类名 kole-m-、令牌 --kole-m-。规格来源:移动端规格 29 · 开关 Switch。">
<link rel="stylesheet" href="../../../.design_library/kole-ui-mobile/colors_and_type.css">
<link rel="stylesheet" href="../style.css">
</head>
<body>
<header class="m-top">
<div class="m-top-inner">
<a class="m-logo" href="../index.html">
<span class="m-logo-mark" data-brand-mark="mono" aria-hidden="true"></span>
<span class="m-logo-text">Kole UI<em>移动端</em></span>
</a>
<div class="m-ver-picker" id="m-ver-picker">
<button id="m-ver-trigger" class="m-ver-trigger" type="button" data-single="1" aria-haspopup="listbox" aria-expanded="false" aria-label="选择版本" title="文档版本(清单来自部署根的 versions.json)">
<span id="m-ver">v1.0.0</span>
<svg class="m-ver-caret" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>
</button>
<div id="m-ver-menu" class="m-ver-menu hidden" role="listbox" aria-label="文档版本">
<div class="m-ver-menu-head" id="m-ver-head">文档版本</div>
</div>
</div>
<nav class="m-nav"> <a href="../index.html" class="active" aria-current="page">组件总览</a>
<a href="../guide.html">快速开始</a>
<a href="../design.html">设计规范</a>
<a href="../faq.html">常见问题</a>
<a href="../changelog.html">更新日志</a></nav>
<div class="m-mode">
<button id="m-mode-trigger" class="m-mode-trigger" type="button" aria-haspopup="listbox" aria-expanded="false" aria-label="主题模式" title="主题模式">
<svg class="m-mode-icon" id="m-icon-moon" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
<svg class="m-mode-icon" id="m-icon-sun" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></svg>
<svg class="m-mode-icon" id="m-icon-auto" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><rect x="3" y="4" width="18" height="13" rx="2"/><path d="M8 21h8M12 17v4"/></svg>
<span id="m-mode-cur">自动模式</span>
<svg class="m-mode-caret" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>
</button>
<div id="m-mode-menu" class="m-menu hidden" role="listbox" aria-label="主题模式">
<div class="m-menu-head">主题模式</div>
<button class="m-menu-opt" type="button" role="option" data-mode="light" aria-selected="false"><span data-mode-label="light">日间模式</span></button>
<button class="m-menu-opt" type="button" role="option" data-mode="dark" aria-selected="false"><span data-mode-label="dark">夜间模式</span></button>
<button class="m-menu-opt" type="button" role="option" data-mode="auto" aria-selected="true"><span data-mode-label="auto">自动模式</span></button>
</div>
</div>
</div>
</header>
<div class="m-body">
<details class="m-side" open>
<summary><span>移动端导航</span><span class="cur">开关</span></summary>
<nav>
<div class="m-side-section"><span>平台</span></div>
<a class="m-side-link m-side-platform" href="../../index.html"><span class="sp-top"><span class="sp-name">PC 端组件</span></span><span class="sp-sub">103 个中后台组件 · 5 端</span></a>
<a class="m-side-link m-side-platform" href="../index.html"><span class="sp-top"><span class="sp-name">移动端组件</span><span class="sp-badge">当前</span></span><span class="sp-sub">47 个组件 · 6 端 · 含 uni-app</span></a>
<div class="m-side-section"><span>开发指南</span></div>
<a class="m-side-link" href="../index.html">组件总览</a>
<a class="m-side-link" href="../guide.html">快速开始</a>
<a class="m-side-link" href="../design.html">设计规范</a>
<a class="m-side-link" href="../faq.html">常见问题</a>
<a class="m-side-link" href="../changelog.html">更新日志</a>
<a class="m-side-link" href="../platform.html">平台与端<em>移动端</em></a>
<a class="m-side-link" href="../../../tests/mobile/index.html" target="_blank">测试与回归<em>移动端</em></a>
<div class="m-side-section"><span>组件</span><span class="cnt">47</span></div>
<div class="m-side-group"><span>通用</span><span class="cnt">5</span></div>
<a class="m-side-link" href="mobile-button.html">按钮<em>Button</em></a>
<a class="m-side-link" href="mobile-divider.html">分割线<em>Divider</em></a>
<a class="m-side-link" href="mobile-icon.html">图标<em>Icon</em></a>
<a class="m-side-link" href="mobile-layout.html">布局<em>Layout</em></a>
<a class="m-side-link" href="mobile-link.html">链接<em>Link</em></a>
<div class="m-side-group"><span>导航</span><span class="cnt">6</span></div>
<a class="m-side-link" href="navbar.html">顶部导航栏<em>NavBar</em></a>
<a class="m-side-link" href="tabbar.html">底部标签栏<em>TabBar</em></a>
<a class="m-side-link" href="mobile-grid.html">宫格<em>Grid</em></a>
<a class="m-side-link" href="mobile-steps.html">步骤条<em>Steps</em></a>
<a class="m-side-link" href="mobile-segmented.html">分段器<em>Segmented</em></a>
<a class="m-side-link" href="mobile-sticky.html">吸顶容器<em>Sticky</em></a>
<div class="m-side-group"><span>数据录入</span><span class="cnt">16</span></div>
<a class="m-side-link" href="mobile-numberkeyboard.html">数字键盘<em>NumberKeyboard</em></a>
<a class="m-side-link" href="mobile-datepicker.html">日期选择器<em>DatePicker</em></a>
<a class="m-side-link" href="mobile-input.html">输入框<em>Input</em></a>
<a class="m-side-link" href="mobile-search.html">搜索框<em>Search</em></a>
<a class="m-side-link" href="mobile-stepper.html">步进器<em>Stepper</em></a>
<a class="m-side-link is-active" href="mobile-switch.html" aria-current="page">开关<em>Switch</em></a>
<a class="m-side-link" href="mobile-textarea.html">多行文本框<em>Textarea</em></a>
<a class="m-side-link" href="mobile-checkbox.html">多选框<em>Checkbox</em></a>
<a class="m-side-link" href="mobile-form.html">表单<em>Form</em></a>
<a class="m-side-link" href="mobile-radio.html">单选框<em>Radio</em></a>
<a class="m-side-link" href="mobile-rate.html">评分<em>Rate</em></a>
<a class="m-side-link" href="mobile-slider.html">滑动选择器<em>Slider</em></a>
<a class="m-side-link" href="mobile-cascader.html">级联选择器<em>Cascader</em></a>
<a class="m-side-link" href="mobile-colorpicker.html">颜色选择器<em>ColorPicker</em></a>
<a class="m-side-link" href="mobile-picker.html">选择器<em>Picker</em></a>
<a class="m-side-link" href="mobile-upload.html">上传<em>Upload</em></a>
<div class="m-side-group"><span>数据展示</span><span class="cnt">8</span></div>
<a class="m-side-link" href="cell.html">单元格<em>Cell</em></a>
<a class="m-side-link" href="mobile-badge.html">徽标<em>Badge</em></a>
<a class="m-side-link" href="mobile-tag.html">标签<em>Tag</em></a>
<a class="m-side-link" href="mobile-avatar.html">头像<em>Avatar</em></a>
<a class="m-side-link" href="mobile-list.html">列表<em>List</em></a>
<a class="m-side-link" href="mobile-collapse.html">折叠面板<em>Collapse</em></a>
<a class="m-side-link" href="mobile-table.html">表格<em>Table</em></a>
<a class="m-side-link" href="mobile-typography.html">排版<em>Typography</em></a>
<div class="m-side-group"><span>反馈</span><span class="cnt">12</span></div>
<a class="m-side-link" href="actionsheet.html">动作面板<em>ActionSheet</em></a>
<a class="m-side-link" href="pullrefresh.html">下拉刷新<em>PullRefresh</em></a>
<a class="m-side-link" href="swipecell.html">滑动单元格<em>SwipeCell</em></a>
<a class="m-side-link" href="mobile-popup.html">弹出层<em>Popup</em></a>
<a class="m-side-link" href="mobile-toast.html">轻提示<em>Toast</em></a>
<a class="m-side-link" href="mobile-dialog.html">对话框<em>Dialog</em></a>
<a class="m-side-link" href="mobile-noticebar.html">通知栏<em>NoticeBar</em></a>
<a class="m-side-link" href="mobile-loading.html">加载<em>Loading</em></a>
<a class="m-side-link" href="mobile-progress.html">进度条<em>Progress</em></a>
<a class="m-side-link" href="mobile-message.html">消息通知<em>Message</em></a>
<a class="m-side-link" href="mobile-overlay.html">遮罩层<em>Overlay</em></a>
<a class="m-side-link" href="mobile-popover.html">弹出气泡<em>Popover</em></a>
</nav>
</details>
<main class="m-main">
<section class="m-hero">
<h1>开关<span class="en" style="font-family: var(--kole-font-family-num); font-size: 14px; color: var(--kole-color-text-secondary); margin-left: 8px;">Switch</span></h1>
<p>即时启停一项配置或业务状态(启用通知、公开数据、自动同步)</p>
<p style="margin-top: 8px;" class="m-chips">
<span class="m-chip m">数据录入</span>
<span class="m-chip">规格 29 · 开关 Switch</span>
<span class="m-chip">6 端实现</span>
<span class="m-chip">触摸优先</span>
</p>
<div class="m-code" style="margin-top: var(--kole-space-12);">
<div class="m-code-head"><span>引入(H5 原生;其余 5 端见「快速开始」)</span><button class="m-copy" type="button" data-copy="code-import">复制</button></div>
<pre id="code-import"><code>&lt;!-- ① 令牌:PC 令牌 + 移动端 --kole-m-* 合成单文件,引一次 --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;kole-ui/mobile/tokens.css&quot;&gt;
&lt;!-- ② 本组件样式(全量则用 kole-ui/mobile/components/index.css) --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;kole-ui/mobile/components/mobile-switch.css&quot;&gt;
&lt;!-- ③ 结构照抄下方任一演示块(类名与 6 端实现一致) --&gt;</code></pre>
</div>
</section>
<section class="m-block">
<h2 id="demo">演示</h2>
<p class="m-sub">每个演示都是真实渲染:预览帧加载 <code>frameworks-mobile/Switch.html?demo=&lt;id&gt;</code>(只显示该演示块),代码是该演示块在演示页里的原文,可复制。全部演示同屏可看 <a href="../../../frameworks-mobile/Switch.html" target="_blank">演示页 ↗</a>。</p>
<h3 class="m-demo-group">01 组件类型</h3>
<div class="m-demo" id="demo-basic">
<div class="m-demo-head"><span class="m-demo-title">基础用法</span><span class="m-chip">size=default</span></div>
<p class="m-demo-desc">整行可点:开关本体只有 48×28,单点本体手指点不中。</p>
<div class="m-demo-stage"><iframe src="../../../frameworks-mobile/Switch.html?demo=basic" title="开关 Switch · 基础用法" loading="lazy"></iframe></div>
<details class="m-details"><summary>查看代码(演示页原文 · 10 行)</summary>
<div class="m-code"><div class="m-code-head"><span>frameworks-mobile/Switch.html · basic</span><button class="m-copy" type="button" data-copy="code-demo-1">复制</button></div>
<pre id="code-demo-1"><code>&lt;section class=&quot;demo-block&quot; data-demo=&quot;basic&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;基础用法(整行可点:开关本体只有 48×28,单点本体手指点不中)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot;&gt;
&lt;button class=&quot;kole-m-switch is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot; id=&quot;sw-product&quot;
data-assert=&quot;switch-basic&quot; data-behavior=&quot;click-toggles-class:#sw-product|is-on&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;商品公开可见&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;</code></pre></div>
</details>
</div>
<div class="m-demo" id="demo-size">
<div class="m-demo-head"><span class="m-demo-title">尺寸两档</span><span class="m-chip">size=default|small</span></div>
<p class="m-demo-desc">default 轨道 48×28;small 轨道 40×22 用于紧凑表单。</p>
<div class="m-demo-stage"><iframe src="../../../frameworks-mobile/Switch.html?demo=size" title="开关 Switch · 尺寸两档" loading="lazy"></iframe></div>
<details class="m-details"><summary>查看代码(演示页原文 · 13 行)</summary>
<div class="m-code"><div class="m-code-head"><span>frameworks-mobile/Switch.html · size</span><button class="m-copy" type="button" data-copy="code-demo-2">复制</button></div>
<pre id="code-demo-2"><code>&lt;section class=&quot;demo-block&quot; data-demo=&quot;size&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;尺寸两档(size=default 48×28 / size=small 40×22,紧凑表单用)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-size&quot;&gt;
&lt;button class=&quot;kole-m-switch is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;default(48×28)&lt;/span&gt;
&lt;/button&gt;
&lt;button class=&quot;kole-m-switch kole-m-switch--small is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;small(40×22)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;</code></pre></div>
</details>
</div>
<div class="m-demo" id="demo-left">
<div class="m-demo-head"><span class="m-demo-title">文字在左</span><span class="m-chip">labelPlacement=left</span></div>
<p class="m-demo-desc">labelPlacement=left:整行右对齐的值区风格,文字在开关左侧。</p>
<div class="m-demo-stage"><iframe src="../../../frameworks-mobile/Switch.html?demo=left" title="开关 Switch · 文字在左" loading="lazy"></iframe></div>
<details class="m-details"><summary>查看代码(演示页原文 · 9 行)</summary>
<div class="m-code"><div class="m-code-head"><span>frameworks-mobile/Switch.html · left</span><button class="m-copy" type="button" data-copy="code-demo-3">复制</button></div>
<pre id="code-demo-3"><code>&lt;section class=&quot;demo-block&quot; data-demo=&quot;left&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;文字在左(labelPlacement=left:整行右对齐的值区风格)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-left&quot;&gt;
&lt;button class=&quot;kole-m-switch kole-m-switch--left is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;自动同步(成功后写入云端)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;</code></pre></div>
</details>
</div>
<h3 class="m-demo-group">02 组件状态</h3>
<div class="m-demo" id="demo-states">
<div class="m-demo-head"><span class="m-demo-title">关态与开态</span><span class="m-chip">状态 off|on</span></div>
<p class="m-demo-desc">两者不能只靠颜色区分:滑块位置 + aria-checked 双通道。</p>
<div class="m-demo-stage"><iframe src="../../../frameworks-mobile/Switch.html?demo=states" title="开关 Switch · 关态与开态" loading="lazy"></iframe></div>
<details class="m-details"><summary>查看代码(演示页原文 · 14 行)</summary>
<div class="m-code"><div class="m-code-head"><span>frameworks-mobile/Switch.html · states</span><button class="m-copy" type="button" data-copy="code-demo-4">复制</button></div>
<pre id="code-demo-4"><code>&lt;section class=&quot;demo-block&quot; data-demo=&quot;states&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;关态与开态(两者不能只靠颜色区分:滑块位置 + aria-checked 双通道)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-states&quot;&gt;
&lt;button class=&quot;kole-m-switch&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;false&quot; data-switch=&quot;off&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;关态(默认)&lt;/span&gt;
&lt;/button&gt;
&lt;button class=&quot;kole-m-switch is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot; id=&quot;sw-on&quot; data-switch=&quot;on&quot;
data-behavior=&quot;click-sets-attr:#sw-on|aria-checked|false&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;开态(滑块右移 + 品牌底色)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;</code></pre></div>
</details>
</div>
<div class="m-demo" id="demo-disabled">
<div class="m-demo-head"><span class="m-demo-title">禁用</span><span class="m-chip">disabled=true</span></div>
<p class="m-demo-desc">置灰且不响应;读屏会播报不可用。</p>
<div class="m-demo-stage"><iframe src="../../../frameworks-mobile/Switch.html?demo=disabled" title="开关 Switch · 禁用" loading="lazy"></iframe></div>
<details class="m-details"><summary>查看代码(演示页原文 · 10 行)</summary>
<div class="m-code"><div class="m-code-head"><span>frameworks-mobile/Switch.html · disabled</span><button class="m-copy" type="button" data-copy="code-demo-5">复制</button></div>
<pre id="code-demo-5"><code>&lt;section class=&quot;demo-block&quot; data-demo=&quot;disabled&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;禁用(置灰且不响应;读屏会播报不可用)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-disabled&quot;&gt;
&lt;button class=&quot;kole-m-switch kole-m-switch--small is-disabled&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;false&quot;
aria-disabled=&quot;true&quot; disabled&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;内测功能(需管理员开启)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;</code></pre></div>
</details>
</div>
</section>
<section class="m-block">
<h2 id="api">API</h2>
<p class="m-sub">props / events / slots 为 6 端实现的公共接口(说明文字取自规格对应小节)。字段名与各端源码逐名核对:node tools/verify-mobile-docs.mjs</p>
<h3 class="m-h3">Props</h3>
<table class="m-table">
<thead><tr><th>名称</th><th>类型</th><th>默认值</th><th>说明</th><th>必传</th></tr></thead>
<tbody>
<tr><td><code>checked</code></td><td><code>boolean</code></td><td><code>false</code></td><td>受控开关值;与 aria-checked 同步写(规格 §29.5)</td><td>N</td></tr>
<tr><td><code>size</code></td><td><code>'default' | 'small'</code></td><td><code>'default'</code></td><td>变体 size:default 轨道 48×28,small 轨道 40×22(规格 §29.3)</td><td>N</td></tr>
<tr><td><code>labelPlacement</code></td><td><code>'right' | 'left'</code></td><td><code>'right'</code></td><td>变体 labelPlacement:文字在开关右侧还是左侧(规格 §29.3)</td><td>N</td></tr>
<tr><td><code>disabled</code></td><td><code>boolean</code></td><td><code>false</code></td><td>状态 disabled:置灰且不可聚焦(规格 §29.4)</td><td>N</td></tr>
<tr><td><code>label</code></td><td><code>string</code></td><td><code>''</code></td><td>文字标签;同时作为无障碍名称落到 aria-label(规格 §29.6)</td><td>N</td></tr>
</tbody>
</table>
<p class="m-note">「必传」按严格定义:实现里**没有默认值**时才为 Y(本门禁逐条核对 props 与各端源码的默认值,防止契约与实现脱节)。</p>
<h3 class="m-h3">事件</h3>
<table class="m-table">
<thead><tr><th>名称</th><th>参数</th><th>说明</th></tr></thead>
<tbody>
<tr><td><code>change</code></td><td><code>(checked)</code></td><td>切换时触发,回传切换后的目标值(规格 §29.5)</td></tr>
</tbody>
</table>
<h3 class="m-h3">插槽</h3>
<table class="m-table">
<thead><tr><th>名称</th><th>说明</th></tr></thead>
<tbody>
<tr><td><code>default</code></td><td>文字标签内容,优先于 label 属性(规格 §29.2 text)</td></tr>
</tbody>
</table>
<h3 class="m-h3">CSS 变量</h3>
<p class="m-sub">组件级变量(在组件样式表里定义)。业务侧可在自己的作用域内覆盖,不必改组件源码。</p>
<table class="m-table">
<thead><tr><th>名称</th><th>默认值</th><th>说明</th></tr></thead>
<tbody>
<tr><td><code>--kole-m-switch-track-w</code></td><td><code>48px</code></td><td>轨道宽</td></tr>
<tr><td><code>--kole-m-switch-track-h</code></td><td><code>28px</code></td><td>轨道高</td></tr>
<tr><td><code>--kole-m-switch-knob</code></td><td><code>24px</code></td><td>滑块直径(轨道高 − 2×2px 内边距)</td></tr>
<tr><td><code>--kole-m-switch-duration</code></td><td><code>150ms</code></td><td>滑块位移与底色过渡时长</td></tr>
<tr><td><code>--kole-m-switch-track-w</code></td><td><code>40px</code></td><td>组件内部默认值,可在业务侧覆盖</td></tr>
<tr><td><code>--kole-m-switch-track-h</code></td><td><code>22px</code></td><td>组件内部默认值,可在业务侧覆盖</td></tr>
<tr><td><code>--kole-m-switch-knob</code></td><td><code>18px</code></td><td>组件内部默认值,可在业务侧覆盖</td></tr>
</tbody>
</table>
</section>
<section class="m-block">
<h2 id="usage">何时使用</h2>
<ul>
<li>即时启停一项配置或业务状态(启用通知、公开数据、自动同步)</li>
<li>移动端开关本体视觉只有 48×28,但整行(开关 + 文字)都是可点热区,行高不小于 44px</li>
<li>关态与开态不能只靠颜色区分,必须同时看到滑块位移</li>
<li>点击切换只需要一次触摸,不要求拖动滑块(拖动是桌面习惯,触屏误触率高)</li>
<li>切换后立即触发 change 事件,不做二次确认(需要确认的场景由宿主先弹对话框)</li>
</ul>
</section>
<section class="m-block">
<h2 id="interaction">交互与触控</h2>
<ul>
<li>整行(开关 + 文字)都是热区,行高不小于 44px;开关本体不可单独缩到 44px 以下</li>
<li>点击切换只需要一次触摸,不要求拖动滑块(拖动是桌面习惯,触屏误触率高)</li>
<li>切换动效是滑块位移 150ms 过渡;减少动态偏好下瞬时切换</li>
<li>关态与开态不能只靠颜色区分:滑块位置 + aria-checked 双通道</li>
<li>切换后立即触发 change 事件,不做二次确认(需要确认的场景由宿主先弹对话框)</li>
</ul>
</section>
<section class="m-block">
<h2 id="a11y">无障碍</h2>
<ul>
<li>用 role=&quot;switch&quot; + aria-checked=&quot;true|false&quot;,而不是 role=&quot;checkbox&quot;(读屏会播报「开关」)</li>
<li>承载控件是原生 button,键盘可聚焦、空格/回车可切换,并有可见焦点环</li>
<li>文字标签在控件内部,读屏播报的名称就是标签本身;无标签时用 label 属性补 aria-label</li>
<li>禁用态用原生 disabled,读屏会播报不可用</li>
</ul>
</section>
<section class="m-block">
<h2 id="related">相似组件</h2>
<p class="m-sub">从「该用哪一个」的角度区分;PC 端的对应实现见 <a href="../../index.html">PC 文档站</a>。</p>
<table class="m-table">
<thead><tr><th>组件</th><th>何时用它而不是本组件</th></tr></thead>
<tbody>
<tr><td><a href="cell.html">单元格</a><em style="font-family: var(--kole-font-family-num); font-style: normal; font-size: 11px; color: var(--kole-color-text-placeholder); margin-left: 6px;">Cell</em></td><td>开关常作为单元格的右侧内容(Cell 负责行结构与分隔线,开关只管切换)</td></tr>
<tr><td><a href="mobile-button.html">按钮</a><em style="font-family: var(--kole-font-family-num); font-style: normal; font-size: 11px; color: var(--kole-color-text-placeholder); margin-left: 6px;">Button</em></td><td>需要用户确认后一次性提交的用按钮;状态需要即时生效的用开关</td></tr>
<tr><td><a href="mobile-dialog.html">对话框</a><em style="font-family: var(--kole-font-family-num); font-style: normal; font-size: 11px; color: var(--kole-color-text-placeholder); margin-left: 6px;">Dialog</em></td><td>切换前需要用户确认(如扣费项)时先弹对话框;开关本身不做二次确认</td></tr>
</tbody>
</table>
</section>
<section class="m-block">
<h2 id="gaps">规格未定 / 禁止发明</h2>
<table class="m-table">
<thead><tr><th>类别</th><th>条目</th></tr></thead>
<tbody>
<tr><td><span class="m-chip">禁止发明</span></td><td>二次确认弹窗与「切换失败回滚」的业务流程</td></tr>
<tr><td><span class="m-chip">禁止发明</span></td><td>三态开关(关 / 开 / 待定)的视觉表达</td></tr>
<tr><td><span class="m-chip">禁止发明</span></td><td>与表单一起提交时的隐藏字段(由宿主添加)</td></tr>
<tr><td><span class="m-chip">规格未定</span></td><td>开关本体是否允许小于 48×28(紧凑表单的下限)</td></tr>
<tr><td><span class="m-chip">规格未定</span></td><td>文案与开关的间距是否跟随字号</td></tr>
<tr><td><span class="m-chip">规格未定</span></td><td>加载态(切换请求进行中)如何表达</td></tr>
</tbody>
</table>
</section>
<section class="m-block">
<h2 id="anatomy">结构(anatomy)</h2>
<table class="m-table">
<thead><tr><th>字段</th><th>说明</th></tr></thead>
<tbody>
<tr><td><code>switch</code></td><td>根元素,一行里放进「轨道 + 文字标签」,整行可点</td></tr>
<tr><td><code>track</code></td><td>轨道,承载背景色与滑块位移的边界</td></tr>
<tr><td><code>knob</code></td><td>滑块,关态靠左、开态靠右(位移是开/关的主要视觉信号)</td></tr>
<tr><td><code>text</code></td><td>可选文字标签,说明这项开关控制什么</td></tr>
<tr><td><code>control</code></td><td>可点的整行控件(原生 button + role=&quot;switch&quot;),承接键盘与读屏</td></tr>
</tbody>
</table>
</section>
<section class="m-block">
<h2 id="variants">变体维度与类名映射</h2>
<p class="m-sub">类名映射由构建脚本从契约 <code>variantClasses</code> 生成,并被 <code>verify:mobile-docs</code> 逐条对照组件 CSS 校验(类/变量必须真实存在)。</p>
<table class="m-table">
<thead><tr><th>维度</th><th>取值</th><th>对应类名 / 变量</th></tr></thead>
<tbody>
<tr><td><code>size</code></td><td>default / small</td><td><div><b>default</b> <span class="m-none">(由数据驱动,无专属类)</span></div><div><b>small</b> <span class="m-cls">.kole-m-switch--small</span></div></td></tr>
<tr><td><code>labelPlacement</code></td><td>right / left</td><td><div><b>right</b> <span class="m-none">(由数据驱动,无专属类)</span></div><div><b>left</b> <span class="m-cls">.kole-m-switch--left</span></div></td></tr>
</tbody>
</table>
</section>
<section class="m-block">
<h2 id="rep-variants">代表变体</h2>
<table class="m-table">
<thead><tr><th>变体</th><th>标签</th></tr></thead>
<tbody>
<tr><td><code>size=default · labelPlacement=right</code></td><td>默认(轨道 48×28,文字在右)</td></tr>
<tr><td><code>size=small · labelPlacement=right</code></td><td>紧凑(轨道 40×22,紧凑表单用)</td></tr>
<tr><td><code>size=default · labelPlacement=left</code></td><td>文字在左(整行右对齐的值区风格)</td></tr>
</tbody>
</table>
</section>
<section class="m-block">
<h2 id="tokens">用到的令牌</h2>
<p class="m-sub">构建时从本组件样式表扫描得出。蓝色为移动端自有令牌,绿色为继承的 PC 令牌(改一处两端生效)。</p>
<div class="m-chips">
<span class="m-chip m" title="正文 / 列表项">--kole-m-font-size-body</span>
<span class="m-chip m" title="页面左右留白">--kole-m-gutter</span>
<span class="m-chip m" title="可点区域最小边长">--kole-m-touch-target</span>
<span class="m-chip inherit" title="品牌主色:主按钮、选中态、链接">--kole-color-brand</span>
<span class="m-chip inherit" title="卡片背景">--kole-color-card-bg</span>
<span class="m-chip inherit" title="输入框聚焦外发光">--kole-color-focus-ring</span>
<span class="m-chip inherit" title="----------------------------------------------------------
status — 成功/警告/错误/信息色
这些色值同时用于「文字」与「填充底」,两处都要达标:
作文字需 ≥4.5:1;作填充底时其上的白字也需 ≥4.5:1。
故取 Ant 色阶 7(色阶 6 的白字/白底对比度均不足)。
----------------------------------------------------------">--kole-color-icon-inactive</span>
<span class="m-chip inherit" title="表头背景">--kole-color-table-header-bg</span>
<span class="m-chip inherit" title="正文文字 · 14.0:1">--kole-color-text-body</span>
<span class="m-chip inherit" title="禁用态文字 · WCAG 1.4.3 对禁用控件豁免(规范定义于组件1/2.txt)">--kole-color-text-disabled</span>
<span class="m-chip inherit" title="">--kole-ease-standard</span>
<span class="m-chip inherit" title="卡片默认">--kole-shadow-low</span>
<span class="m-chip inherit" title="">--kole-space-12</span>
<span class="m-chip inherit" title="">--kole-space-8</span>
<span class="m-chip" title="组件级变量(可在业务侧覆盖)">--kole-m-switch-duration</span>
<span class="m-chip" title="组件级变量(可在业务侧覆盖)">--kole-m-switch-knob</span>
<span class="m-chip" title="组件级变量(可在业务侧覆盖)">--kole-m-switch-track-h</span>
<span class="m-chip" title="组件级变量(可在业务侧覆盖)">--kole-m-switch-track-w</span>
</div>
</section>
<section class="m-block">
<h2 id="sources">6 端源码</h2>
<p class="m-sub">同一组件的六份实现(生产环境的类名与结构一致,差异只在技术栈写法与单位)。点开查看,右侧可复制。</p>
<details class="m-details">
<summary>frameworks-mobile/Switch.css · 纯样式(CSS) · 98 行</summary>
<div class="m-code">
<div class="m-code-head"><span>frameworks-mobile/Switch.css</span><button class="m-copy" type="button" data-copy="code-175">复制</button></div>
<pre id="code-175"><code>/* Kole UI Mobile · Switch 样式 — 对齐移动端规格 §29
开关:轨道 + 滑块,开态用「滑块位移 + 品牌底色」双通道表达(不靠颜色单独表意);
整行(开关 + 文字)是热区,行高不小于 44px —— 开关本体视觉只有 48×28,单点本体手指点不中;
控件是原生 button + role=&quot;switch&quot; + aria-checked(读屏播报「开关」,不是「复选框」)。 */
.kole-m-switch {
/* 组件级变量:业务侧可在容器上覆盖 */
--kole-m-switch-track-w: 48px; /* 轨道宽 */
--kole-m-switch-track-h: 28px; /* 轨道高 */
--kole-m-switch-knob: 24px; /* 滑块直径(轨道高 − 2×2px 内边距) */
--kole-m-switch-duration: 150ms; /* 滑块位移与底色过渡时长 */
box-sizing: border-box;
display: flex;
align-items: center;
gap: var(--kole-space-12);
width: 100%;
min-height: var(--kole-m-touch-target);
padding: var(--kole-space-8) var(--kole-m-gutter);
border: 0;
background: var(--kole-color-card-bg);
color: var(--kole-color-text-body);
font-family: inherit;
font-size: var(--kole-m-font-size-body);
line-height: 1.4;
text-align: start;
cursor: pointer;
touch-action: manipulation;
}
/* 变体 labelPlacement=left:文字在左、开关在右(整行右对齐的值区风格) */
.kole-m-switch--left { flex-direction: row-reverse; }
/* 变体 labelPlacement=left 时文字占满,开关贴右 */
.kole-m-switch--left .kole-m-switch__text { text-align: end; }
.kole-m-switch__track {
position: relative;
flex: 0 0 auto;
display: inline-block;
box-sizing: border-box;
width: var(--kole-m-switch-track-w);
height: var(--kole-m-switch-track-h);
border-radius: 999px;
/* 关态轨道用「未选中图标」色(3.23:1):开关状态承载信息,需满足 WCAG 1.4.11 的 3:1 */
background: var(--kole-color-icon-inactive);
transition: background var(--kole-m-switch-duration) var(--kole-ease-standard);
}
.kole-m-switch__knob {
position: absolute;
top: 2px;
left: 2px;
box-sizing: border-box;
width: var(--kole-m-switch-knob);
height: var(--kole-m-switch-knob);
border-radius: 50%;
background: var(--kole-color-card-bg);
box-shadow: var(--kole-shadow-low);
transition: transform var(--kole-m-switch-duration) var(--kole-ease-standard);
}
.kole-m-switch__text { flex: 1 1 auto; min-width: 0; }
/* 状态 on:滑块右移 + 轨道变品牌色(与 aria-checked=&quot;true&quot; 同步写) */
.kole-m-switch.is-on .kole-m-switch__track { background: var(--kole-color-brand); }
.kole-m-switch.is-on .kole-m-switch__knob {
transform: translateX(calc(var(--kole-m-switch-track-w) - var(--kole-m-switch-knob) - 4px));
}
/* 变体 size=small:紧凑表单用(轨道 40×22) */
.kole-m-switch--small {
--kole-m-switch-track-w: 40px;
--kole-m-switch-track-h: 22px;
--kole-m-switch-knob: 18px;
}
/* 状态 disabled:置灰且不响应 */
.kole-m-switch.is-disabled {
color: var(--kole-color-text-disabled);
cursor: not-allowed;
}
.kole-m-switch.is-disabled:active { background: var(--kole-color-card-bg); }
.kole-m-switch:active:not(.is-disabled) { background: var(--kole-color-table-header-bg); }
.kole-m-switch:focus-visible {
outline: 2px solid var(--kole-color-focus-ring);
outline-offset: -2px;
}
/* 减少动态偏好:滑块瞬时到位,不做过场动画 */
@media (prefers-reduced-motion: reduce) {
.kole-m-switch__track,
.kole-m-switch__knob { transition: none; }
}
</code></pre>
</div>
</details>
<details class="m-details">
<summary>frameworks-mobile/Switch.html · H5 原生(无框架) · 123 行</summary>
<div class="m-code">
<div class="m-code-head"><span>frameworks-mobile/Switch.html</span><button class="m-copy" type="button" data-copy="code-176">复制</button></div>
<pre id="code-176"><code>&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;zh-CN&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, viewport-fit=cover&quot;&gt;
&lt;title&gt;Kole UI Mobile · Switch(H5)&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;../.design_library/kole-ui-mobile/colors_and_type.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;Switch.css&quot;&gt;
&lt;style&gt;
body { margin: 0; background: var(--kole-color-page-bg); font-family: var(--kole-font-family); color: var(--kole-color-text-body); }
.demo { max-width: 375px; margin: 0 auto; padding: var(--kole-m-gutter) 0; }
.demo-label { margin: 0; padding: var(--kole-space-12) var(--kole-m-gutter) var(--kole-space-8);
font-size: var(--kole-m-font-size-label); color: var(--kole-color-text-secondary); }
.demo-box { background: var(--kole-color-card-bg); border-block: 1px solid var(--kole-color-border); }
.demo-block[hidden] { display: none; }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;demo&quot;&gt;
&lt;section class=&quot;demo-block&quot; data-demo=&quot;basic&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;基础用法(整行可点:开关本体只有 48×28,单点本体手指点不中)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot;&gt;
&lt;button class=&quot;kole-m-switch is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot; id=&quot;sw-product&quot;
data-assert=&quot;switch-basic&quot; data-behavior=&quot;click-toggles-class:#sw-product|is-on&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;商品公开可见&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;demo-block&quot; data-demo=&quot;states&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;关态与开态(两者不能只靠颜色区分:滑块位置 + aria-checked 双通道)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-states&quot;&gt;
&lt;button class=&quot;kole-m-switch&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;false&quot; data-switch=&quot;off&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;关态(默认)&lt;/span&gt;
&lt;/button&gt;
&lt;button class=&quot;kole-m-switch is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot; id=&quot;sw-on&quot; data-switch=&quot;on&quot;
data-behavior=&quot;click-sets-attr:#sw-on|aria-checked|false&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;开态(滑块右移 + 品牌底色)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;demo-block&quot; data-demo=&quot;size&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;尺寸两档(size=default 48×28 / size=small 40×22,紧凑表单用)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-size&quot;&gt;
&lt;button class=&quot;kole-m-switch is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;default(48×28)&lt;/span&gt;
&lt;/button&gt;
&lt;button class=&quot;kole-m-switch kole-m-switch--small is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;small(40×22)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;demo-block&quot; data-demo=&quot;left&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;文字在左(labelPlacement=left:整行右对齐的值区风格)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-left&quot;&gt;
&lt;button class=&quot;kole-m-switch kole-m-switch--left is-on&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;自动同步(成功后写入云端)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;demo-block&quot; data-demo=&quot;disabled&quot;&gt;
&lt;p class=&quot;demo-label&quot;&gt;禁用(置灰且不响应;读屏会播报不可用)&lt;/p&gt;
&lt;div class=&quot;demo-box&quot; data-assert=&quot;switch-disabled&quot;&gt;
&lt;button class=&quot;kole-m-switch kole-m-switch--small is-disabled&quot; type=&quot;button&quot; role=&quot;switch&quot; aria-checked=&quot;false&quot;
aria-disabled=&quot;true&quot; disabled&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;kole-m-switch__text&quot;&gt;内测功能(需管理员开启)&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;/div&gt;
&lt;script&gt;
/* 演示页脚本:真实的切换。
- 点击:切 is-on + aria-checked(两者必须同步写,只切类会让读屏读到旧状态)
- 禁用项不响应(原生 disabled 已拦住 click,这里再兜一次)
真实业务里这份状态由宿主管理(受控 checked + change 事件),此处是最小可运行实现。 */
(function () {
function sync(btn) {
var on = btn.classList.contains('is-on');
btn.setAttribute('aria-checked', on ? 'true' : 'false');
}
Array.prototype.forEach.call(document.querySelectorAll('.kole-m-switch'), function (btn) {
if (btn.disabled || btn.classList.contains('is-disabled')) return;
btn.addEventListener('click', function () {
btn.classList.toggle('is-on');
sync(btn);
});
});
})();
&lt;/script&gt;
&lt;script&gt;
/* ?demo=&lt;id&gt; → 只显示该演示块(文档站按块预览用;无参数时全部显示,测试与回归走无参数路径) */
(function () {
var id = new URLSearchParams(location.search).get('demo');
if (!id) return;
var blocks = Array.prototype.slice.call(document.querySelectorAll('.demo-block'));
var hit = false;
blocks.forEach(function (b) {
var on = b.getAttribute('data-demo') === id;
if (on) hit = true;
b.hidden = !on;
});
if (!hit) { blocks.forEach(function (b) { b.hidden = false; }); return; }
document.body.classList.add('demo-single');
blocks.forEach(function (b) {
var label = b.querySelector('.demo-label');
if (label &amp;&amp; !b.hidden) label.hidden = true;
});
})();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
</div>
</details>
<details class="m-details">
<summary>frameworks-mobile/Switch.jsx · React · 47 行</summary>
<div class="m-code">
<div class="m-code-head"><span>frameworks-mobile/Switch.jsx</span><button class="m-copy" type="button" data-copy="code-177">复制</button></div>
<pre id="code-177"><code>import React from 'react';
import './Switch.css';
/* 开关(移动端)— 规格 §29
控件是原生 button + role=&quot;switch&quot; + aria-checked(读屏播报「开关」,不是「复选框」)。
整行(轨道 + 文字)都是热区,行高不小于 44px —— 开关本体视觉只有 48×28。
开态用「滑块位移 + 品牌底色」双通道表达,不靠颜色单独表意。
本端只回传切换意图(onChange),是否真的生效由宿主决定(规格 §29.5)。 */
export default function Switch({
checked = false,
size = 'default',
labelPlacement = 'right',
disabled = false,
label = '',
onChange,
children = null,
}) {
const cls =
'kole-m-switch' +
(size === 'small' ? ' kole-m-switch--small' : '') +
(labelPlacement === 'left' ? ' kole-m-switch--left' : '') +
(checked ? ' is-on' : '') +
(disabled ? ' is-disabled' : '');
return (
&lt;button
className={cls}
type=&quot;button&quot;
role=&quot;switch&quot;
aria-checked={checked ? 'true' : 'false'}
aria-label={label || undefined}
aria-disabled={disabled ? 'true' : undefined}
disabled={disabled}
onClick={() =&gt; {
if (disabled) return;
if (onChange) onChange(!checked);
}}
&gt;
&lt;span className=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;
&lt;span className=&quot;kole-m-switch__knob&quot; /&gt;
&lt;/span&gt;
{children || label ? &lt;span className=&quot;kole-m-switch__text&quot;&gt;{children || label}&lt;/span&gt; : null}
&lt;/button&gt;
);
}
</code></pre>
</div>
</details>
<details class="m-details">
<summary>frameworks-mobile/Switch.vue2.vue · Vue 2 · 56 行</summary>
<div class="m-code">
<div class="m-code-head"><span>frameworks-mobile/Switch.vue2.vue</span><button class="m-copy" type="button" data-copy="code-178">复制</button></div>
<pre id="code-178"><code>&lt;template&gt;
&lt;button
class=&quot;kole-m-switch&quot;
:class=&quot;switchClass&quot;
type=&quot;button&quot;
role=&quot;switch&quot;
:aria-checked=&quot;checked ? 'true' : 'false'&quot;
:aria-label=&quot;label || null&quot;
:aria-disabled=&quot;disabled ? 'true' : null&quot;
:disabled=&quot;disabled&quot;
@click=&quot;onClick&quot;
&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;span v-if=&quot;$slots.default || label&quot; class=&quot;kole-m-switch__text&quot;&gt;&lt;slot&gt;{{ label }}&lt;/slot&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;/template&gt;
&lt;script&gt;
/* 开关(移动端)— 规格 §29
控件是原生 button + role=&quot;switch&quot; + aria-checked(读屏播报「开关」,不是「复选框」)。
整行(轨道 + 文字)都是热区,行高不小于 44px —— 开关本体视觉只有 48×28。
开态用「滑块位移 + 品牌底色」双通道表达,不靠颜色单独表意。
本端只回传切换意图(change),是否真的生效由宿主决定(规格 §29.5)。 */
export default {
name: 'KoleMSwitch',
props: {
checked: { type: Boolean, default: false },
size: { type: String, default: 'default' },
labelPlacement: { type: String, default: 'right' },
disabled: { type: Boolean, default: false },
label: { type: String, default: '' }
},
computed: {
switchClass: function () {
return [
this.size === 'small' ? 'kole-m-switch--small' : '',
this.labelPlacement === 'left' ? 'kole-m-switch--left' : '',
this.checked ? 'is-on' : '',
this.disabled ? 'is-disabled' : ''
].filter(Boolean);
}
},
methods: {
onClick: function () {
if (this.disabled) return;
this.$emit('change', !this.checked);
}
}
};
&lt;/script&gt;
&lt;style src=&quot;./Switch.css&quot;&gt;&lt;/style&gt;
</code></pre>
</div>
</details>
<details class="m-details">
<summary>frameworks-mobile/Switch.vue3.vue · Vue 3 · 51 行</summary>
<div class="m-code">
<div class="m-code-head"><span>frameworks-mobile/Switch.vue3.vue</span><button class="m-copy" type="button" data-copy="code-179">复制</button></div>
<pre id="code-179"><code>&lt;template&gt;
&lt;button
class=&quot;kole-m-switch&quot;
:class=&quot;switchClass&quot;
type=&quot;button&quot;
role=&quot;switch&quot;
:aria-checked=&quot;checked ? 'true' : 'false'&quot;
:aria-label=&quot;label || null&quot;
:aria-disabled=&quot;disabled ? 'true' : null&quot;
:disabled=&quot;disabled&quot;
@click=&quot;onClick&quot;
&gt;
&lt;span class=&quot;kole-m-switch__track&quot; aria-hidden=&quot;true&quot;&gt;
&lt;span class=&quot;kole-m-switch__knob&quot;&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;span v-if=&quot;$slots.default || label&quot; class=&quot;kole-m-switch__text&quot;&gt;&lt;slot&gt;{{ label }}&lt;/slot&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;/template&gt;
&lt;script setup&gt;
/* 开关(移动端)— 规格 §29
控件是原生 button + role=&quot;switch&quot; + aria-checked(读屏播报「开关」,不是「复选框」)。
整行(轨道 + 文字)都是热区,行高不小于 44px —— 开关本体视觉只有 48×28。
开态用「滑块位移 + 品牌底色」双通道表达,不靠颜色单独表意。
本端只回传切换意图(change),是否真的生效由宿主决定(规格 §29.5)。 */
import { computed } from 'vue';
const props = defineProps({
checked: { type: Boolean, default: false },
size: { type: String, default: 'default' },
labelPlacement: { type: String, default: 'right' },
disabled: { type: Boolean, default: false },
label: { type: String, default: '' }
});
const emit = defineEmits(['change']);
const switchClass = computed(() =&gt; [
props.size === 'small' ? 'kole-m-switch--small' : '',
props.labelPlacement === 'left' ? 'kole-m-switch--left' : '',
props.checked ? 'is-on' : '',
props.disabled ? 'is-disabled' : ''
].filter(Boolean));
function onClick() {
if (props.disabled) return;
emit('change', !props.checked);
}
&lt;/script&gt;
&lt;style src=&quot;./Switch.css&quot;&gt;&lt;/style&gt;
</code></pre>
</div>
</details>
<details class="m-details">
<summary>frameworks-mobile/Switch.uniapp.vue · uni-app(跨端:小程序 / App / H5) · 121 行</summary>
<div class="m-code">
<div class="m-code-head"><span>frameworks-mobile/Switch.uniapp.vue</span><button class="m-copy" type="button" data-copy="code-180">复制</button></div>
<pre id="code-180"><code>&lt;template&gt;
&lt;view
class=&quot;kole-m-switch&quot;
:class=&quot;switchClass&quot;
:role=&quot;disabled ? '' : 'switch'&quot;
:aria-checked=&quot;checked ? 'true' : 'false'&quot;
:aria-disabled=&quot;disabled ? 'true' : 'false'&quot;
:aria-label=&quot;label || ''&quot;
@tap=&quot;onTap&quot;
&gt;
&lt;view class=&quot;kole-m-switch__track&quot;&gt;
&lt;view class=&quot;kole-m-switch__knob&quot;&gt;&lt;/view&gt;
&lt;/view&gt;
&lt;text v-if=&quot;label&quot; class=&quot;kole-m-switch__text&quot;&gt;{{ label }}&lt;/text&gt;
&lt;slot&gt;&lt;/slot&gt;
&lt;/view&gt;
&lt;/template&gt;
&lt;script setup&gt;
/* uni-app 端 · 开关(移动端)— 规格 §29
跨端差异:
① 小程序端不用 uni 的 &lt;switch&gt; 基础组件:它无法承载「文字在左 / 紧凑尺寸」这类形态,
也带不进 kole-m- 的样式与令牌,故用 view 自绘 + role=&quot;switch&quot; + aria-checked;
② 点击用 @tap(触屏),不依赖 pointer/mouse;
③ 滑块位移与底色过渡由 CSS transition 承担,代码里不做动画。
尺寸用 rpx:88rpx = 375pt 下的 44px 触控最小边长,故整行最小高 88rpx,
轨道 96rpx×56rpx(= 48×28),滑块 48rpx(= 24)。 */
import { computed } from 'vue';
const props = defineProps({
checked: { type: Boolean, default: false },
size: { type: String, default: 'default' },
labelPlacement: { type: String, default: 'right' },
disabled: { type: Boolean, default: false },
label: { type: String, default: '' }
});
const emit = defineEmits(['change']);
const switchClass = computed(() =&gt; [
props.size === 'small' ? 'kole-m-switch--small' : '',
props.labelPlacement === 'left' ? 'kole-m-switch--left' : '',
props.checked ? 'is-on' : '',
props.disabled ? 'is-disabled' : ''
].filter(Boolean));
function onTap() {
if (props.disabled) return;
emit('change', !props.checked);
}
&lt;/script&gt;
&lt;style&gt;
.kole-m-switch {
--kole-m-switch-track-w: 96rpx; /* 轨道宽(96rpx = 48px) */
--kole-m-switch-track-h: 56rpx; /* 轨道高(56rpx = 28px) */
--kole-m-switch-knob: 48rpx; /* 滑块直径(48rpx = 24px) */
--kole-m-touch-target: 88rpx;
--kole-m-font-size-body: 32rpx;
--kole-m-gutter: 32rpx;
box-sizing: border-box;
display: flex;
align-items: center;
width: 100%;
min-height: var(--kole-m-touch-target);
padding: 16rpx var(--kole-m-gutter);
background-color: var(--kole-color-card-bg);
color: var(--kole-color-text-body);
font-size: var(--kole-m-font-size-body);
}
.kole-m-switch--left { flex-direction: row-reverse; }
.kole-m-switch--left .kole-m-switch__text { text-align: right; }
.kole-m-switch__track {
position: relative;
flex-shrink: 0;
width: var(--kole-m-switch-track-w);
height: var(--kole-m-switch-track-h);
border-radius: 999rpx;
background-color: var(--kole-color-icon-inactive);
transition: background-color 150ms ease;
}
.kole-m-switch__knob {
position: absolute;
top: 4rpx;
left: 4rpx;
width: var(--kole-m-switch-knob);
height: var(--kole-m-switch-knob);
border-radius: 50%;
background-color: var(--kole-color-card-bg);
transition: transform 150ms ease;
}
/* 状态 on:滑块右移 + 轨道变品牌色 */
.kole-m-switch.is-on .kole-m-switch__track { background-color: var(--kole-color-brand); }
.kole-m-switch.is-on .kole-m-switch__knob {
transform: translateX(calc(var(--kole-m-switch-track-w) - var(--kole-m-switch-knob) - 8rpx));
}
/* 变体 size=small:通道 80rpx×44rpx(= 40×22),滑块 36rpx(= 18) */
.kole-m-switch--small {
--kole-m-switch-track-w: 80rpx;
--kole-m-switch-track-h: 44rpx;
--kole-m-switch-knob: 36rpx;
}
.kole-m-switch__text {
flex: 1;
padding-left: 24rpx;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.kole-m-switch--left .kole-m-switch__text { padding-left: 0; padding-right: 24rpx; }
.kole-m-switch.is-disabled { color: var(--kole-color-text-disabled); }
&lt;/style&gt;
</code></pre>
</div>
</details>
</section>
<section class="m-block">
<h2 id="tests">测试与回归</h2>
<p class="m-sub">断言在真实的 375×640 设备帧里跑(引擎与 PC 侧共用 <code>tests/_runtime.js</code>,触控行为动词来自移动端 <code>tests/mobile/_behaviors.js</code>)。</p>
<p>
<span class="m-status pass">断言 17 条 · 全部通过</span>
<span class="m-status">报告 2026-09-22 23:01:05</span>
</p>
<div class="m-code">
<div class="m-code-head"><span>复现命令</span><button class="m-copy" type="button" data-copy="code-test">复制</button></div>
<pre id="code-test"><code>node site/dev-server.js &amp;
REG_BASE=http://127.0.0.1:3311 node tools/run-mobile-regression.mjs # 全量 5 个组件
npm run verify:mobile-docs # 本页内容完整性 + API 与源码一致性</code></pre>
</div>
</section>
<section class="m-block">
<h2 id="contract">设计契约</h2>
<details class="m-details">
<summary>components/mobile-switch.json(点击展开原始 JSON)</summary>
<div class="m-code"><pre><code>{
"schemaVersion": 1,
"sourceKind": "authored-spec",
"provenance": "authored-in-repo",
"specFile": "spec/移动端规格.md",
"specSection": "29 · 开关 Switch",
"confidence": "high",
"slug": "mobile-switch",
"name": "开关 Switch",
"semanticTypeCandidates": [
"switch",
"toggle",
"boolean-input"
],
"variantDimensions": [
{
"name": "size",
"values": [
"default",
"small"
]
},
{
"name": "labelPlacement",
"values": [
"right",
"left"
]
}
],
"representativeVariants": [
{
"size": "default",
"labelPlacement": "right",
"label": "默认(轨道 48×28,文字在右)"
},
{
"size": "small",
"labelPlacement": "right",
"label": "紧凑(轨道 40×22,紧凑表单用)"
},
{
"size": "default",
"labelPlacement": "left",
"label": "文字在左(整行右对齐的值区风格)"
}
],
"anatomy": {
"switch": "根元素,一行里放进「轨道 + 文字标签」,整行可点",
"track": "轨道,承载背景色与滑块位移的边界",
"knob": "滑块,关态靠左、开态靠右(位移是开/关的主要视觉信号)",
"text": "可选文字标签,说明这项开关控制什么",
"control": "可点的整行控件(原生 button + role=\"switch\"),承接键盘与读屏"
},
"structurePatterns": {
"size": "default(轨道 48×28)/ small(轨道 40×22,用于紧凑表单)",
"labelPlacement": "right(文字在开关右侧,默认)/ left(文字在左侧,值区右对齐时用)"
},
"usageHints": [
"即时启停一项配置或业务状态(启用通知、公开数据、自动同步)",
"移动端开关本体视觉只有 48×28,但整行(开关 + 文字)都是可点热区,行高不小于 44px",
"关态与开态不能只靠颜色区分,必须同时看到滑块位移",
"点击切换只需要一次触摸,不要求拖动滑块(拖动是桌面习惯,触屏误触率高)",
"切换后立即触发 change 事件,不做二次确认(需要确认的场景由宿主先弹对话框)"
],
"doNotInvent": [
"二次确认弹窗与「切换失败回滚」的业务流程",
"三态开关(关 / 开 / 待定)的视觉表达",
"与表单一起提交时的隐藏字段(由宿主添加)"
],
"unknowns": [
"开关本体是否允许小于 48×28(紧凑表单的下限)",
"文案与开关的间距是否跟随字号",
"加载态(切换请求进行中)如何表达"
],
"interaction": [
"整行(开关 + 文字)都是热区,行高不小于 44px;开关本体不可单独缩到 44px 以下",
"点击切换只需要一次触摸,不要求拖动滑块(拖动是桌面习惯,触屏误触率高)",
"切换动效是滑块位移 150ms 过渡;减少动态偏好下瞬时切换",
"关态与开态不能只靠颜色区分:滑块位置 + aria-checked 双通道",
"切换后立即触发 change 事件,不做二次确认(需要确认的场景由宿主先弹对话框)"
],
"accessibility": [
"用 role=\"switch\" + aria-checked=\"true|false\",而不是 role=\"checkbox\"(读屏会播报「开关」)",
"承载控件是原生 button,键盘可聚焦、空格/回车可切换,并有可见焦点环",
"文字标签在控件内部,读屏播报的名称就是标签本身;无标签时用 label 属性补 aria-label",
"禁用态用原生 disabled,读屏会播报不可用"
],
"api": {
"source": "implementation",
"note": "props / events / slots 为 6 端实现的公共接口(说明文字取自规格对应小节)。字段名与各端源码逐名核对:node tools/verify-mobile-docs.mjs",
"requiredNote": "「必传」按严格定义:实现里**没有默认值**时才为 Y(本门禁逐条核对 props 与各端源码的默认值,防止契约与实现脱节)。",
"props": [
{
"name": "checked",
"type": "boolean",
"default": "false",
"desc": "受控开关值;与 aria-checked 同步写(规格 §29.5)",
"required": false
},
{
"name": "size",
"type": "'default' | 'small'",
"default": "'default'",
"desc": "变体 size:default 轨道 48×28,small 轨道 40×22(规格 §29.3)",
"required": false
},
{
"name": "labelPlacement",
"type": "'right' | 'left'",
"default": "'right'",
"desc": "变体 labelPlacement:文字在开关右侧还是左侧(规格 §29.3)",
"required": false
},
{
"name": "disabled",
"type": "boolean",
"default": "false",
"desc": "状态 disabled:置灰且不可聚焦(规格 §29.4)",
"required": false
},
{
"name": "label",
"type": "string",
"default": "''",
"desc": "文字标签;同时作为无障碍名称落到 aria-label(规格 §29.6)",
"required": false
}
],
"events": [
{
"name": "change",
"params": "(checked)",
"desc": "切换时触发,回传切换后的目标值(规格 §29.5)"
}
],
"slots": [
{
"name": "default",
"desc": "文字标签内容,优先于 label 属性(规格 §29.2 text)"
}
]
},
"variantClasses": {
"size": {
"default": [],
"small": [
".kole-m-switch--small"
]
},
"labelPlacement": {
"right": [],
"left": [
".kole-m-switch--left"
]
}
},
"demos": [
{
"id": "basic",
"group": "01 组件类型",
"title": "基础用法",
"desc": "整行可点:开关本体只有 48×28,单点本体手指点不中。",
"variant": "size=default"
},
{
"id": "states",
"group": "02 组件状态",
"title": "关态与开态",
"desc": "两者不能只靠颜色区分:滑块位置 + aria-checked 双通道。",
"variant": "状态 off|on"
},
{
"id": "size",
"group": "01 组件类型",
"title": "尺寸两档",
"desc": "default 轨道 48×28;small 轨道 40×22 用于紧凑表单。",
"variant": "size=default|small"
},
{
"id": "left",
"group": "01 组件类型",
"title": "文字在左",
"desc": "labelPlacement=left:整行右对齐的值区风格,文字在开关左侧。",
"variant": "labelPlacement=left"
},
{
"id": "disabled",
"group": "02 组件状态",
"title": "禁用",
"desc": "置灰且不响应;读屏会播报不可用。",
"variant": "disabled=true"
}
],
"related": [
{
"slug": "cell",
"why": "开关常作为单元格的右侧内容(Cell 负责行结构与分隔线,开关只管切换)"
},
{
"slug": "mobile-button",
"why": "需要用户确认后一次性提交的用按钮;状态需要即时生效的用开关"
},
{
"slug": "mobile-dialog",
"why": "切换前需要用户确认(如扣费项)时先弹对话框;开关本身不做二次确认"
}
]
}</code></pre></div>
</details>
</section>
<footer class="m-foot">
<p>生成物:本页由 <code>node tools/build-mobile.mjs</code> 从契约 <code>components/mobile-switch.json</code> 与 6 端源码生成,请勿手改。</p>
<p>规格原文:<code>.design_library/kole-ui-mobile/spec/移动端规格.md</code> · 隔离门禁:<code>node tools/verify-mobile-isolation.mjs</code></p>
</footer>
</main>
</div>
<script>
(function () {
/* 左栏:≤1000px 默认收起(手机上不让导航占满一屏),跨断点时同步一次;不监听 resize,
免得跟用户手动的展开/收起抢状态。桌面不设 open 之外的动作。 */
var side = document.querySelector('details.m-side');
if (side && window.matchMedia) {
var mq = window.matchMedia('(max-width: 1000px)');
var sync = function () { if (mq.matches) side.removeAttribute('open'); else side.setAttribute('open', ''); };
sync();
if (mq.addEventListener) mq.addEventListener('change', sync);
else if (mq.addListener) mq.addListener(sync);
}
/* 单演示预览帧按内容高度自适应(同源可读 contentDocument;上限 360px、下限 140px)。
固定高度会在内容少的演示上留一大片空白(实测 SwipeCell 单个演示只用 ~120px)。 */
function fitDemoFrames() {
var frames = document.querySelectorAll('.m-demo-stage iframe');
Array.prototype.forEach.call(frames, function (f) {
var fit = function () {
try {
var d = f.contentDocument;
if (!d || !d.body) return;
/* 只量 body.scrollHeight:documentElement.scrollHeight 等于**帧视口高度**(html 撑满视口),
拿它做 max 会形成「帧高→视口高→量到的高度」反馈环,永远收敛在初始值(实测锁在 308px)。 */
var h = d.body.scrollHeight;
if (!h) h = d.documentElement ? d.documentElement.scrollHeight : 0;
if (!h) return;
f.style.height = Math.min(360, Math.max(140, h + 8)) + 'px';
} catch (e) { /* 跨源或未就绪:保持 CSS 兜底高度 */ }
};
f.addEventListener('load', function () { fit(); setTimeout(fit, 300); });
fit();
});
}
fitDemoFrames();
window.addEventListener('load', fitDemoFrames);
/* 主题模式(三态):与 PC 完全同一约定 —— localStorage[kole-mode] ∈ light|dark|auto,
auto 跟随系统,反色靠 html.kole-dark(令牌文件里已写好该组,两端共用一份令牌)。
用户在 PC 站选夜间,进移动端站也是夜间。 */
var MODE_KEY = 'kole-mode';
var MODE_LABEL = { light: '日间模式', dark: '夜间模式', auto: '自动模式' };
function readMode() {
try { var v = localStorage.getItem(MODE_KEY); if (v === 'light' || v === 'dark' || v === 'auto') return v; } catch (e) {}
return 'auto';
}
function systemDark() {
return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
}
function applyMode(mode) {
var dark = mode === 'dark' || (mode === 'auto' && systemDark());
document.documentElement.classList.toggle('kole-dark', dark);
document.documentElement.style.colorScheme = dark ? 'dark' : 'light';
var cur = document.getElementById('m-mode-cur');
if (cur) cur.textContent = MODE_LABEL[mode] || MODE_LABEL.auto;
Array.prototype.forEach.call(document.querySelectorAll('.m-menu-opt'), function (o) {
o.setAttribute('aria-selected', o.getAttribute('data-mode') === mode ? 'true' : 'false');
});
/* 三态图标与 PC 同口径:显式选择时按时态给图标(日间=月亮 / 夜间=太阳),
自动模式下恒为「跟随系统」的显示器图标(此时两个显式图标都不显示)。 */
var moon = document.getElementById('m-icon-moon');
var sun = document.getElementById('m-icon-sun');
var auto = document.getElementById('m-icon-auto');
if (moon) moon.style.display = (mode === 'light') ? '' : 'none';
if (sun) sun.style.display = (mode === 'dark') ? '' : 'none';
if (auto) auto.style.display = (mode === 'auto') ? '' : 'none';
}
function bindModeMenu() {
var trigger = document.getElementById('m-mode-trigger');
var menu = document.getElementById('m-mode-menu');
if (!trigger || !menu) return;
var close = function (refocus) {
menu.classList.add('hidden'); trigger.setAttribute('aria-expanded', 'false');
if (refocus) trigger.focus();
};
trigger.addEventListener('click', function () {
var open = menu.classList.toggle('hidden');
trigger.setAttribute('aria-expanded', open ? 'false' : 'true');
});
trigger.addEventListener('keydown', function (e) {
if (e.key === 'Escape') close(true);
});
Array.prototype.forEach.call(menu.querySelectorAll('.m-menu-opt'), function (opt) {
opt.addEventListener('click', function () {
var mode = opt.getAttribute('data-mode');
try { localStorage.setItem(MODE_KEY, mode); } catch (e) {}
applyMode(mode);
close(true);
});
});
document.addEventListener('click', function (e) {
if (!menu.classList.contains('hidden') && !menu.contains(e.target) && !trigger.contains(e.target)) close(false);
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !menu.classList.contains('hidden')) close(true);
});
/* 自动模式下跟随系统切换 */
if (window.matchMedia) {
var mq = window.matchMedia('(prefers-color-scheme: dark)');
var onChange = function () { if (readMode() === 'auto') applyMode('auto'); };
if (mq.addEventListener) mq.addEventListener('change', onChange);
else if (mq.addListener) mq.addListener(onChange);
}
applyMode(readMode());
}
bindModeMenu();
/* 版本下拉:与 PC 顶栏同一实现口径 —— 清单从部署根取(<prefix>/versions.json 与
<prefix>/site/versions.json 两份按序尝试,取有可用条目的那份),拿不到就退回不可点的
纯角标(data-single="1"),不留一个点不开的下拉。
清单不许把用户带去外站:path 只允许 ".."(站点根自身)或 x.y.z 快照目录名。 */
var VERSION_KEY_PATH = '..';
function isValidVersionPath(p) {
if (typeof p !== 'string' || !p.length || p.indexOf('\\') >= 0 || p.indexOf('//') >= 0) return false;
if (p === VERSION_KEY_PATH) return true;
return /^\d+\.\d+\.\d+$/.test(p);
}
/* 清单里的 path 相对部署根,".." = /site/、x.y.z = /x.y.z/site/。
当前页可能不在部署根下(快照页),故先还原部署根绝对路径再算回相对当前页。 */
function versionHref(absSite) {
var i = location.pathname.indexOf('/site/');
var prefix = i >= 0 ? location.pathname.slice(0, i) : '';
if (!prefix || absSite.indexOf(prefix + '/') !== 0) return absSite;
var rest = absSite.slice(prefix.length + 1);
var up = '';
var segs = prefix.slice(1).split('/');
for (var k = 0; k < segs.length; k++) up += "../";
return up + rest;
}
function buildVersionList(doc) {
var raw = doc && doc.versions;
if (!raw || !raw.length) return [];
var i = location.pathname.indexOf('/site/');
var prefix = i >= 0 ? location.pathname.slice(0, i) : '';
var list = [];
for (var k = 0; k < raw.length; k++) {
var v = raw[k] || {};
var ver = v.version ? String(v.version) : '';
var path = v.path ? String(v.path) : '';
if (!ver || !isValidVersionPath(path)) continue;
var absSite = (path === VERSION_KEY_PATH ? '' : '/' + path) + '/site/';
list.push({ version: ver, date: v.date ? String(v.date) : '', latest: !!v.latest, href: versionHref(absSite), absSite: absSite, current: false });
}
if (list.length) {
/* 当前版本 = 绝对路径上命中的最长前缀(最具体的那个)——与 PC 的判定一致,
快照页与根站点都能算对。 */
var here = location.pathname;
var best = -1;
for (var j = 0; j < list.length; j++) {
if (here.indexOf(list[j].absSite) === 0 && list[j].absSite.length > best) best = list[j].absSite.length;
}
for (var m = 0; m < list.length; m++) list[m].current = best >= 0 && list[m].absSite.length === best;
if (best < 0 && list[0]) list[0].current = true;
}
return list;
}
function bindVersionMenu() {
var trigger = document.getElementById('m-ver-trigger');
var menu = document.getElementById('m-ver-menu');
var chip = document.getElementById('m-ver');
if (!trigger || !menu) return;
var close = function (refocus) {
menu.classList.add('hidden'); trigger.setAttribute('aria-expanded', 'false');
if (refocus) trigger.focus();
};
var interactive = false;
var render = function (list) {
interactive = !!(list && list.length);
/* 只有清单拿不到才退回纯角标;只要有一版就是下拉(里面至少能看清当前是哪个版本) */
trigger.setAttribute('data-single', interactive ? '0' : '1');
if (!interactive) { menu.classList.add("hidden"); return; }
var cur = null;
for (var k = 0; k < list.length; k++) if (list[k].current) cur = list[k];
if (chip) chip.textContent = 'v' + ((cur && cur.version) || VERSION);
/* 重建选项(幂等:重复调用不会叠加) */
Array.prototype.slice.call(menu.querySelectorAll('.m-ver-opt')).forEach(function (n) { n.remove(); });
list.forEach(function (it) {
var opt = document.createElement('button');
opt.type = 'button'; opt.className = 'm-ver-opt'; opt.setAttribute('role', 'option');
opt.setAttribute('data-version', it.version);
opt.setAttribute('aria-selected', it.current ? 'true' : 'false');
var tag = it.current ? '当前浏览' : (it.latest ? '最新版' : (it.date || ''));
opt.innerHTML = '<span class="m-ver-opt-num">v' + it.version + '</span><span class="m-ver-opt-tag">' + tag + '</span>';
if (it.href) opt.setAttribute('data-href', it.href);
opt.addEventListener("click", function () {
if (!it.current && it.href) location.href = it.href;
else close(true);
});
menu.appendChild(opt);
});
};
trigger.addEventListener('click', function () {
if (!interactive) return;
if (menu.classList.contains('hidden')) { menu.classList.remove('hidden'); trigger.setAttribute('aria-expanded', 'true'); }
else close(false);
});
trigger.addEventListener('keydown', function (e) {
if (!interactive) return;
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { e.preventDefault(); menu.classList.remove('hidden'); trigger.setAttribute('aria-expanded', 'true'); }
else if (e.key === 'Escape') close(true);
});
document.addEventListener('click', function (e) {
if (!menu.classList.contains('hidden') && !menu.contains(e.target) && !trigger.contains(e.target)) close(false);
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !menu.classList.contains('hidden')) close(true);
});
/* 两份清单按序尝试(不并行:快照页那份必然 404,并行会把 404 记进控制台,
而站点门禁的「零控制台错误」是硬断言)。 */
if (!window.fetch || location.protocol === 'file:') return;
var i = location.pathname.indexOf('/site/');
if (i < 0) return;
var prefix = location.pathname.slice(0, i);
var urls = [prefix + '/versions.json', prefix + '/site/versions.json'];
var attempt = function (idx) {
if (idx >= urls.length) return;
fetch(urls[idx], { cache: 'no-store' })
.then(function (r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); })
.then(function (doc) { var list = buildVersionList(doc); if (list.length) render(list); else attempt(idx + 1); })
.catch(function () { attempt(idx + 1); });
};
attempt(0);
}
bindVersionMenu();
function done(btn) {
var old = btn.textContent; btn.textContent = '已复制'; btn.classList.add('is-done');
setTimeout(function () { btn.textContent = old; btn.classList.remove('is-done'); }, 1200);
}
document.addEventListener('click', function (e) {
var btn = e.target && e.target.closest ? e.target.closest('.m-copy') : null;
if (!btn) return;
var src = document.getElementById(btn.getAttribute('data-copy') || '');
var text = src ? (src.innerText || src.textContent || '') : '';
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(function () { done(btn); }, function () { fallback(btn, text); });
} else { fallback(btn, text); }
});
function fallback(btn, text) {
try {
var ta = document.createElement('textarea'); ta.value = text; ta.setAttribute('readonly', '');
ta.style.position = 'fixed'; ta.style.opacity = '0'; document.body.appendChild(ta);
ta.select(); document.execCommand("copy"); document.body.removeChild(ta); done(btn);
} catch (err) { btn.textContent = '复制失败'; }
}
})();
</script>
</body>
</html>