Communitygithub.com

YepAPI/skills

使用 Radix/shadcn 的对话框、确认弹窗、移动端面板、焦点陷阱、滚动锁定及嵌套对话框支持。

skills 是什么?

该技能为开发者提供了一套基于 Radix UI 和 shadcn 组件的完整模态框与对话框解决方案。它涵盖了从基础的可访问性(焦点陷阱、滚动锁定)到高级交互模式(确认对话框、移动端底部面板、表单内嵌对话框)的全方位需求。技能强调使用成熟的库(如 Radix Dialog、shadcn AlertDialog 和 Sheet)来避免手动管理焦点、滚动或键盘事件,从而减少 bug 并提升用户体验。支持移动端自适应,推荐在窄屏上使用底部抽屉(Sheet)替代居中对话框。同时提供了清晰的模式示例(如删除确认、移动端菜单)和明确的避免清单(如不要覆盖 Radix 默认的 Escape 键行为、不要在表单验证失败时关闭对话框)。该技能适用于任何使用 React、Radix UI 和 shadcn 的 Web 应用,尤其适合需要高可访问性和一致交互模式的产品。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/modals-dialogs

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

模态框与对话框

规则

  • 使用 Radix Dialog 或 shadcn <Dialog> — 默认支持无障碍、焦点陷阱、滚动锁定
  • 用于破坏性操作的确认对话框:显示“删除此项目?”并附带“取消”(次要按钮)和“删除”(危险按钮)
  • 移动端使用面板/抽屉:来自 shadcn 的 <Sheet> — 从底部或侧面滑出,比居中模态框更适合触摸操作
  • 焦点陷阱:Radix 自动处理 — 打开时聚焦第一个可聚焦元素,关闭时返回原焦点
  • 滚动锁定:模态框打开时阻止 body 滚动 — Radix 通过 <Dialog.Overlay> 处理
  • 关闭触发器:Escape 键、点击遮罩外部、显式关闭按钮 — 均由 Radix 提供
  • 受控状态:open + onOpenChange props — 对于由外部事件触发的模态框,不要依赖内部状态
  • 对话框中的表单:Enter 提交,成功后关闭,失败时内联显示错误 — 验证失败时不关闭对话框
  • 移动端:在小屏幕上使用 <Sheet>(底部抽屉)代替居中 <Dialog> 来执行操作

模式

// 使用 shadcn 的确认对话框
import {
  AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
  AlertDialogDescription, AlertDialogFooter, AlertDialogHeader,
  AlertDialogTitle, AlertDialogTrigger,
} from "@/components/ui/alert-dialog";

function DeleteButton({ onDelete }: { onDelete: () => Promise<void> }) {
  const [loading, setLoading] = useState(false);
  return (
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <Button variant="destructive">删除项目</Button>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>删除此项目?</AlertDialogTitle>
          <AlertDialogDescription>
            此操作将永久删除该项目及其所有数据。此操作无法撤销。
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>取消</AlertDialogCancel>
          <AlertDialogAction
            onClick={async () => { setLoading(true); await onDelete(); setLoading(false); }}
            disabled={loading}
          >
            {loading ? "删除中..." : "删除"}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

// 用于移动端友好操作的 Sheet
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet";

function MobileMenu() {
  return (
    <Sheet>
      <SheetTrigger asChild><Button variant="ghost" size="icon"><MenuIcon /></Button></SheetTrigger>
      <SheetContent side="bottom">
        <SheetHeader><SheetTitle>操作</SheetTitle></SheetHeader>
        <nav className="flex flex-col gap-2 py-4">
          <Button variant="ghost">编辑</Button>
          <Button variant="ghost">分享</Button>
          <Button variant="destructive">删除</Button>
        </nav>
      </SheetContent>
    </Sheet>
  );
}

避免

  • 使用 position: fixed 和手动焦点管理构建自定义模态框 — 请使用 Radix/shadcn Dialog
  • 缺少 Escape 键和点击外部关闭功能 — Radix 已处理两者,不要覆盖它们
  • 在移动端将操作菜单放在居中模态框中 — 使用底部 Sheet/抽屉以获得更好的拇指触达范围
  • 表单验证失败时关闭对话框 — 保持打开状态,内联显示错误,让用户修正后重新提交
  • 嵌套对话框缺少正确的堆叠上下文 — 如果需要,请使用 Radix 的嵌套 Dialog 支持,并配合正确的 z-index

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能