-
Notifications
You must be signed in to change notification settings - Fork 121
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
Problem with nested NiceModal Provider #100
Comments
Why are you wrapping |
@alexandredev3 That's a good question. My app structure: <ThemeProvider value={globalTheme}>
<NiceModal.Provider>
// some other levels of children
<ThemeProvider value={localTheme}>
<MyComponent />
</ThemeProvider>
// ...
</NiceMpdal.Provider>
</ThemeProvider> MyComponent: // ...
NiceModal.show(ModalComponent)
return ... ModalComponent: // ...
const theme = useTheme()
// ... Here it will return |
Well, if I'm not wrong, you can't have a Something like this: <ThemeProvider value={globalTheme}>
// First NiceModal Provider
<NiceModal.Provider>
<ComponentChildren />
</NiceModal.Provider>
// Second NiceModal Provider
<NiceModal.Provider>
<ThemeProvider value={localTheme}>
<MyComponent />
</ThemeProvider>
</NiceModal.Provider>
</ThemeProvider> |
I have a suggestion about how to support nested NiceModal Provider: Give NiceModal.Provider a optional user defined id prop, and user can declare which provider the <ThemeProvider value={globalTheme}>
// First NiceModal Provider, id is "default", it will use globalTheme
<NiceModal.Provider>
<ComponentChildren />
// Second NiceModal Provider, id is "local", it will use localTheme
<ThemeProvider value={localTheme}>
<NiceModal.Provider id="local">
<MyComponent />
</NiceModal.Provider>
</ThemeProvider>
</NiceModal.Provider>
</ThemeProvider>
// static API to show
NiceModal.show(CustomModal, {
provider: 'local', // if not set, it will use "default"
// other options
}) If want automatic use nearest provider, use a new hooks to wrap it const provider = NiceModal.useProvider()
// static API
NiceModal.show(CustomModal, {
provider: provider.id,
// other options
})
// hooks API
provider.show(CustomModal) |
Thanks @kainstar ! I was thinking about a similar solution (not tried yet), use some concept like Something like, const mountNode = useRef()
NiceModal.show(modal, props, mountNode.current)
... The benefit is no need for nested wrapper provider. The disadvantage is the holder can only be defined where modal is used. It's very like using declarative way. So I've not decided it. |
The Overall, I believe |
Steps to reproduce
Case 1:
Case 2:
Github repository
https://github.com/quangphuchuynh95/test-nice-modal
Codesandbox
https://codesandbox.io/s/wizardly-goldberg-rp3vtz?file=/src/index.js
The text was updated successfully, but these errors were encountered: