# 拖动与调整大小

移动和调整对象的大小，显示滑动操作，按压并按住，选取点，绘制范围，并预览选项。

`createMove` 移动对象，`createResize` 改变它们的边界，`createSwipeActions` 露出行背后的操作，`bindHold` 响应持续按住的按压，`bindRepeat` 在按钮被按住期间持续步进。`createArea` 选取二维值，`createSpans` 编辑轨道上的范围，`createPreview` 让人在选定之前试用一个值。

从 `@ui-sdk/core` 导入这些辅助函数。当辅助函数需要 `set` 回调时，把它接到你的[变更](https://ui-sdk.cn/docs/core/kernel/handle-changes#changes)上。要把条目排序，见[拖放](/docs/core/behaviors/drag-and-drop)。

## 在二维中移动
### 放置任意对象
把位置保存在 `Record<string, MovePoint>` 里，并另起一个 `MoveDraft | null` 字段，初始值为 `null`。在 `api` 里把它们接到 `createMove`：

```ts
const change = createChange(context)
const move = createMove(context, {
  key: "positions",
  draft: "moving",
  set: (value, details) => change("positions", value, details),
  step: () => 4,
  bounds: () => ({ minX: 0, minY: 0, maxX: 264, maxY: 144 }),
})
```

从 `api` 返回 `move`。定位槽位使用已提交的坐标作为基础位置。把 `moving` 加到原语的 `live` 字段里，并让实时槽位绑定写入拖动偏移：

```ts
attrs: (state, { key }) => ({
  style: css({
    position: "absolute",
    left: `${state.positions[key].x}px`,
    top: `${state.positions[key].y}px`,
    "touch-action": "none",
  }),
}),
live: live((state, { api, key }) => ({
  "data-dragging": api.move.dragging(state, key) ? "" : undefined,
  style: api.move.style(state, key),
})),
on: {
  keydown: (event, { api, key }) => api.move.keydown(event, key),
  pointerdown: (event, { el, api, key }) => api.move.pointerdown(event, key, el!),
},
```

在一个已定位的容器里，把这个槽位渲染成可获得焦点、带有无障碍名称的元素。实时绑定会更新 `translate`、`--move-x` 和 `--move-y`，而不必为每次指针移动触发框架渲染。

拖动一张贴纸，或者聚焦它并按 Alt 加方向键移动 4 像素。加上 Shift 会移动十倍的距离。拖动过程中按 Escape 会把它放回原处。

`step` 是放下的位置所落到的网格，也是 Alt 加方向键移动的距离；Shift 会按 `largeStep` 移动，默认是十步。`bounds` 限制已提交的坐标；拖动预览在松开之前可以超出这些坐标。放下会报告 `drag`，键盘微移会报告 `move-key`，屏幕阅读器会听到新的位置。

### 一次移动多个
加上 `group`，让选中的一组一起移动：

```ts
group: (key) => {
  const { selected } = context.store.get()
  return selected.includes(key) ? selected : [key]
},
step: () => 20,
disabled: (key) => key === "ship",
label: (key) => titles[key],
```

`step` 设置网格以及 Alt 加方向键的距离，默认为 1。`disabled` 阻止指针和键盘移动，并把该条目从正在移动的分组中移除。

点击便签将其选中，或按住 Shift 点击以加入一个。拖动已选中的便签来移动整个分组。Alt 加方向键会把它微移 20 像素；固定的便签留在原处。

在 pointerdown 时先选择，再调用 `move.pointerdown`。当按压的便签已经在选择中时，保持选择不变。演示把它和[空间焦点](https://ui-sdk.cn/docs/core/behaviors/focus#spatial-focus)结合起来，所以普通方向键可以在便签之间移动。

`label` 提供播报用的名称。`message` 可以用移动的 `keys`、第一个条目的 `label` 以及它新的 `at` 点来自定义句子。`moveMessage` 提供默认文案。

### 缩放与用代码移动
在缩放过的画布上，告诉行为多少个屏幕像素代表一个单位：

```ts
scale: () => context.store.get().zoom,
```

指针距离会除以这个缩放比例。用代码发起的移动仍然使用画布自己的单位：

```ts
move.moveTo("circle", { x: 100, y: 60 })
move.moveBy(["circle"], 10, 0)
```

改变缩放，然后拖动形状。用按钮在不拖动的情况下移动圆形。

两个方法都会报告 `imperative` 并应用 `bounds`。它们不会应用拖动的网格，也不会过滤已禁用的键，所以如有需要，在调用它们之前先检查这些应用规则。

## 调整盒子大小
### 盒子上的手柄
一个盒子有 `{ x, y, width, height }`。把盒子保存在状态里，另起一个 `ResizeDraft | null` 字段并初始化为 `null`，然后在 `api` 里添加 `createResize`：

```ts
const resize = createResize(context, {
  boxes: (state) => state.boxes,
  draft: "draft",
  set: (value, details) => change("boxes", value, details),
  min: () => ({ width: 60, height: 40 }),
  bounds: () => ({ left: 0, top: 0, right: 320, bottom: 200 }),
  step: () => 4,
})
```

盒子槽位用 `boxStyle` 读取已提交的尺寸。把 `draft` 列进原语的 `live` 字段，然后通过实时绑定应用 `resize.style`：

```ts
box: slot({
  attrs: (state, { api }) => ({ style: api.resize.boxStyle(state, "frame") }),
  live: live((state, { api }) => ({ style: api.resize.style(state, "frame") })),
  on: {
    pointerdown: (event, { el, api }) => api.resize.pointerdown(event, "frame", "move", el!),
  },
}),
```

把盒子绝对定位，并给每个手柄自己的 ID。手柄使用罗盘方向名：`n`、`e`、`s`、`w`、`ne`、`nw`、`se` 和 `sw`。盒子本身使用 `move`。

把手柄渲染成按钮，名称例如 “Resize bottom right”。它们的槽位调用 `resize.handleAttrs(handle)` 来处理光标和触摸行为，调用 `resize.pointerdown(event, key, handle, el)` 来处理拖动，调用 `resize.keydown(event, key, handle)` 来处理方向键。

拖动手柄调整框架大小，或拖动框架移动它。聚焦一个手柄并使用方向键；加上 Shift 可以走更大的步长。Escape 取消拖动。

`handleAttrs` 不提供标签，也不让元素可获得焦点。按钮负责这些。方向键按 `step` 移动一条边，也就是放下的盒子所落到的网格，或者加上 Shift 时按 `largeStep` 移动，默认十步；边上的手柄沿自己的轴响应。变更会报告 `drag` 或 `resize-key`。

### 保持形状
在调整大小的选项里设置宽高比和尺寸限制：

```ts
aspect: () => (context.store.get().locked ? 16 / 9 : undefined),
min: () => ({ width: 96, height: 54 }),
max: () => ({ width: 256, height: 144 }),
largeStep: () => 16,
```

在勾选 16:9 时调整视频框架的大小。取消勾选即可自由调整，或在拖动角时按住 Shift，以保持框架起始时的比例。

`aspect` 是宽度除以高度。角会跟随移动得更远的那条边；边上的手柄围绕中心调整另一个维度。`min` 和 `max` 限制尺寸，而 `bounds` 描述周围的区域。这里 `largeStep` 让 Shift 加方向键移动 16 像素，而普通方向键仍然移动 1。

### 多个盒子，以及用代码操作
同一个行为可以管理多个盒子。为缩放过的桌面加上 `scale`，为用户不应移动或调整大小的盒子加上 `disabled`：

```ts
scale: () => 0.75,
disabled: (key) => key === "notes",
label: (key) => titles[key],
```

在缩放过的桌面上移动或调整 Mail 的大小。Notes 已锁定。Maximize 会设置 Mail 的盒子，Grow 会从右下角把它放大。

有两种用代码更改盒子的方式：

```ts
resize.resizeTo("mail", { x: 0, y: 0, width: 400, height: 240 })
resize.resizeBy("mail", "se", 40, 40)
```

`resizeBy` 会应用手柄的尺寸限制和禁用规则。`resizeTo` 直接写入提供的盒子，所以尺寸和权限由你自己校验。两者都会报告 `imperative`。

要渲染实时尺寸标签，就把草稿排除在 `live` 之外，并读取 `resize.box(state, key)`。它在拖动过程中返回草稿盒子，其他时候返回已提交的盒子。`dragging(state, key)` 返回活动的手柄。`label` 和 `message` 自定义播报，默认由 `resizeMessage` 提供。

## 把行滑到一旁
### 行背后的操作
`createSwipeActions` 一次只保持一行打开。把 `open` 初始化为 `SwipeOpen | null`，把 `swiping` 初始化为 `SwipeDraft | null`，两者都是 `null`：

```ts
const swipe = createSwipeActions(context, {
  key: "open",
  draft: "swiping",
})
```

每一行都有会移动的内容，覆盖在 `start` 或 `end` 一侧的操作之上。用 `bindRow(el, key)` 绑定行，用 `bindActions(el, rowKey, side)` 绑定每个操作组。从槽位返回这些清理函数。

把 `swiping` 放进 `live`。内容槽位把 transform 和实时属性结合起来：

```ts
attrs: () => ({ style: css({ transform: "translateX(var(--swipe-x, 0px))" }) }),
live: live((state, { api, key }) => api.swipe.contentAttrs(state, key)),
```

在行上调用 `swipe.pointerdown(event, key, el)` 和 `swipe.keydown(event, key)`。给它 `position: relative`、`overflow: hidden` 和 `touch-action: pan-y`。操作槽位用 `swipe.actionsAttrs(state, rowKey, side)` 让隐藏的操作变为不可交互（inert）。

向左滑动一条消息，露出 Delete。使用键盘时，聚焦一行并按 Left 到达它的操作，然后按 Escape 关闭。

滑动会等待横向移动，这样纵向滚动仍然可用。超过操作宽度的一半再松开，就会保持打开；快速轻扫可以更早地打开或关闭。操作的宽度来自你的布局。

`start` 操作会在朝行的末端滑动时出现，在英文中是从左到右。在从右到左的布局中方向相反。`contentAttrs` 提供 `--swipe-x`、`data-swipe` 和 `data-swiping`，因此你可以在指针移动时禁用过渡。

### 完整滑动
加上 `onFullSwipe`，在用户滑动得足够远时执行操作。回调会完成实际工作；辅助函数不会替你点击操作按钮：

```ts
onFullSwipe: (key, side) => {
  context.options().onAction?.(key, side)
  return true
},
fullSwipe: () => 0.5,
disabled: (key) => key === context.options().pinned,
```

返回操作是否被接受。`fullSwipe` 是行宽的一个比例，默认 0.6。当松开就会触发操作时，内容会获得 `data-full`。

向右滑动把消息标为已读，向左滑动将其归档。超过行宽的一半后，松开即可执行操作。固定的消息保持关闭；Peek 和 Close 用代码控制操作。

`swipe.show(key, "end", { focus: true })` 打开一行并聚焦它的第一个操作。`swipe.close({ focus: true })` 关闭它并把焦点送回行。省略 `focus` 就让焦点留在原处。`swipe.open()` 读取打开的行。

### 拖动关闭
一条 toast 或卡片被拖动得足够远时，会在松开时关闭。给 `scrubTo` 一个位于 0 的吸附点：在阈值之内，它的结果带有那个 `point`，所以 toast 会弹回；超过阈值后，就没有可返回的点。每次移动都询问一次，这样 toast 才能显示松开后会发生什么：

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

const keeps = { min: -width, max: width, step: 1, snap: { points: [0], threshold: width * 0.3 } }

onMove: ({ x }) => {
  moved = x - startX
  setOffset(moved)
  setDismissing(scrubTo(moved, keeps).point === undefined)
},
onEnd: () => (scrubTo(moved, keeps).point === undefined ? dismiss(Math.sign(moved)) : setOffset(0)),
```

给 `data-dismissing` 设置样式，让变更在松开之前就清楚可见，像 iOS 那样：这里 toast 背后的层会变红，并显示 “Release to dismiss”。拖回阈值以内就会恢复。为键盘在每条 toast 里保留一个 Dismiss 按钮。

把 toast 向侧面拖动：超过它宽度的三分之一后，背后的层会变红，松开就会关闭它。拖回来，或按 Escape，可以保留它。

## 按压并按住
### 按住确认
当一次按压持续超过 `delay`（默认 500 毫秒）时，`bindHold` 会运行 `onHold`。给它一个按钮，并保留它的清理函数：

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

const unbind = bindHold(button, {
  delay: 1500,
  onHold: () => deleteProject(),
})
```

它适用于鼠标、手指、触控笔，以及按钮获得焦点时的 Space 或 Enter。按压期间，按钮带有 `data-holding`；按住之后，再带有 `data-held`。`--hold-delay` 保存这个延迟，所以 CSS 可以在完全相同的时间内填充按钮：

```css
[data-holding] .fill {
  transform: scaleX(1);
  transition: transform var(--hold-delay) linear;
}
```

按住 Delete Project，直到它填满。过早松开则不会删除任何内容。使用键盘时，聚焦它并按住 Space。

按压结束后，`onEnd` 会带着 `held` 和 `canceled` 运行。在按住之前指针移动超过 `tolerance` 像素（默认 10）、手指开始滚动、按下 Escape，或焦点离开按钮或窗口时，按压会被取消。被取消的按压不会点击，Escape 也不会到达按钮周围的对话框或浮层。在按钮的标签或描述里说明它需要按住，就像这里的提示那样。

### 按住时重复
`bindRepeat` 在按钮被按下时步进一次，然后在按住期间持续步进，像步进器的按钮、滚动箭头或遥控器的音量键那样。告诉它一步要做什么：

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

const unbind = bindRepeat(button, {
  repeat: 80,
  onStep: () => zoomBy(5),
})
```

在 `delay` 之后（默认 400 毫秒），`onStep` 每隔 `repeat` 毫秒再次运行（默认 60），直到按压结束。从 `onStep` 返回 `false` 可以停止重复，比如到达边界时。`count` 表示这是本次按压的第几步，从 1 开始；按压结束后，`onEnd` 会收到总数。

你不需要点击处理函数。鼠标或按键在按下时就步进，随后的点击不会再次步进。手指或触控笔在抬起时步进，或者在按住之后步进，所以在按钮上开始的滚动永远不会步进。没有按压支撑的点击，比如屏幕阅读器发出的点击，会步进一次。

按一次 Zoom In 增加 5%。按住它，缩放会持续上升，直到你松开或达到 400%。按住时滑出按钮，它会等到你回来。

按住期间，只要指针离开按钮，重复就会等待，指针回来时再继续，就像滚动条的箭头那样。短按 Space 或 Enter 会步进一次；按住时，它们会以和指针相同的节奏重复，无论键盘重复按键有多快。传入 `keys: []` 把按键交给浏览器，它发送的每次点击都会步进一次。松开、浏览器取消、Escape，或焦点离开按钮或窗口，都会结束这次按压。

`bindRepeat` 构建在 `bindHold` 之上，所以按钮在按压期间带有 `data-holding`，开始重复后带有 `data-held`。`bindHold` 也接受 `repeat`，用于那种要等到按压被按住之后才第一次运行的操作。

按压开始时，`onPress` 会运行，`input` 会被设为 `mouse`、`touch`、`pen` 或 `keyboard`。返回 `false` 可以忽略它，比如一个只在触摸时长按打开的上下文菜单区域，因为鼠标有右键。传入 `disabled` 可以拒绝按压，并阻止按住重复。当手指或触控笔按压时，浏览器自带的长按菜单和 iOS 的 callout 会保持关闭。清理函数会停止正在进行的按压，但不调用 `onEnd`。

## 二维值
### 面板上的一个点
`createArea` 把一个表面映射为两个值。从状态里读取这个点，并把每次变更写回去：

```ts
const area = createArea(context, {
  value: (state) => ({ x: state.mix.warmth, y: state.mix.energy }),
  set: (value, details) =>
    change("mix", { warmth: value.x, energy: value.y }, details),
  x: () => ({ min: 0, max: 100, step: 1, label: "Warmth" }),
  y: () => ({ min: 0, max: 100, step: 1, label: "Energy" }),
})
```

表面负责开始指针跟踪；滑块手柄提供键盘控制和滑块属性：

```ts
pad: slot({
  on: { pointerdown: (event, { el, api }) => api.area.pointerdown(event, el!) },
}),
thumb: slot({
  attrs: (state, { api }) => ({ ...api.area.thumbAttrs(state), style: api.area.thumbStyle(state) }),
  on: { keydown: (event, { api }) => api.area.keydown(event) },
}),
```

让表面定位，并设置 `touch-action: none`。滑块手柄的位置相对于它。和移动草稿不同，这个选择器在整个手势期间都会调用 `set`，使用滑块报告的原因：按压时是 `track-press`，移动时是 `drag`，按键时是 `step-key`。

在面板上点击或拖动来改变 warmth 和 energy。聚焦滑块手柄并使用方向键，加 Shift 可以走更大的步长。

每个轴默认是 0–1，步长为 0.01。y 轴从下到上，x 跟随文字方向。方向键移动一步，Shift 移动十步，Page Up 和 Page Down 把 y 移动其范围的十分之一，Home 和 End 则到达 x 的两端。

`thumbAttrs` 暴露一个滑块，其值文本描述两个轴。`thumbStyle` 设置它的百分比位置以及 `--area-x` 和 `--area-y` 的比例。

### 播报什么，以及何时落定
用 `valueText` 提供可读的描述，用 `onCommit` 做应当等到手势结束才执行的工作：

```ts
valueText: ({ x, y }) => `${x}% saturation, ${y}% brightness`,
onCommit: (value) => context.options().onColorCommit?.(value),
disabled: () => context.store.get().locked,
```

拖动来调整饱和度和亮度。色块会立即跟随；保存的值会在松开后或键盘变更后记录到控制台。Lock 会禁用选择器，Reset 用代码设置它的值。

`onCommit` 在指针跟踪结束时或键盘变更成功后运行。`disabled` 阻止用户输入，并把滑块手柄从 Tab 顺序中移除。

`area.set({ x: 60, y: 80 })` 会以 `imperative` 原因吸附并钳制一个程序化值。它在禁用时仍然有效，也不会调用 `onCommit`。`area.fractions(state)` 给出每个值在其轴内的位置，对渐变或色块很有用。

## 轨道上的范围
### 时间轴上的预订
一个 `Span` 有 `id`、`track`，以及以你的单位表示的 `start` 和 `end` 值。对于会议室预订，这些单位可以是从午夜起算的分钟数。把预订保存在数组里，并另起一个 `SpansDraft<Booking> | null` 字段，初始值为 `null`：

```ts
type Booking = Span & { title: string }
type RoomsState = { bookings: Booking[]; draft: SpansDraft<Booking> | null }

let booked = 0
const spans = createSpans<RoomsState, Booking>(context, {
  key: "bookings",
  draft: "draft",
  set: (value, details) => change("bookings", value, details),
  axis: "x",
  min: () => 9 * 60,
  max: () => 17 * 60,
  step: () => 30,
  overlap: false,
  create: (span) => ({ ...span, id: `booking-${++booked}`, title: "New" }),
})
```

渲染 `spans.spans(state)`，这样拖动期间会显示草稿。放下时会把数组提交一次，详情里带有受影响的 `id` 和 `action`。

用 `bindTrack(el, key)` 绑定轨道，用 `bindSpan(el, id)` 绑定预订。轨道上的按压调用 `trackPointerdown(event, key)`；预订上的按压调用 `spanPointerdown(event, id)`。给预订内部的调整大小手柄加上 `data-span-edge="start"` 或 `"end"`。

在空白处拖动来预订房间。拖动预订可以移动它，拖动它的末端可以调整大小。值会吸附到半小时，预订之间不能重叠。

用 `spanStyle(booking)` 定位预订，用 `spanAttrs(state, id)` 获取 `data-span-id` 以及绘制、拖动或调整大小的状态。能否获得焦点以及无障碍名称由你自己提供；`label(booking)` 提供播报中使用的名称。给轨道和可拖动的范围设置 `touch-action: none`。

在预订上调用 `keydown(event, id)`。沿轨道的方向键移动它；配合 Shift 时移动它的末端。跨轨道的方向键更换房间，Delete 或 Backspace 删除它。每次按键都会立即提交。

`create` 提供新预订的 ID 和数据。没有它时，空轨道不会创建预订。`overlap: false` 会拒绝与同一轨道上另一个范围重叠的提议范围。

### 向下排列的日期
轨道默认纵向排列。为日历加上持续时间限制、点击持续时间和显式的轨道顺序：

```ts
minSize: () => 60,
maxSize: () => 180,
size: () => 60,
tracks: () => ["Mon", "Tue", "Wed"],
createAutoScroll: { edge: 32 },
```

在一天里向下拖动来绘制事件，或点击创建一小时的事件。在一到三小时之间移动或调整事件大小。拖动时把指针保持在顶部或底部附近即可滚动。

`minSize` 和 `maxSize` 限制持续时间；`size` 决定点击创建什么。`tracks` 控制键盘跨日期行进的顺序。自动滚动接受[与其他拖动相同的选项](https://ui-sdk.cn/docs/core/behaviors/drag-and-drop#scrolling-while-dragging)。

允许重叠时，给每个事件一条泳道，让它们共享一天的宽度：

```ts
const lanes = stackSpans(plans.filter((other) => other.track === plan.track))
const style = spans.spanStyle(plan, lanes.get(plan.id))
```

`spansOn(state, track)` 读取一条轨道上的范围。`valueAt(track, x, y)` 把指针位置转换成该轨道范围内的值，并吸附到 `step`。`action(state, id)` 报告当前活动的指针动作。

### 用代码更改范围
对于表单字段或操作按钮，使用 `update` 和 `remove`：

```ts
spans.update("standup", { start: booking.start + 30, end: booking.end + 30 })
spans.update("standup", { end: booking.end + 30 })
spans.remove("standup")
```

`update` 会应用范围和重叠规则。这些方法会报告 `imperative`；它们不检查 `disabled`，后者只适用于指针和键盘输入。`isSpanFree(bookings, proposed)` 检查提议的预订是否与它所在轨道上的另一个预订重叠。

用按钮移动、延长或删除 Standup。Book noon 会检查房间是否空闲。board meeting 已锁定，拖动期间离开窗口会取消拖动。

`spans.cancel()` 结束拖动但不提交。如果你在窗口失去焦点时取消，就在原语销毁时移除那个监听器：

```ts
window.addEventListener("blur", spans.cancel)
context.onDestroy(() => window.removeEventListener("blur", spans.cancel))
```

`message` 自定义播报，默认由 `spansMessage` 提供。当你的轨道不需要边缘滚动时，设置 `createAutoScroll: false`。

## 预览
### 按压并滑动
预览让人在选定之前先试用一个值。把一个 `string | null` 的预览放在已选值旁边，并渲染 `preview ?? value`：

```ts
const preview = createPreview(context, {
  key: "preview",
  slot: "font",
  value: () => context.store.get().value,
  choose: (key, details) => change("value", key, details),
})
```

预览直接写入它的状态字段。如果它还需要经过变更回调，就加上 `set`。

保留字体槽位已有的游走焦点绑定，并加上这些处理函数：

```ts
on: {
  pointerdown: (event, { el, api, key }) => api.preview.pointerdown(event, key, el!.parentElement!),
  keydown: (event, { el, api, key }) => {
    if (api.preview.keydown(event, key)) {
      return
    }
    const moved = api.roving.keydown(event, el!)
    if (moved) {
      api.preview.show(moved.key, { reason: "list-navigation", event })
    }
  },
},
```

按下一个字体，然后滑过其他字体来预览它们。在其中一个上松开即可选定，在外面松开则取消。使用键盘时，方向键预览，Enter 或 Space 选定。

把共同的父级表面传给 `pointerdown`，以便跨选项跟踪手势。`show(key)` 预览一个，`choose(key)` 提交它，`clear()` 回到已选的值。Enter 和 Space 选定；Escape 清除。

当焦点离开这一组时清除预览，除非指针仍在滑动：

```ts
focusout: (event, { el, api }) => {
  if (api.preview.dragging() || el?.contains(event.relatedTarget as Node)) {
    return
  }
  api.preview.clear({ reason: "focus-out", event })
},
```

按压并滑动也可以试用一种顺序。在按压开始时测量每个标签页，并把被拖动标签页可能占据的每个槽位的左边缘设为一个吸附点。`scrubTo` 会在标签页靠近时把它拉进槽位，最近的槽位显示它将形成的顺序，松开就保留它：

```ts
const slots = lefts([...others, key], widths)

onMove: ({ x, event }) => {
  const { value: left } = scrubTo(start + x, { min: 0, max: slots.at(-1), step: 1, snap: { points: slots, threshold: 12 } }, {
    snap: !event.metaKey,
  })
  preview(insert(others, key, nearest(slots, left)), left)
},
```

对于键盘，`stepFromKey` 和 `stepBy` 把聚焦的标签页移动一个位置，或用 Home 和 End 移到任意一端，方式和滑块步进一样。

沿标签栏拖动一个标签页：它靠近每个位置时会吸附进去，其他标签页会让出空间。按住 Command 或 Ctrl 可以自由放置，或按 Escape 取消。使用键盘时，Option 或 Alt 加方向键移动聚焦的标签页。

### 悬停，以及播报什么
对于悬停预览，在进入和离开时使用 `show` 和 `clear`，然后在点击时使用 `choose`：

```ts
pointerenter: (event, { api, key }) => api.preview.show(key, { reason: "item-hover", event }),
pointerleave: (event, { api }) => api.preview.clear({ reason: "item-hover", event }),
click: (event, { api, key }) => api.preview.choose(key, { event }),
```

悬停在一个主题上试用它，点击即可保留。方向键也可以预览；Enter 选定，Escape 清除。Contrast 已禁用。

`disabled(key)` 阻止预览或选定选项。`label(key)` 和 `message(label)` 为原因为 `list-navigation` 的预览自定义播报；指针预览不会在每次经过选项时都播报。

## API 参考
选项表格会链接回介绍它们的示例。

- `key` (`keyof State & string`): The state key that holds every item's position, a `Record<string, MovePoint>`. Example: [Place Anything](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#place-anything).
- `draft` (`keyof State & string`): The state key that holds the `MoveDraft` while a pointer drags. Start it at `null`, and list it in `live`. Example: [Place Anything](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#place-anything).
- `set` (`(value: Record<string, MovePoint>, details: { reason: MoveReason; event?: Event; }) => boolean`): Asks for new positions and returns whether they went through. Pass the primitive's change: `set: (value, details) => change("positions", value, details)`. Example: [Place Anything](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#place-anything).
- `group?` (`((key: string) => string[])`): The keys that move together with the one pressed, like the selection when it holds it. Defaults to that one key. Example: [Several at Once](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-at-once).
- `step?` (`(() => number)`): The grid a dropped position lands on, and how far Alt with an arrow nudges. Defaults to 1. Example: [Several at Once](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-at-once).
- `largeStep?` (`(() => number)`): How far Alt with Shift and an arrow nudges. Defaults to ten steps. Example: [Place Anything](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#place-anything).
- `scale?` (`(() => number)`): How many screen pixels one unit of position takes, like a canvas's zoom, so a drag follows the pointer when the items are scaled. Defaults to 1. Example: [Zoom and Moving from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#zoom-and-moving-from-code).
- `bounds?` (`((key: string) => { minX: number; minY: number; maxX: number; maxY: number; } | undefined)`): Keeps positions inside a box, like the board's size minus a card's. Example: [Place Anything](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#place-anything).
- `disabled?` (`((key: string) => boolean)`): Whether an item can't be moved. Example: [Several at Once](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-at-once).
- `label?` (`((key: string) => string | undefined)`): The name screen readers hear for an item. Example: [Several at Once](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-at-once).
- `message?` (`((message: MoveMessage) => string)`): The text screen readers hear after a move. Defaults to English. Example: [Several at Once](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-at-once).

- `boxes` (`(state: State) => Record<string, ResizeBox>`): Every box in state, by key, like `(state) => state.boxes` or `(state) => ({ crop: state.crop })`. Example: [Handles on a Box](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#handles-on-a-box).
- `draft` (`keyof State & string`): The state key that holds the `ResizeDraft` while a pointer drags. Start it at `null`, and list it in `live`. Example: [Handles on a Box](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#handles-on-a-box).
- `set` (`(value: Record<string, ResizeBox>, details: { reason: ResizeReason; event?: Event; }) => boolean`): Asks for new boxes and returns whether they went through. Pass the primitive's change: `set: (value, details) => change("boxes", value, details)`, or `change("crop", value.crop, details)` for a primitive with one box. Example: [Handles on a Box](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#handles-on-a-box).
- `min?` (`((key: string) => { width: number; height: number; })`): The smallest a box gets. Defaults to 1 by 1. Example: [Handles on a Box](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#handles-on-a-box).
- `max?` (`((key: string) => { width: number; height: number; } | undefined)`): The largest a box gets. Example: [Keeping a Shape](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#keeping-a-shape).
- `bounds?` (`((key: string) => { left: number; top: number; right: number; bottom: number; } | undefined)`): Keeps a box inside an area, like the image a crop sits on. Example: [Handles on a Box](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#handles-on-a-box).
- `aspect?` (`((key: string) => number | undefined)`): Keeps width over height at this ratio, like `16 / 9`. Without one, Shift on a corner keeps the ratio the box had when the drag started. Example: [Keeping a Shape](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#keeping-a-shape).
- `step?` (`(() => number)`): The grid a dropped box lands on, and how far an arrow moves an edge. Defaults to 1. Example: [Handles on a Box](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#handles-on-a-box).
- `largeStep?` (`(() => number)`): How far Shift with an arrow moves an edge. Defaults to ten steps. Example: [Keeping a Shape](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#keeping-a-shape).
- `scale?` (`(() => number)`): How many screen pixels one unit takes, like a canvas's zoom. Defaults to 1. Example: [Several Boxes, and from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-boxes-and-from-code).
- `disabled?` (`((key: string) => boolean)`): Whether a box can't be changed. Example: [Several Boxes, and from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-boxes-and-from-code).
- `label?` (`((key: string) => string | undefined)`): The name screen readers hear for a box. Example: [Several Boxes, and from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-boxes-and-from-code).
- `message?` (`((message: ResizeMessage) => string)`): The text screen readers hear after a change. Defaults to English. Example: [Several Boxes, and from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#several-boxes-and-from-code).

- `key` (`keyof State & string`): The state key that holds the `SwipeOpen` row, or `null`. Example: [Actions Behind a Row](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#actions-behind-a-row).
- `draft` (`keyof State & string`): The state key that holds the `SwipeDraft` while a pointer swipes. Start it at `null`, and list it in `live`. Example: [Actions Behind a Row](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#actions-behind-a-row).
- `onFullSwipe?` (`((key: string, side: SwipeSide, details: { event: Event; }) => boolean)`): Runs a side's first action when a swipe goes past `fullSwipe` of the row's width, and returns whether it did. Without it, a long swipe only opens the row. Example: [Full Swipes](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#full-swipes).
- `fullSwipe?` (`(() => number)`): How far across the row a swipe runs the first action, as a fraction of its width. Example: [Full Swipes](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#full-swipes).
- `disabled?` (`((key: string) => boolean)`): Whether a row can't be swiped. Example: [Full Swipes](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#full-swipes).

- `delay?` (`number`): How long a press lasts before it's held, in milliseconds. Example: [Hold to Confirm](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#hold-to-confirm).
- `repeat?` (`number`): Runs `onHold` again this often while held, in milliseconds, like a stepper's button. Without it, `onHold` runs once. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `tolerance?` (`number`): How far a pointer can move before it's held, in pixels, before the press is canceled, like when a finger starts to scroll. Example: [Hold to Confirm](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#hold-to-confirm).
- `keys?` (`string[]`): The keys that press it while it has focus. A short press clicks it, as a pointer's does. An empty list leaves keys alone. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `disabled?` (`(() => boolean)`): Whether a press can't start, and a held one stops repeating. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `onPress?` (`((details: HoldDetails) => boolean | void)`): Runs as a press starts. Return `false` to ignore it, like a mouse press where only touch holds. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `onHold?` (`((details: HoldDetails & { count: number; }) => boolean | void)`): Runs once the press is held, then every `repeat` milliseconds. Return `false` to stop repeating, like at a bound; the press goes on until it ends. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `onEnd?` (`((details: HoldEndDetails) => void)`): Runs once when the press ends, released or canceled. Example: [Hold to Confirm](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#hold-to-confirm).

- `delay?` (`number`): How long a press lasts before it starts repeating, in milliseconds. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `repeat?` (`number`): How often it steps again while held, in milliseconds. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `keys?` (`string[]`): The keys that press it while it has focus. An empty list leaves keys to the browser, which clicks a button once per key press. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `disabled?` (`(() => boolean)`): Whether a press can't start, and a held one stops repeating. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `onPress?` (`((details: HoldDetails) => boolean | void)`): Runs as a press starts. Return `false` to ignore it. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `onStep?` (`((details: RepeatDetails) => boolean | void)`): Runs once for each press, then every `repeat` milliseconds while it's held. Return `false` to stop repeating, like at a bound. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).
- `onEnd?` (`((details: RepeatEndDetails) => void)`): Runs once when a press ends, released or canceled, and after a click's step. Example: [Repeat While Held](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#repeat-while-held).

- `value` (`(state: State) => AreaValue`): Reads the point from state, like `(state) => state.value`, or one made from other state, like a color's saturation and brightness. Example: [A Point on a Pad](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#a-point-on-a-pad).
- `set` (`(value: AreaValue, details: { reason: AreaReason; event?: Event; }) => boolean`): Asks for a new value and returns whether it went through. Pass the primitive's change: `set: (value, details) => change("value", value, details)`. Example: [A Point on a Pad](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#a-point-on-a-pad).
- `x?` (`(() => AreaAxis)`): The x axis, left to right. Example: [A Point on a Pad](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#a-point-on-a-pad).
- `y?` (`(() => AreaAxis)`): The y axis, bottom to top, like a chart: a higher value sits higher. Example: [A Point on a Pad](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#a-point-on-a-pad).
- `valueText?` (`((value: AreaValue) => string)`): The text screen readers hear for the value. Defaults to each axis's label and percentage. Example: [What's Read Out, and When It Settles](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#whats-read-out-and-when-it-settles).
- `onCommit?` (`((value: AreaValue, details: { reason: AreaReason; event?: Event; }) => void)`): Runs when a press or a drag ends, or a key changed the value, with the value it ended on, like Slider's `onValueCommit`. Example: [What's Read Out, and When It Settles](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#whats-read-out-and-when-it-settles).
- `disabled?` (`(() => boolean)`): Whether it can't change. Example: [What's Read Out, and When It Settles](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#whats-read-out-and-when-it-settles).

- `key` (`keyof State & string`): The state key that holds the spans, an array of `Span`. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `draft` (`keyof State & string`): The state key that holds the `SpansDraft` while a pointer drags. Start it at `null`. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `set` (`(value: S[], details: { reason: SpansReason; event?: Event; id: string; action: SpanAction; }) => boolean`): Asks for the new spans and returns whether they went through. Pass the primitive's change: `set: (value, details) => change("spans", value, details)`. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `min?` (`(() => number)`): Where tracks start, in their units. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `max?` (`(() => number)`): Where tracks end. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `step?` (`(() => number)`): What every start and end snaps to, and what arrows move by. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `minSize?` (`(() => number)`): The shortest a span can be. Example: [Days That Run Down](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#days-that-run-down).
- `maxSize?` (`(() => number)`): The longest a span can be. Example: [Days That Run Down](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#days-that-run-down).
- `axis?` (`"x" | "y"`): The direction tracks run: `y` down, like a calendar's days, or `x` across, like a Gantt chart's rows. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `tracks?` (`(() => string[])`): The tracks in the order the arrows across them go through. Defaults to the bound tracks in document order. Example: [Days That Run Down](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#days-that-run-down).
- `overlap?` (`boolean`): Whether spans on one track can overlap. When they can't, a drag stops where it would. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `create?` (`((span: Span) => S | null)`): Makes the span a press on a track starts to draw, with a new `id` and anything else it needs, like a title, or returns `null` to not draw. Runs on every press on an empty stretch of track. Without it, nothing is drawn. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `size?` (`(() => number | undefined)`): How long a span a click on an empty track makes. Without it, a click makes none. Example: [Days That Run Down](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#days-that-run-down).
- `disabled?` (`((span: S) => boolean)`): Whether a span can't change. Example: [Changing Ranges from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#changing-ranges-from-code).
- `label?` (`((span: S) => string)`): What screen readers call a span, like `"Standup, Tuesday 9:00 to 9:30"`. Defaults to its track and range. Example: [Bookings on a Timeline](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#bookings-on-a-timeline).
- `message?` (`((message: SpansMessage<S>) => string)`): The text screen readers hear for each change. Defaults to English. Example: [Changing Ranges from Code](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#changing-ranges-from-code).
- `createAutoScroll?` (`false | Omit<AutoScrollOptions, "ignore" | "onScroll">`): How a drag near a scroller's edge scrolls it, or `false` not to. Example: [Days That Run Down](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#days-that-run-down).

- `key` (`keyof State & string`): The state key that holds the previewed option's key, or `null`. Example: [Press and Slide](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#press-and-slide).
- `slot` (`string`): The slot whose parts are the options. Example: [Press and Slide](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#press-and-slide).
- `set?` (`((value: string | null, details: { reason: PreviewReason; event?: Event; }) => boolean)`): Asks for a new preview. Defaults to setting the state key; pass the primitive's change to fire `onPreviewChange`. Example: [Press and Slide](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#press-and-slide).
- `choose` (`(key: string, details: { reason: "item-press"; event?: Event; }) => boolean`): Chooses an option. Pass the primitive's change for its value. Example: [Press and Slide](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#press-and-slide).
- `value?` (`(() => string | null)`): The chosen option now, which previews as nothing. Example: [Press and Slide](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#press-and-slide).
- `disabled?` (`((key: string) => boolean)`): Whether an option can't be previewed or chosen. Example: [Hovering, and What's Read Out](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#hovering-and-whats-read-out).
- `label?` (`((key: string) => string)`): An option's name for screen readers. Defaults to its key. Example: [Hovering, and What's Read Out](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#hovering-and-whats-read-out).
- `message?` (`((label: string) => string)`): What screen readers hear when the keyboard previews one. Defaults to English. Example: [Hovering, and What's Read Out](https://ui-sdk.cn/docs/core/behaviors/direct-manipulation#hovering-and-whats-read-out).
