Skip to content

Commit 29766b3

Browse files
committed
Update example to use colors
1 parent ce8ea65 commit 29766b3

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {createContext} from '@lit/context';
22

3-
export type Level = {level: number; prefix: string};
3+
export type Level = {level: number; color: string};
44

5-
export const levelContext = createContext<Level>(Symbol('prefix'));
5+
export const levelContext = createContext<Level>(Symbol('level'));

packages/lit-dev-content/samples/examples/context-consume-provide/my-app.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import './my-heading.js';
66
@customElement('my-app')
77
export class MyApp extends LitElement {
88
render() {
9-
// <my-heading> adjusts what heading tag to use based on the level context
10-
// provided to it and prefixes with a provided numbering.
11-
129
// <my-section> serves as both context provider and consumer. It provides a
1310
// level value that is 1 greater than what's provided to it. This allows
1411
// nested <my-section> to provide a different value based on its depth.
12+
13+
// <my-heading> adjusts what heading tag to use and the color based on the
14+
// level context.
1515
return html`
1616
<my-section>
1717
<my-heading>Heading level 1</my-heading>
@@ -51,6 +51,9 @@ export class MyApp extends LitElement {
5151
<my-heading>Heading level 3</my-heading>
5252
<my-section>
5353
<my-heading>Heading level 4</my-heading>
54+
<my-section>
55+
<my-heading>Heading level 5</my-heading>
56+
</my-section>
5457
</my-section>
5558
</my-section>
5659
</my-section>
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
import {LitElement} from 'lit';
22
import {html, literal} from 'lit/static-html.js';
3+
import {styleMap} from 'lit/directives/style-map.js';
34
import {ContextConsumer} from '@lit/context';
45
import {levelContext} from './level-context.js';
56

67
export class MyHeading extends LitElement {
78
_levelContext = new ContextConsumer(this, {context: levelContext});
89

910
get _tag() {
10-
switch (this._levelContext.value.level) {
11-
case 1:
11+
switch (this._levelContext.value?.level) {
12+
case 0:
1213
return literal`h1`;
13-
case 2:
14+
case 1:
1415
return literal`h2`;
15-
case 3:
16+
case 2:
1617
return literal`h3`;
17-
case 4:
18+
case 3:
1819
return literal`h4`;
19-
case 5:
20+
case 4:
2021
return literal`h5`;
21-
case 6:
22+
case 5:
2223
return literal`h6`;
2324
default:
2425
return literal`p`;
2526
}
2627
}
2728

2829
render() {
29-
return html`<${this._tag}><slot></slot></${this._tag}>`;
30+
return html`
31+
<${this._tag}
32+
style=${styleMap({color: this._levelContext.value?.color})}
33+
>
34+
<slot></slot>
35+
</${this._tag}>`;
3036
}
3137
}
3238
customElements.define('my-heading', MyHeading);
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
import {LitElement} from 'lit';
22
import {html, literal} from 'lit/static-html.js';
33
import {customElement} from 'lit/decorators.js';
4+
import {styleMap} from 'lit/directives/style-map.js';
45
import {consume} from '@lit/context';
56
import {levelContext, type Level} from './level-context.js';
67

78
@customElement('my-heading')
89
export class MyHeading extends LitElement {
10+
// Consume the level and color from parent provider
911
@consume({context: levelContext})
1012
private _level?: Level;
1113

1214
private get _tag() {
1315
switch (this._level?.level) {
14-
case 1:
16+
case 0:
1517
return literal`h1`;
16-
case 2:
18+
case 1:
1719
return literal`h2`;
18-
case 3:
20+
case 2:
1921
return literal`h3`;
20-
case 4:
22+
case 3:
2123
return literal`h4`;
22-
case 5:
24+
case 4:
2325
return literal`h5`;
24-
case 6:
26+
case 5:
2527
return literal`h6`;
2628
default:
2729
return literal`p`;
2830
}
2931
}
3032

3133
render() {
32-
return html`<${this._tag}>${this._level?.prefix} <slot></slot></${this._tag}>`;
34+
return html`
35+
<${this._tag} style=${styleMap({color: this._level?.color})}>
36+
<slot></slot>
37+
</${this._tag}>`;
3338
}
3439
}

packages/lit-dev-content/samples/examples/context-consume-provide/my-section.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,38 @@ import {customElement} from 'lit/decorators.js';
33
import {ContextProvider, ContextConsumer} from '@lit/context';
44
import {levelContext} from './level-context.js';
55

6+
const COLORS = ['blue', 'orange', 'green', 'purple'];
7+
68
@customElement('my-section')
79
export class MySection extends LitElement {
8-
// Serve as a context provider with an initial level 1 and numbering prefix
9-
// based on its numbering among siblings.
10+
// Serve as a context provider with an initial level 1 and the color for that
11+
// level
1012
private _provider = new ContextProvider(this, {
1113
context: levelContext,
12-
initialValue: {
13-
level: 1,
14-
prefix: String(this._siblingNumber) + '.',
15-
},
14+
initialValue: {level: 0, color: COLORS[0]},
1615
});
1716

1817
// Consumes level context from a parent provider. If a parent provider is
1918
// found, update own provider level to be 1 greater than provided value and
20-
// append its sibling number.
19+
// its corresponding color.
2120
private _consumer = new ContextConsumer(this, {
2221
context: levelContext,
23-
callback: ({level, prefix}) => {
22+
callback: ({level}) => {
2423
this._provider.setValue({
2524
level: level + 1,
26-
prefix: prefix + String(this._siblingNumber) + '.',
25+
color: COLORS[(level + 1) % COLORS.length],
2726
});
2827
},
2928
});
3029

31-
private get _siblingNumber() {
32-
return (
33-
Array.from(this.parentNode!.children)
34-
.filter((el) => el instanceof MySection)
35-
.indexOf(this) + 1
36-
);
37-
}
38-
3930
render() {
4031
return html`<section><slot></slot></section>`;
4132
}
4233

4334
static styles = css`
4435
:host {
4536
display: block;
46-
margin-left: 1rem;
37+
text-align: center;
4738
}
4839
`;
4940
}

0 commit comments

Comments
 (0)