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

wx-4-custom-elements

v0.0.1

Published

Svelte 4 supports [custom elements](https://svelte.dev/docs/custom-elements-api) but there are a few tricky bits that can make life a little difficult 😠. This project shows some solutions to common problems.

Downloads

21

Readme

Svelte custom elements starter project

Svelte 4 supports custom elements but there are a few tricky bits that can make life a little difficult 😠. This project shows some solutions to common problems.

Problem 1: Components ignore global css

Custom elements can be used with or without the shadow dom. With the shadow dom, components are isolated and cannot by styled from the page's css. Without the shadow dom, components cannot use slots, a very important feature that you likely depend on.

One solution is to use <link> tags within your component but this results in a flash of unstyled content

The solution is to use constructable stylesheets and attached them to the shadowRoot of the element. Now, you get the best of both worlds - global styles and slots.

Problem 2: Too much boilerplate

The official svelte docs recommend you place the following in your main.js / main.ts:

import MyElement from './MyElement.svelte';
customElements.define('my-element', MyElement.element);

What if you have lots and lots of components? You would have to repeat this many times. Probably not a massive issue but we can do better. If you are using Vite, use its glob import feature to remove the repetition:

const modules = import.meta.glob('./lib/**/*.svelte', {eager: true})
for (const path in modules) {
    const mod = modules[path] as SvelteModule
    const el = mod.default.element

    // Determine component name from file name.
    const filename = path.split('/').pop()
    const componentName = filename.slice(0,-7)

    // Register custom element.
    customElements.define(`${prefix}-${componentName}`, el)
}