Stencil State Tunnel

This project was built to provide additional functionality to developers working with stencil components and sharing state across components and through slots. The API was inspired by React's Context.

There are many cases within component based application development where you will want to drill state down through props to children and children's children and so on. This project was created in order to alleviate the need for passing props deep through an application's component structure.

There are 2 primary use cases where this tool makes sense.

Building a stencil application and need to store application state in a centralized location.

Building a collection of components that need to share state but have no direct 'line of sight' between the component relationships (ie most likely from using slot).

  1. Create a Tunnel.
  2. Pass state to the Tunnel's Provider Component.
  3. Place your state consumption points.

1. Create a tunnel.

Define the structure of data to be passed through the tunnel. The first argument of createProviderConsumer is a default value. The first argument is the default value and the second argument is how the consumer works. Mostly you will just need to copy and paste this. 'context-consumer' is a component that is included in this package.

// Filename: './data/message.tsx'

import { createProviderConsumer } from '@stencil/state-tunnel';

export interface State {
  message: string,
  increment?: () => void

export default createProviderConsumer<State>({
    message: 'Hello!'
  (subscribe, child) => (
    <context-consumer subscribe={subscribe} renderer={child} />

2. Pass state to the tunnel Provider Component.

Usually this will be at the top of your component tree. Think of this as a way to take state from the parent component and then make it available to components further down the tree. That is essentially what the Provider Component does.

// Filename: './components/my-app.tsx'

import Tunnel from './data/message'; // Import the Tunnel

  tag: 'my-app',
export class MyApp {

  @Prop() intro: string = 'Hello!';
  @State() message: string = '';

  count: number = 0;

  componentWillLoad() {

  increment = () => {
    this.count = this.count + 1;
    this.message = `${this.intro} ${this.count}`;

  render() {
    const tunnelState = {
      message: this.message,
      increment: this.increment
    return (
          <h1>Stencil App Starter</h1>
        <Tunnel.Provider state={tunnelState}>

3. Place your state consumption points.

You can then create an exit point anywhere within your component tree that lives below the Tunnel.Provider. In this case we are accessing the state using a Consumer Component. There are other approaches to consuming the information but in most cases within an application you will use a Consumer Component.

// Filename: './components/way-down-child.tsx'

import { Component } from '@stencil/core';
import Tunnel from './data/message'; // Import the tunnel

  tag: 'way-down-child',
export class WayDownChild {
  render() {
    return (
        {({ message, increment }) => (
          <div class='app-profile'>
            <button onClick={increment}>Increment Num</button>
Back Next