-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[playground] New context example with element that both consumes and …
…provides (#1315)
- Loading branch information
Showing
9 changed files
with
208 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/lit-dev-content/samples/examples/context-consume-provide/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script type="module" src="./my-app.js"></script> | ||
<style> | ||
:root { | ||
font-family: sans-serif; | ||
} | ||
</style> | ||
<p> | ||
Example inspired by: | ||
<a | ||
href="https://react.dev/learn/passing-data-deeply-with-context" | ||
target="_blank" | ||
>https://react.dev/learn/passing-data-deeply-with-context</a | ||
> | ||
</p> | ||
<my-app></my-app> |
5 changes: 5 additions & 0 deletions
5
packages/lit-dev-content/samples/examples/context-consume-provide/level-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {createContext} from '@lit/context'; | ||
|
||
export type Level = {level: number; color: string}; | ||
|
||
export const levelContext = createContext<Level>(Symbol('level')); |
72 changes: 72 additions & 0 deletions
72
packages/lit-dev-content/samples/examples/context-consume-provide/my-app.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {html, LitElement} from 'lit'; | ||
import {customElement} from 'lit/decorators.js'; | ||
import './my-section.js'; | ||
import './my-heading.js'; | ||
|
||
@customElement('my-app') | ||
export class MyApp extends LitElement { | ||
render() { | ||
// <my-section> 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 <my-section> to provide a different value based on its depth. | ||
|
||
// <my-heading> adjusts what heading tag to use and the color based on the | ||
// level context. | ||
return html` | ||
<my-section> | ||
<my-heading>Heading level 1</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 2</my-heading> | ||
</my-section> | ||
<my-section> | ||
<my-heading>Heading level 2</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 3</my-heading> | ||
</my-section> | ||
<my-section> | ||
<my-heading>Heading level 3</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 4</my-heading> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
<my-section> | ||
<my-heading>Heading level 2</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 3</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 4</my-heading> | ||
</my-section> | ||
<my-section> | ||
<my-heading>Heading level 4</my-heading> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
<my-section> | ||
<my-heading>Heading level 1</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 2</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 3</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 4</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 5</my-heading> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
<my-section> | ||
<my-heading>Heading level 2</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 3</my-heading> | ||
<my-section> | ||
<my-heading>Heading level 4</my-heading> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
</my-section> | ||
`; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {LitElement} from 'lit'; | ||
import {html, literal} from 'lit/static-html.js'; | ||
import {styleMap} from 'lit/directives/style-map.js'; | ||
import {ContextConsumer} from '@lit/context'; | ||
import {levelContext} from './level-context.js'; | ||
|
||
export class MyHeading extends LitElement { | ||
_levelContext = new ContextConsumer(this, {context: levelContext}); | ||
|
||
get _tag() { | ||
const level = this._levelContext.value?.level; | ||
if (typeof level === 'number' && level >= 0 && level <= 5) { | ||
return unsafeStatic(`h${level + 1}`); | ||
} else { | ||
return literal`p`; | ||
} | ||
} | ||
|
||
render() { | ||
return html` | ||
<${this._tag} | ||
style=${styleMap({color: this._levelContext.value?.color})} | ||
> | ||
<slot></slot> | ||
</${this._tag}>`; | ||
} | ||
} | ||
customElements.define('my-heading', MyHeading); |
29 changes: 29 additions & 0 deletions
29
packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {LitElement} from 'lit'; | ||
import {html, literal, unsafeStatic} from 'lit/static-html.js'; | ||
import {customElement} from 'lit/decorators.js'; | ||
import {styleMap} from 'lit/directives/style-map.js'; | ||
import {consume} from '@lit/context'; | ||
import {levelContext, type Level} from './level-context.js'; | ||
|
||
@customElement('my-heading') | ||
export class MyHeading extends LitElement { | ||
// Consume the level and color from parent provider | ||
@consume({context: levelContext}) | ||
private _level?: Level; | ||
|
||
private get _tag() { | ||
const level = this._level?.level; | ||
if (typeof level === 'number' && level >= 0 && level <= 5) { | ||
return unsafeStatic(`h${level + 1}`); | ||
} else { | ||
return literal`p`; | ||
} | ||
} | ||
|
||
render() { | ||
return html` | ||
<${this._tag} style=${styleMap({color: this._level?.color})}> | ||
<slot></slot> | ||
</${this._tag}>`; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/lit-dev-content/samples/examples/context-consume-provide/my-section.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {html, css, LitElement} from 'lit'; | ||
import {customElement} from 'lit/decorators.js'; | ||
import {ContextProvider, ContextConsumer} from '@lit/context'; | ||
import {levelContext} from './level-context.js'; | ||
|
||
const COLORS = ['blue', 'orange', 'green', 'purple']; | ||
|
||
@customElement('my-section') | ||
export class MySection extends LitElement { | ||
// Serve as a context provider with an initial level 1 and the color for that | ||
// level | ||
private _provider = new ContextProvider(this, { | ||
context: levelContext, | ||
initialValue: {level: 0, color: COLORS[0]}, | ||
}); | ||
|
||
// Consumes level context from a parent provider. If a parent provider is | ||
// found, update own provider level to be 1 greater than provided value and | ||
// its corresponding color. | ||
private _consumer = new ContextConsumer(this, { | ||
context: levelContext, | ||
callback: ({level}) => { | ||
this._provider.setValue({ | ||
level: level + 1, | ||
color: COLORS[(level + 1) % COLORS.length], | ||
}); | ||
}, | ||
}); | ||
|
||
render() { | ||
return html`<section><slot></slot></section>`; | ||
} | ||
|
||
static styles = css` | ||
:host { | ||
display: block; | ||
text-align: center; | ||
} | ||
`; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/lit-dev-content/samples/examples/context-consume-provide/project.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "/samples/base.json", | ||
"title": "Context Consume and Provide", | ||
"description": "Showcase an element both consuming and providing the same context.", | ||
"section": "Managing Data", | ||
"files": { | ||
"my-app.ts": {}, | ||
"my-section.ts": {}, | ||
"my-heading.ts": {}, | ||
"level-context.ts": {}, | ||
"index.html": {} | ||
} | ||
} |