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(ui): Storybook - Add theme switcher & noBorder option to <Sample /> #29765

Merged
merged 2 commits into from
Nov 12, 2021

Conversation

vuluongj20
Copy link
Member

Adding two optional props to <Sample />:

  • noBorder: remove border and padding around the sample wrapper
  • showThemeSwitcher: add a toggle for switching between light and dark mode. Sample now provides a local theme context to automatically update the styles of all of its children.

Without noBorder:
Screen Shot 2021-11-03 at 4 36 58 PM

With noBorder:
Screen Shot 2021-11-03 at 4 37 57 PM

With showThemeSwitcher enabled:
Screen Shot 2021-11-03 at 4 35 07 PM
Screen Shot 2021-11-03 at 4 35 17 PM

@vuluongj20 vuluongj20 requested review from a team as code owners November 3, 2021 23:39

IconMoon.displayName = 'IconMoon';

export {IconMoon};
Copy link
Member Author

Choose a reason for hiding this comment

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

IconMoon is already part of the icon set in Figma. I just copied the SVG markup from there.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2021

size-limit report

Path Base Size (9a5c1a2) Current Size Change
src/sentry/static/sentry/dist/entrypoints/app.js 52.76 KB 52.77 KB +0.02% 🔺
src/sentry/static/sentry/dist/entrypoints/sentry.css 70.89 KB 70.89 KB 0%

};

/** Expose the selected theme to children of <Sample /> */
export const SampleThemeContext = createContext<ThemeName>('light');
Copy link
Member Author

Choose a reason for hiding this comment

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

Exporting SampleThemeContext so children of Sample /> can consume it (via useContext(SampleThemeContext)) for their own purposes.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm why would children need to use this value?

Copy link
Member Author

Choose a reason for hiding this comment

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

In the Colors page that I'm working on, there are color samples for both light and dark mode:
Screen Shot 2021-11-03 at 4 35 07 PM
Screen Shot 2021-11-03 at 4 35 17 PM

These color samples need to show different values based on the current theme in <Sample />, e.g. Gray 500 shows #2B2233 in light mode and #EBE6EF in dark mode. This means they need to know which theme (light/dark) has been selected, hence the theme context.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh... are we going to have a separate palette for dark and light modes? I think the initial plan was to have a single palette (but not sure if that's still the plan)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the plan is to have separate palettes 🌈

@vuluongj20 vuluongj20 requested a review from billyvg November 3, 2021 23:45
Adding two optional props to <Sample />:
 - noBorder: remove border and padding around the wrapper
 - showThemeSwitcher: add a toggle for switching between light and dark mode. Sample now provides a local theme context to automatically update the styles of all of its children.
Comment on lines 13 to 14
showThemeSwitcher?: boolean;
noBorder?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Might be nice to add some /** */ comments here

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point

Comment on lines 33 to 35
themeObject = theme === 'light' ? lightTheme : darkTheme;
} else {
themeObject = useTheme();
Copy link
Member

Choose a reason for hiding this comment

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

You probably want themeObject to be in state, it probably works fine most of the time because you call setTheme, but this could lead to unwanted behavior because it's not in state.

return (
<Wrap>
{showThemeSwitcher && (
<ThemeSwitcher onClick={() => toggleTheme()} active={theme === 'dark'}>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<ThemeSwitcher onClick={() => toggleTheme()} active={theme === 'dark'}>
<ThemeSwitcher onClick={toggleTheme} active={theme === 'dark'}>

};

/** Expose the selected theme to children of <Sample /> */
export const SampleThemeContext = createContext<ThemeName>('light');
Copy link
Member

Choose a reason for hiding this comment

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

Hmm why would children need to use this value?

Based on @billyvg's comments in #29765.
@vuluongj20
Copy link
Member Author

@billyvg Added a commit addressing your points above. Tell me if it looks good (esp. how I added themeObject to state)!

@vuluongj20 vuluongj20 requested a review from billyvg November 9, 2021 16:54
Copy link
Member

@billyvg billyvg left a comment

Choose a reason for hiding this comment

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

Codewise, looks good

};

/** Expose the selected theme to children of <Sample /> */
export const SampleThemeContext = createContext<ThemeName>('light');
Copy link
Member

Choose a reason for hiding this comment

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

Ahh... are we going to have a separate palette for dark and light modes? I think the initial plan was to have a single palette (but not sure if that's still the plan)

Comment on lines +40 to +41
setThemeName('dark');
setTheme(darkTheme);
Copy link
Member

Choose a reason for hiding this comment

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

wonder if we should have the current theme name in theme itself... (I was avoiding it so that we wouldn't have logic based on theme name, and instead push people towards using theme aliases).

Copy link
Member Author

Choose a reason for hiding this comment

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

thought of this myself! Would definitely make this cleaner, but wasn't sure if it's worth changing theme.tsx for it.

@vuluongj20 vuluongj20 requested a review from billyvg November 11, 2021 22:43
@vuluongj20 vuluongj20 merged commit 96dd778 into master Nov 12, 2021
@vuluongj20 vuluongj20 deleted the vuluong/storybook-theme-switcher branch November 12, 2021 22:37
@github-actions github-actions bot locked and limited conversation to collaborators Nov 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants