Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  • Importing and Setting Up
  • Creating the Accordion Structure
  • Key Concepts
  • Minimal Working Example
  1. Installing Useful Libraries
  2. SvelteKit Melt-ui

First Component - accordion

Certainly! I'll explain the provided code in great detail, following the guidelines you've outlined. Let's start by showing the original code and then break it down step by step.

<script>
  import { createAccordion, melt } from '@melt-ui/svelte';
  const { elements: { root, item, trigger, content }, helpers: { isSelected } } = createAccordion();
</script>

<div use:melt={$root}>
  <div use:melt={$item('item-1')}>
    <h2><button use:melt={$trigger('item-1')}>Title</button></h2>
    {#if $isSelected('item-1')}
      <div use:melt={$content('item-1')}>Content</div>
    {/if}
  </div>
</div>

Now, let's break down this code and explain it in detail:

Importing and Setting Up

<script>
  import { createAccordion, melt } from '@melt-ui/svelte';
  const { elements: { root, item, trigger, content }, helpers: { isSelected } } = createAccordion();
</script>

This section sets up the necessary components for creating an accordion using the Melt UI library in Svelte.

  1. We import two functions from '@melt-ui/svelte':

    • createAccordion: This function creates the accordion functionality.

    • melt: This is a utility function used to apply Melt UI behavior to elements.

  2. We call createAccordion() and use destructuring to extract several properties:

    • elements: Contains references to different parts of the accordion (root, item, trigger, content).

    • helpers: Provides utility functions, in this case, isSelected.

The createAccordion() function returns an object with various properties and methods to manage the accordion's state and behavior.

Creating the Accordion Structure

<div use:melt={$root}>
  <div use:melt={$item('item-1')}>
    <h2><button use:melt={$trigger('item-1')}>Title</button></h2>
    {#if $isSelected('item-1')}
      <div use:melt={$content('item-1')}>Content</div>
    {/if}
  </div>
</div>

This section creates the HTML structure for the accordion:

  1. The outermost <div> uses use:melt={$root} to apply the root accordion behavior.

  2. Inside, we have another <div> that represents an accordion item:

    • use:melt={$item('item-1')} applies item-specific behavior and identifies this as 'item-1'.

  3. The accordion item contains:

    • A <h2> with a <button> inside. This button acts as the trigger to expand/collapse the item.

    • use:melt={$trigger('item-1')} applies trigger behavior to the button for 'item-1'.

  4. The content of the accordion item is conditionally rendered:

    • {#if $isSelected('item-1')} checks if 'item-1' is currently selected (expanded).

    • If true, it renders a <div> with the content.

    • use:melt={$content('item-1')} applies content-specific behavior to this div.

The use:melt directive is a Svelte action that applies Melt UI's behavior to HTML elements. It connects the DOM elements to the accordion's logic.

Key Concepts

  1. Reactive Statements: The $ prefix (e.g., $root, $isSelected) indicates these are reactive statements in Svelte. They will automatically update when their underlying values change.

  2. Conditional Rendering: The {#if} block ensures that the content is only rendered when the item is selected, improving performance by not rendering hidden content.

  3. Accessibility: Melt UI handles much of the accessibility concerns for accordions, such as proper ARIA attributes and keyboard navigation.

This example shows only one accordion item. In a real-world scenario, you'd likely have multiple items, each with a unique identifier.

Minimal Working Example

Here's a minimal working example of a simple accordion with two items:

<script>
  import { createAccordion, melt } from '@melt-ui/svelte';
  const { elements: { root, item, trigger, content }, helpers: { isSelected } } = createAccordion();
</script>

<div use:melt={$root}>
  {#each ['item-1', 'item-2'] as id}
    <div use:melt={$item(id)}>
      <button use:melt={$trigger(id)}>Item {id.slice(-1)}</button>
      {#if $isSelected(id)}
        <div use:melt={$content(id)}>Content for item {id.slice(-1)}</div>
      {/if}
    </div>
  {/each}
</div>

This example demonstrates how to create a simple accordion with two items using a loop. It showcases the core functionality while keeping the code concise and easy to understand.

PreviousSvelteKit Melt-uiNextInstalling Shadcn/ui Svelte

Last updated 9 months ago