-
Notifications
You must be signed in to change notification settings - Fork 37
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
dispatch theme changes via custom events #1731
Conversation
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.
I left a few nit comments in ThemeProvider.tsx
. They were mainly for me to better understand the code. So, feel free to ignore those suggestions if the code is self-explanatory as-is and also maybe also correct me if I misunderstood what the highlighted code does.
@mayank99, since this will require the v2 package to be up to date for the event to be fired, and you need a ref to the element anyway, would |
@raplemie i find mutation observers to be too unwiedly. events feel more natural because of their "dispatch"/"subscribe" model. however, considering how bad applications are at consuming the latest version (many of them are still on 2.11.x), it is the better option overall. i will create another PR, thanks for the suggestion. |
Changes
Context (no pun intended): In #1478, we made
theme="inherit"
more resilient, by reading thedata-iui-theme
attribute when react Context fails (for example, v2 Context cannot be consumed in v3). However this only works on first load. Subsequent theme changes are not reflected.This PR: The ThemeProvider root element will now dispatch native custom events which can be subscribed to anywhere in the tree regardless of Context or package version. Specifically,
iui:themechange
events will be dispatched every time thetheme
prop changes. Then, for thetheme="inherit"
fallback, we will listen to these events and update the theme each time.Subsequent PR: The same change will need to be made in v2, so that v3 can listen to theme changes from v2.
Testing
A little difficult to test locally without making a release, but it can be emulated in vite playground by manually dispatching events (pretend these events are dispatched by v2 ThemeProvider).
main.tsx
Added unit tests as well.
After this PR is merged, I will backport it to v2, make a release, then test with actual v2 ThemeProvider.
Docs
N/A