-
Notifications
You must be signed in to change notification settings - Fork 638
styled-react: Add ThemeProvider and BaseStyles #6958
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
Merged
+604
−10
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1a8ef65
tmp
siddharthkp c3476b5
add base styles to styled-react
siddharthkp a60f6f2
export themeprovider and basestyles
siddharthkp 103ab98
next example uses styled-react
siddharthkp 64374f5
add use client like primer/react
siddharthkp ff23c05
missed the classname!
siddharthkp c92a752
missed couple things
siddharthkp 15bda35
update exports snapshot
siddharthkp d7da338
install styled-react as example dependency
siddharthkp 63456f8
Merge branch 'main' into styled-react-themeprovider
siddharthkp cc49f5e
fix lint + test
siddharthkp 0ad5765
add changeset
siddharthkp d6d180b
export local useTheme from styled-react
siddharthkp 02a371a
Merge branch 'styled-react-themeprovider' of github.com:primer/react …
siddharthkp 7d248e8
removed unused props
siddharthkp 888faad
feature flag the switch
siddharthkp 80e2d9d
Merge branch 'main' into styled-react-themeprovider
siddharthkp 92f3196
add sad timeout
siddharthkp 61165e5
wait longer
siddharthkp 0f30134
Merge branch 'iconbutton-fix-flaky-visual-test' into styled-react-the…
siddharthkp 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,7 @@ | ||
--- | ||
"@primer/react": patch | ||
"@primer/styled-react": minor | ||
--- | ||
|
||
@primer/react: Export `useId` and `useSyncedState` | ||
@primer/styled-react: Add `ThemeProvider` and `BaseStyles` |
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
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
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
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 |
---|---|---|
@@ -1,5 +1,39 @@ | ||
import {Button} from '@primer/react' | ||
'use client' | ||
|
||
import {Button, Stack, Box} from '@primer/react' | ||
import {useTheme} from '@primer/styled-react' | ||
import styled from 'styled-components' | ||
|
||
const StyledDiv = styled.div(({theme}) => { | ||
return { | ||
padding: theme.space[5], | ||
backgroundColor: theme.colors.btn.primary.bg, | ||
} | ||
}) | ||
|
||
const ThemeUser = () => { | ||
const {theme} = useTheme() | ||
return ( | ||
<div | ||
style={{ | ||
padding: theme?.space[5], | ||
backgroundColor: theme?.colors.btn.primary.bg, | ||
}} | ||
> | ||
Hello world | ||
</div> | ||
) | ||
} | ||
|
||
export default function IndexPage() { | ||
return <Button>Hello world</Button> | ||
return ( | ||
<Stack direction="horizontal"> | ||
<Button variant="primary" sx={{padding: 5}}> | ||
Hello world | ||
</Button> | ||
<Box sx={{padding: 5, backgroundColor: 'btn.primary.bg'}}>Hello world</Box> | ||
<StyledDiv>Hello world</StyledDiv> | ||
<ThemeUser /> | ||
</Stack> | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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,152 @@ | ||
import type React from 'react' | ||
import {type CSSProperties, type PropsWithChildren} from 'react' | ||
import {clsx} from 'clsx' | ||
// eslint-disable-next-line import/no-namespace | ||
import type * as styledSystem from 'styled-system' | ||
import {useTheme} from './ThemeProvider' | ||
|
||
import 'focus-visible' | ||
import {createGlobalStyle} from 'styled-components' | ||
|
||
export interface SystemCommonProps | ||
extends styledSystem.ColorProps, | ||
styledSystem.SpaceProps, | ||
styledSystem.DisplayProps {} | ||
|
||
export interface SystemTypographyProps extends styledSystem.TypographyProps { | ||
whiteSpace?: 'normal' | 'nowrap' | 'pre' | 'pre-wrap' | 'pre-line' | ||
} | ||
|
||
const GlobalStyle = createGlobalStyle<{colorScheme?: 'light' | 'dark'}>` | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
margin: 0; | ||
} | ||
|
||
table { | ||
/* stylelint-disable-next-line primer/borders */ | ||
border-collapse: collapse; | ||
} | ||
|
||
[data-color-mode='light'] input { | ||
color-scheme: light; | ||
} | ||
|
||
[data-color-mode='dark'] input { | ||
color-scheme: dark; | ||
} | ||
|
||
@media (prefers-color-scheme: light) { | ||
[data-color-mode='auto'][data-light-theme*='light'] { | ||
color-scheme: light; | ||
} | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
[data-color-mode='auto'][data-dark-theme*='dark'] { | ||
color-scheme: dark; | ||
} | ||
} | ||
|
||
[role='button']:focus:not(:focus-visible):not(:global(.focus-visible)), | ||
[role='tabpanel'][tabindex='0']:focus:not(:focus-visible):not(:global(.focus-visible)), | ||
button:focus:not(:focus-visible):not(:global(.focus-visible)), | ||
summary:focus:not(:focus-visible):not(:global(.focus-visible)), | ||
a:focus:not(:focus-visible):not(:global(.focus-visible)) { | ||
outline: none; | ||
box-shadow: none; | ||
} | ||
|
||
[tabindex='0']:focus:not(:focus-visible):not(:global(.focus-visible)), | ||
details-dialog:focus:not(:focus-visible):not(:global(.focus-visible)) { | ||
outline: none; | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
|
||
.BaseStyles { | ||
font-family: var(--BaseStyles-fontFamily, var(--fontStack-system)); | ||
/* stylelint-disable-next-line primer/typography */ | ||
line-height: var(--BaseStyles-lineHeight, 1.5); | ||
/* stylelint-disable-next-line primer/colors */ | ||
color: var(--BaseStyles-fgColor, var(--fgColor-default)); | ||
|
||
/* Global styles for light mode */ | ||
&:has([data-color-mode='light']) { | ||
input & { | ||
color-scheme: light; | ||
} | ||
} | ||
|
||
/* Global styles for dark mode */ | ||
&:has([data-color-mode='dark']) { | ||
input & { | ||
color-scheme: dark; | ||
} | ||
} | ||
|
||
/* Low-specificity default link styling */ | ||
:where(a:not([class*='prc-']):not([class*='PRC-']):not([class*='Primer_Brand__'])) { | ||
color: var(--fgColor-accent, var(--color-accent-fg)); | ||
text-decoration: none; | ||
|
||
&:hover { | ||
text-decoration: underline; | ||
} | ||
} | ||
} | ||
` | ||
|
||
export type BaseStylesProps = PropsWithChildren & { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
as?: React.ComponentType<any> | keyof JSX.IntrinsicElements | ||
className?: string | ||
style?: CSSProperties | ||
color?: string // Fixes `color` ts-error | ||
} & SystemTypographyProps & | ||
SystemCommonProps | ||
|
||
export function BaseStyles({ | ||
children, | ||
color, | ||
fontFamily, | ||
lineHeight, | ||
className, | ||
as: Component = 'div', | ||
style, | ||
...rest | ||
}: BaseStylesProps) { | ||
const {colorMode, colorScheme, dayScheme, nightScheme} = useTheme() | ||
|
||
const baseStyles = { | ||
['--BaseStyles-fgColor']: color, | ||
['--BaseStyles-fontFamily']: fontFamily, | ||
['--BaseStyles-lineHeight']: lineHeight, | ||
} | ||
|
||
return ( | ||
<Component | ||
className={clsx('BaseStyles', className)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note to self: might want to call this prc-BaseStyles for consistency |
||
data-portal-root | ||
/** | ||
* We need to map valid primer/react color modes onto valid color modes for primer/primitives | ||
* valid color modes for primer/primitives: auto | light | dark | ||
* valid color modes for primer/primer: auto | day | night | light | dark | ||
*/ | ||
data-color-mode={colorMode === 'auto' ? 'auto' : colorScheme?.includes('dark') ? 'dark' : 'light'} | ||
data-light-theme={dayScheme} | ||
data-dark-theme={nightScheme} | ||
style={{ | ||
...baseStyles, | ||
...style, | ||
}} | ||
{...rest} | ||
> | ||
<GlobalStyle colorScheme={colorScheme?.includes('dark') ? 'dark' : 'light'} /> | ||
{children} | ||
</Component> | ||
) | ||
} |
Oops, something went wrong.
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.
The CSS selector
input &
appears to be invalid. The&
placeholder should come beforeinput
to properly target input elements within the BaseStyles component. Consider changing to& input
instead.Copilot uses AI. Check for mistakes.
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.
Copied it from BaseStyles