# 样式与动画

为渲染出的元素设置样式，并添加进入和退出过渡。

部件渲染出来没有样式。组件演示用的是普通 CSS，同样的状态属性和 CSS 变量在 Tailwind CSS 或任何其他样式表里都能用。

## 为部件设置样式
给接收行为的元素设置样式。对 Toggle 来说，就是它外层容器里的按钮。在这个页面里导入 `@ui-sdk/web/toggle`：

```html
<ui-toggle>
  <button
    class="rounded-md border border-neutral-300 px-2.5 py-1.5 text-sm data-pressed:bg-neutral-100 data-disabled:opacity-50"
  >
    Bold
  </button>
</ui-toggle>
```

`data-pressed`、`data-disabled` 这类状态属性要么存在，要么不存在。另一些带有值，比如 `data-side="top"`。每个组件的“样式”一节都会列出它的属性和 CSS 变量。

## CSS Modules
CSS Modules 需要能导入类名映射的打包器。用普通 CSS 时，改用普通的类名，并加载样式表。导入模块，把它的类名加到按钮上：

```html
<ui-toggle id="bold"><button>Bold</button></ui-toggle>
```

```ts
import "@ui-sdk/web/toggle"
import styles from "./toggle.module.css"

const button = document.querySelector("#bold button")!
button.classList.add(styles.toggle)
```

```css title="toggle.module.css"
.toggle {
  cursor: default;
  border: 1px solid var(--input, #e5e5e5);
  border-radius: 1.125rem;
  background: transparent;
  padding: 0.375rem 0.625rem;
  color: inherit;
  font: inherit;
  font-size: 0.875rem;
}

.toggle[data-pressed] {
  background: var(--muted, #f5f5f5);
}

.toggle[data-disabled] {
  opacity: 0.5;
}

.toggle:focus-visible {
  outline: 2px solid var(--ring, #a3a3a3);
  outline-offset: 2px;
}
```

## 为打开和关闭添加动画
导入 `@ui-sdk/web/popover`，然后分别给进入的 `data-starting-style` 和退出的 `data-ending-style` 设置样式：

```html
<ui-popover id="details">
  <ui-popover-trigger><button>Details</button></ui-popover-trigger>
  <ui-popover-content
    class="origin-(--transform-origin) transition-[opacity,scale] duration-150 data-starting-style:scale-96 data-starting-style:opacity-0 data-ending-style:scale-96 data-ending-style:opacity-0 motion-reduce:transition-none"
  >
    <ui-popover-title>Project Details</ui-popover-title>
    <p>Last updated today.</p>
    <ui-popover-close><button>Done</button></ui-popover-close>
  </ui-popover-content>
</ui-popover>
```

浮层会一直显示，直到它的退出过渡或动画结束，然后变为隐藏。把退出样式放在 `data-ending-style` 上；`data-closed` 描述状态，但不会等待退出。

从脚本启动的退出动画也会被等待，前提是它在关闭浮层的同一个任务里启动，比如在 `openchange` 监听器里。稍后从观察器启动的，不会被等待。

浮层提供 `--anchor-width` 和 `--available-height` 用于设置尺寸。用这些和 `data-slot`，不要用生成的 ID。[浮层](/docs/web-components/popups) 介绍放置位置和容器。

## ui-sdk 级联层
有些行为需要一点 CSS，比如让关闭的浮层保持隐藏，或者给每个元素一个默认的 `display`，所以 ui-sdk 会把它加进页面，放在名为 `ui-sdk` 的级联层里。在页面引入的样式表顶部声明这个级联层，这样无论页面把 ui-sdk 的样式放在哪里，你自己的样式都能盖过它。用 Tailwind 时，把它放在 `@import "tailwindcss"` 上面：

```css
@layer ui-sdk;
```

同一个级联层还会从 Popover、Dialog、Drawer、Tooltip 和 Hover Card 的内容上，去掉浏览器自带的 popover 边框、内边距和背景。Menu、Select、Combobox 和 Date Picker 的内容保留这些，所以你要给这些部件设置自己的 `border`、`padding` 和 `background`。`web.css` 已经这么做了。

## 内容安全策略
内容安全策略（Content Security Policy）的 `style-src` 里没有 `'unsafe-inline'` 时，会拦截 ui-sdk 加进页面的 CSS。在页面的 `<head>` 里，紧挨着服务器写入策略的地方，用一个 meta 标签把策略的 nonce 交给 ui-sdk：

```html
<meta property="csp-nonce" nonce="your-nonce" />
```

用 Vite 时，在 Vite 配置里把 `html.cspNonce` 设为一个占位符，Vite 会替你写出这个标签。你的服务器在每次请求时用新的 nonce 替换占位符。Shadow root 不需要做任何事：ui-sdk 在那里采用构造样式表（constructed style sheets），任何策略都不会拦截。

如果你的策略不给样式提供 nonce，就加上不带 `nonce` 属性的标签，ui-sdk 在页面上也会采用构造样式表。如果你的代码替换了 `document.adoptedStyleSheets`，ui-sdk 会在元素更新或被加入时把它的样式表加回去。
