Popover浮层
在触发按钮附近展示说明与表单,支持对齐、键盘操作与焦点返回。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add popover安装
在项目根目录运行,安装源码。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add popover使用
React
"use client";
import { useState } from "react";import { BriefcaseBusiness, CalendarDays, Sun } from "lucide-react";import { Radio } from "@base-ui/react/radio";import { KalosPopover } from "@/components/ui/kalos-popover";import { KalosButton } from "@/components/ui/kalos-button";import { FieldLegend, FieldSet } from "@/components/ui/field";import { RadioGroup } from "@/components/ui/radio-group";
const schedules = [ { value: "daily", label: "每天", description: "每天 09:00", icon: Sun }, { value: "weekdays", label: "工作日", description: "周一至周五 09:00", icon: BriefcaseBusiness }, { value: "weekly", label: "每周一", description: "汇总上一周的文档", icon: CalendarDays },] as const;
export default function Example() { const [open, setOpen] = useState(false); const [saved, setSaved] = useState("weekly"); const [schedule, setSchedule] = useState(saved); const currentSchedule = schedules.find(option => option.value === saved) ?? schedules[2];
return <KalosPopover trigger={<><CalendarDays data-icon="inline-start" />{currentSchedule.label}</>} label="发送日程" align="end" contentClassName="w-[300px] max-w-[calc(100vw-32px)] rounded-xl p-3.5" open={open} onOpenChange={next => { if (next) setSchedule(saved); setOpen(next); }} > <form className="flex flex-col gap-4" onSubmit={event => { event.preventDefault(); setSaved(schedule); setOpen(false); // 在这里保存发送日程。 }}> <div className="flex flex-col gap-1"> <h3 className="text-[13px] font-medium leading-5">发送日程</h3> <p className="text-xs leading-[18px] text-muted-foreground">自动生成摘要,并在完成后发送。</p> </div> <FieldSet className="gap-0"> <FieldLegend className="sr-only">重复频率</FieldLegend> <RadioGroup value={schedule} onValueChange={setSchedule} aria-label="重复频率" className="gap-1"> {schedules.map(({ value, label, description, icon: Icon }) => ( <Radio.Root key={value} value={value} className="flex min-h-[52px] cursor-pointer items-center gap-3 rounded-lg p-2 outline-none transition-colors duration-180 hover:bg-muted data-checked:bg-muted focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring"> <Icon aria-hidden="true" className="size-4 shrink-0 text-muted-foreground" /> <span className="flex min-w-0 flex-1 flex-col"><span>{label}</span><span className="text-xs leading-[18px] text-muted-foreground">{description}</span></span> <Radio.Indicator className="size-1.5 shrink-0 rounded-full bg-current" /> </Radio.Root> ))} </RadioGroup> </FieldSet> <div className="flex justify-end gap-2"> <KalosButton type="button" variant="outline" onClick={() => setOpen(false)}>取消</KalosButton> <KalosButton type="submit">保存</KalosButton> </div> </form> </KalosPopover>;}基础用法
对齐
浮层表单
API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
trigger | ReactNode | — | 触发按钮文字或图标;不嵌套另一个按钮 |
triggerProps | { variant?: "outline" | "ghost"; size?: "default" | "icon"; "aria-label"?: string } | — | 触发按钮外观;图标按钮必须提供 aria-label |
label | string | 文字 trigger | 浮层与操作列表的可访问名称;图标触发器应显式提供 |
items | { value: string; label: string; disabled?: boolean }[] | [] | 操作列表;禁用项不响应操作 |
onAction | (value: string) => void | — | 激活列表项后关闭浮层并回调 |
children | ReactNode | — | 说明、表单等自定义内容;传入时替代操作列表 |
contentClassName | string | — | 浮层容器的样式类,可调整内容宽度 |
align | "start" | "center" | "end" | start | 浮层与触发元素的对齐方式 |
open | boolean | — | 受控打开状态 |
defaultOpen | boolean | false | 初始打开状态 |
onOpenChange | (open: boolean) => void | — | 打开状态变化 |
disabled | boolean | false | 禁用触发按钮 |
键盘操作
- Enter / Space
- 打开浮层或激活当前操作
- Tab / Shift+Tab
- 在浮层内的字段与按钮之间移动
- ↑ / ↓
- 在操作列表中移动到相邻的可用项
- Home / End
- 在操作列表中移动到首项或末项
- Esc
- 关闭并返回触发按钮