-
Notifications
You must be signed in to change notification settings - Fork 19
State instanceid context #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d9cbbca
Adds entries for context and instanceID
5dabcce
Adds context documentation
ab560d9
Updates context doc
153a99c
Adds instance ID
929529b
Updates intance id doc
05f7694
Updates spellcheck to include Morphdom
db01c1d
Update app/docs/md/elements/state/context.md
kristoferjoseph 3585775
Update app/docs/md/elements/state/context.md
kristoferjoseph fe322b5
Update app/docs/md/elements/state/context.md
kristoferjoseph fb8e29d
Update app/docs/md/elements/state/context.md
kristoferjoseph 898fc55
Update app/docs/md/elements/state/context.md
kristoferjoseph 122fd2e
closes #171
4993e54
Use classes for theme toggle querySelectors
colepeters 4183903
1.16.6
colepeters edcd7c9
Pin masthead to 1.0.0
colepeters 8070b9b
Bump masthead (#183)
colepeters 97d5d1f
1.17.0
colepeters de194ee
Resolves review comments.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,77 @@ | ||
--- | ||
title: Context | ||
--- | ||
|
||
The context object enables passing state to child elements without needing to resort to passing attributes down through multiple elements. | ||
|
||
## Set parent context | ||
|
||
The context object is passed as a key on the state object. Add data to the context object and it will be available to child elements. | ||
|
||
[Follow along by checking out the context demo from GitHub →](https://github.com/enhance-dev/context-demo) | ||
|
||
Given this markup you can use the context object to pass state directly to a deeply nested child element. Consider the page structure in the example below: | ||
|
||
<doc-code filename="app/pages/index.html"> | ||
|
||
```html | ||
<context-parent> | ||
<my-container> | ||
<my-container> | ||
<my-container> | ||
<context-heading></context-heading> | ||
</my-container> | ||
</my-container> | ||
</my-container> | ||
</context-parent> | ||
``` | ||
|
||
</doc-code> | ||
|
||
|
||
Add a heading key to the context object in the parent element: | ||
|
||
<doc-code filename="app/pages/index.html"> | ||
|
||
```javascript | ||
export default function ContextParent({ html, state }) { | ||
const { context } = state | ||
context.heading = 'Heading set via context' | ||
return html` | ||
<style> | ||
:host { | ||
display: block; | ||
height: 100dvh; | ||
padding-top: 3rem; | ||
text-align: center; | ||
font-family: sans-serif; | ||
} | ||
</style> | ||
<slot></slot> | ||
` | ||
} | ||
``` | ||
|
||
</doc-code> | ||
|
||
Render the heading passed via context in the deeply nested child element: | ||
<doc-code filenam="app/elements/context/heading.mjs"> | ||
|
||
```javascript | ||
export default function ContextHeading({ html, state }) { | ||
const { context } = state | ||
const { heading='Default heading' } = context | ||
return html` | ||
<style> | ||
:host > h1 { | ||
font-size: 1.5rem; | ||
} | ||
</style> | ||
<h1>${heading}</h1> | ||
` | ||
} | ||
``` | ||
|
||
</doc-code> | ||
|
||
|
This file contains hidden or 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
This file contains hidden or 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,23 @@ | ||
--- | ||
title: Instance ID | ||
--- | ||
|
||
`instanceID` is a unique identifier that is generated per Custom Element instance. This enables you to differentiate between multiple instances of the same element on a page. It can also be useful when using a string based diffing library like [Morphdom](https://github.com/patrick-steele-idem/morphdom) which keys on unique identifiers to know when to update versus replace an element. | ||
|
||
```javascript | ||
export default function MyCard({ html, state }) { | ||
const { attrs={}, instanceID='' } = state | ||
const { content='', heading='' } = attrs | ||
|
||
return html` | ||
<figure id="figure-${instanceID}"> | ||
<h2>${heading}</h2> | ||
<p>${content}</p> | ||
</figure> | ||
` | ||
} | ||
``` | ||
|
||
<doc-callout mark="ℹ️"> | ||
Pro tip: As in the example above, prefix the id with the element name in order to use the id with multiple child elements. | ||
</doc-callout> |
This file contains hidden or 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -48,6 +48,7 @@ lowercas(e|ed) | |
MDN | ||
MDN's | ||
mixi(n|ns) | ||
Morphdom | ||
natively | ||
Netlify | ||
polyfills | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.