Skip to content

Modal 模态框

实时预览

引入

html
<link rel="stylesheet" href="/tokens/tokens.css">
<link rel="stylesheet" href="/components/modal/modal.css">

代码

HTML

html
<!-- Modal Component — 模态对话框,带动画和遮罩 -->


<!-- Demo: Modal 组件 (静态展示) -->
<div class="modal-overlay" onclick="this.style.display='none'">
  <div class="modal" onclick="event.stopPropagation()">
    <div class="modal__header">
      <h2 class="modal__title">确认删除</h2>
      <button class="modal__close">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 6 6 18M6 6l12 12"></path></svg>
      </button>
    </div>
    <div class="modal__body">
      <p>此操作不可撤销。确定要删除这个组件吗?</p>
      <p style="margin-top:var(--space-3);">删除后,所有引用该组件的页面将不再显示此组件。</p>
    </div>
    <div class="modal__footer">
      <button class="btn btn--ghost btn--md">取消</button>
      <button class="btn btn--danger btn--md">确认删除</button>
    </div>
  </div>
</div>

CSS

css
/* Modal.css */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  z-index: var(--z-modal);
  opacity: 0;
  animation: modal-fade-in var(--duration-normal) var(--ease-out) forwards;
}

.modal-overlay--hidden {
  animation: modal-fade-out var(--duration-fast) var(--ease-in) forwards;
}

@keyframes modal-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes modal-fade-out {
  from { opacity: 1; }
  to   { opacity: 0; }
}

/* ── 对话框 ─────────────────────────── */
.modal {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transform: scale(0.95) translateY(10px);
  animation: modal-slide-in var(--duration-normal) var(--ease-spring) forwards;
}

.modal-overlay--hidden .modal {
  animation: modal-slide-out var(--duration-fast) var(--ease-in) forwards;
}

@keyframes modal-slide-in {
  from { transform: scale(0.95) translateY(10px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}
@keyframes modal-slide-out {
  from { transform: scale(1) translateY(0); opacity: 1; }
  to   { transform: scale(0.95) translateY(10px); opacity: 0; }
}

/* ── 头部 ───────────────────────────── */
.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--color-border);
}

.modal__title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
  margin: 0;
}

.modal__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: all var(--duration-fast);
}
.modal__close:hover {
  background: var(--color-bg-muted);
  color: var(--color-text);
}

/* ── 内容 ───────────────────────────── */
.modal__body {
  padding: var(--space-6);
  overflow-y: auto;
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
  line-height: var(--leading-relaxed);
}

/* ── 底部 ───────────────────────────── */
.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
}

/* ── 尺寸 ──────────────────────────── */
.modal--sm { max-width: 380px; }
.modal--lg { max-width: 720px; }

AI 使用说明

组件名: modal
选择器: .modal
依赖: /tokens/tokens.css
文件: /components/modal/modal.html

纯 HTML + CSS,零框架依赖,AI 友好