All Projects → tal1992 → svelte-webcomponents

tal1992 / svelte-webcomponents

Licence: MIT License
A ready-to-use project template to build custom elements (web components) with Svelte 3 with support and examples for web components, jest, sass, nested components with props, eslinting, stylelinting, Github actions, propagating custom events from shadow-DOM to real-DOM etc.

Programming Languages

javascript
184084 projects - #8 most used programming language
Svelte
593 projects
HTML
75241 projects

Projects that are alternatives of or similar to svelte-webcomponents

svelte-headlessui
Unofficial Svelte port of Headless UI components
Stars: ✭ 564 (+2463.64%)
Mutual labels:  svelte, sveltejs, svelte3
grid-container
A grid for the future, CSS Grid Layout + Web Components (Custom Elements v1 + Shadow DOM v1)
Stars: ✭ 51 (+131.82%)
Mutual labels:  web-components, custom-elements, shadow-dom
Shadow Dom In Depth
Everything you need to know about Shadow DOM
Stars: ✭ 191 (+768.18%)
Mutual labels:  web-components, custom-elements, shadow-dom
Nutmeg
Build, test, and publish vanilla Web Components with a little spice
Stars: ✭ 111 (+404.55%)
Mutual labels:  web-components, custom-elements, shadow-dom
s-date-range-picker
📅 A date range picker built with Svelte
Stars: ✭ 13 (-40.91%)
Mutual labels:  svelte, sveltejs, svelte3
Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+55140.91%)
Mutual labels:  web-components, custom-elements, shadow-dom
Ion Phaser
A web component to use Phaser Framework with Angular, React, Vue, etc 🎮
Stars: ✭ 152 (+590.91%)
Mutual labels:  web-component, web-components, custom-elements
Svelte Material Ui
Svelte Material UI Components
Stars: ✭ 2,081 (+9359.09%)
Mutual labels:  svelte, sveltejs, svelte3
focus-trap
A lightweight web component that traps focus within a DOM node
Stars: ✭ 44 (+100%)
Mutual labels:  web-components, custom-elements, shadow-dom
svelte-portal
Svelte component for rendering outside the DOM of parent component
Stars: ✭ 261 (+1086.36%)
Mutual labels:  svelte, sveltejs, svelte3
Custom Elements Ts
Create native custom elements using Typescript
Stars: ✭ 52 (+136.36%)
Mutual labels:  web-components, custom-elements, shadow-dom
svelte-starter-kit
Svelte with brilliant bells and useful whistles
Stars: ✭ 384 (+1645.45%)
Mutual labels:  svelte, sveltejs, svelte3
Xy Ui
🎨面向未来的原生 web components UI组件库
Stars: ✭ 603 (+2640.91%)
Mutual labels:  web-components, custom-elements, shadow-dom
cwco
Powerful and Fast Web Component Library with a Simple API
Stars: ✭ 27 (+22.73%)
Mutual labels:  web-component, web-components, custom-elements
Remount
Mount React components to the DOM using custom elements
Stars: ✭ 522 (+2272.73%)
Mutual labels:  web-components, custom-elements, shadow-dom
Use Custom Element
Custom hook to bridge Custom Elements (Web Components) to React.
Stars: ✭ 77 (+250%)
Mutual labels:  web-component, web-components, custom-elements
svelte-interview-questions
Concepts and Questions related to Svelte - Part of official Svelte resources list
Stars: ✭ 18 (-18.18%)
Mutual labels:  svelte, sveltejs, svelte3
ui-svelte
A component library for Svelte
Stars: ✭ 18 (-18.18%)
Mutual labels:  svelte, sveltejs, svelte3
element
Fast and simple custom elements.
Stars: ✭ 65 (+195.45%)
Mutual labels:  web-components, custom-elements, shadow-dom
svelte-accessible-dialog
An accessible dialog component for Svelte apps
Stars: ✭ 24 (+9.09%)
Mutual labels:  svelte, sveltejs, svelte3
svelte logo

svelte-webcomponents

Website v1.0.0 License: MIT Powered By Svelte Language: Javascript Tweet

If you’re using web-components or if you like the project, please this repository to show your support! 🤩

The world's most easiest, ready to use template for web-components.

About 📚

A ready to use project template to build custom elements (web components) with Svelte 3 with support and examples for web components, jest, sass, nested components with props, eslinting, stylelinting, Github actions and custom events from shadow-DOM to real-DOM etc.

Installation 🚀

Run below command inside the working folder

$ git clone https://github.com/tal1992/svelte-webcomponents.git
** or **
$ npx degit tal1992/svelte-webcomponents

Installation and build 📌

📦 $ npm install 

🔨 developer mode -> $ npm run dev
🔨  production mode -> $ npm run build

Using web-components in HTML 📌

  <component-name propOne="Lorem" propTwo="Ipsum"></component-name>

Using web-components as a widget 📌

function addScript(src) {
  var s = document.createElement("script");
  s.setAttribute("src", src);
  document.querySelector("body").appendChild(s);
}
//replace the url with your hosted path of bundle.js
addScript("https://loremipsumdolarsir/build/bundle.js", "", "");

Now that your bundle.js file is included in the html , we can use the web components.

      let foo = document.createElement('component-foo');
      let header = document.getElementByTagName('header');
      foo.setAttribute('propOne', "lorem");
      foo.setAttribute('propTwo', "Ipsum");
      // please replace header with the element where you want to add your custom element.
      header.parentNode.replaceChild(foo, header);

Nested Custom Elements 📌

Register your custom-element inside App.svelte

App.svelte
 import foo from './foo.svelte';
 import bar from './bar.svelte';

No need to import the custom element inside parent declared component, use custom tag names while nesting.

Parent.svelte
<svelte:options tag="component-parent"></svelte:options>

<div class="parent">
    <component-foo name="John" background="orange"></component-foo>
    <component-bar name="Doe" background="lightgreen"></component-bar>
</div>

Writing SCSS inside svelte 📌

This template comes with in-built support for scss.

foo.svelte
<style lang="scss">
    h2 {
        padding: 20px;
    }
</style>

Test cases 📌

Write test cases inside __tests __ folder

Note : Dont treat webcomponents as a special case for testing, they should be tested as normal svelte components.

import { render } from "@testing-library/svelte";
import App from "../src/App.svelte";

describe("App component", () => {
  test("should render component correctly", () => {
    const { container } = render(App);

    expect(container).toContainHTML("<body><div><h1>Hello from svelte</h1></div></body>");
  });
});

Use normal component name and not the webcomponent name in the test case.

$ npm run test

ESLINT 📌

$ npm run lintjs

Style lint 📌

$ npm run lintcss

Event propagation from Shadow DOM to Real DOM 📌

Foo.svelte (web component)

<script>
    import { get_current_component } from "svelte/internal";
    const thisComponent = get_current_component();

    const dispatchEvent = (name, detail) => {
        thisComponent.dispatchEvent(new CustomEvent(name, {
        detail,
        composed: true, // propagate to the Real DOM, handled in index.html
        }));
    };

    function handleClick(event) {
       event.preventDefault();
       dispatchEvent("customclick", name)
    }    
</script>

<svelte:options tag="component-foo"></svelte:options>

<button on:click={event => handleClick(event)}>Click me</button>

Inside Real DOM

<script>
	window.onload = function () {
	  let myelem = document.querySelectorAll('component-foo');

	  myelem.forEach(function (elem) {
	    elem.addEventListener('customclick', (e) => {
	      alert(e.detail + ' clicked');
	    });
	  });
	};
</script>

Developer

Linkedin Twitter Email

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].