Skip to main content
Light Dark System
Get ready for more awesome! Web Awesome, the next iteration of Shoelace, is on Kickstarter. Read Our Story

Svelte

Svelte plays nice with custom elements, so you can use Shoelace in your Svelte apps with ease.

Installation

To add Shoelace to your Svelte app, install the package from npm.

npm install @shoelace-style/shoelace

Next, include a theme and set the base path for icons and other assets. In this example, we’ll import the light theme and use the CDN as a base path.

// main.js or main.ts
import '@shoelace-style/shoelace/dist/themes/light.css';
import { setBasePath } from '@shoelace-style/shoelace/dist/utilities/base-path';

setBasePath('https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.19.1/cdn/');

Usage

QR code generator example

<h1>Live editing</h1>

<sl-input label="Message" value={message} oninput={event => message = event.target.value}></sl-input>

<sl-alert open>
  <sl-icon slot="icon" name="info-circle"></sl-icon>
  {message}
</sl-alert>

<script>
  import '@shoelace-style/shoelace/dist/components/alert/alert.js'
  import '@shoelace-style/shoelace/dist/components/input/input.js';

  let message = $state('')
</script>

Two-way Binding

One caveat is there’s currently Svelte only supports bind:value directive in <input>, <textarea> and <select>, but you can still achieve two-way binding manually.

// This doesn't work
<sl-input bind:value="name"></sl-input>
// This works, but it's a bit longer
<sl-input value={name} oninput={event => message = event.target.value}></sl-input>

Slots

Slots in Shoelace/web components are functionally the same as basic slots in Svelte. Slots can be assigned to elements using the slot attribute followed by the name of the slot it is being assigned to.

Here is an example:

<sl-drawer label="Drawer" placement="start" class="drawer-placement-start" bind:open={drawerIsOpen}>
  This drawer slides in from the start.
  <div slot="footer">
    <sl-button variant="primary" onclick={() => (drawerIsOpen = false)}>
      Close
    </sl-button>
  </div>
</sl-drawer>