File tree Expand file tree Collapse file tree 7 files changed +169
-0
lines changed
packages/lit-dev-content/samples/examples/context-consume-provide Expand file tree Collapse file tree 7 files changed +169
-0
lines changed Original file line number Diff line number Diff line change
1
+ < script type ="module " src ="./my-app.js "> </ script >
2
+ < style >
3
+ : root {
4
+ font-family : sans-serif;
5
+ }
6
+ </ style >
7
+ < p >
8
+ Example taken from:
9
+ < a
10
+ href ="https://react.dev/learn/passing-data-deeply-with-context "
11
+ target ="_blank "
12
+ > https://react.dev/learn/passing-data-deeply-with-context</ a
13
+ >
14
+ </ p >
15
+ < my-app > </ my-app >
Original file line number Diff line number Diff line change
1
+ import { createContext } from '@lit/context' ;
2
+
3
+ export const levelContext = createContext < number > ( Symbol ( 'level' ) ) ;
Original file line number Diff line number Diff line change
1
+ import { html , LitElement } from 'lit' ;
2
+ import { customElement } from 'lit/decorators.js' ;
3
+ import './my-section.js' ;
4
+ import './my-heading.js' ;
5
+
6
+ @customElement ( 'my-app' )
7
+ export class MyApp extends LitElement {
8
+ render ( ) {
9
+ // <my-heading> adjusts what heading tag to use based on the level context
10
+ // provided to it.
11
+
12
+ // <my-section> serves as both context provider and consumer. It provides a
13
+ // level value that is 1 greater than what's provided to it. This allows
14
+ // nested <my-section> to provide a different value based on its depth.
15
+
16
+ return html `
17
+ < my-section >
18
+ < my-heading > This is h1</ my-heading >
19
+ < 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 >
23
+ < 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 >
27
+ < 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 >
31
+ </ my-section >
32
+ </ my-section >
33
+ </ my-section >
34
+ </ my-section >
35
+ ` ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ import { LitElement } from 'lit' ;
2
+ import { html , literal } from 'lit/static-html.js' ;
3
+ import { ContextConsumer } from '@lit/context' ;
4
+ import { levelContext } from './level-context.js' ;
5
+
6
+ export class MyHeading extends LitElement {
7
+ levelContext = new ContextConsumer ( this , { context : levelContext } ) ;
8
+
9
+ get tag ( ) {
10
+ switch ( this . levelContext . value ) {
11
+ case 1 :
12
+ return literal `h1` ;
13
+ case 2 :
14
+ return literal `h2` ;
15
+ case 3 :
16
+ return literal `h3` ;
17
+ case 4 :
18
+ return literal `h4` ;
19
+ case 5 :
20
+ return literal `h5` ;
21
+ case 6 :
22
+ return literal `h6` ;
23
+ default :
24
+ return literal `p` ;
25
+ }
26
+ }
27
+
28
+ render ( ) {
29
+ return html `< ${ this . tag } > < slot > </ slot > </ ${ this . tag } > ` ;
30
+ }
31
+ }
32
+
33
+ customElements . define ( 'my-heading' , MyHeading ) ;
Original file line number Diff line number Diff line change
1
+ import { LitElement } from 'lit' ;
2
+ import { html , literal } from 'lit/static-html.js' ;
3
+ import { customElement } from 'lit/decorators.js' ;
4
+ import { consume } from '@lit/context' ;
5
+ import { levelContext } from './level-context.js' ;
6
+
7
+ @customElement ( 'my-heading' )
8
+ export class MyHeading extends LitElement {
9
+ @consume ( { context : levelContext } )
10
+ level : number | undefined ;
11
+
12
+ get tag ( ) {
13
+ switch ( this . level ) {
14
+ case 1 :
15
+ return literal `h1` ;
16
+ case 2 :
17
+ return literal `h2` ;
18
+ case 3 :
19
+ return literal `h3` ;
20
+ case 4 :
21
+ return literal `h4` ;
22
+ case 5 :
23
+ return literal `h5` ;
24
+ case 6 :
25
+ return literal `h6` ;
26
+ default :
27
+ return literal `p` ;
28
+ }
29
+ }
30
+
31
+ render ( ) {
32
+ return html `< ${ this . tag } > < slot > </ slot > </ ${ this . tag } > ` ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ import { html , css , LitElement } from 'lit' ;
2
+ import { customElement } from 'lit/decorators.js' ;
3
+ import { ContextProvider , ContextConsumer } from '@lit/context' ;
4
+ import { levelContext } from './level-context.js' ;
5
+
6
+ @customElement ( 'my-section' )
7
+ export class MySection extends LitElement {
8
+ // Serve as a context provider with an initial value of 1.
9
+ provider = new ContextProvider ( this , {
10
+ context : levelContext ,
11
+ initialValue : 1 ,
12
+ } ) ;
13
+
14
+ // Consumes from any parent provider. If a parent provider is found,
15
+ // update own provided value to be 1 greater than provided value.
16
+ consumer = new ContextConsumer ( this , {
17
+ context : levelContext ,
18
+ callback : ( value ) => {
19
+ this . provider . setValue ( value + 1 ) ;
20
+ } ,
21
+ } ) ;
22
+
23
+ render ( ) {
24
+ return html `<section> <slot> </ slot> </ section> ` ;
25
+ }
26
+
27
+ static styles = css `
28
+ : host {
29
+ dis play: block;
30
+ bor der: 1px solid black;
31
+ padding: 1rem;
32
+ }
33
+ ` ;
34
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "extends" : " /samples/base.json" ,
3
+ "title" : " Context Consume and Provide" ,
4
+ "description" : " Showcase an element both consuming and providing the same context." ,
5
+ "section" : " Managing Data" ,
6
+ "files" : {
7
+ "my-app.ts" : {},
8
+ "my-section.ts" : {},
9
+ "my-heading.ts" : {},
10
+ "level-context.ts" : {},
11
+ "index.html" : {}
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments