--- meta: title: Usage description: Learn more about using custom elements. --- # Usage Shoelace components are just regular HTML elements, or [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) to be precise. You can use them like any other element. Each component has detailed documentation that describes its full API, including properties, events, methods, and more. If you're new to custom elements, often referred to as "web components," this section will familiarize you with how to use them. ## Attributes & Properties Many components have properties that can be set using attributes. For example, buttons accept a `size` attribute that maps to the `size` property which dictates the button's size. ```html Click me ``` Some properties are boolean, so they only have true/false values. To activate a boolean property, add the corresponding attribute without a value. ```html Click me ``` In rare cases, a property may require an array, an object, or a function. For example, to customize the color picker's list of preset swatches, you set the `swatches` property to an array of colors. This must be done with JavaScript. ```html ``` Refer to a component's documentation for a complete list of its properties. ## Events You can listen for standard events such as `click`, `mouseover`, etc. as you normally would. However, it's important to note that many events emitted within a component's shadow root will be [retargeted](https://dom.spec.whatwg.org/#retarget) to the host element. This may result in, for example, multiple `click` handlers executing even if the user clicks just once. Furthermore, `event.target` will point to the host element, making things even more confusing. As a result, you should almost always listen for custom events instead. For example, instead of listening to `click` to determine when an `` gets toggled, listen to `sl-change`. ```html Check me ``` All custom events are prefixed with `sl-` to prevent collisions with standard events and other libraries. Refer to a component's documentation for a complete list of its custom events. ## Methods Some components have methods you can call to trigger various behaviors. For example, you can set focus on a Shoelace input using the `focus()` method. ```html ``` Refer to a component's documentation for a complete list of its methods and their arguments. ## Slots Many components use slots to accept content inside of them. The most common slot is the _default_ slot, which includes any content inside the component that doesn't have a `slot` attribute. For example, a button's default slot is used to populate its label. ```html Click me ``` Some components also have _named_ slots. A named slot can be populated by adding a child element with the appropriate `slot` attribute. Notice how the icon below has the `slot="prefix"` attribute? This tells the component to place the icon into its `prefix` slot. ```html Settings ``` The location of a named slot doesn't matter. You can put it anywhere inside the component and the browser will move it to the right place automatically! Refer to a component's documentation for a complete list of available slots. ## Don't Use Self-closing Tags Custom elements cannot have self-closing tags. Similar to ` ``` ## Component Rendering and Updating Shoelace components are built with [Lit](https://lit.dev/), a tiny library that makes authoring custom elements easier, more maintainable, and a lot of fun! As a Shoelace user, here is some helpful information about rendering and updating you should probably be aware of. To optimize performance and reduce re-renders, Lit batches component updates. This means changing multiple attributes or properties at the same time will result in just a single re-render. In most cases, this isn't an issue, but there may be times you'll need to wait for the component to update before continuing. Consider this example. We're going to change the `checked` property of the checkbox and observe its corresponding `checked` attribute, which happens to reflect. ```js const checkbox = document.querySelector('sl-checkbox'); checkbox.checked = true; console.log(checkbox.hasAttribute('checked')); // false ``` Most developers will expect this to be `true` instead of `false`, but the component hasn't had a chance to re-render yet so the attribute doesn't exist when `hasAttribute()` is called. Since changes are batched, we need to wait for the update before proceeding. This can be done using the `updateComplete` property, which is available on all Lit-based components. ```js const checkbox = document.querySelector('sl-checkbox'); checkbox.checked = true; checkbox.updateComplete.then(() => { console.log(checkbox.hasAttribute('checked')); // true }); ``` This time we see an empty string, which means the boolean attribute is now present! :::tip Avoid using `setTimeout()` or `requestAnimationFrame()` in situations like this. They might work, but it's far more reliable to use `updateComplete` instead. ::: ## Code Completion ### VS Code Shoelace ships with a file called `vscode.html-custom-data.json` that can be used to describe it's custom elements to Visual Studio Code. This enables code completion for Shoelace components (also known as "code hinting" or "IntelliSense"). To enable it, you need to tell VS Code where the file is. 1. [Install Shoelace locally](/getting-started/installation#local-installation) 2. If it doesn't already exist, create a folder called `.vscode` at the root of your project 3. If it doesn't already exist, create a file inside that folder called `settings.json` 4. Add the following to the file ```js { "html.customData": ["./node_modules/@shoelace-style/shoelace/dist/vscode.html-custom-data.json"] } ``` If `settings.json` already exists, simply add the above line to the root of the object. Note that you may need to restart VS Code for the changes to take affect. ### JetBrains IDEs If you are using a [JetBrains IDE](https://www.jetbrains.com/) and you are installing Shoelace from NPM, the editor will automatically detect the `web-types.json` file from the package and you should immediately see component information in your editor. If you are installing from the CDN, you can [download a local copy](https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace/cdn/web-types.json) and add it to the root of your project. ### Other Editors Most popular editors support custom code completion with a bit of configuration. Please [submit a feature request](https://github.com/shoelace-style/shoelace/issues/new/choose) for your editor of choice. PRs are also welcome!