# 平台

播报变更，跟踪页面的方向，并按用户的平台调整键盘快捷键。

用 `announce` 向屏幕阅读器发送消息，用 `isRTL` 读取文字方向，用快捷键辅助函数按用户的平台匹配和标注键盘快捷键。

这些辅助函数可以从 `@ui-sdk/core` 引入。你既可以在原语（primitive）里使用它们，也可以单独使用。

## 播报
假设你做了一个复制按钮。复制成功后，用 `announce` 让屏幕阅读器知道：

```ts
import { announce } from "@ui-sdk/core"

async function copyText(text: string) {
  await navigator.clipboard.writeText(text)
  announce("Copied")
}
```

`announce` 写入一个共享的实时区域（live region）。默认情况下，它会让屏幕阅读器等到停顿之后再朗读消息。可以用屏幕阅读器试一试：

对于原本不会被播报的结果，这很有用，比如列表完成排序或者文件复制完成。在动作成功之后调用它，放在事件处理函数或 `bind` 里。`attrs` 和 `state` 里不要有副作用。

## 紧急播报
需要立即引起注意的内容，用 `priority: "assertive"`：

```ts
announce("Enter an email address", { priority: "assertive" })
```

这会要求屏幕阅读器打断当前消息。少用为妙，大多数更新都可以等。

有些屏幕阅读器要求实时区域在文字变化之前就已存在。如果组件出现后很快就会播报，就在它的根槽位的 `bind` 里准备好这些区域：

```ts
import { prepareAnnouncements } from "@ui-sdk/core"

root: slot({
  bind: (el) => prepareAnnouncements(el.ownerDocument),
}),
```

`announce` 默认使用当前文档。对于 frame 里的元素，也要传入它的文档：`announce("Copied", { doc: el.ownerDocument })`。

## 方向
在从左到右的分页器里，按右方向键是前进。在从右到左的分页器里，按左方向键是前进。用 `isRTL` 读取元素计算后的方向：

```ts
import { isRTL } from "@ui-sdk/core"

function isForwardKey(event: KeyboardEvent, el: HTMLElement) {
  const forward = isRTL(el) ? "ArrowLeft" : "ArrowRight"
  return event.key === forward
}
```

在按键处理函数里调用它，这样它会跟随页面方向的变化。聚焦下面的分页器，试试方向键，然后打开 **Right to left**：

[游走焦点（roving focus）](https://ui-sdk.cn/docs/core/behaviors/focus#roving-focus) 已经为 Left 和 Right 处理了这一点。要处理语言而不是方向，见[页面语言](https://ui-sdk.cn/docs/core/text#the-pages-language)。要跟随人们选择的或浏览器偏好的浅色或深色配色，见[偏好设置](/docs/core/behaviors/preferences)。

## 快捷键
在 Apple 平台上用 `mod` 表示 Command，在其他平台上表示 Control。在 `keydown` 处理函数里，先匹配快捷键，再调用你的动作：

```ts
import { matchesShortcut } from "@ui-sdk/core"

if (matchesShortcut(event, "mod+b")) {
  event.preventDefault()
  toggleBold()
}
```

这里的 `toggleBold` 是你自己的函数。`matchesShortcut` 只检查按键；由处理函数阻止浏览器的默认动作，并运行你的动作。

同一个快捷键字符串既能给出可见的标签，也能给出 `aria-keyshortcuts` 的值：

```ts
import { ariaKeyshortcuts, formatShortcut } from "@ui-sdk/core"

formatShortcut("mod+b") // Displays “⌘B” on Apple platforms, “Ctrl+B” elsewhere.
ariaKeyshortcuts("mod+b") // "Meta+B" or "Control+B".
```

这些标签取决于用户的平台。在原语里，把它们放在 `bind` 里设置，这样服务器和客户端的首次渲染就能保持一致：

```ts
keys: slot({
  bind: (el) => {
    el.textContent = formatShortcut("mod+b")
  },
}),
```

在笔记里输入时试试这个快捷键：

`formatShortcut` 用两个不可见字符包裹标签，让它在从右到左的页面上也保持从左到右的顺序。测试时，从 `@ui-sdk/core/testing` 引入 `setPlatform`，调用 `setPlatform("apple")` 或 `setPlatform("other")`。

## 图层与定位
要构建浮层？[放置浮层](https://ui-sdk.cn/docs/core/behaviors/popups#placing-a-popup)介绍了 `position`。[自己的图层](https://ui-sdk.cn/docs/core/behaviors/popups#layers-of-your-own)讲的是 `bindLayer`、顶层，以及让元素在模态之上仍然可用。

## API 参考
| 辅助函数                           | 用途                                   |
| ---------------------------------- | -------------------------------------- |
| `announce(text, options?)`         | 播报结果，可选用 `priority` 和 `doc`。 |
| `prepareAnnouncements(doc)`        | 在第一次播报之前创建实时区域。         |
| `isRTL(el)`                        | 读取元素的文字方向。                   |
| `matchesShortcut(event, shortcut)` | 检查按键是否匹配某个快捷键。           |
| `formatShortcut(shortcut)`         | 用平台的符号或名称显示快捷键。         |
| `ariaKeyshortcuts(shortcut)`       | 生成 `aria-keyshortcuts` 的值。        |
