Snap Guides

Superposition de guides d'alignement — lignes verticales et horizontales en pointilles qui apparaissent pendant un glissement. Installez Snap Guides depuis le registre VLLNT UI avec la CLI shadcn.

Web
Signaler un bug

Superposition de guides d'alignement — lignes verticales et horizontales en pointilles qui apparaissent pendant un glissement. Fait partie de la famille data-display dans VLLNT UI, il est publie comme entree de registre lisible par machine — copiez la source directement dans votre application avec la CLI shadcn et possedez-la, sans dependance d'execution a une bibliotheque de composants.

Apercu

Basculez entre clair et sombre pour inspecter l'apercu Storybook integre.

Installation

Ajoutez Snap Guides a votre projet avec la CLI shadcn. La source arrive dans votre code, prete a etre adaptee :

pnpm dlx shadcn@latest add https://ui.vllnt.com/r/snap-guides.json

Source

"use client"; import type { ComponentPropsWithoutRef } from "react"; import { cn } from "../../lib/utils"; /** * One alignment line. * * @public */ export type SnapGuide = | { /** Stable id (used as React key). */ id: string; /** Horizontal guide — runs left to right at this `y` in container px. */ orientation: "horizontal"; y: number; } | { /** Stable id (used as React key). */ id: string; /** Vertical guide — runs top to bottom at this `x` in container px. */ orientation: "vertical"; x: number; }; /** * Localizable strings. * * @public */ export type SnapGuidesLabels = { /** Aria-label for the layer. Defaults to `"Snap guides"`. */ region?: string; }; const DEFAULT_LABELS = { region: "Snap guides", } as const satisfies Required<SnapGuidesLabels>; /** * Props for {@link SnapGuides}. * * @public */ export type SnapGuidesProps = { /** Active alignment lines. Pass an empty array to hide all. */ guides: SnapGuide[]; /** Localizable strings. */ labels?: SnapGuidesLabels; } & ComponentPropsWithoutRef<"div">; /** * Alignment guide overlay for a canvas. Renders dashed lines at the * `x` / `y` coordinates supplied by the host snapper. Pure * presentation — the host computes which guides are active during a * drag and unmounts them on release. * * @example * ```tsx * <SnapGuides * guides={[ * { id: "x-200", orientation: "vertical", x: 200 }, * { id: "y-160", orientation: "horizontal", y: 160 }, * ]} * /> * ``` * * @public */ export const SnapGuides = ({ ref, ...props }: SnapGuidesProps & { ref?: React.Ref<HTMLDivElement> }) => { const { className, guides, labels, ...rest } = props; const resolvedLabels = { ...DEFAULT_LABELS, ...labels }; return ( <div aria-label={resolvedLabels.region} className={cn("pointer-events-none absolute inset-0 z-30", className)} data-snap-guide-count={guides.length} ref={ref} {...rest} > {guides.map((guide) => { const isVertical = guide.orientation === "vertical"; const offset = isVertical ? guide.x : guide.y; return ( <span aria-hidden="true" className={cn( "absolute border-primary/70", isVertical ? "inset-y-0 w-px border-l border-dashed" : "inset-x-0 h-px border-t border-dashed", )} data-snap-guide-id={guide.id} data-snap-orientation={guide.orientation} key={guide.id} style={ isVertical ? { left: `${offset.toString()}px` } : { top: `${offset.toString()}px` } } /> ); })} </div> ); }; SnapGuides.displayName = "SnapGuides";

Stories

Explorez chaque variante et etat dans le Storybook interactif :

Apercu

Basculez entre clair et sombre pour inspecter l'apercu Storybook integre.

Dependances

  • @vllnt/ui@^0.3.0