组件目录

Drawer抽屉

从屏幕四边滑入的表单面板,支持拖拽关闭、初始化骨架屏和确认加载状态。

npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add drawer
MCP

安装

在项目根目录运行,安装源码。

npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add drawer

使用

表单示例还需安装 Input、Select、Time Picker、Range Picker 与 Checkbox:运行安装命令时追加 input select time-picker date-range-picker checkbox。示例计时器演示初始化与确认加载,实际接入时由数据请求与保存操作控制。

React
"use client";
import { useEffect, useRef, useState, type FormEvent } from "react";import { FieldGroup } from "@/components/ui/field";import { KalosDrawer } from "@/components/ui/kalos-drawer";import { KalosInput } from "@/components/ui/kalos-input";import { KalosSelect } from "@/components/ui/kalos-select";import { KalosTimePicker } from "@/components/ui/kalos-time-picker";import { KalosCheckbox } from "@/components/ui/kalos-checkbox";import { KalosDateRangePicker } from "@/components/ui/kalos-date-range-picker";import { KalosLoading, KalosSkeleton } from "@/components/ui/kalos-skeleton";
type ContactValues = { name: string; email: string; time: string };type ContactErrors = Partial<Record<keyof ContactValues, string>>;
function validateContact(values: ContactValues): ContactErrors {  const errors: ContactErrors = {};  if (!values.name.trim()) errors.name = "请输入姓名";  if (!values.email.trim()) errors.email = "请输入邮箱地址";  else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(values.email)) errors.email = "请输入有效的邮箱地址";  if (!values.time) errors.time = "请选择提醒时间";  return errors;}
function ContactSkeleton() {  return <KalosLoading label="正在加载表单" className="flex flex-col gap-3.5">    {Array.from({ length: 5 }, (_, index) => <div key={index} className="flex flex-col gap-1.5">      <KalosSkeleton width={index % 2 ? 64 : 40} height={20} />      <KalosSkeleton height={32} radius={7} />    </div>)}    <div className="grid grid-cols-2 gap-3">      {[0, 1].map(index => <div key={index} className="flex min-w-0 flex-col gap-1.5">        <KalosSkeleton width={64} height={20} />        <KalosSkeleton height={32} radius={7} />      </div>)}    </div>    <div className="flex items-center gap-3">      <KalosSkeleton width={16} height={16} radius={4.5} />      <KalosSkeleton width={80} height={20} />    </div>  </KalosLoading>;}
export default function Example() {  const [open, setOpen] = useState(false);  const [loading, setLoading] = useState(false);  const [confirming, setConfirming] = useState(false);  const [values, setValues] = useState<ContactValues>({ name: "", email: "", time: "" });  const [submitted, setSubmitted] = useState(false);  const formRef = useRef<HTMLFormElement>(null);  const nameRef = useRef<HTMLInputElement>(null);  const emailRef = useRef<HTMLInputElement>(null);  const timeRef = useRef<HTMLInputElement>(null);  const errors = submitted ? validateContact(values) : {};
  function confirm(event: FormEvent<HTMLFormElement>) {    event.preventDefault();    if (loading || confirming) return;    setSubmitted(true);    const nextErrors = validateContact(values);    if (nextErrors.name) nameRef.current?.focus();    else if (nextErrors.email) emailRef.current?.focus();    else if (nextErrors.time) timeRef.current?.focus();    else setConfirming(true);  }
  useEffect(() => {    if (!open) return;    // 演示初始化加载;实际接入时由数据请求控制 loading。    const timer = setTimeout(() => setLoading(false), 1000);    return () => clearTimeout(timer);  }, [open]);
  useEffect(() => {    if (!confirming || !open) return;    // 演示异步确认;实际接入时等待保存操作完成。    const timer = setTimeout(() => { setOpen(false); setConfirming(false); }, 800);    return () => clearTimeout(timer);  }, [confirming, open]);
  return <div className="flex flex-col items-center gap-4">    <KalosDrawer      trigger="打开抽屉"      title="联系人设置"      swipeDirection="right"      open={open}      onOpenChange={next => {        setOpen(next);        setLoading(next);        setConfirming(false);        if (next) { setValues({ name: "", email: "", time: "" }); setSubmitted(false); }      }}      loading={loading}      loadingContent={<ContactSkeleton />}      confirming={confirming}      onConfirm={() => formRef.current?.requestSubmit()}    >      <form ref={formRef} noValidate onSubmit={confirm} aria-label="联系人设置">      <FieldGroup className="gap-3.5">        <KalosInput ref={nameRef} name="name" label="姓名" placeholder="请输入姓名" autoComplete="off" required value={values.name} error={errors.name} onChange={event => setValues(current => ({ ...current, name: event.target.value }))} onClear={() => setValues(current => ({ ...current, name: "" }))} />        <KalosInput ref={emailRef} name="email" label="邮箱地址" type="email" placeholder="you@example.com" autoComplete="off" required value={values.email} error={errors.email} onChange={event => setValues(current => ({ ...current, email: event.target.value }))} onClear={() => setValues(current => ({ ...current, email: "" }))} />        <KalosSelect label="联系人分组" clearable placeholder="请选择分组" items={[          { value: "colleagues", label: "同事" },          { value: "friends", label: "朋友" },          { value: "family", label: "家人" },        ]} />        <KalosTimePicker ref={timeRef} name="time" label="提醒时间" placeholder="请选择时间" step={30} required value={values.time || null} error={errors.time} onChange={time => setValues(current => ({ ...current, time: time ?? "" }))} />        <KalosDateRangePicker label="日期范围" name="dateRange" defaultValue={null} className="w-full!" />        <FieldGroup className="grid! grid-cols-2 items-start gap-3!">          <div className="min-w-0"><KalosSelect label="联系渠道" clearable placeholder="请选择渠道" name="channel" items={[            { value: "phone", label: "电话" },            { value: "wechat", label: "微信" },            { value: "other", label: "其他" },          ]} /></div>          <div className="min-w-0"><KalosInput label="联系方式" name="contact" placeholder="请输入联系方式" autoComplete="off" /></div>        </FieldGroup>        <KalosCheckbox label="接收消息通知" />      </FieldGroup>      </form>    </KalosDrawer>  </div>;}

弹出方向

API

属性类型默认值说明
trigger / triggerPropsReactNode / object—触发内容与按钮属性;图标触发器需提供 aria-label
titlestring—抽屉标题与可访问名称
descriptionstring—仅在需要补充信息时显示
children / footerReactNode—内容与额外底部操作;默认提供取消与确定按钮
swipeDirection"up" | "right" | "down" | "left"right抽屉所在边缘与拖拽关闭方向
loading / loadingContentboolean / ReactNodefalse / 表单骨架屏初始化时使用 KalosSkeleton;可替换加载内容
confirmingbooleanfalse仅确定按钮显示 KalosSpinner 并禁用;取消与 X 仍直接关闭
onConfirm() => void—点击确定时调用;传入后由调用方控制 open 与 confirming,完成后关闭,失败时保留内容并显示错误
showSwipeHandlebooleantrue显示与抽屉方向对应的拖拽柄;表单内容仍可选择文字和滚动
confirmLabel / cancelLabelstring确定 / 取消底部确定与取消按钮文字
open / defaultOpenboolean— / falseBase UI 受控或非受控打开状态
onOpenChange(open, eventDetails) => void—保留 Base UI 事件原因与取消 API
onOpenChangeComplete(open: boolean) => void—打开或关闭动画完成后调用
initialFocusBase UI Popup initialFocus抽屉容器显式指定时才将焦点移到表单控件
contentClassNamestring—内容容器的样式类
modal / disablePointerDismissalboolean | trap-focus / booleantrue / false保留 Base UI 模态与点击外部关闭配置

键盘操作

Enter / Space
打开抽屉或激活当前按钮
Tab / Shift + Tab
在抽屉内移动焦点;初始化后不会自动聚焦表单
Esc
关闭顶层浮层或抽屉,并返回触发按钮
关闭完成
焦点返回触发按钮