-
Notifications
You must be signed in to change notification settings - Fork 661
chore: forward @primer/react theming from @primer/styled-react under FF #7800
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
Open
francinelucca
wants to merge
6
commits into
main
Choose a base branch
from
chore/styled-react-forward-react-theming-under-ff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae8f678
chore: forward @primer/react theming from @primer/styled-react under FF
francinelucca ddb7f7e
Update theming for @primer/react
francinelucca 09833e6
Forward theming from @primer/styled-react
francinelucca c4f0799
chore: auto-fix lint and formatting issues
francinelucca da45ff7
lint
francinelucca 45b46a7
Merge branch 'chore/styled-react-forward-react-theming-under-ff' of g…
francinelucca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@primer/react": patch | ||
| "@primer/styled-react": patch | ||
| --- | ||
|
|
||
| chore: forward @primer/react theming from @primer/styled-react under FF | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/styled-react/src/__tests__/FeatureFlaggedTheming.browser.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import {render, screen} from '@testing-library/react' | ||
| import {describe, expect, it, vi} from 'vitest' | ||
| import React from 'react' | ||
| import {ThemeProvider, useTheme, BaseStyles} from '../' | ||
| import {FeatureFlags} from '@primer/react/experimental' | ||
|
|
||
|
francinelucca marked this conversation as resolved.
|
||
| // window.matchMedia() is not implemented by JSDOM so we have to create a mock: | ||
| Object.defineProperty(window, 'matchMedia', { | ||
| writable: true, | ||
| value: vi.fn().mockImplementation(query => ({ | ||
| matches: false, | ||
| media: query, | ||
| onchange: null, | ||
| addListener: vi.fn(), | ||
| removeListener: vi.fn(), | ||
| addEventListener: vi.fn(), | ||
| removeEventListener: vi.fn(), | ||
| dispatchEvent: vi.fn(), | ||
| })), | ||
| }) | ||
|
|
||
| describe('FeatureFlaggedTheming', () => { | ||
| describe('when primer_react_styled_react_use_primer_theme_providers is disabled', () => { | ||
| it('ThemeProvider does not render a wrapper div with data-color-mode', () => { | ||
| render( | ||
| <FeatureFlags flags={{primer_react_styled_react_use_primer_theme_providers: false}}> | ||
| <ThemeProvider> | ||
| <div data-testid="child">Hello</div> | ||
| </ThemeProvider> | ||
| </FeatureFlags>, | ||
| ) | ||
|
|
||
| // The styled ThemeProvider uses styled-components SCThemeProvider which | ||
| // does not inject a wrapper div. The child should not have a parent with data-color-mode. | ||
| const child = screen.getByTestId('child') | ||
| expect(child.parentElement).not.toHaveAttribute('data-color-mode') | ||
| }) | ||
|
|
||
| it('useTheme returns styled theme context values', () => { | ||
| function ThemeConsumer() { | ||
| const theme = useTheme() | ||
| return <div data-testid="theme-consumer">{theme.colorMode ?? 'day'}</div> | ||
| } | ||
|
|
||
| render( | ||
| <FeatureFlags flags={{primer_react_styled_react_use_primer_theme_providers: false}}> | ||
| <ThemeProvider colorMode="night"> | ||
| <ThemeConsumer /> | ||
| </ThemeProvider> | ||
| </FeatureFlags>, | ||
| ) | ||
|
|
||
| expect(screen.getByTestId('theme-consumer')).toHaveTextContent('night') | ||
| }) | ||
|
|
||
| it('BaseStyles renders with data-color-mode and without data-component', () => { | ||
| render( | ||
| <FeatureFlags flags={{primer_react_styled_react_use_primer_theme_providers: false}}> | ||
| <ThemeProvider> | ||
| <BaseStyles data-testid="base-styles"> | ||
| <div>Hello</div> | ||
| </BaseStyles> | ||
| </ThemeProvider> | ||
| </FeatureFlags>, | ||
| ) | ||
|
|
||
| const baseStyles = screen.getByTestId('base-styles') | ||
| expect(baseStyles).toHaveAttribute('data-color-mode') | ||
| expect(baseStyles).toHaveAttribute('data-light-theme') | ||
| expect(baseStyles).toHaveAttribute('data-dark-theme') | ||
| expect(baseStyles).not.toHaveAttribute('data-component') | ||
| }) | ||
| }) | ||
|
|
||
| describe('when primer_react_styled_react_use_primer_theme_providers is enabled', () => { | ||
| it('ThemeProvider renders a wrapper div with data-color-mode', () => { | ||
| render( | ||
| <FeatureFlags flags={{primer_react_styled_react_use_primer_theme_providers: true}}> | ||
| <ThemeProvider> | ||
| <div data-testid="child">Hello</div> | ||
| </ThemeProvider> | ||
| </FeatureFlags>, | ||
| ) | ||
|
|
||
| // The @primer/react ThemeProvider renders a <div> with data-color-mode | ||
| const child = screen.getByTestId('child') | ||
| expect(child.parentElement).toHaveAttribute('data-color-mode') | ||
| expect(child.parentElement).toHaveAttribute('data-light-theme') | ||
| expect(child.parentElement).toHaveAttribute('data-dark-theme') | ||
| }) | ||
|
|
||
| it('useTheme returns primer theme context values', () => { | ||
| function ThemeConsumer() { | ||
| const theme = useTheme() | ||
| return <div data-testid="theme-consumer">{theme.colorMode ?? 'day'}</div> | ||
| } | ||
|
|
||
| render( | ||
| <FeatureFlags flags={{primer_react_styled_react_use_primer_theme_providers: true}}> | ||
| <ThemeProvider colorMode="night"> | ||
| <ThemeConsumer /> | ||
| </ThemeProvider> | ||
| </FeatureFlags>, | ||
| ) | ||
|
|
||
| expect(screen.getByTestId('theme-consumer')).toHaveTextContent('night') | ||
| }) | ||
|
|
||
| it('BaseStyles renders with data-component and without data-color-mode', () => { | ||
| render( | ||
| <FeatureFlags flags={{primer_react_styled_react_use_primer_theme_providers: true}}> | ||
| <ThemeProvider> | ||
| <BaseStyles data-testid="base-styles"> | ||
| <div>Hello</div> | ||
| </BaseStyles> | ||
| </ThemeProvider> | ||
| </FeatureFlags>, | ||
| ) | ||
|
|
||
| const baseStyles = screen.getByTestId('base-styles') | ||
| expect(baseStyles).toHaveAttribute('data-component', 'BaseStyles') | ||
| expect(baseStyles).not.toHaveAttribute('data-color-mode') | ||
| expect(baseStyles).not.toHaveAttribute('data-light-theme') | ||
| expect(baseStyles).not.toHaveAttribute('data-dark-theme') | ||
| }) | ||
| }) | ||
| }) | ||
47 changes: 47 additions & 0 deletions
47
packages/styled-react/src/components/FeatureFlaggedTheming.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import type React from 'react' | ||
| import { | ||
| ThemeProvider as PrimerThemeProvider, | ||
| useTheme as primerUseTheme, | ||
| BaseStyles as PrimerBaseStyles, | ||
| } from '@primer/react' | ||
| import type { | ||
| ThemeProviderProps as PrimerThemeProviderProps, | ||
| BaseStylesProps as PrimerBaseStylesProps, | ||
| } from '@primer/react' | ||
| import {useFeatureFlag} from '@primer/react/experimental' | ||
| import {ThemeProvider as StyledThemeProvider, useTheme as styledUseTheme, useColorSchemeVar} from './ThemeProvider' | ||
| import type {ThemeProviderProps as StyledThemeProviderProps} from './ThemeProvider' | ||
| import {BaseStyles as StyledBaseStyles} from './BaseStyles' | ||
| import type {BaseStylesProps as StyledBaseStylesProps} from './BaseStyles' | ||
|
|
||
| export type ThemeProviderProps = StyledThemeProviderProps | ||
|
|
||
| export type BaseStylesProps = StyledBaseStylesProps | ||
|
|
||
| export const ThemeProvider: React.FC<React.PropsWithChildren<ThemeProviderProps>> = ({children, ...props}) => { | ||
| const enabled = useFeatureFlag('primer_react_styled_react_use_primer_theme_providers') | ||
| if (enabled) { | ||
| return <PrimerThemeProvider {...(props as PrimerThemeProviderProps)}>{children}</PrimerThemeProvider> | ||
| } | ||
| return <StyledThemeProvider {...props}>{children}</StyledThemeProvider> | ||
| } | ||
|
francinelucca marked this conversation as resolved.
|
||
|
|
||
| export function useTheme() { | ||
| const enabled = useFeatureFlag('primer_react_styled_react_use_primer_theme_providers') | ||
| const styledTheme = styledUseTheme() | ||
| const primerTheme = primerUseTheme() | ||
| if (enabled) { | ||
| return primerTheme | ||
| } | ||
| return styledTheme | ||
| } | ||
|
|
||
| export {useColorSchemeVar} | ||
|
|
||
| export function BaseStyles(props: BaseStylesProps) { | ||
| const enabled = useFeatureFlag('primer_react_styled_react_use_primer_theme_providers') | ||
| if (enabled) { | ||
| return <PrimerBaseStyles {...(props as PrimerBaseStylesProps)} /> | ||
| } | ||
| return <StyledBaseStyles {...props} /> | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(nit) would this same changelog show up for both the packages?
I'd suggest splitting them or skipping the changelog for primer/react