Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cristinecula
Copy link
Contributor

This hook enables you to provide values to one or multiple contexts from the same component.

Instead of nesting multiple Provider elements:

<app-state-provider .value=${appState}>
  <settings-provider .value=${settings}>
    <main-app></main-app>
  </settings-provider>
</app-state-provider>

you can do all of it in the same component hook:

useProvideContext(AppStateContext, appState, [appState]);
useProvideContext(SettingsContext, settings, [settings]);

@changeset-bot
Copy link

changeset-bot bot commented Nov 4, 2022

🦋 Changeset detected

Latest commit: 23b7541

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
haunted Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Nov 4, 2022

Deploy Preview for hauntedhooks ready!

Name Link
🔨 Latest commit 23b7541
🔍 Latest deploy log https://app.netlify.com/sites/hauntedhooks/deploys/636500be564c3b0009b5c0c1
😎 Deploy Preview https://deploy-preview-452--hauntedhooks.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@cristinecula cristinecula force-pushed the feature/use-provide-context branch 2 times, most recently from e285f9c to 3906e83 Compare November 4, 2022 08:39
"haunted": minor
---

New hook: useProvideContext
Copy link
Collaborator

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

Copy link
Contributor Author

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)?

This hook enables you to provide values to one or multiple contexts from the same component.

Instead of:
```html
<app-state-provider .value=${appState}>
  <settings-provider .value=${settings}>
    <main-app></main-app>
  </settings-provider>
</app-state-provider>
```

you can do:
```js
useProvideContext(AppStateContext, appState, [appState]);
useProvideContext(SettingsContext, settings, [settings]);
```
@cristinecula
Copy link
Contributor Author

cristinecula commented Nov 4, 2022

@bennypowers this code duplicates a lot of the code from the Provider in makeContext. The Provider component could be rewritten as:

Provider = component(({value}) => useProvideContext(Context, value), {useShadowDOM: false})

Do you think that would be a good idea?

@bennypowers
Copy link
Collaborator

will these interop with @lit-labs/context or vanilla context protocol elements?

@cristinecula
Copy link
Contributor Author

I don't know. This PR is just re-implementing the existing haunted Context Provider as a hook.

@matthewp
Copy link
Owner

matthewp commented Feb 6, 2023

We need to at some point have a conversation on how to add features to Haunted without unnecessarily increasingly the bundle size. Like, this feature seems useful but a large number of projects will never need context. Does it make sense to make this kind of thing available via import 'haunted/context' instead?

@cristinecula
Copy link
Contributor Author

@matthewp isn't this already mitigated by the fact that you're publishing the package as an es6 library? I think most bundlers will tree-shake the context if it's not used at all.
In any case, import "haunted/context" sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants