-
Notifications
You must be signed in to change notification settings - Fork 92
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
feat: new hook useProvideContext #452
Open
cristinecula
wants to merge
2
commits into
matthewp:main
Choose a base branch
from
Neovici:feature/use-provide-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,5 @@ | ||
--- | ||
"haunted": minor | ||
--- | ||
|
||
New hook: useProvideContext | ||
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
layout: layout-api | ||
package: haunted | ||
module: lib/use-provide-context.js | ||
--- | ||
|
||
# Hooks >> useProvideContext | ||
|
||
Makes your component become a Context provider. It updates every nested consumer with the current value. You can optionally use a deps array (useful when the context value is mutated instead of being replaced). | ||
|
||
```js playground use-provide-context use-provide-context.js | ||
import { | ||
html, | ||
component, | ||
useState, | ||
useContext, | ||
createContext, | ||
useProvideContext, | ||
} from "haunted"; | ||
|
||
const ThemeContext = createContext("dark"); | ||
|
||
customElements.define("theme-consumer", ThemeContext.Consumer); | ||
|
||
function Consumer() { | ||
const context = useContext(ThemeContext); | ||
return context; | ||
} | ||
|
||
customElements.define("my-consumer", component(Consumer)); | ||
|
||
function App() { | ||
const [theme, setTheme] = useState("light"); | ||
useProvideContext(ThemeContext, theme); | ||
|
||
return html` | ||
<select value=${theme} @change=${(event) => setTheme(event.target.value)}> | ||
<option value="dark">Dark</option> | ||
<option value="light">Light</option> | ||
</select> | ||
|
||
<my-consumer></my-consumer> | ||
|
||
<theme-provider .value=${theme === "dark" ? "light" : "dark"}> | ||
<theme-consumer | ||
.render=${(value) => | ||
html` | ||
<h1>${value}</h1> | ||
`} | ||
></theme-consumer> | ||
</theme-provider> | ||
`; | ||
} | ||
|
||
customElements.define("use-provide-context", component(App)); | ||
``` | ||
|
||
```html playground-file use-provide-context index.html | ||
<script type="module" src="use-provide-context.js"></script> | ||
<use-provide-context></use-provide-context> | ||
``` | ||
|
||
## API |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Context, ContextDetail } from "./create-context"; | ||
import { Hook, hook } from "./hook"; | ||
import { State } from "./state"; | ||
import { contextEvent } from "./symbols"; | ||
|
||
/** | ||
* @function | ||
* @template T | ||
* @param {Context} Context Context to provide a value for | ||
* @param {T} value the current value | ||
* @param {unknown[]} values dependencies to the value update | ||
* @return void | ||
*/ | ||
export const useProvideContext = hook( | ||
class<T> extends Hook<[Context<T>, T, unknown[]], void, Element> { | ||
listeners: Set<(value: T) => void>; | ||
|
||
constructor( | ||
id: number, | ||
state: State<Element>, | ||
private context: Context<T>, | ||
private value: T, | ||
private values?: unknown[] | ||
) { | ||
super(id, state); | ||
this.context = context; | ||
this.value = value; | ||
this.values = values; | ||
|
||
this.listeners = new Set(); | ||
this.state.host.addEventListener(contextEvent, this); | ||
} | ||
|
||
disconnectedCallback() { | ||
this.state.host.removeEventListener(contextEvent, this); | ||
} | ||
|
||
handleEvent(event: CustomEvent<ContextDetail<T>>): void { | ||
const { detail } = event; | ||
|
||
if (detail.Context === this.context) { | ||
detail.value = this.value; | ||
detail.unsubscribe = this.unsubscribe.bind(this, detail.callback); | ||
|
||
this.listeners.add(detail.callback); | ||
|
||
event.stopPropagation(); | ||
} | ||
} | ||
|
||
unsubscribe(callback: (value: T) => void): void { | ||
this.listeners.delete(callback); | ||
} | ||
|
||
update(context: Context<T>, value: T, values?: unknown[]): void { | ||
if (this.hasChanged(values)) { | ||
this.values = values; | ||
this.value = value; | ||
for (const callback of this.listeners) { | ||
callback(value); | ||
} | ||
} | ||
} | ||
|
||
hasChanged(values?: unknown[]) { | ||
const lastValues = this.values; | ||
|
||
if (lastValues == null || values == null) { | ||
return true; | ||
} | ||
|
||
return values.some((value, i) => lastValues[i] !== value); | ||
} | ||
} | ||
); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be documented in readme and on the docs site
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bennypowers I have added documentation for the hook.
Do I have to also update custom-elements.json (with the results of
npm run analyze
)?