Skip to content

Drawer 抽屉

呼出一个临时的侧边面板,可以从多个方向呼出。

引入

html
<script type="module">
  import "./node_modules/easy-component-ui/dist/components/ea-drawer.js";
</script>
js
import "easy-component-ui/ea-drawer";

自定义样式

移步到 CSS PartCSS Custom Properties

基本用法

可以在 ea-drawer 标签上添加 direction 属性来指定呼出方向。

从左往右开从右往左开从上往下开从下往上开Hi, there!
查看代码
html
<div class="demo">
  <ea-button variant="primary" id="openDrawerBtn--ltr">从左往右开</ea-button>
  <ea-button variant="primary" id="openDrawerBtn--rtl">从右往左开</ea-button>
  <ea-button variant="primary" id="openDrawerBtn--ttb">从上往下开</ea-button>
  <ea-button variant="primary" id="openDrawerBtn--btt">从下往上开</ea-button>

  <ea-drawer id="drawer" heading="I am the title" direction="ltr">
    <span>Hi, there!</span>
  </ea-drawer>
</div>
js
const Drawer = {
  drawer: document.querySelector("#drawer"),
  ltrBtn: document.querySelector("#openDrawerBtn--ltr"),
  rtlBtn: document.querySelector("#openDrawerBtn--rtl"),
  ttbBtn: document.querySelector("#openDrawerBtn--ttb"),
  bttBtn: document.querySelector("#openDrawerBtn--btt"),

  init() {
    this.ltrBtn.addEventListener("click", () => {
      this.drawer.direction = "ltr";
      this.drawer.visible = true;
    });

    this.rtlBtn.addEventListener("click", () => {
      this.drawer.direction = "rtl";
      this.drawer.visible = true;
    });

    this.ttbBtn.addEventListener("click", () => {
      this.drawer.direction = "ttb";
      this.drawer.visible = true;
    });

    this.bttBtn.addEventListener("click", () => {
      this.drawer.direction = "btt";
      this.drawer.visible = true;
    });

    this.drawer.beforeClose = done => {
      $confirm("Are you confirm to close?", "Warning", {
        confirmButtonText: "OK",
        cancelButtonText: "Cancel",
        type: "warning",
      })
        .then(action => {
          done();
        })
        .catch(action => {
          done(true);
        });
    };

    this.drawer.addEventListener("ea-close", () => {
      console.log("close");
    });
  },
};

Drawer.init();

不添加 Title

当你不需要标题的时候,你可以将它移除。通过设置 with-header 属性为 false 来控制是否显示标题。如果你的应用需要具备可访问性,请务必设置好 heading

openHi, there!
查看代码
html
<div class="demo">
  <ea-button variant="primary" id="noHeaderBtn">open</ea-button>
  <ea-drawer
    id="noHeaderDrawer"
    heading="I am the title"
    direction="ltr"
    with-header="false"
  >
    <span>Hi, there!</span>
  </ea-drawer>
</div>
js
const noHeaderExample = {
  drawer: document.querySelector("#noHeaderDrawer"),
  openBtn: document.querySelector("#noHeaderBtn"),

  init() {
    this.openBtn.addEventListener("click", () => {
      this.drawer.visible = true;
    });
  },
};
noHeaderExample.init();

自定义内容

Drawer 可以在其内部嵌套各种丰富的操作。

打开自定义内容的抽屉 Lilyiro Lord of the Wild Lunacrest Continent Thunderous Veins Daredevil She was once an elf lord, defending the border from goblin invaders. She was then a goblin warrior, protecting her clan from being slaughtered by elves. She has the unwavering courage to uphold justice in her heart and she is prepared to betray or be betrayed for the greater good. Despite her inner gentleness, Lilyiro, who has spilled so much blood on battlefields, is more straightforward than men.
CancelConfirm
查看代码
html
<div class="demo">
  <ea-button variant="primary" id="openCustomDrawerBtn"
    >打开自定义内容的抽屉</ea-button
  >
  <ea-drawer id="customDrawer" heading="我是标题" direction="ltr">
    <ea-descriptions title="User Info">
      <ea-descriptions-item label="Username"> Lilyiro </ea-descriptions-item>
      <ea-descriptions-item label="Essence">
        Lord of the Wild
      </ea-descriptions-item>
      <ea-descriptions-item label="Place">
        Lunacrest Continent
      </ea-descriptions-item>
      <ea-descriptions-item label="Traits">
        <ea-tag size="small" type="warning" style="margin-right: 1rem">
          Thunderous Veins
        </ea-tag>
        <ea-tag size="small" type="info">Daredevil</ea-tag>
      </ea-descriptions-item>
      <ea-descriptions-item label="Description">
        She was once an elf lord, defending the border from goblin invaders. She
        was then a goblin warrior, protecting her clan from being slaughtered by
        elves. She has the unwavering courage to uphold justice in her heart and
        she is prepared to betray or be betrayed for the greater good. Despite
        her inner gentleness, Lilyiro, who has spilled so much blood on
        battlefields, is more straightforward than men.
      </ea-descriptions-item>
    </ea-descriptions>
    <footer slot="footer" style="text-align: right">
      <ea-button id="customCancelBtn" plain>Cancel</ea-button>
      <ea-button id="customConfirmBtn" variant="primary">Confirm</ea-button>
    </footer>
  </ea-drawer>
