Skip to content

Commit ce8ea65

Browse files
committed
Spice up example a bit
1 parent b3ec3bf commit ce8ea65

File tree

6 files changed

+75
-27
lines changed

6 files changed

+75
-27
lines changed

packages/lit-dev-content/samples/examples/context-consume-provide/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
}
66
</style>
77
<p>
8-
Example taken from:
8+
Example inspired by:
99
<a
1010
href="https://react.dev/learn/passing-data-deeply-with-context"
1111
target="_blank"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import {createContext} from '@lit/context';
22

3-
export const levelContext = createContext<number>(Symbol('level'));
3+
export type Level = {level: number; prefix: string};
4+
5+
export const levelContext = createContext<Level>(Symbol('prefix'));

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

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,59 @@ import './my-heading.js';
77
export class MyApp extends LitElement {
88
render() {
99
// <my-heading> adjusts what heading tag to use based on the level context
10-
// provided to it.
10+
// provided to it and prefixes with a provided numbering.
1111

1212
// <my-section> serves as both context provider and consumer. It provides a
1313
// level value that is 1 greater than what's provided to it. This allows
1414
// nested <my-section> to provide a different value based on its depth.
15-
1615
return html`
1716
<my-section>
18-
<my-heading>This is h1</my-heading>
17+
<my-heading>Heading level 1</my-heading>
18+
<my-section>
19+
<my-heading>Heading level 2</my-heading>
20+
</my-section>
21+
<my-section>
22+
<my-heading>Heading level 2</my-heading>
23+
<my-section>
24+
<my-heading>Heading level 3</my-heading>
25+
</my-section>
26+
<my-section>
27+
<my-heading>Heading level 3</my-heading>
28+
<my-section>
29+
<my-heading>Heading level 4</my-heading>
30+
</my-section>
31+
</my-section>
32+
</my-section>
33+
<my-section>
34+
<my-heading>Heading level 2</my-heading>
35+
<my-section>
36+
<my-heading>Heading level 3</my-heading>
37+
<my-section>
38+
<my-heading>Heading level 4</my-heading>
39+
</my-section>
40+
<my-section>
41+
<my-heading>Heading level 4</my-heading>
42+
</my-section>
43+
</my-section>
44+
</my-section>
45+
</my-section>
46+
<my-section>
47+
<my-heading>Heading level 1</my-heading>
48+
<my-section>
49+
<my-heading>Heading level 2</my-heading>
50+
<my-section>
51+
<my-heading>Heading level 3</my-heading>
52+
<my-section>
53+
<my-heading>Heading level 4</my-heading>
54+
</my-section>
55+
</my-section>
56+
</my-section>
1957
<my-section>
20-
<my-heading>This is h2</my-heading>
21-
<my-heading>This is h2</my-heading>
22-
<my-heading>This is h2</my-heading>
58+
<my-heading>Heading level 2</my-heading>
2359
<my-section>
24-
<my-heading>This is h3</my-heading>
25-
<my-heading>This is h3</my-heading>
26-
<my-heading>This is h3</my-heading>
60+
<my-heading>Heading level 3</my-heading>
2761
<my-section>
28-
<my-heading>This is h4</my-heading>
29-
<my-heading>This is h4</my-heading>
30-
<my-heading>This is h4</my-heading>
62+
<my-heading>Heading level 4</my-heading>
3163
</my-section>
3264
</my-section>
3365
</my-section>

packages/lit-dev-content/samples/examples/context-consume-provide/my-heading.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class MyHeading extends LitElement {
77
_levelContext = new ContextConsumer(this, {context: levelContext});
88

99
get _tag() {
10-
switch (this._levelContext.value) {
10+
switch (this._levelContext.value.level) {
1111
case 1:
1212
return literal`h1`;
1313
case 2:
@@ -29,5 +29,4 @@ export class MyHeading extends LitElement {
2929
return html`<${this._tag}><slot></slot></${this._tag}>`;
3030
}
3131
}
32-
3332
customElements.define('my-heading', MyHeading);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {LitElement} from 'lit';
22
import {html, literal} from 'lit/static-html.js';
33
import {customElement} from 'lit/decorators.js';
44
import {consume} from '@lit/context';
5-
import {levelContext} from './level-context.js';
5+
import {levelContext, type Level} from './level-context.js';
66

77
@customElement('my-heading')
88
export class MyHeading extends LitElement {
99
@consume({context: levelContext})
10-
private _level: number | undefined;
10+
private _level?: Level;
1111

1212
private get _tag() {
13-
switch (this._level) {
13+
switch (this._level?.level) {
1414
case 1:
1515
return literal`h1`;
1616
case 2:
@@ -29,6 +29,6 @@ export class MyHeading extends LitElement {
2929
}
3030

3131
render() {
32-
return html`<${this._tag}><slot></slot></${this._tag}>`;
32+
return html`<${this._tag}>${this._level?.prefix} <slot></slot></${this._tag}>`;
3333
}
3434
}

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,45 @@ import {levelContext} from './level-context.js';
55

66
@customElement('my-section')
77
export class MySection extends LitElement {
8-
// Serve as a context provider with an initial value of 1.
8+
// Serve as a context provider with an initial level 1 and numbering prefix
9+
// based on its numbering among siblings.
910
private _provider = new ContextProvider(this, {
1011
context: levelContext,
11-
initialValue: 1,
12+
initialValue: {
13+
level: 1,
14+
prefix: String(this._siblingNumber) + '.',
15+
},
1216
});
1317

1418
// Consumes level context from a parent provider. If a parent provider is
15-
// found, update own provider value to be 1 greater than provided value.
19+
// found, update own provider level to be 1 greater than provided value and
20+
// append its sibling number.
1621
private _consumer = new ContextConsumer(this, {
1722
context: levelContext,
18-
callback: (value) => {
19-
this._provider.setValue(value + 1);
23+
callback: ({level, prefix}) => {
24+
this._provider.setValue({
25+
level: level + 1,
26+
prefix: prefix + String(this._siblingNumber) + '.',
27+
});
2028
},
2129
});
2230

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+
2339
render() {
2440
return html`<section><slot></slot></section>`;
2541
}
2642

2743
static styles = css`
2844
:host {
2945
display: block;
30-
border: 1px solid black;
31-
padding: 1rem;
46+
margin-left: 1rem;
3247
}
3348
`;
3449
}

0 commit comments

Comments
 (0)