Svelte Custom Element Template screenshot

Svelte Custom Element Template

Author Avatar Theme by Dan6erbond
Updated: 27 Jul 2021
5 Stars

Svelte custom elements (WebComponent) template with Prettier and ESLint!

Overview

The Svelte Custom Element Template is a project template designed for creating Svelte custom elements. It provides the necessary files and structure to quickly get started with building custom elements using Svelte. The template includes features such as reactivity, slots, CSS scoping, event emitting and listening, as well as helpful code comments for understanding the concepts behind building Web Components.

Features

  • Svelte custom elements with reactivity, slots, and CSS: The template provides the necessary setup to create custom elements using Svelte with support for reactivity, slots, and CSS styling.
  • Nested custom elements with shadow DOM and CSS scoping: In addition to basic custom elements, the template also showcases the usage of nested custom elements with shadow DOM and CSS scoping.
  • Event emitting and event listening in custom elements and index.html: The template includes examples of how to emit and listen to custom events within custom elements as well as in the main index.html file.
  • Using reserved keywords in Svelte script tags: The template demonstrates how to use reserved keywords in Svelte script tags, allowing for more flexibility and control in custom element development.

Installation

To create a new project based on this template, follow these steps:

  1. Clone the repository: git clone https://github.com/Dan6erbond/svelte-custom-element-template
  2. Make sure you have Node.js and Yarn installed on your machine.
  3. Install dependencies by running yarn install in the project directory.
  4. Run the development server by running yarn dev in the project directory.
  5. Open http://localhost:5000 in your browser to see the sample page with a “Hello, world!” message and a counter.
  6. Edit a component file in the src directory, and the page in the browser will automatically update thanks to Rollup’s livereload plugin.

By default, the server only responds to requests from localhost. If you want to allow connections from other computers, you can edit the srv commands in the package.json file to include the --host 0.0.0.0 option, or add the option when running the development server.

Summary

The Svelte Custom Element Template is a useful starting point for creating custom elements in Svelte. It provides essential features such as reactivity, slots, CSS scoping, event handling, and code comments to assist in the development of Web Components. By following the installation guide, developers can get started quickly and easily with building custom elements using Svelte.