npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte_webcomponents

v1.1.0

Published

Boilerplate for developing web components in svelte, using nested components, passing props, sass and jest setup, eslint and style-lint etc

Downloads

4

Readme

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

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