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
[system] Fix color-scheme implementation #34639
Merged
Merged
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
88bd060
add resolved mode and add comments
siriwatknp b1fa4d8
remove unnecessary enableColorScheme
siriwatknp 1964a6b
use palette.mode as color-scheme
siriwatknp b69e21b
add mode to palette in joy
siriwatknp d971b0b
remove enableColorScheme test (moved to regression)
siriwatknp 119858c
fix joy dark mode
siriwatknp c1ae81a
remove omit
siriwatknp fd3268c
add initial setup from design system
siriwatknp 317b915
revert to make code review easier
siriwatknp 51fa80e
move enable color scheme to CssBaseline
siriwatknp a79c794
Add CssBaseline to Joy
siriwatknp b5da1b6
run proptypes
siriwatknp 88b48f7
fix lint
siriwatknp 23c27ee
fix backdrop color
siriwatknp bea2b11
fix Joy color scheme styles
siriwatknp 784a5f6
add visual regression
siriwatknp 4ae0ff0
fix color scheme styles generation
siriwatknp d7456f9
fix implementation
siriwatknp 4187694
apply logic to ScopedCssBaseline
siriwatknp fb0a14b
update regression
siriwatknp 65eaac9
fix mui CssBaseline
siriwatknp 3c61b0b
fix logic and regression
siriwatknp 269d647
fix naming
siriwatknp 2b51a0c
flip the flag to disableColorScheme
siriwatknp 49a4707
Add ScopedCssBaseline to Joy
siriwatknp c1822b3
Add JoyScopedBaseline regression
siriwatknp 5bc8db9
add unit tests
siriwatknp 44ac5cb
export GlobalStyles
siriwatknp 89681bc
add CssBaseline doc
siriwatknp 8fa3c77
fix tests
siriwatknp f4a7d86
Update packages/mui-joy/src/styles/defaultTheme.ts
siriwatknp de1ab16
content fix
siriwatknp 1bfdb50
Merge branch 'system/fix-server-mode' of github.com:siriwatknp/materi…
siriwatknp File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,84 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { GlobalStyles } from '@mui/system'; | ||
import { Theme, DefaultColorScheme, ColorSystem } from '../styles/types'; | ||
import { CssBaselineProps } from './CssBaselineProps'; | ||
|
||
/** | ||
* Kickstart an elegant, consistent, and simple baseline to build upon. | ||
*/ | ||
function CssBaseline({ children, enableColorScheme = false }: CssBaselineProps) { | ||
return ( | ||
<React.Fragment> | ||
<GlobalStyles | ||
styles={(theme: Theme) => { | ||
const colorSchemeStyles: Record<string, any> = {}; | ||
if (enableColorScheme) { | ||
// The CssBaseline is wrapped inside a CssVarsProvider | ||
( | ||
Object.entries(theme.colorSchemes) as Array<[DefaultColorScheme, ColorSystem]> | ||
).forEach(([key, scheme]) => { | ||
colorSchemeStyles[theme.getColorSchemeSelector(key).replace(/\s*&/, '')] = { | ||
colorScheme: scheme.palette?.mode, | ||
}; | ||
}); | ||
} | ||
return { | ||
html: { | ||
WebkitFontSmoothing: 'antialiased', | ||
MozOsxFontSmoothing: 'grayscale', | ||
// Change from `box-sizing: content-box` so that `width` | ||
// is not affected by `padding` or `border`. | ||
boxSizing: 'border-box', | ||
// Fix font resize problem in iOS | ||
WebkitTextSizeAdjust: '100%', | ||
}, | ||
'*, *::before, *::after': { | ||
boxSizing: 'inherit', | ||
}, | ||
'strong, b': { | ||
fontWeight: theme.vars.fontWeight.lg, | ||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
body: { | ||
margin: 0, // Remove the margin in all browsers. | ||
color: theme.vars.palette.text.primary, | ||
...(theme.typography.body1 as any), | ||
backgroundColor: theme.vars.palette.background.body, | ||
'@media print': { | ||
// Save printer ink. | ||
backgroundColor: theme.vars.palette.common.white, | ||
}, | ||
// Add support for document.body.requestFullScreen(). | ||
// Other elements, if background transparent, are not supported. | ||
'&::backdrop': { | ||
backgroundColor: theme.vars.palette.background.backdrop, | ||
}, | ||
}, | ||
...colorSchemeStyles, | ||
}; | ||
}} | ||
/> | ||
{children} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
CssBaseline.propTypes /* remove-proptypes */ = { | ||
// ----------------------------- Warning -------------------------------- | ||
// | These PropTypes are generated from the TypeScript type definitions | | ||
// | To update them edit TypeScript types and run "yarn proptypes" | | ||
// ---------------------------------------------------------------------- | ||
/** | ||
* You can wrap a node. | ||
*/ | ||
children: PropTypes.node, | ||
/** | ||
* Enable `color-scheme` CSS property to use `theme.palette.mode`. | ||
* For more details, check out https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme | ||
* For browser support, check out https://caniuse.com/?search=color-scheme | ||
* @default false | ||
*/ | ||
enableColorScheme: PropTypes.bool, | ||
} as any; | ||
|
||
export default CssBaseline; |
This file contains 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,13 @@ | ||
export interface CssBaselineProps { | ||
/** | ||
* You can wrap a node. | ||
*/ | ||
children?: React.ReactNode; | ||
/** | ||
* Enable `color-scheme` CSS property to use `theme.palette.mode`. | ||
* For more details, check out https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme | ||
* For browser support, check out https://caniuse.com/?search=color-scheme | ||
* @default false | ||
*/ | ||
enableColorScheme?: boolean; | ||
} |
This file contains 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,2 @@ | ||
export { default } from './CssBaseline'; | ||
export * from './CssBaselineProps'; |
This file contains 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 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 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 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 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 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 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 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 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.
Can you clarify what is this doing?
We have it in many places, maybe we should extract the logic?
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.
theme.getColorSchemeSelector(key)
returns a string,[data-joy-color-scheme="$key"] &
, with an&
at the end so that developers can use it to write custom styles instyled
orsx
like this:However, the CssBaseline define the selector as global so the
.replace(/\s*&/, '')
is required to remove&
at the end:I think this case would be rare but I could provide theattribute
from the theme to be used like: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.
Thinking again, I rather go with
theme.getColorSchemeSelector(key).replace(/\s*&/, '')
for now because I don't want to add a new property to the theme. Let's wait to see how the community would like to use the API.