Melt UI Documentation

repository·develop·Indexed 26 days ago

https://github.com/melt-ui/melt-ui

A set of headless, accessible component builders for Svelte that provide logic and WAI-ARIA accessibility while leaving styling to the developer. Includes builders for components such as Accordion, Avatar, and Calendar, and utilizes the `use:melt` Svelte action to attach builders to elements.

Tokens
32.4K
Snippets
85
Records
165
Agent score
85%

What's inside Melt UI

  1. Overview of the Date Field builder

    develop
    The Date Field is an enhanced, accessible alternative to the native HTML date input. It provides a user-friendly interface for selecting dates and times across all modern browsers and devices. It is composed of several parts: a Field (container for date segments), individual Segments (day, month, year, etc.), a Label, a Hidden Input (holding the actual value), and a Validation container.
  2. Overview of the Date Range Field builder

    develop

    The Date Range Field is a builder designed to provide a user-friendly, accessible interface for selecting a range of dates and times. It serves as an enhanced alternative to using two separate native date inputs, offering a more cohesive experience across modern browsers and devices.

    Note: Before using this builder, ensure you understand how Melt handles dates and times by reviewing the Melt dates documentation.

  3. Understand Scroll Area anatomy

    develop

    The Scroll Area component consists of several structural parts that you can customize or style:

    • Root: The main container wrapping all parts.
    • Viewport: The container wrapping the scrollable content.
    • Content: The actual scrollable content.
    • Corner: The element displayed at the intersection when both X and Y scrollbars are visible.
    • ScrollbarX: The track for the horizontal scrollbar.
    • ThumbX: The draggable/movable thumb for the horizontal scrollbar.
    • ScrollbarY: The track for the vertical scrollbar.
    • ThumbY: The draggable/movable thumb for the vertical scrollbar.
  4. Context Menu Anatomy

    develop

    The Context Menu builder consists of the following structural components:

    • Trigger: The element which when right clicked, opens the context menu.
    • Menu: The root container for the popover menu.
    • Item: A menuitem which can be a link or a function.
    • Checkbox Item: A menu item which can be checked or unchecked.
    • Radio Group: A group of radio items.
    • Radio Item: A menu item which can be selected from a group of items.
    • Sub Trigger: A button which toggles the submenu's open state.
    • Sub Menu: A menu which is nested inside another menu.
    • Separator: A visual divider between menu items.
    • Arrow: An optional element used to render a pointer towards the trigger.
  5. Menubar Anatomy and Components

    develop

    A Menubar is composed of the following structural elements:

    • Trigger: The button that toggles the menu's open state.
    • Menu: The root container for the popover menu.
    • Item: A menu item that can function as a link or a trigger for a function.
    • Checkbox Item: A menu item that can be toggled between checked and unchecked states.
    • Radio Group: A container for a group of radio items.
    • Radio Item: A menu item that can be selected from a group of radio items.
    • Sub Trigger: A button within a menu that toggles a submenu's open state.
    • Sub Menu: A menu nested inside another menu.
    • Separator: A visual divider used to group menu items.
  6. Use the Combobox builder

    develop

    The Combobox is an input with an associated popup containing a list of options, commonly used for autocomplete functionality and command palettes.

    Anatomy

    • Input: Opens, closes, filters the list, and displays the selected value.
    • Menu: The popover menu containing the options.
    • Option: Individual combobox items.
    • Label: The label for the input.
    • Arrow: An optional element pointing to the menu's input.
    • Group: Optional elements used to group options together.
  7. Understand the Melt UI Builder API

    develop

    Melt UI is a headless UI library that provides builders instead of pre-styled components. A builder is a function that returns a collection of properties (elements and states) which you can apply to any HTML element or Svelte component using the use:melt action.

    Key concepts:

    • Builders: Functions like createCollapsible that generate logic and attributes.
    • Elements: Objects (e.g., root, trigger, content) that contain the necessary attributes and event handlers.
    • States: Reactive stores (e.g., open) that allow you to track and react to the component's internal state.
    • use:melt: A Svelte action used to bind the builder's properties to your elements.
    <script>
    	import { createCollapsible, melt } from '@melt-ui/svelte'
    	const {
    		elements: { root, content, trigger },
    		states: { open }
    	} = createCollapsible()
    </script>
    
    <div use:melt={$root}>
    	<button use:melt={$trigger}>{$open ? 'Close' : 'Open'}</button>
    	<div use:melt={$content}>Obi-Wan says: Hello there!</div
    </div>
  8. Quickstart with Melt UI builders

    develop

    Melt UI uses component builders to manage state and accessibility. You can use the melt action to apply builder elements to Svelte elements. A builder typically returns elements (stores that return attributes/properties for elements) and states (stores representing the component's state).

    <script>
    	import { createCollapsible, melt } from '@melt-ui/svelte'
    	const {
    		elements: { root, content, trigger },
    		states: { open }
    	} = createCollapsible()
    </script>
    
    <div use:melt={$root}>
    	<button use:melt={$trigger}>{$open ? 'Close' : 'Open'}</button>
    	<div use:melt={$content}>Obi-Wan says: Hello there!</div
    </div>
  9. Enable Modal Behavior for Context Menus

    develop

    By default, context menus do not block interaction with the rest of the page. To implement modal behavior—where an overlay is rendered behind the menu to prevent interaction with the rest of the page while the menu is open—include the overlay element from the createContextMenu builder.

    <script lang="ts">
    	import { createContextMenu, melt } from '@melt-ui/svelte'
    	const {
    		elements: { trigger, menu, item, separator, arrow, overlay }
    	} = createContextMenu()
    </script>
    
    <div use:melt={$trigger}>Right click to open</div>
    <div use:melt={$overlay} />
    
    <div use:melt={$menu}>
    	<div use:melt={$arrow} />
    	<div use:melt={$item}>Menu Item</div>
    </div>