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

Fix ThemeProvider bug of remounting and losing state #2390

Merged
merged 9 commits into from
Jan 8, 2025

Conversation

r100-stack
Copy link
Member

@r100-stack r100-stack commented Jan 6, 2025

Changes

A user pointed out a bug where ThemeProvider used to reset the state of its children when inherit was toggled between undefined and defined. (playground)

Fixes the regression introduced in 3.14.0 by #2153. This PR also now passes the e2e test in iTwin/appui#1133 meant to confirm that this regression is fixed.


The reason for the reset of the state was because of the following code:

<ToastProvider
inherit={themeProp === 'inherit' && !portalContainerProp}
>

// Re-use existing ToastProvider if found
if (React.useContext(ToasterStateContext) && inherit) {
return children;
}
return (
<ToasterDispatchContext.Provider value={dispatch}>
<ToasterStateContext.Provider value={toasterState}>
{children}
</ToasterStateContext.Provider>
</ToasterDispatchContext.Provider>
);
};

Here, when portalContainer/portalContainerProp changes to undefined, the inherit changes from false to true. As a result, ToastProvider now returned children wrapper with ToasterDispatchContext.Provider and ToasterStateContext.Provider instead of just children. When the UI tree position changes, the state is reset (docs).

The fix is to thus keep the UI tree position of children the same regardless of inherit.

Testing

  • Confirm that it works in the playground.
  • Confirm that the AppUI e2e test passes. (Will be doing it after the PR since @GerardasB said that if the playground passes, the test will also likely pass)

Not sure why some react-workshop Table test images failed. But since they actually look more correct than the old images (i.e. correct element is focused), I approved them for now.

Docs

Added changeset

@r100-stack r100-stack self-assigned this Jan 6, 2025
@r100-stack r100-stack changed the title Fix ThemeProvider bug of remounting (and losing state) Fix ThemeProvider bug of remounting and losing state Jan 6, 2025
@r100-stack r100-stack changed the base branch from main to uyen/modified-new-year-repo January 6, 2025 21:14
Base automatically changed from uyen/modified-new-year-repo to main January 6, 2025 21:23
@r100-stack r100-stack marked this pull request as ready for review January 8, 2025 13:27
@r100-stack r100-stack requested a review from a team as a code owner January 8, 2025 13:27
@r100-stack r100-stack requested review from mayank99 and smmr-dn and removed request for a team January 8, 2025 13:27
Copy link
Contributor

@mayank99 mayank99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that we are now introducing two additional contexts (which are arguably unnecessary), but it solves the problem. Good fix 👍

packages/itwinui-react/src/core/Toast/Toaster.tsx Outdated Show resolved Hide resolved
@r100-stack r100-stack merged commit 310ddc9 into main Jan 8, 2025
19 checks passed
@r100-stack r100-stack deleted the r/themeprovider-children-state-loss-fix branch January 8, 2025 18:11
@imodeljs-admin imodeljs-admin mentioned this pull request Jan 8, 2025
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.

3 participants