# shadcn-svelte shadcn-svelte is a community-led Svelte port of shadcn/ui that provides accessible and customizable components for Svelte and SvelteKit applications. Rather than installing components as npm packages, shadcn-svelte uses a CLI tool to copy component source code directly into your project, giving you complete ownership and customization control. Components are built with Tailwind CSS, use Tailwind Variants for styling variants, and leverage modern Svelte 5 features like runes and snippets. The project consists of a CLI tool for managing components, a component registry system, and comprehensive documentation. The CLI handles project initialization, component installation, and updates, while the registry provides a centralized source for component definitions, dependencies, and metadata. This architecture allows developers to pick and choose specific components, modify them freely, and maintain full control over their UI layer without being locked into a package ecosystem. ## CLI Commands ### Initialize Project Initialize shadcn-svelte in your SvelteKit project by creating a `components.json` configuration file and setting up path aliases. ```bash # Interactive initialization npx shadcn-svelte@latest init # Non-interactive with options npx shadcn-svelte@latest init \ --cwd ./my-project \ --base-color slate \ --css src/app.css \ --components-alias '$lib/components' \ --utils-alias '$lib/utils' \ --hooks-alias '$lib/hooks' \ --ui-alias '$lib/components/ui' \ --lib-alias '$lib' ``` **Generated components.json:** ```json { "$schema": "https://www.shadcn-svelte.com/schema.json", "aliases": { "lib": "$lib", "components": "$lib/components", "ui": "$lib/components/ui", "utils": "$lib/utils", "hooks": "$lib/hooks" }, "tailwind": { "css": "src/app.css", "baseColor": "slate" }, "typescript": true, "registry": "https://www.shadcn-svelte.com/registry" } ``` ### Add Components Add one or more components to your project. Components are copied directly into your codebase with all dependencies resolved. ```bash # Interactive selection npx shadcn-svelte@latest add # Add specific components npx shadcn-svelte@latest add button card input # Add all components npx shadcn-svelte@latest add --all # Skip confirmation and dependency installation npx shadcn-svelte@latest add button --yes --no-deps # Use proxy for registry access npx shadcn-svelte@latest add button --proxy http://proxy.example.com:8080 # Add to specific directory npx shadcn-svelte@latest add button --cwd ./my-project ``` **Example: Adding button component creates the following structure:** ``` src/lib/ ├── components/ │ └── ui/ │ └── button/ │ └── button.svelte └── utils.ts ``` ### Update Components Update existing components to their latest versions from the registry. ```bash # Interactive selection npx shadcn-svelte@latest update # Update specific components npx shadcn-svelte@latest update button card # Update all components npx shadcn-svelte@latest update --all --yes ``` ## Component Usage ### Button Component Flexible button component with multiple variants and sizes using Tailwind Variants. ```svelte Create Account Enter your details below to create your account
``` ### Input Component Form input component with label integration and error states. ```svelte
``` ## Configuration API ### components.json Schema The configuration file that defines project structure and aliases for the CLI. ```typescript { // Schema for validation "$schema": "https://www.shadcn-svelte.com/schema.json", // Path aliases for imports "aliases": { "lib": "$lib", // Root library alias "components": "$lib/components", // Components directory "ui": "$lib/components/ui", // UI components "utils": "$lib/utils", // Utility functions "hooks": "$lib/hooks" // Custom hooks }, // Tailwind configuration "tailwind": { "css": "src/app.css", // Global CSS file path "baseColor": "slate" // Base color theme: slate|gray|zinc|neutral|stone }, // TypeScript configuration "typescript": true, // boolean or { "config": "path/to/tsconfig.json" } // Registry URL for fetching components "registry": "https://www.shadcn-svelte.com/registry" } ``` **Loading config programmatically:** ```typescript import * as cliConfig from "shadcn-svelte/utils/get-config"; // Load configuration from disk const config = cliConfig.loadConfig("/path/to/project"); // Get resolved configuration with absolute paths const resolved = await cliConfig.getConfig("/path/to/project"); console.log(resolved.resolvedPaths.ui); // /path/to/project/src/lib/components/ui // Write configuration cliConfig.writeConfig("/path/to/project", { aliases: { lib: "$lib", components: "$lib/components", ui: "$lib/components/ui", utils: "$lib/utils", hooks: "$lib/hooks" }, tailwind: { css: "src/app.css", baseColor: "slate" }, typescript: true }); ``` ### Path Alias Resolution Configure TypeScript/JavaScript path aliases in `tsconfig.json` or `jsconfig.json` to match the aliases defined in `components.json`. ```json { "compilerOptions": { "baseUrl": ".", "paths": { "$lib": ["./src/lib"], "$lib/*": ["./src/lib/*"] } } } ``` **SvelteKit Configuration:** ```javascript // svelte.config.js import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; /** @type {import('@sveltejs/kit').Config} */ const config = { preprocess: vitePreprocess(), kit: { alias: { $lib: './src/lib', '$lib/*': './src/lib/*' } } }; export default config; ``` ## Registry API ### Fetching Registry Index Retrieve the complete list of available components from the registry. ```typescript import * as registry from "shadcn-svelte/utils/registry"; const registryUrl = "https://www.shadcn-svelte.com/registry"; const index = await registry.getRegistryIndex(registryUrl); // Index structure console.log(index); // [ // { // name: "button", // type: "registry:ui", // registryDependencies: [], // dependencies: ["tailwind-variants"], // devDependencies: [] // }, // { // name: "card", // type: "registry:ui", // registryDependencies: [], // dependencies: [] // } // ] // Filter by type const uiComponents = index.filter(item => item.type === "registry:ui"); const blocks = index.filter(item => item.type === "registry:block"); ``` ### Resolving Component Dependencies Resolve a component and all its registry dependencies recursively. ```typescript import * as registry from "shadcn-svelte/utils/registry"; const registryUrl = "https://www.shadcn-svelte.com/registry"; const index = await registry.getRegistryIndex(registryUrl); // Resolve component tree with dependencies const resolved = await registry.resolveRegistryItems({ registryIndex: index, items: ["button", "card"] }); console.log(resolved); // Returns all components including transitive dependencies // Fetch full component data with file contents const components = await registry.fetchRegistryItems({ baseUrl: registryUrl, items: resolved }); console.log(components[0]); // { // name: "button", // type: "registry:ui", // files: [ // { // type: "registry:ui", // target: "button/button.svelte", // content: "