OriginUI Svelte

repository·main·Indexed 41 days ago

https://github.com/max-got/originui-svelte

A UI component library for Svelte providing high-quality, accessible, and composable components. It includes a variety of UI elements such as Accordions, Alert Dialogs, Avatars, and Breadcrumbs, as well as specialized navbar components and a `clickOutside` Svelte action. The library features a component registry for managing and discovering components across different categories.

Tokens
17.2K
Snippets
53
Records
69
Agent score
51%

What's inside OriginUI Svelte

  1. Distinguish between 'Ready' and 'Todo' components

    main

    The registry distinguishes between components that are fully implemented and those that are marked as tasks (todo).

    • OUIReadyComponents: A union of all components that are not marked as todo.svelte.
    • OUITodoComponents: A union of all components that end in todo.svelte.
    • OUIReadyComponent<T>: Returns the ready components specifically within a directory T.
    • OUITodoComponent<T>: Returns the todo components specifically within a directory T.
  2. Access component metadata and directory status

    main

    The registry provides types for inspecting the state and structure of the component library:

    • OUIComponentMetadata: Provides an overview of each directory, including its name, totalComponents, status, and whether it hasInProgress components.
    • OUIDirectoryComponentCounts: Returns the total component count and the status for each directory.
    • OUIDirectoriesWithTodo: A filtered type that only includes directories that have components marked as todo (where status.todo > 0).
  3. ESLint Configuration for originui-svelte

    main

    The project uses a flat ESLint configuration based on typescript-eslint. It integrates support for Svelte, TypeScript, and Prettier, and enforces specific code organization patterns using eslint-plugin-perfectionist.

    Key features include:

    • Svelte Support: Configured to parse .svelte, .svelte.ts, and .svelte.js files using the Svelte parser and the project's svelte.config.js.
    • Global Variables: Includes browser and node globals, and explicitly defines Picture as a readonly global.
    • Custom Import Sorting: Uses perfectionist/sort-imports with custom groups to handle Svelte-specific patterns:
      • svelte-internal: Matches imports from $types (e.g., import type { PageServerLoad } from './$types.js';).
      • alias-imports: Matches alias imports ending in .js, .ts, or .svelte (e.g., import Component from '$lib/component.svelte';).
    • Strictness:
      • no-console is an error, but allows warn, error, and info.
      • Unused variables (@typescript-eslint/no-unused-vars) are warnings, but allow variables/arguments starting with an underscore (_).
    // Summary of active rules and patterns
    export default tseslint.config(
      // ... recommended configs
      {
        rules: {
          '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
          'no-console': ['error', { allow: ['warn', 'error', 'info'] }],
          'perfectionist/sort-imports': [
            'error',
            {
              customGroups: {
                type: { 'svelte-internal': ['.\/\$types'] },
                value: { 'alias-imports': ['^\$?.*(\.js|\.ts|\.svelte)$'] }
              },
              groups: ['svelte-internal', 'type', ['alias-imports'], ['builtin', 'external'], 'internal-type', 'internal', ['parent-type', 'sibling-type', 'index-type'], ['parent', 'sibling', 'index'], 'object', 'unknown']
            }
          ]
        }
      }
    );
  4. Use the Popover component

    main

    The Popover component is built on top of bits-ui and provides a floating container that appears when a trigger is activated. It consists of four main parts:

    • Popover (aliased from Root): The root component that manages the open/closed state.
    • PopoverTrigger: The element that toggles the popover visibility.
    • PopoverContent: The container that holds the actual content to be displayed.
    • PopoverClose: A component used within the content to close the popover.

    To use it, wrap your trigger and content within the Popover component.

    <script>
      import { 
        Popover, 
        PopoverTrigger, 
        PopoverContent, 
        PopoverClose 
      } from '$lib/components/ui/popover';
    </script>
    
    <Popover>
      <PopoverTrigger>Open Menu</PopoverTrigger>
      <PopoverContent>
        <div>Popover content goes here</div>
        <PopoverClose>Close</PopoverClose>
      </PopoverContent>
    </Popover>
  5. Get component files in specific directories

    main

    You can retrieve lists of component filenames for one or multiple directories using getFiles on the registry instance or the getComponentFileNames helper.

    • getComponentFileNames(directory): Returns a Promise containing an array of filenames for the specified OUIDirectory.
    • registry.getFiles(directories[]): Returns an array of filenames flattened from the provided list of directories.
    import { getComponentFileNames } from '$lib/componentRegistry';
    
    // Single directory
    const uiFiles = await getComponentFileNames('ui');
    
    // Using the registry instance for multiple directories
    const registry = await getComponentRegistry();
    const multipleFiles = registry.getFiles(['ui', 'extras']);
  6. Retrieve component metadata via getRegistryMetaData()

    main

    The getRegistryMetaData() method returns a comprehensive overview of the entire component library. This is useful for building documentation sites, component browsers, or discovery tools.

    The returned RegistryMetadata object includes:

    • totalComponents: Total count of all components.
    • totalDirectories: Total count of directories.
    • directoriesBreakdown: A mapping of OUIDirectory to DirectoryMetadata, which contains:
      • apiUrl: The endpoint for component JSON data.
      • componentCount: Number of components in that directory.
      • components: An array of ComponentMetadata (name, path, githubUrl, and state).
      • stateBreakdown: Counts of components categorized as ready, soon, or todo.
    const registry = await getComponentRegistry();
    const metadata = registry.getRegistryMetaData();
    
    console.log(metadata.totalComponents);
    console.log(metadata.directoriesBreakdown['ui'].components);
    // Example component metadata:
    // { 
    //   name: 'button', 
    //   path: 'src/lib/components/ui/button', 
    //   state: 'ready', 
    //   githubUrl: '...' 
    // }
  7. Use the Command component

    main

    The Command component is a wrapper around bits-ui's Command primitive, providing a styled container for building command menus or CLI-like interfaces. It manages the selection state via a value prop and provides a reference to the underlying element via ref.

    Props

    PropTypeDefaultDescription
    classstring-Additional CSS classes to apply to the component container.
    refHTMLElementnull (bindable)A reference to the root element of the command component.
    valuestring'' (bindable)The currently selected value in the command menu.
    ...restPropsCommandPrimitive.RootProps-All other props supported by the underlying bits-ui Command primitive.
    <script>
      import { Command } from '$lib/components/ui/command';
    
      let selectedValue = $state('');
      let commandRef = $state(null);
    </script>
    
    <Command bind:value={selectedValue} bind:ref={commandRef} class="my-custom-class">
      <!-- Command sub-components go here -->
    </Command>
  8. Use the ScrollArea component

    main

    The ScrollArea component provides a custom scrollable area with a styled scrollbar. It is exported via the Root component. You can also use the ScrollArea alias for the same component.

    To implement a scrollable area, use ScrollArea (or Root) as the container and Scrollbar (or ScrollAreaScrollbar) to render the scrollbar element within that container.

    <script>
      import { ScrollArea } from '$lib/components/ui/scroll-area';
    </script>
    
    <ScrollArea class="h-[200px] w-[350px] rounded-md border p-4">
      <!-- Your scrollable content here -->
    </ScrollArea>
  9. Use the Collapsible component

    main

    The Collapsible component is a set of primitives used to create expandable/collapsible sections. It is built on top of bits-ui and consists of three main parts: Collapsible (the root container), CollapsibleTrigger (the element that toggles the state), and CollapsibleContent (the element that is shown or hidden).

    <Collapsible>
    	<CollapsibleTrigger>Toggle</CollapsibleTrigger>
    	<CollapsibleContent>
    		This is the collapsible content.
    	</CollapsibleContent>
    </Collapsible>