跳转到主要内容

DModal API

Discourse 3.1.0.beta6 附带了全新的基于 <DModal> 组件的 API。DModal 是 UI 工具包 的一部分,从 discourse/ui-kit/d-modal 导入。

:information_source: 这取代了旧的基于控制器的 API,该 API 现已弃用。如果您已有使用旧 API 的模态框,请查看此处的迁移指南。

渲染模态框

模态框通过在 handlebars 模板中包含 <DModal> 组件来渲染。如果您还没有合适的模板,请查看 https://meta.discourse.org/t/using-plugin-outlet-connectors-from-a-theme-or-plugin/32727.

一个简单的模态框大致如下:

<DButton
  @translatedLabel="Show Modal"
  @action={{fn (mut this.modalIsVisible) true}}
/>

{{#if this.modalIsVisible}}
  <DModal @title="My Modal" @closeModal={{fn (mut this.modalIsVisible) false}}>
    Hello world, this is some content in a modal
  </DModal>
{{/if}}

:information_source: 这里使用 mut 辅助器 作为一种仅限 hbs 的设置值方式。您也可以使用任何其他标准的 Ember 方法来设置 modalIsVisible。

此示例将创建一个如下所示的简单模态框:

SCR-20230614-mxwd|690x255, 30%

封装为组件

在引入更多复杂度之前,通常最好将您的新模态框封装到其自己的组件定义中。让我们把 <DModal> 的内容移入一个新的 <MyModal /> 组件

// components/my-modal.gjs
<template>
  <DModal @title="My Modal" @closeModal={{@closeModal}}>
    Hello world, this is some content in a modal
  </DModal>
</template>

将这个 .gjs 文件升级为基于类的组件,将允许您引入更复杂的逻辑和状态。

要使用新组件,请更新调用位置以引用它,确保传入一个 @closeModal 参数。

<DButton
  @translatedLabel="Show Modal"
  @action={{fn (mut this.modalIsVisible) true}}
/>

{{#if this.modalIsVisible}}
  <MyModal @closeModal={{fn (mut this.modalIsVisible) false}} />
{{/if}}

添加页脚

许多模态框都有某种行动号召。在 Discourse 中,它们通常位于模态框的底部。为了实现这一点,DModal 提供了多个“命名块”,可以在其中渲染内容。下面是更新后的示例,页脚中包含两个按钮,其中一个是我们的标准 DModalCancel 按钮

<DModal @title="My Modal" @closeModal={{@closeModal}}>
  <:body>
    Hello world, this is some content in a modal
  </:body>
  <:footer>
    <DButton class="btn-primary" @translatedLabel="Submit" />
    <DModalCancel @close={{@closeModal}} />
  </:footer>
</DModal>

SCR-20230614-njze|690x388, 30%

从非 hbs 上下文渲染模态框

理想情况下,<DModal> 实例应当使用上文演示的声明式技术,在 Ember 模板内渲染。如果这在您的使用场景中不可行,可以通过注入 modal 服务并调用 modal.show() 来实现。

确保您已按照上文所述,将模态框封装到其自己的组件中。然后,通过将您的组件类的引用传递给 showModal 来触发模态框:

import MyModal from "discourse/components/my-modal";

// (inject the modal service in the relevant place)

// Add this call whenever you want to open the modal.
// A `@closeModal` argument will be passed to your component automatically.
this.modal.show(MyModal);

// Optionally, pass a '`model`' parameter. Passed as `@model` to your component.
// This can include data, and also actions/callbacks for your Modal to use.
this.modal.show(MyModal, {
  model: { topic: this.topic, someAction: this.someAction },
});

// `modal.show()` returns a promise, so you can wait for it to be closed
// It will resolve with the data passed to the `@closeModal` action
const result = await this.modal.show(MyModal);

更高的可定制性!

<DModal> 拥有多个命名块和参数。

参数

参数 用途
@closeModal 要让忽略 UI 出现,此项为必选。
@title 渲染 <h1 id="discourse-modal-title">;绑定 aria-labelledby。
@subtitle 标题下方的小号文字。
@flash / @flashType 模态框顶部的内联警告(DFlashMessage)。
@hideHeader, @hideFooter 隐藏整个区域。
@headerClass, @bodyClass 页眉/正文包装元素上的额外 class。
@dismissable 设置 @closeModal 时默认为 true。禁用 Esc / 遮罩层点击 / X 按钮。
@autofocus 默认为 true。通过 dTrapTab 自动聚焦第一个可聚焦元素。
@submitOnEnter 默认为 true。按 Enter 会点击 .d-modal__footer .btn-primary,除非焦点位于表单 / 文本区域 / select-kit 中。
@beforeClose async ({ initiatedBy }) => boolean。返回 false 以取消关闭(例如表单有未保存更改时的确认)。
@hidden 暂停键盘处理;在嵌套模态框位于顶层时使用。
@tagName "div"(默认)或 "form"。表单请使用 "form",以便原生提交正常工作。

禁止

禁止 位置 使用时机
default / :body 主要内容区域 默认区域
:aboveHeader 最顶部,页眉前 很少需要;用于必须位于标题栏上方的内容(例如横幅)。
:headerAboveTitle 页眉内部,标题前 存在但未使用。很少需要。
:belowModalTitle .d-modal__title 内部,<h1> 之后 放置补充元信息的绝佳位置。
:headerBelowTitle 页眉内部,标题禁止之后 属于页眉一部分的标签页、子导航或搜索输入框。
:headerPrimaryAction 仅移动端下的页眉右侧 将 X 关闭按钮替换为主要操作(例如“保存”)。同时会在左侧自动渲染一个“取消”按钮,并向页眉添加 .--has-primary-action。
:belowHeader 页眉与正文之间 位于可滚动正文之外、持续显示的子页眉内容(例如搜索栏),因此会固定显示。
:aboveFooter 正文与页脚之间 设置 @hideFooter 时会被抑制。用于与页脚关联但在其外部的内容。同样少见。
:footer 底部操作栏 主要 + 次要按钮。此处第一个 .btn-primary 即按进入键时触发的按钮。
:belowFooter 页脚之后 很少需要;忽略 @hideFooter。适用于带边框的页脚区域外部的状态文本。

参数参考交互式风格指南,命名区块参考 d-modal 模板实现。

CSS

使用 .d-modal 类作为锚点来覆盖核心样式,避免使用旧版 `.modal 选择器。

提供 4 个修饰符:

  • .--large 将最大 宽度设为 800px(仅桌面端)
  • .--max 将最大 宽度设为 90vw(仅桌面端)
  • .has-search 设置固定 高度(80vh):适用于带搜索/筛选系统的模态框,以避免高度随结果数量变化(仅桌面端)
  • .--stacked 将页脚按钮设为堆叠排列(仅移动端)