All Projects → blikblum → wc-context

blikblum / wc-context

Licence: MIT license
Context for Web Components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to wc-context

hello-web-components
A simple starter <hello-world /> web component written in typescript, using lit-html and lit-element. Unit tested with jest and e2e tested with puppeteer and jest-puppeteer.
Stars: ✭ 15 (-42.31%)
Mutual labels:  web-components, lit-element
lit-components
Moved to https://github.com/vaadin/component-mixins
Stars: ✭ 59 (+126.92%)
Mutual labels:  web-components, lit-element
pharos
JSTOR's design system
Stars: ✭ 48 (+84.62%)
Mutual labels:  web-components, lit-element
byu-theme-components
Web Components implementing the BYU web theme.
Stars: ✭ 21 (-19.23%)
Mutual labels:  web-components, lit-element
bui
‹b› Web components for creating applications – built by Blackstone Publishing using lit-html and lit-element
Stars: ✭ 29 (+11.54%)
Mutual labels:  web-components, lit-element
Open Wc
Open Web Components: guides, tools and libraries for developing web components.
Stars: ✭ 1,670 (+6323.08%)
Mutual labels:  web-components, lit-element
Wired Elements
Collection of custom elements that appear hand drawn. Great for wireframes or a fun look.
Stars: ✭ 8,848 (+33930.77%)
Mutual labels:  web-components, lit-element
pwa-lit-template
A template for building Progressive Web Applications using Lit and Vaadin Router.
Stars: ✭ 159 (+511.54%)
Mutual labels:  web-components, lit-element
Shadow-DOM-inject-styles
🎉 A helper function to easily modify Shadow DOM CSS.
Stars: ✭ 47 (+80.77%)
Mutual labels:  web-components
grid-container
A grid for the future, CSS Grid Layout + Web Components (Custom Elements v1 + Shadow DOM v1)
Stars: ✭ 51 (+96.15%)
Mutual labels:  web-components
elcontext
Context-based actions for Emacs
Stars: ✭ 18 (-30.77%)
Mutual labels:  context
context
A proof of concept implementation of scoped context
Stars: ✭ 16 (-38.46%)
Mutual labels:  context
angular-react-microfrontend
🚧 React vs Angular ? Why not both ! Micro frontend demo using Angular and React alongs with a NodeJS API
Stars: ✭ 17 (-34.62%)
Mutual labels:  web-components
smart-custom-element
Smart a lightweight web component library that provides capabilities for web components, such as data binding, using es6 native class inheritance. This library is focused for providing the developer the ability to write robust and native web components without the need of dependencies and an overhead of a framework.
Stars: ✭ 17 (-34.62%)
Mutual labels:  web-components
vaadin-board
Web Component for creating flexible responsive layouts and building nice looking dashboards.
Stars: ✭ 17 (-34.62%)
Mutual labels:  web-components
page-title
A Polymer element for easily updating a webpage's title, such as in a SPA.
Stars: ✭ 13 (-50%)
Mutual labels:  web-components
smart-webcomponents-community
Material & Bootstrap Web Components built with Smart
Stars: ✭ 30 (+15.38%)
Mutual labels:  web-components
nestjs-cls
A continuation-local storage (async context) module compatible with NestJS's dependency injection.
Stars: ✭ 110 (+323.08%)
Mutual labels:  context
rocket-pipes
Powerful pipes for TypeScript, that chain Promise and ADT for you 🚌 -> ⛰️ -> 🚠 -> 🏂 -> 🚀
Stars: ✭ 18 (-30.77%)
Mutual labels:  context
Hunch
Hunch provides functions like: All, First, Retry, Waterfall etc., that makes asynchronous flow control more intuitive.
Stars: ✭ 94 (+261.54%)
Mutual labels:  context

wc-context

A comprehensive context implementation for web components

Features

    ✓ Small and fast. No Internet Explorer support
    ✓ Flexible ways to define context providers and consumers
    ✓ Ability to provide or consume one or more contexts per element
    ✓ Context can be provided or consumed by any HTML element
    ✓ Context can be identified by string or unique identifier
    ✓ Works with shadow dom and slotted content (handles timing issues)
    ✓ Easy to implement unit tests. Most of the time, same as components without context
    ✓ Builtin integration with LitElement
    ✓ Builtin ContextProvider (Reactive Controller) with primitives for lazy loading
    ✓ Builtin context-provider and context-consumer elements

Live examples

Usage

Context can be identified by string or an unique identifier returned by a call to createContext

import { createContext } from 'wc-context'

const themeContext = createContext('theme') // or just a string like 'theme'

To define how a context is provided or consumed, use one of the methods described below. It is possible to mix different methods. For example, a context provided using Lit integration can be consumed by a dedicated custom element and vice versa.

Lit integration

The easiest way to use wc-context is with the Lit integration exported in the lit namespace (wc-context/lit). It provides a withContext class mixin that hooks into the property reactivity system allowing to define context using the property declaration. The context is automatically propagated when the property is updated.

Providing a context

To provide a context add providedContext to the property declaration

import { withContext } from 'wc-context/lit'
import { LitElement } from 'lit'

class Provider extends withContext(LitElement) {
  static properties = {
    value: { type: String, providedContext: 'theme' },
    activeTitle: { type: String, providedContext: 'title' },
  }

  toggleTheme() {
    this.value = 'newtheme'
  }

  toggleTitle() {
    this.activeTitle = 'New title'
  }
}

Consuming a context

To consume a context add context to the property declaration

import { withContext } from 'wc-context/lit'
import { LitElement, html } from 'lit'

class Consumer extends withContext(LitElement) {
  static properties = {
    theme: { type: String, context: 'theme' },
    titleProp: { type: String, context: 'title' },
  }

  render() {
    return html`<div>Theme is ${this.theme}, title is ${this.titleProp}</div>`
  }
}

Custom elements integration

The withContext class mixin exported in the root namespace, implements an API similar to DOM observedAttributes/attributeChangedCallback.

Contexts are defined in an custom element through static providedContexts field where the key is the context name and value holds a configuration object. The configuration can have a value property defining the default context value or a property one defining from what component property the context will retrieve its value.

This mixin can be used in any web component including the created with Lit or other libraries

Providing a context

import { withContext } from 'wc-context'

class Provider extends withContext(HTMLElement) {
  static providedContexts = {
    theme: { value: 'blue' },
    title: 'activeTitle', // shorthand for { property: 'activeTitle' }
  }

  get activeTitle() {
    return this._activeTitle
  }

  set activeTitle(value) {
    this._activeTitle = value
    this.updateContext('title')
  }

  toggleTheme() {
    this.updateContext('theme', 'newtheme')
  }

  toggleTitle() {
    this.activeTitle = 'New title'
  }
}

Consuming a context

import { withContext } from 'wc-context'

class Consumer extends withContext(HTMLElement) {
  static observedContexts = ['theme', ['title', 'titleProp']]

  contextChangedCallback(name, oldValue, value) {
    console.log(`theme changed from "${oldValue}" to "${value}"`)
    // updates el accordingly
  }

  connectedCallback() {
    super.connectedCallback()
    this.innerHTML = `<div>Theme is ${this.theme}, title is ${this.titleProp}</div>`
  }
}

Dedicated custom elements

The context-provider and context-consumer custom elements allows to provide and consume contexts declaratively.

In both elements, the context attribute / property defines the context. Setting the value property of context-provider will change the context value propagating to context-consumer value property (or any other context consumer)

An context-update event is triggered on context-consumer when context value changes

import 'wc-context/context-provider.js'
import 'wc-context/context-consumer.js'

document.body.innerHTML = `
<context-provider context="theme" value="light">
  <div>
    <context-consumer context="theme"></context-consumer>
  </div>
</context-provider>`

const provider = document.querySelector('context-provider')
const consumer = document.querySelector('context-consumer')

consumer.addEventListener('context-update', ({ context, value }) => {
  console.log(`Context ${context}:${value}`)
})

provider.value = 'dark'

Low level API

The low level functions are exported in wc-context/core and can be used to handle specific cases or create a new interface / integration.

For example, is possible to provide a context in body element to be consumed anywhere in the page.

import { registerContext, updateContext } from 'wc-context/core'

registerContext(document.body, 'theme', 'light')

document.querySelector('#theme-toggle-button').addEventListener('click', () => {
  updateContext(document.body, 'theme', 'dark')
})

License

MIT Copyright © 2022 Luiz Américo Pereira Câmara

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].