Code Monkey home page Code Monkey logo

context-provider's Introduction

Context Provider & Consumer for Web Components

npm version Build Status Commitizen friendly semantic-release codecov Dependency status

Allows to use context Provider and Consumer components in a similar manner than the Context in React.

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

This can be useful for properties or state that is considered global to an application like the language, user information or media query breakpoints.

The value of the nearest Provider component is available to Consumer components in their context property regardless the level of nesting without the need to pass properties from parents to children.

Usage

Install

npm i @kuscamara/context-provider

Create a Provider

import { createContextProvider } from '@kuscamara/context-provider';

const globals = /* App globals */;
const ContextProvider = createContextProvider(globals);

window.customElements.define('context-provider', ContextProvider);

Add Consumer capabilities to a component

import { contextConsumerMixin } from '@kuscamara/context-provider';

class MyComponent extends contextConsumerMixin(HTMLElement) {
  render() {
    // do whatever you need with your context property
    return `<p>My context is ${this.context}</p>`;
  }
}

Set and change the Provider value

You must set the property, not the attribute. LitElement is used in this example to emphasize the property binding syntax (.property).

import { LitElement, html } from 'lit-element';

class App extends LitElement {
  static get properties() {
    return {
      globals: { type: Object },
    };
  }

  constructor() {
    super();
    this.globals = {
      lang: 'es',
    };
  }

  onLangChange(event) {
    this.globals = {
      ...this.globals,
      lang: event.detail.value,
    };
  }

  render() {
    return html`
      <context-provider .value=${this.globals}>
        <lang-selector @change=${this.onLangChange}></lang-selector>

        <!-- context-consumer has access to the provider value in its "context" property -->
        <context-consumer></context-consumer>
      </context-provider>
    `;
  }
}

Subscribe to changes in the Provider value from consumers

The onContextChanged() hook should be implemented by components using the Consumer mixin that want to react to changes in the value property of the Provider.

import { contextConsumerMixin } from '@kuscamara/context-provider';

class MyComponent extends contextConsumerMixin(HTMLElement) {
  // Called when the Provider value changes
  onContextChanged() {
    this.render();
  }

  render() {
    return `<p>My context is ${this.context}</p>`;
  }
}

Development

  • yarn: install dependencies (required for linting).
  • yarn start: run the development server.
  • yarn test:watch: run the test in watch mode.
  • yarn test: run test with coverage output.

This repository uses Conventional Commits to automatically generate the releases and Changelog.

Acknowledgments

The Provider and Consumer are highly inspired by Masquerades, a library for styled Web Components.

License

This project is licensed under the MIT License.

context-provider's People

Contributors

dependabot[bot] avatar kcmr avatar pinkhominid avatar semantic-release-bot avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

nchauhan91

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.