diff --git a/packages/lit-dev-content/samples/examples/context-basics/elements.js b/packages/lit-dev-content/samples/examples/context-basics/elements.js new file mode 100644 index 000000000..d7a1eb91e --- /dev/null +++ b/packages/lit-dev-content/samples/examples/context-basics/elements.js @@ -0,0 +1,63 @@ +import {html, LitElement} from 'lit'; +import {ContextProvider, ContextConsumer, createContext} from '@lit/context'; + +import {providerStyles} from './styles.js'; + +const contextKey = Symbol('contextKey'); + +// Context object, which acts like a key for the context. +// The context object acts as a key. A consumer will only receive +// values from a provider if their contexts match. A Symbol ensures +// that this context will be unique. +const context = createContext(contextKey); + +export class ProviderEl extends LitElement { + static styles = providerStyles; + + static properties = { + data: {}, + }; + + constructor() { + super(); + this._provider = new ContextProvider(this, {context}); + this.data = 'Initial'; + } + + /** + * `data` will be provided to any consumer that is in the DOM tree below it. + */ + set data(value) { + this._data = value; + this._provider.setValue(value); + } + + get data() { + return this._data; + } + + render() { + return html` +
${this.data}
${this.providedData}
${this.providedData}
${this.providedData}