1.0.15 • Published 4 years ago

lit-context v1.0.15

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

LitContext · License npm version Build Status PRs Welcome

LitContext is a JavaScript library for dealing with data context with LitElement components.

Installation

Coming soon

npm install lit-context

Getting started

Providing properties

import { LitElement, html, customElement, property } from 'lit-element';
import { createContext } from "lit-context";

// Create a context
// Context creation will define a custom element provider with the given name <message-provider>
const { consume: consumeMessage } = createContext('message');


// Create a consumer custom element
@customElement("simple-message")
@consumeMessage()
class SimpleMessage extends LitElement {
  @property({ type: String })
  message = "";
  
  render() {
    return html`      
      <p>${this.message}</p>
    `;
  }
}

// Provide the context

// Provide the context
@customElement("main-app")
class MainApp extends LitElement {
  @property({ type : Number })
  counter = 0;

  get providerValue(){
    return { 
      message: `The values is ${this.counter}` 
    };
  }
  
  increase = () => {
    this.value += 1;
  }
  
  render() {    
    return html`      
      <button @click=${this.increase}>Add</button>
      <br/>      
      <!-- All providers have only a value property -->
      <message-provider .value=${this.providerValue}>
          <!-- All consumers under the provider (light or shadow dom) will get updates (even if they are slotted or inside another custom element) -->
          <simple-message></simple-message>
      </message-provider>
    `;
  }
}  

Dependency Injection like behavior

import { LitElement, html, customElement, property } from 'lit-element';
import { createContext } from "lit-context";

const { consume: consumeHttp } = createContext('http', {
  httpGet: async url => {
    const response = await fetch(url);
    const data = await response.json();
    return data;
  }
});

@customElement("some-list")
@consumeHttp()
class SomeList extends LitElement {
  @property({ type: Array })
  items = [];
  
  async loadItems(){
    this.items = this.httpGet('https://someapi.com/api/v1/items');
  }
  
  firstUpdated(){
    this.loadItems();
  }
  
  render() {
    return html`      
      <ul>
        ${this.items.map(item => html`
          <li>${item}</li>
        `)}
      </ul>
    `;
  }
}


@customElement("main-app")
class MainApp extends LitElement {
  render() {    
    return html`      
      <http-provider>
        <some-list></some-list>
      </http-provider>
    `;
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago