Drawer.vue 3.47 KB
<script setup>
/**
 * Drawer —— 右侧抽屉(v-model:open)。用于详情类内容(如调用日志的一条记录)。
 * ESC 关闭、backdrop 关闭、焦点陷阱 —— 焦点陷阱与 `Modal.vue` 共用 `use-focus-trap.js`
 * 同一份实现;本组件只多两件自己的事:从右侧滑出、宽度可调。
 */

import { ref } from 'vue'
import { useFocusTrap } from '../composables/use-focus-trap.js'

const props = defineProps({
  /** 受控开关。 */
  open: { type: Boolean, default: false },
  /** 标题文案(不传则不渲染标题栏文字,关闭按钮仍在)。 */
  title: { type: String, default: '' },
  /** 抽屉宽度(任意 CSS 长度)。 */
  width: { type: String, default: '480px' },
})

const emit = defineEmits(['update:open', 'close'])

/** @type {import('vue').Ref<HTMLElement|null>} 抽屉根元素,用于焦点陷阱。 */
const panelEl = ref(null)

/**
 * 关闭:同步 v-model 并发 close 事件。
 * @returns {void}
 */
function close() {
  emit('update:open', false)
  emit('close')
}

// ESC 关闭 + Tab 焦点循环 + 打开聚焦 / 关闭还焦点,与 Modal 共用同一份实现。
useFocusTrap(panelEl, () => props.open, close)
</script>

<template>
  <Teleport to="body">
    <div
      v-if="open"
      class="drama-drawer__backdrop"
      @click.self="close"
    >
      <aside
        ref="panelEl"
        class="drama-drawer__panel"
        :style="{ width }"
        role="dialog"
        aria-modal="true"
        tabindex="-1"
      >
        <header class="drama-drawer__header">
          <h2 class="drama-drawer__title">
            {{ title }}
          </h2>
          <button
            class="drama-drawer__close"
            type="button"
            aria-label="Close"
            @click="close"
          >
            ×
          </button>
        </header>
        <div class="drama-drawer__body">
          <slot />
        </div>
        <footer
          v-if="$slots.footer"
          class="drama-drawer__footer"
        >
          <slot name="footer" />
        </footer>
      </aside>
    </div>
  </Teleport>
</template>

<style scoped>
.drama-drawer__backdrop {
  position: fixed;
  inset: 0;
  display: flex;
  justify-content: flex-end;
  background-color: var(--drama-color-overlay);
  z-index: 1000;
}

.drama-drawer__panel {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  height: 100%;
  background-color: var(--drama-color-surface);
  color: var(--drama-color-text);
  border-left: 1px solid var(--drama-color-border);
  box-shadow: var(--drama-shadow-lg);
  outline: none;
}

.drama-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--drama-space-md);
  padding: var(--drama-space-lg) var(--drama-space-xl);
  border-bottom: 1px solid var(--drama-color-border);
}

.drama-drawer__title {
  margin: 0;
  font-family: var(--drama-font-family-heading);
  font-size: var(--drama-font-size-lg);
  font-weight: var(--drama-font-weight-bold);
}

.drama-drawer__close {
  background: transparent;
  border: none;
  color: var(--drama-color-text-muted);
  font-size: var(--drama-font-size-xl);
  line-height: 1;
  cursor: pointer;
  padding: 0 var(--drama-space-sm);
}

.drama-drawer__body {
  flex: 1;
  overflow: auto;
  padding: var(--drama-space-xl);
}

.drama-drawer__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--drama-space-md);
  padding: var(--drama-space-lg) var(--drama-space-xl);
  border-top: 1px solid var(--drama-color-border);
}
</style>