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

stencil-wormhole

v3.4.1

Published

Pass props down component trees easily via wormholes.

Downloads

17,154

Readme

stencil-wormhole

package-badge size-badge license-badge semantic-release-badge

Introduction

This is a super simple and lightweight library, that helps pass props down Stencil component trees easily. It's similar to React.Context and stencil-state-tunnel.

Why not just use stencil-state-tunnel? Simply because it's not instance scoped at this time (issue #8). In addition, this library prefers injecting props instead of consuming them in JSX because:

  1. Leads to better component design as you're aware of all the props required by the component.
  2. Much better testability as you don't have to mock out some part of the component tree when testing state changes. You simply pass in props the same way they're injected.

There's only two concepts to learn for this library:

  • Universe. This is like a Context.Provider in react and Tunnel.Provider in stencil-state-tunnel. It holds the current state of the sub-tree, and it's responsible for updating all of its children when the state changes.
  • Wormhole. This is like Context.Consumer in react and Tunnel.injectProps in stencil-state-tunnel. It simply opens a connection to its closest ancestor universe and requests props to be injected.

This is a simple diagram on what this library achieves:

  • Universe
    • Child A
    • Child B
      • Child C (Open wormhole here and inject props)

This is a simple diagram on how a multiverse works, in which a universe's nested inside another one:

  • Universe A
    • Child A (Bound to state of Universe A)
    • Child B (Bound to state of Universe A)
    • Child C (Assume this is Universe B)
      • Child D (Bound to state of Universe B)
      • Child E (Bound to state of Universe B)

Important to note, you can only nest universes if they live inside separate components.

Guide

Install

# npm
$: npm install stencil-wormhole

# yarn
$: yarn add stencil-wormhole

# pnpm
$: pnpm install stencil-wormhole

Create Universe

import { h, State, Component } from '@stencil/core'
import { Universe } from 'stencil-wormhole'

@Component({
    tag: 'my-parent'
})
export class MyParent {
    // 1. Setup your state.
    @State() state: Record<string, any> = {
        message: 'apples',
        data: { content: 1 },
        // ...
    };

    componentWillLoad() {
        // 2. Create the universe (it has to be called in this lifecycle method).
        Universe.create(this, this.state);
    }   

    // 3. Update your state as usual.

    render() {
        return (
          // 4. Create the universe provider.
          <Universe.Provider state={this.state}>
            <my-child />
          </Universe.Provider>
        );   
    }
}

Open Wormhole

import { h, Prop, Component } from '@stencil/core'
import { openWormhole } from 'stencil-wormhole'

@Component({
    tag: 'my-child'
})
export class MyChild {
    // 1. Setup all props that are being injected.
    @Prop() message!: string;
    @Prop() data!: object;

    render() {
        return (
            <div>{this.message}</div>
        );   
    }
}

// 2. Open the wormhole and pass in the props to be injected.
openWormhole(MyChild, ['message', 'data']);

If you want stricter typing on the openWormhole function then simply create a higher-order function.

import { openWormhole, WormholeConsumerConstructor } from 'stencil-wormhole'

interface SpecialProps {
    apples: string
}

export const openSpecialWormhole = (
    Component: WormholeConsumerConstructor, 
    props: (keyof SpecialProps)[]
) => openWormhole(Component, props);