AniBTUI
✦ 组件@anibt/separatorregistry JSON

Separator

A 的分隔是一条虚线缝线:4px 线、4px 空,1px 粗;也可以是 1px 的实线。

喵萌奶茶屋

简繁内封 · 1080p · 每周四更新

资源 1,284
作品 96
成员 12

上面两条:缝线(默认)和实线(variant="line")。

安装

npx shadcn@latest add @anibt/separator

第一次用之前:在 components.json 里登记 @anibt,再单独运行一次 npx shadcn@latest add @anibt/theme(见快速开始)。只有单独安装主题时,CLI 才会把 shadcn 的标准颜色换成白樱 / 夜樱;随组件带进来的主题只补上 AniBT 自己的变量和配方。

手动安装
  1. 1. 先装主题组件只读 @anibt/theme 的 token 和配方,没有主题就没有样子。
    npx shadcn@latest add @anibt/theme
  2. 2. 安装依赖
    npm install class-variance-authority radix-ui
  3. 3. 把下面的文件放进项目
    components/ui/separator.tsx
    "use client"
    
    import * as React from "react"
    import { cva, type VariantProps } from "class-variance-authority"
    import { Separator as SeparatorPrimitive } from "radix-ui"
    
    import { cn } from "@/lib/utils"
    
    /**
     * Separator — the AniBT 「贴纸手账」 rule.
     *
     * A's rule between rows and sections is the dashed stitch: 4px of the control
     * edge colour, 4px of nothing, 1px thick (`variant="stitch"`, the default).
     * `variant="line"` is the solid 1px panel edge, for the rare place a stitch
     * would read as a seam in the paper rather than a division.
     *
     * Vertical rules stretch to their row (`self-stretch`) and stitch downwards.
     * Decorative by default, as in shadcn: a screen reader skips it. Pass
     * `decorative={false}` when the rule separates two groups a reader should
     * hear as separate.
     *
     * The element carries Radix's `data-orientation` and Base UI's
     * `data-horizontal` / `data-vertical`, so a shadcn `base-*` component that
     * styles either keeps working. The orientation classes are plain utilities
     * picked from the `orientation` prop, so `cn` merges a caller's class over
     * them (the sidebar's `mx-2 w-auto`, a `h-4` on a vertical rule).
     */
    const separatorVariants = cva("shrink-0", {
      variants: {
        variant: {
          stitch: "",
          line: "bg-border",
        },
        orientation: {
          horizontal: "h-px w-full",
          vertical: "w-px self-stretch",
        },
      },
      compoundVariants: [
        { variant: "stitch", orientation: "horizontal", class: "bg-(image:--anibt-stitch) bg-size-[8px_1px] bg-repeat-x" },
        { variant: "stitch", orientation: "vertical", class: "bg-(image:--anibt-stitch-y) bg-size-[1px_8px] bg-repeat-y" },
      ],
      defaultVariants: {
        variant: "stitch",
        orientation: "horizontal",
      },
    })
    
    type SeparatorProps = React.ComponentProps<typeof SeparatorPrimitive.Root> &
      VariantProps<typeof separatorVariants>
    
    function Separator({
      className,
      orientation = "horizontal",
      decorative = true,
      variant,
      ...props
    }: SeparatorProps) {
      return (
        <SeparatorPrimitive.Root
          data-slot="separator"
          data-variant={variant ?? "stitch"}
          data-horizontal={orientation === "horizontal" ? "" : undefined}
          data-vertical={orientation === "vertical" ? "" : undefined}
          decorative={decorative}
          orientation={orientation}
          className={cn(separatorVariants({ variant, orientation }), className)}
          {...props}
        />
      )
    }
    
    export { Separator, separatorVariants }
    export type { SeparatorProps }

用法

用法
import { Separator } from "@/components/ui/separator"

<Separator />
<Separator orientation="vertical" />
<Separator variant="line" />

行与行、节与节之间用缝线(默认的 variant="stitch"),颜色是控件边色。variant="line" 是面板边那种 1px 实线,只在缝线读起来像纸上的接缝、而不是分隔时用。

竖线会撑满所在行的高度(self-stretch),所以放在 flex 行里;也可以用 className="h-4" 定高。

默认是装饰性的,屏幕阅读器会跳过;它分开的是两组读者应该分开听的内容时,写 decorative={false}。

元素同时带 Radix 的 data-orientation 和 Base UI 的 data-horizontal / data-vertical,shadcn 里按任一种写样式的组件都能继续用。

属性

Separator
variant"stitch" | "line"默认 "stitch"虚线缝线 / 1px 实线
orientation"horizontal" | "vertical"默认 "horizontal"横 / 竖
decorativeboolean默认 truefalse 时以 role=separator 暴露给辅助技术