shadcn-svelte
repository·main·Indexed 27 days ago
https://github.com/huntabyte/shadcn-svelteA community-driven Svelte port of the shadcn/ui component library. It provides accessible, customizable, and copy-pasteable components for Svelte developers, including a CLI for project initialization, component installation, and the ability to build custom component registries.
What's inside shadcn-svelte
- shadcn-svelte is an unofficial, community-led Svelte port of shadcn/ui. It provides accessible and customizable components designed to be copied and pasted directly into your applications. It is intended to be used as a foundation for building your own component libraries within the Svelte ecosystem.
Understand shadcn-svelte theming architecture
mainTheming is driven by CSS variables defined in:root(for light mode) and.dark(for dark mode). Tailwind CSS maps these variables to utility classes (e.g.,bg-primary,text-muted-foreground). Components consume these utilities, so updating the CSS variable values globally updates all referencing components.Understand the shadcn-svelte philosophy
mainshadcn-svelte is not a traditional NPM component library. Instead, it is a system for building your own component library. Unlike standard libraries where you import pre-built components, shadcn-svelte provides the actual source code for components, allowing for full transparency and customization.
Key principles include:
- Open Code: You own the component code in your project, allowing you to edit it directly rather than wrapping components or overriding styles.
- Composition: Components use a common, predictable, and composable interface.
- Distribution: Uses a flat-file schema and a CLI tool to distribute and install components.
- Beautiful Defaults: Provides a consistent design system out-of-the-box using Bits UI and Tailwind CSS.
- AI-Ready: The open-code nature and consistent APIs make it easy for LLMs to read, understand, and generate components.
Understand the shadcn-svelte skill capabilities
mainThe shadcn-svelte skill provides AI assistants with several layers of project-specific knowledge:
- Project Context: Automatically reads your
components.jsonto identify your framework (SvelteKit, Vite, Astro), Tailwind version, aliases, installed components, icon library, and resolved file paths. - CLI Commands: Provides a full reference for
init,add, andregistry buildcommands, including flags and smart merge workflows. - Theming and Customization: Guidance on using CSS variables, OKLCH colors, dark mode, custom colors, border radius, and component variants for both Tailwind v3 and v4.
- Registry Authoring: Instructions for building and publishing custom component registries, including
registry.jsonformats, item types, file objects, dependencies, and hosting.
- Project Context: Automatically reads your
About the Calendar component
mainThe
<Calendar />component is built on top of the Bits UI Calendar component. It utilizes the@internationalized/datepackage for date handling.For range-specific selection, use the Range Calendar component instead.
Use the Range Calendar component
mainThe<RangeCalendar />component allows users to select a range of dates. It is built on top of the Bits UI Range Calendar and requires the@internationalized/datepackage for date handling.Understand the Sidebar component structure
mainThe
Sidebaris a composable component system. The core parts are:Sidebar.Provider: Manages the collapsible state and provides context.Sidebar.Root: The main container for the sidebar.Sidebar.Header: A sticky top section.Sidebar.Footer: A sticky bottom section.Sidebar.Content: The scrollable area of the sidebar.Sidebar.Group: A logical section withinSidebar.Content.Sidebar.Trigger: The element used to toggle the sidebar visibility.
Build forms with Formsnap, Superforms, and Zod
mainThe
Formcomponents inshadcn-svelteare wrappers aroundformsnapandsveltekit-superforms. They provide composable components, form field scoping, Zod-based validation, and automatic ARIA attribute application.Anatomy of a Form Field
Each field follows this structure:
<Form.Field> <Form.Control> <Form.Label /> <!-- Any Form input component --> </Form.Control> <Form.Description /> <Form.FieldErrors /> </Form.Field><form> <Form.Field> <Form.Control> <Form.Label /> <!-- Any Form input component --> </Form.Control> <Form.Description /> <Form.FieldErrors /> </Form.Field> </form>Install the Radio Group component
mainYou can install the Radio Group component using the CLI or manually.
CLI Installation: Run the following command to add the component via the shadcn-svelte CLI:
# Use your package manager to run the add command # The specific command depends on your CLI setup, typically: # npx shadcn-svelte@latest add radio-groupManual Installation:
- Install the required dependency
bits-ui:bits-ui -D - Copy the component source code into your project files.
- Install the required dependency
Install the Progress component
mainYou can install the Progress component using the shadcn-svelte CLI or manually.
Using CLI: Run the following command to add the component to your project:
Manual Installation:
- Install the required dependency
bits-ui:bits-ui -D - Copy the component source code into your project files.
- Install the required dependency
Install the Range Calendar component
mainYou can install the
RangeCalendarcomponent using the shadcn-svelte CLI or manually.Using CLI
Run the following command to add the component to your project:
# Replace with your actual CLI command if different, but based on the component name: n shadcn-svelte add range-calendarManual Installation
- Install the required dependencies
bits-uiand@internationalized/date:
bits-ui @internationalized/date -D- Copy the component source code into your project files.
bits-ui @internationalized/date -D- Install the required dependencies
Secure your registry with authentication
mainThe
shadcn-svelteCLI does not have built-in authentication. To protect your registry, implement authorization on your registry server.A common pattern is to use a
tokenquery parameter (e.g.,http://localhost:5173/r/hello-world.json?token=[SECURE_TOKEN_HERE]). If the token is invalid, your server should return a401 Unauthorizedresponse. Theshadcn-svelteCLI will detect this response and display an error message to the user.