Astro shadcn/ui Template

repository·main·Indexed 42 days ago

https://github.com/area44/astro-shadcn-ui-template

A modern starter template for building fast, accessible, and themeable web applications using Astro, Tailwind CSS, and shadcn/ui. Version 26.02.24 includes pre-configured components such as CommandMenu, ErrorBoundary, ThemeToggle, and MainNav, along with a centralized siteConfig for managing global metadata and navigation.

Tokens
2.1K
Snippets
8
Records
9
Agent score
52%

What's inside Astro shadcn/ui Template

  1. Setup the Astro shadcn/ui template

    main

    To get a local development environment running, clone the repository, install dependencies using pnpm, and start the development server. The application will be available at http://localhost:4321.

    git clone https://github.com/area44/astro-shadcn-ui-template
    cd astro-shadcn-ui-template
    pnpm install
    pnpm dev
  2. Add shadcn/ui components to the template

    main

    This template is pre-configured for shadcn/ui. To add new components (like Accordion or Dialog), you must first follow the official shadcn/ui Astro installation guide. Once the CLI/installation is configured, you can pick components from the shadcn/ui library and follow their specific usage instructions.

    Note: Since shadcn/ui components are typically React-based, you may need to reference the Astro Framework Components guide to understand how to use React components within your Astro pages.

  3. Configure site metadata with SiteConfig

    main

    The siteConfig object is used to define global site metadata, including the site name, description, URL, author information, social links, and navigation items. You can customize these values in src/lib/config.ts to match your project's identity.

    Configuration Options

    KeyTypeDescription
    namestringThe name of your site.
    descriptionstringA brief description of your site.
    urlstringThe base URL of your site.
    authorobjectContains name (string) and url (string) for the site author.
    linksobjectContains github (string) for the project's GitHub repository.
    navItemsarrayAn array of navigation objects. Each object contains href (string), label (string), and an optional external (boolean) flag to indicate if the link leads to an external site.
    import { siteConfig } from './lib/config';
    
    // Example of how the configuration structure looks
    export const siteConfig = {
      name: "My Awesome Site",
      description: "A description of my site",
      url: "https://example.com",
      author: {
        name: "John Doe",
        url: "https://example.com/author",
      },
      links: {
        github: "https://github.com/user/repo",
      },
      navItems: [
        { href: "/about", label: "About" },
        { href: "https://google.com", label: "Google", external: true },
      ],
    };
  4. Use the Icons component

    main

    The Icons object provides a collection of SVG icon components. Each icon component accepts standard React.HTMLAttributes<SVGElement> props, allowing you to pass attributes like className, style, width, height, and other SVG properties directly to the icon.

    import { Icons } from "@/components/Icons";
    
    // Example usage with className for styling
    const MyComponent = () => (
      <Icons.gitHub className="w-6 h-6 text-blue-500" />
    );
  5. Use the CommandMenu component

    main

    The CommandMenu component provides a site-wide command palette for navigation and quick actions. It can be triggered by clicking a search button or by using the keyboard shortcut ⌘K (macOS) or Ctrl+K (Windows/Linux).

    Inside the menu, users can search through available commands via a text input. The component includes built-in support for theme switching (light/dark) and navigation. To customize the menu, you can modify the commands array within the component to include your own CommandItem objects.

    import { CommandMenu } from "@/components/CommandMenu";
    
    // Usage in a layout or header
    export default function Layout() {
      return (
        <header>
          <CommandMenu />
        </header>
      );
    }
  6. Use the ThemeToggle component

    main

    The ThemeToggle component provides a UI button to switch between light and dark modes. It manages theme state via localStorage and synchronizes the theme with the document.documentElement class list.

    Key Features:

    • Persistence: Saves the user's preference in localStorage under the key theme.
    • System Sync: Automatically detects and responds to system-level color scheme changes (prefers-color-scheme) if no manual preference is stored.
    • Keyboard Shortcuts: Supports toggling the theme using Cmd+Shift+D, Ctrl+Shift+D, or the single key d (when not typing in input fields).
    • Accessibility: Includes aria-label and aria-pressed attributes, and uses a visually hidden sr-only span for screen readers.
    import { ThemeToggle } from "@/components/ThemeToggle";
    
    // Use it within your Astro/React components
    export default function MyComponent() {
      return (
        <nav>
          <ThemeToggle />
        </nav>
      );
    }
  7. Use the ErrorBoundary component to catch UI errors

    main

    The ErrorBoundary component is a React class component used to catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of crashing the entire application.

    When an error is caught, it renders a styled fallback UI containing:

    • A 'Something went wrong' heading.
    • The error message (if available).
    • A 'Try again' button that calls resetErrorBoundary to attempt to clear the error state and re-render the children.

    Wrap any component or section of your UI that is prone to runtime errors with <ErrorBoundary> to ensure a graceful degradation of the user experience.

    import { ErrorBoundary } from "./components/ErrorBoundary";
    
    function MyComponent() {
      return (
        <ErrorBoundary>
          <RiskyComponent />
        </ErrorBoundary>
      );
    }
  8. Use the MainNav component

    main

    The MainNav component renders the primary site navigation based on the configuration defined in siteConfig.navItems. It automatically handles external links by applying target="_blank" and rel="noopener noreferrer" to any item marked with external: true.

    It accepts standard HTML <nav> attributes via ...props and allows for custom styling through the className prop using the cn utility.

    import { MainNav } from "@/components/MainNav";
    
    // Usage in a layout or page
    <MainNav className="my-custom-class" />
  9. Define custom commands for CommandMenu

    main

    To add new functionality to the CommandMenu, define objects that conform to the CommandItem interface. Each item requires a unique id, a title, an icon component, and an action function that executes when the item is selected.

    CommandItem Interface:

    • id: string - A unique identifier for the command.
    • title: string - The primary text displayed for the command.
    • subtitle?: string - Optional descriptive text displayed below the title.
    • icon: React.ComponentType<{ className?: string }> - A React component used as the command's icon.
    • action: () => void - The function to execute when the command is clicked or selected.
    interface CommandItem {
      id: string;
      title: string;
      subtitle?: string;
      icon: React.ComponentType<{ className?: string }>;
      action: () => void;
    }