</div>
js
const CustomDrawer = {
  drawer: document.querySelector("#customDrawer"),
  openBtn: document.querySelector("#openCustomDrawerBtn"),
  cancelBtn: document.querySelector("#customCancelBtn"),
  confirmBtn: document.querySelector("#customConfirmBtn"),

  bindBeforeClose(actionType) {
    this.drawer.beforeClose = done => {
      const actionText = actionType === "cancel" ? "cancel" : "confirm";
      $confirm(`Are you sure you want to ${actionText}?`, "Warning", {
        confirmButtonText: "OK",
        cancelButtonText: "Cancel",
        type: "warning",
      })
        .then(action => {
          done();
        })
        .catch(action => {
          done(true);
        });
    };
  },

  init() {
    this.bindBeforeClose("cancel");

    this.openBtn.addEventListener("click", () => {
      this.drawer.visible = true;
    });

    this.cancelBtn.addEventListener("click", () => {
      this.bindBeforeClose("cancel");
      this.drawer.visible = false;
    });

    this.confirmBtn.addEventListener("click", () => {
      this.bindBeforeClose("confirm");
      this.drawer.visible = false;
    });

    this.drawer.addEventListener("ea-closed", () => {
      this.bindBeforeClose("cancel");
    });
  },
};

CustomDrawer.init();

嵌套抽屉

你可以像 Dialog 一样拥有多层嵌套的 Drawer。如果你需要在不同图层中多个抽屉,你必须设置 append-to-body 属性为 true

open
Click me!

_(:зゝ∠)_

查看代码
html
<div class="demo">
  <ea-button variant="primary" id="outerBtn">open</ea-button>
  <ea-drawer id="outerDrawer" heading="I'm outer Drawer" size="50%">
    <div>
      <ea-button id="innerBtn">Click me!</ea-button>
      <ea-drawer id="innerDrawer" heading="I'm inner Drawer" append-to-body>
        <p>_(:зゝ∠)_</p>
      </ea-drawer>
    </div>
  </ea-drawer>
</div>
js
const nestingExample = {
  outerBtn: document.querySelector("#outerBtn"),
  innerBtn: document.querySelector("#innerBtn"),
  outerDrawer: document.querySelector("#outerDrawer"),
  innerDrawer: document.querySelector("#innerDrawer"),

  init() {
    this.outerBtn.addEventListener("click", () => {
      this.outerDrawer.visible = true;
    });

    this.innerBtn.addEventListener("click", () => {
      this.innerDrawer.visible = true;
    });
  },
};
nestingExample.init();

EaDrawer API

EaDrawer Attributes

参数说明类型可选值默认值
heading标题文本string""
visible是否可见(受控属性)booleanfalse
size抽屉尺寸,支持百分比或固定宽度/高度(根据 direction 决定是宽或高)string例如: "30%", "400px"30%
modal是否显示遮罩层booleantrue
direction抽屉方向stringltr / rtl / ttb / bttrtl
close-on-click-modal点击遮罩是否关闭booleantrue
close-on-press-escape按下 Esc 是否关闭booleantrue
show-close是否显示右上角关闭图标booleantrue
with-header是否显示头部booleantrue
append-to-body是否挂载到 body(用于嵌套抽屉确保层级正确)booleanfalse
append-to指定挂载宿主元素的选择器string例如: "#app" 或 "body"body
z-index自定义层级string
background-color遮罩层背景色string
content-width内容宽度string
content-max-width内容最大宽度string
content-height内容高度string

EaDrawer Properties

参数说明类型默认值
beforeClose关闭前的回调函数,接收 done 函数作为参数。done() 确认关闭,done(true) 取消关闭Functionnull

EaDrawer CSS Part

名称说明
container抽屉容器元素(对应模板中 .ea-drawer
header头部元素,包含 heading 和 close-icon
heading标题文本元素,对应 slot[name="title"] 的显示位置
close-icon关闭图标元素
content主体内容元素,对应默认 slot
footer底部元素,对应 slot[name="footer"]

EaDrawer Slots

名称说明
default抽屉主体内容
title自定义标题内容
footer自定义底部内容

EaDrawer Methods

方法名说明参数
show显示抽屉
hide隐藏抽屉

EaDrawer Events

事件名说明回调参数(event.detail)
ea-open抽屉打开时触发
ea-opened抽屉打开动画结束时触发
ea-close抽屉关闭时触发
ea-closed抽屉关闭动画结束时触发

EaDrawer CSS Custom Properties

属性名说明默认值
--ea-drawer-size抽屉尺寸(宽度或高度,取决于方向)30%
--ea-drawer-padding抽屉内边距var(--spacing-lg)
--ea-drawer-heading-color标题颜色var(--grey-900)
--ea-drawer-close-icon-color关闭图标颜色var(--grey-500)
--ea-drawer-content-color内容颜色var(--grey-700)
--ea-drawer-bg-color抽屉背景色var(--white)
--ea-drawer-heading-font-size标题字号var(--font-size-lg)
--ea-drawer-close-icon-size关闭图标尺寸var(--font-size-lg)