<a id="ui-components"></a>

# UI 组件

本文档是 Wagtail 用户界面（UI）组件的参考，这些组件用于构建 CMS 内的各项功能。这些组件的演示可通过 [Styleguide 应用](../../contributing/ui_guidelines.html.md#styleguide)和[模式库](../../contributing/ui_guidelines.html.md#pattern-library)查看。

关于由 JavaScript 驱动的组件和 Stimulus 控制器，请按控制器或类名搜索，并按照 [JavaScript 组件](client.html.md#javascript-components) 中的 JavaScript 参考阅读。对 `ClipboardController`、`DialogController`、`DropdownController`、`PreviewController`、`RulesController` 和 `TooltipController` 这类组件尤其有用。

#### NOTE
我们为这些组件提供文档，但不把它们纳入弃用政策所约定的同等稳定性保证。也就是说，某个组件的 API 可能在次要版本里直接变化，而不先经历弃用流程。如果你在自己的代码里用到了这些组件，请务必准备好测试流程，以便在每次升级 Wagtail 时发现破坏性变更。

## Fragment

### wagtail.admin.templatetags.wagtailadmin_tags.fragment(parser, token)

Store a template fragment as a variable.

Usage:

```html+django
{% fragment as header_title %}
    {% blocktrans trimmed %}Welcome to the {{ site_name }} Wagtail CMS{% endblocktrans %}
{% endfragment %}
{% include "my/custom/header.html" with title=header_title %}
```

Adopted from [slippers' fragment template tag](https://mitchel.me/slippers/docs/template-tags-filters/#fragment) with a few tweaks.

To strip leading and trailing whitespace produced in the fragment, use the
`stripped` option. This is useful if you need to check if the resulting
fragment is empty (after leading and trailing spaces are removed):

```html+django
{% fragment stripped as recipient %}
    {{ title }} {{ first_name }} {{ last_name }}
{% endfragment %}
{% if recipient %}
    Recipient: {{ recipient }}
{% endif %}
```

Note that the stripped option only strips leading and trailing spaces, unlike
`{% blocktrans trimmed %}` that also does line-by-line stripping. This is because
the fragment may contain HTML tags that are sensitive to whitespace, such as
`<pre>` and `<code>`.

## Dialog

#### *class* wagtail.admin.templatetags.wagtailadmin_tags.DialogNode

用于在模态对话框中显示信息的对话框。由 [`DialogController`](/reference/ui/client/classes/controllers_DialogController.DialogController.html#)（`w-dialog`）驱动。要创建对话框，可以使用 `{% dialog %}` 和 `{% enddialog %}` 模板标签。

```html+django
{% dialog icon_name="globe" title="Dialog with critical error" id="my-dialog" subtitle="This is a testing subtitle" message_status="critical" message_heading="There was an issue with the thing" message_description="This is a subtext for the message" %}
    <p>This dialog message was generated by passing message_status=critical as well as message_heading and message_description to the dialog template tag</p>
{% enddialog %}
```

`{% dialog %}` 标签的必需参数：

- `id`：对话框的 ID，供 [`{% dialog_toggle %}`](#wagtail.admin.templatetags.wagtailadmin_tags.dialog_toggle) 标签使用。
- `title`：对话框的标题。

`{% dialog %}` 标签的可选参数：

- `dialog_root_selector`：对话框根元素的 CSS 选择器，默认为 `body`。当你希望把对话框渲染到 DOM 的特定位置（例如某个 `<form>` 元素内）时很有用。
- `icon_name`：对话框头部要显示的图标名称。
- `subtitle`：对话框的副标题。
- `theme`：对话框的主题，取值为空或 `floating`。
- `classname`：用于对话框样式的 CSS 类。

对话框的内容可以是在 `{% dialog %}` 与 `{% enddialog %}` 标签之间书写的任意 Django 模板代码。

对话框还可以在顶部显示状态消息，例如错误或警告。可以用以下可选参数渲染该消息：

- `message_status`：消息的状态级别，可以是 `info`、`warning`、`critical`、`success` 之一。
- `message_heading`：消息的标题。
- `message_description`：消息的描述。

### 对话框开关

#### wagtail.admin.templatetags.wagtailadmin_tags.dialog_toggle()

一个用于打开指定 ID 对话框的按钮组件。要创建打开对话框的按钮，可以使用 `{% dialog_toggle %}` 模板标签。

```html+django
{% dialog_toggle classname='button' dialog_id='my-dialog' text='Dialog with error' %}
```

`{% dialog_toggle %}` 标签的参数：

- `dialog_id`：要打开的对话框的 ID。
- `text`：按钮上显示的文本。
- `classname`（可选）：用于按钮样式的 CSS 类。

## Dropdown

#### *class* wagtail.admin.templatetags.wagtailadmin_tags.DropdownNode

用于显示操作或选项列表的下拉菜单。由 [`DropdownController`](/reference/ui/client/classes/controllers_DropdownController.DropdownController.html#)（`w-dropdown`）驱动。要创建下拉菜单，可以使用 `{% dropdown %}` 和 `{% enddropdown %}` 模板标签。

```html+django
{% dropdown toggle_icon="dots-horizontal" toggle_aria_label="Actions" %}
    <a href="/admin/pages/2/move/">{% icon name="arrow-right-full" %} Move</a>
    <a href="/admin/pages/2/copy/">{% icon name="copy" %} Copy</a>
    <a href="/admin/pages/2/delete/">{% icon name="bin" %} Delete</a>
    <a href="/admin/pages/2/unpublish/">{% icon name="download" %} Unpublish</a>
{% enddropdown %}
```

下拉菜单的内容只能包含 `a` 和 `button` 元素。

`{% dropdown %}` 标签的参数：

- `toggle_icon`：开关按钮上要显示的图标名称。
- `toggle_label`：开关按钮的可见标签，显示在图标之前。
- `toggle_suffix`：开关按钮的可见内容，显示在图标之后。
- `toggle_aria_label`：开关按钮的 `aria-label`。
- `toggle_describedby`：开关按钮的 `aria-describedby`。
- `toggle_classname`：用于开关按钮样式的 CSS 类。
- `toggle_tooltip_offset`：下拉菜单的偏移值，格式与 [Tooltip](#ui-tooltip) 的偏移值类似。
- `toggle_tippy_offset`：下拉菜单开关的 [Tooltip](#ui-tooltip) 的偏移值。
- `hide_on_click`：点击下拉菜单内容内部时是否隐藏。
- `keep_mounted` 隐藏时下拉菜单是否保持其 DOM 节点挂载。
- `classname`：用于下拉菜单样式的 CSS 类。
- `attrs`：要添加到下拉菜单元素的 HTML 属性。

以上所有参数都是可选的，但必须提供 `toggle_label`，或者同时提供 `toggle_icon` 与 `toggle_aria_label`，以确保下拉菜单开关的可访问性。

### 带下拉菜单的按钮

#### *class* wagtail.admin.templatetags.wagtailadmin_tags.DropdownButtonNode

一个旁边带下拉菜单的按钮。这是[下拉菜单](#wagtail.admin.templatetags.wagtailadmin_tags.DropdownNode)组件的专用版本。要创建带下拉菜单的按钮，需要把已渲染的按钮 HTML 片段与 `{% dropdown_button %}` 和 `{% enddropdown_button %}` 模板标签组合起来。

```html+django
{% load wagtailadmin_tags %}
{% fragment as button %}
    <button type="button" class="button">Main button</button>
{% endfragment %}
{% dropdown_button button=button toggle_icon="arrow-up" %}
    <button type="submit" class="button">First item</button>
    <a class="button" href="#">Second item</a>
{% enddropdown_button %}
```

下拉菜单的内容只能包含 `a` 和 `button` 元素。

`{% dropdown_button %}` 标签的参数：

- `button`：主按钮的 HTML 片段。
- `toggle_icon`：开关按钮上要显示的图标名称。默认为 `arrow-down`。
- `toggle_classname`：用于开关按钮样式的 CSS 类。
- `keep_mounted`：隐藏时下拉菜单是否保持其 DOM 节点挂载。
- `classname`：用于包裹按钮和下拉菜单的元素样式的 CSS 类。

只有 `button` 参数是必需的，其余都是可选的。

<a id="ui-tooltip"></a>

## Tooltip

可附加到 HTML 元素上、在悬停或获得焦点时显示附加信息的工具提示。由 [`TooltipController`](/reference/ui/client/classes/controllers_TooltipController.TooltipController.html#)（`w-tooltip`）驱动。要添加工具提示，请把 `w-tooltip` 控制器附加到元素上，并用 data 属性指定各项属性。

```html
<button
    type="button"
    data-controller="w-tooltip"
    data-w-tooltip-content-value="More detail here"
    data-w-tooltip-offset-value="[10, 15]"
    data-w-tooltip-placement-value="top"
>
  A button with a tooltip
</button>
```

如果希望工具提示显示富内容，可以在 `w-tooltip` 元素内使用带 `data-w-tooltip-target="content"` 属性的 HTML 元素作为内容目标：

```html
<button
    type="button"
    data-controller="w-tooltip"
    data-w-tooltip-offset-value="[10, 15]"
    data-w-tooltip-placement-value="top"
>
  <template data-w-tooltip-target="content">
    More <strong>detail</strong> here
  </template>
  A button with a tooltip
</button>
```

`w-tooltip` 可用的取值属性：

- `data-w-tooltip-content-value`：工具提示的内容。如果改用内容目标，则该属性可选。
- `data-w-tooltip-offset-value`（可选）：工具提示相对元素的偏移，以两个数字组成的数组表示（`[skidding, distance]`）。默认为 `[0, 10]`。
- `data-w-tooltip-placement-value`（可选）：工具提示相对元素的位置。可选值为 `top`、`top-start`、`top-end`、`right`、`right-start`、`right-end`、`bottom`、`bottom-start`、`bottom-end`、`left`、`left-start`、`left-end`。默认为 `bottom`。
