Shoelace Web Component Library

repository·next·Indexed 11 days ago

https://github.com/shoelace-style/shoelace

A library of framework-agnostic, accessible, and CSS-customizable web components. Note: Shoelace is currently sunset and has been succeeded by Web Awesome.

Tokens
102.8K
Snippets
439
Records
480
Agent score
95%

What's inside Shoelace

  1. What is Shoelace and why use it?

    next

    Shoelace is a library of framework-agnostic web components. Because it is built on web standards, it provides several advantages over framework-specific component libraries:

    • Framework Agnostic: Works with React, Vue, Angular, or no framework at all.
    • Longevity: Since it uses standard Web Components, it is not tied to the lifecycle of a specific framework version.
    • Customizable: Fully stylable with CSS.
    • Interoperability: You can build multiple apps using different frameworks that all share the same UI components.
    • Accessibility: Built with accessibility in mind.
  2. Overview of Shoelace features

    next

    Shoelace is a library of web components designed with the following characteristics:

    • Framework Agnostic: Works with all frontend frameworks.
    • CDN Support: Can be loaded via CDNs.
    • Customizable: Fully customizable using CSS.
    • Theming: Includes an official dark theme.
    • Accessible: Built with accessibility in mind.
    • Open Source: Available under the MIT license.
  3. Use the Card component

    next

    The sl-card component is used to group related subjects in a container. It supports several slots to customize its layout:

    • Default slot: Used for the main body content of the card.
    • slot="header": Used to display titles or controls at the top of the card.
    • slot="footer": Used to display actions, summaries, or other relevant content at the bottom.
    • slot="image": Used to display an image at the top of the card that stretches to fit the width.
    <sl-card>
      <div slot="header">Header Content</div>
      Main body content goes here.
      <div slot="footer">Footer Content</div>
    </sl-card>
  4. Important: Shoelace is sunset

    next

    Shoelace is sunset

    Shoelace is no longer under active development. While the library remains available under the MIT license for existing use, all new development, bug reports, feature requests, and pull requests should be directed to Web Awesome, the successor project.

    • Do not open issues or pull requests here.
    • Migration: If you are currently using Shoelace, follow the migration guide to move to Web Awesome.
    • Successor Project: Web Awesome provides an expanded library of components, themes, utilities, and patterns.
  5. What is the Popup component and when to use it

    next

    The <sl-popup> component is a low-level positioning utility that allows you to declaratively anchor a container to another element. It uses Floating UI under the hood to handle positioning for tooltips, dropdowns, and other floating elements.

    Important Considerations:

    • No Styles: The component provides positioning logic only; it does not provide any default styles for the popup content.
    • Not an Accessible Component: Unlike the Tooltip component, <sl-popup> does not handle accessibility (ARIA roles, keyboard interactions, etc.) automatically. It is intended to be a building block for higher-level, accessible components.
    • Anchor Styling: Do not use display: contents on the anchor element, as this prevents the component from calculating correct coordinates. If the anchor is a <slot>, the component will automatically use the first assigned element as the anchor.
  6. Prevent Tooltip clipping with the hoist attribute

    next

    If a tooltip is inside a container with overflow: auto, overflow: hidden, or overflow: scroll, it may be clipped.

    Adding the hoist attribute forces the tooltip to use a fixed positioning strategy. This allows the tooltip to break out of the container and position itself relative to its containing block (usually the viewport).

    <div style="overflow: hidden;">
      <sl-tooltip content="I am not clipped" hoist>
        <sl-button>Hover Me</sl-button>
      </sl-tooltip>
    </div>
  7. Customize the Toast Stack position

    next

    The toast stack is a singleton element managed internally by the alert component. It stacks toasts vertically and is positioned at the top-right of the viewport by default.

    You can change the position of all toasts by targeting the .sl-toast-stack class in your CSS. For example, to move the stack to the top-left:

    .sl-toast-stack {
      left: 0;
      right: auto;
    }

    Note: It is not possible to show toasts in more than one stack simultaneously.

  8. Prevent clipping with the `shift` attribute

    next

    The shift attribute prevents a popup from being clipped when it is longer than its anchor element. When enabled, the popup will shift along its axis to stay within view.

    You can customize this behavior using:

    • shift-padding: Sets the padding for the shift operation.
    • shiftBoundary: Defines the boundary for the shift behavior.
    <sl-popup placement="top" shift shift-padding="10" active>
      <span slot="anchor"></span>
      <div class="box"></div>
    </sl-popup>
  9. How Shoelace form controls interact with native forms

    next

    Shoelace components use Shadow DOM to encapsulate markup and styles. Because native <form> elements cannot see inside a shadow root, they typically cannot recognize form controls located there.

    Shoelace overcomes this by intercepting the formdata and submit events. When a form is submitted, Shoelace automatically injects the values of its form controls into the FormData object.

    Important Integration Note: If you are attaching your own event listeners to a <form> (e.g., for custom submission logic), you must attach them after the Shoelace form controls have been connected to the DOM. If you attach them too early, your logic may execute before Shoelace has had the chance to inject the form data or trigger validation.

  10. Use Default and Named Slots

    next

    Shoelace components use slots to allow you to pass content into specific areas of the component.

    • Default Slot: Content placed directly inside the component tags without a slot attribute. Used for things like button labels.
    • Named Slots: Content assigned to a specific location using the slot attribute (e.g., slot="prefix"). The browser automatically moves these elements to the correct position within the component's shadow DOM.
    <!-- Default slot usage -->
    <sl-button>Click me</sl-button>
    
    <!-- Named slot usage -->
    <sl-button>
      <sl-icon slot="prefix" name="gear"></sl-icon>
      Settings
    </sl-button>
  11. Use the Hover Bridge in SlPopup

    next

    When there is a gap between the anchor element and the popup, you can enable the hover-bridge option. This adds an invisible element that fills the gap, preventing mouseover or mouseout events from triggering prematurely when the user moves the pointer between the anchor and the popup. The bridge is only drawn when the popover is active and can be styled using the hover-bridge part.

    Key properties:

    • hover-bridge: Boolean. Enables the bridge.
    • distance: Number. The gap size between the anchor and the popup.
    • skidding: Number. Adjusts the offset/positioning of the popup relative to the anchor.
    <sl-popup 
      placement="top" 
      hover-bridge 
      distance="10" 
      skidding="0" 
      active
    >
      <span slot="anchor"></span>
      <div class="box"></div>
    </sl-popup>
    
    <style>
      /* Style the bridge using the hover-bridge part */
      sl-popup::part(hover-bridge) {
        background: tomato;
        opacity: 0.5;
      }
    </style>