From ce8ea655e937194041af786daa331bad54d6932e Mon Sep 17 00:00:00 2001 From: Augustine Kim Date: Wed, 13 Mar 2024 17:48:39 -0700 Subject: [PATCH] Spice up example a bit --- .../context-consume-provide/index.html | 2 +- .../context-consume-provide/level-context.ts | 4 +- .../context-consume-provide/my-app.ts | 56 +++++++++++++++---- .../context-consume-provide/my-heading.js | 3 +- .../context-consume-provide/my-heading.ts | 8 +-- .../context-consume-provide/my-section.ts | 29 +++++++--- 6 files changed, 75 insertions(+), 27 deletions(-) diff --git a/packages/lit-dev-content/samples/examples/context-consume-provide/index.html b/packages/lit-dev-content/samples/examples/context-consume-provide/index.html index 99dd0d15e..cd3e926c5 100644 --- a/packages/lit-dev-content/samples/examples/context-consume-provide/index.html +++ b/packages/lit-dev-content/samples/examples/context-consume-provide/index.html @@ -5,7 +5,7 @@ }

- Example taken from: + Example inspired by: (Symbol('level')); +export type Level = {level: number; prefix: string}; + +export const levelContext = createContext(Symbol('prefix')); diff --git a/packages/lit-dev-content/samples/examples/context-consume-provide/my-app.ts b/packages/lit-dev-content/samples/examples/context-consume-provide/my-app.ts index 7afb16083..986539819 100644 --- a/packages/lit-dev-content/samples/examples/context-consume-provide/my-app.ts +++ b/packages/lit-dev-content/samples/examples/context-consume-provide/my-app.ts @@ -7,27 +7,59 @@ import './my-heading.js'; export class MyApp extends LitElement { render() { // adjusts what heading tag to use based on the level context - // provided to it. + // provided to it and prefixes with a provided numbering. // serves as both context provider and consumer. It provides a // level value that is 1 greater than what's provided to it. This allows // nested to provide a different value based on its depth. - return html` - This is h1 + Heading level 1 + + Heading level 2 + + + Heading level 2 + + Heading level 3 + + + Heading level 3 + + Heading level 4 + + + + + Heading level 2 + + Heading level 3 + + Heading level 4 + + + Heading level 4 + + + + + + Heading level 1 + + Heading level 2 + + Heading level 3 + + Heading level 4 + + + - This is h2 - This is h2 - This is h2 + Heading level 2 - This is h3 - This is h3 - This is h3 + Heading level 3 - This is h4 - This is h4 - This is h4 + Heading level 4 diff --git a/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.js b/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.js index 841f0b99f..daa3d950f 100644 --- a/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.js +++ b/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.js @@ -7,7 +7,7 @@ export class MyHeading extends LitElement { _levelContext = new ContextConsumer(this, {context: levelContext}); get _tag() { - switch (this._levelContext.value) { + switch (this._levelContext.value.level) { case 1: return literal`h1`; case 2: @@ -29,5 +29,4 @@ export class MyHeading extends LitElement { return html`<${this._tag}>`; } } - customElements.define('my-heading', MyHeading); diff --git a/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.ts b/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.ts index 7a10e17c6..2615ff8d6 100644 --- a/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.ts +++ b/packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.ts @@ -2,15 +2,15 @@ import {LitElement} from 'lit'; import {html, literal} from 'lit/static-html.js'; import {customElement} from 'lit/decorators.js'; import {consume} from '@lit/context'; -import {levelContext} from './level-context.js'; +import {levelContext, type Level} from './level-context.js'; @customElement('my-heading') export class MyHeading extends LitElement { @consume({context: levelContext}) - private _level: number | undefined; + private _level?: Level; private get _tag() { - switch (this._level) { + switch (this._level?.level) { case 1: return literal`h1`; case 2: @@ -29,6 +29,6 @@ export class MyHeading extends LitElement { } render() { - return html`<${this._tag}>`; + return html`<${this._tag}>${this._level?.prefix} `; } } diff --git a/packages/lit-dev-content/samples/examples/context-consume-provide/my-section.ts b/packages/lit-dev-content/samples/examples/context-consume-provide/my-section.ts index a97385c98..8bc3859c5 100644 --- a/packages/lit-dev-content/samples/examples/context-consume-provide/my-section.ts +++ b/packages/lit-dev-content/samples/examples/context-consume-provide/my-section.ts @@ -5,21 +5,37 @@ import {levelContext} from './level-context.js'; @customElement('my-section') export class MySection extends LitElement { - // Serve as a context provider with an initial value of 1. + // Serve as a context provider with an initial level 1 and numbering prefix + // based on its numbering among siblings. private _provider = new ContextProvider(this, { context: levelContext, - initialValue: 1, + initialValue: { + level: 1, + prefix: String(this._siblingNumber) + '.', + }, }); // Consumes level context from a parent provider. If a parent provider is - // found, update own provider value to be 1 greater than provided value. + // found, update own provider level to be 1 greater than provided value and + // append its sibling number. private _consumer = new ContextConsumer(this, { context: levelContext, - callback: (value) => { - this._provider.setValue(value + 1); + callback: ({level, prefix}) => { + this._provider.setValue({ + level: level + 1, + prefix: prefix + String(this._siblingNumber) + '.', + }); }, }); + private get _siblingNumber() { + return ( + Array.from(this.parentNode!.children) + .filter((el) => el instanceof MySection) + .indexOf(this) + 1 + ); + } + render() { return html`

`; } @@ -27,8 +43,7 @@ export class MySection extends LitElement { static styles = css` :host { display: block; - border: 1px solid black; - padding: 1rem; + margin-left: 1rem; } `; }