-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
43 additions
and
38 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {createContext} from '@lit/context'; | ||
|
||
export type Level = {level: number; prefix: string}; | ||
export type Level = {level: number; color: string}; | ||
|
||
export const levelContext = createContext<Level>(Symbol('prefix')); | ||
export const levelContext = createContext<Level>(Symbol('level')); |
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
22 changes: 14 additions & 8 deletions
22
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 |
---|---|---|
@@ -1,32 +1,38 @@ | ||
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() { | ||
switch (this._levelContext.value.level) { | ||
case 1: | ||
switch (this._levelContext.value?.level) { | ||
case 0: | ||
return literal`h1`; | ||
case 2: | ||
case 1: | ||
return literal`h2`; | ||
case 3: | ||
case 2: | ||
return literal`h3`; | ||
case 4: | ||
case 3: | ||
return literal`h4`; | ||
case 5: | ||
case 4: | ||
return literal`h5`; | ||
case 6: | ||
case 5: | ||
return literal`h6`; | ||
default: | ||
return literal`p`; | ||
} | ||
} | ||
|
||
render() { | ||
return html`<${this._tag}><slot></slot></${this._tag}>`; | ||
return html` | ||
<${this._tag} | ||
style=${styleMap({color: this._levelContext.value?.color})} | ||
> | ||
<slot></slot> | ||
</${this._tag}>`; | ||
} | ||
} | ||
customElements.define('my-heading', MyHeading); |
19 changes: 12 additions & 7 deletions
19
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 |
---|---|---|
@@ -1,34 +1,39 @@ | ||
import {LitElement} from 'lit'; | ||
import {html, literal} 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() { | ||
switch (this._level?.level) { | ||
case 1: | ||
case 0: | ||
return literal`h1`; | ||
case 2: | ||
case 1: | ||
return literal`h2`; | ||
case 3: | ||
case 2: | ||
return literal`h3`; | ||
case 4: | ||
case 3: | ||
return literal`h4`; | ||
case 5: | ||
case 4: | ||
return literal`h5`; | ||
case 6: | ||
case 5: | ||
return literal`h6`; | ||
default: | ||
return literal`p`; | ||
} | ||
} | ||
|
||
render() { | ||
return html`<${this._tag}>${this._level?.prefix} <slot></slot></${this._tag}>`; | ||
return html` | ||
<${this._tag} style=${styleMap({color: this._level?.color})}> | ||
<slot></slot> | ||
</${this._tag}>`; | ||
} | ||
} |
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