[theme] Add HighContrast theme enhancer #48319
Conversation
Deploy preview
Bundle size
Check out the code infra dashboard for more information about this PR. |
7f8ca62 to
27ef658
Compare
cf4a588 to
4e8f75d
Compare
mnajdova
left a comment
There was a problem hiding this comment.
Please add documentation page for it? It will help with the discoverability.
b3d5287 to
8f1ea64
Compare
There was a problem hiding this comment.
Pull request overview
Adds a new Material UI theme enhancer (enhanceHighContrast) that layers Windows High Contrast / forced-colors component overrides on top of an existing theme, and wires it into the docs demo theming plus documentation.
Changes:
- Export
enhanceHighContrastfrom@mui/material/styles(JS + TS entrypoints). - Introduce
enhanceHighContrastimplementation that injects@media (forced-colors: active)overrides for multiple components with configurable system-color tokens. - Update docs demos to apply the enhancer and add documentation describing usage and token customization.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/mui-material/src/styles/index.js | Re-exports the new enhanceHighContrast API from the styles entrypoint. |
| packages/mui-material/src/styles/index.d.ts | Adds TypeScript exports for enhanceHighContrast and HighContrastTokens. |
| packages/mui-material/src/styles/enhanceHighContrast.ts | Implements the theme enhancer and token interface; injects forced-colors overrides for multiple components. |
| packages-internal/core-docs/src/Demo/DemoThemeProviders.tsx | Applies enhanceHighContrast to the docs demo theme setup. |
| docs/data/material/customization/palette/palette.md | Documents Windows High Contrast Mode usage and available tokens. |
Comments suppressed due to low confidence (1)
packages-internal/core-docs/src/Demo/DemoThemeProviders.tsx:56
- The enhancer is applied before
runtimeThemeis deep-merged. This means any demo-providedruntimeThemeoverrides can overwrite/remove the forced-colors component overrides, which is the opposite of the stated goal of layering high-contrast styles on top of an existing theme. Consider deep-mergingruntimeThemefirst and then runningenhanceHighContrast(...)last so HCM overrides reliably win.
const resultTheme = enhanceHighContrast(
createTheme(
{
cssVariables: {
colorSchemeSelector: 'data-mui-color-scheme',
},
colorSchemes: {
light: true,
dark: true,
},
direction: direction as 'ltr' | 'rtl',
},
dense ? highDensity : {},
),
);
if (upperMode) {
Object.assign(resultTheme, resultTheme.colorSchemes[upperMode]);
}
if (runtimeTheme && Object.prototype.toString.call(runtimeTheme) === '[object Object]') {
try {
return deepmerge(resultTheme, runtimeTheme);
} catch {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
23fcc50 to
a67759e
Compare
mj12albert
left a comment
There was a problem hiding this comment.
Implementation looks good ~ commented on a few docs-related things, additionally enhanceHighContrast could be documented here as well: https://master--material-ui.netlify.app/material-ui/customization/theming/#api
| import { Theme } from './createTheme'; | ||
|
|
||
| export interface HighContrastTokens { | ||
| /** Color for disabled elements. Default: `'GrayText'` */ |
There was a problem hiding this comment.
| /** Color for disabled elements. Default: `'GrayText'` */ | |
| /** | |
| * Color for disabled elements. | |
| * @default 'GrayText' | |
| */ |
We use the JSDOM default tag for default values. Please update everywhere.
| * Follows the same signature as `responsiveFontSizes`: accepts a fully-created | ||
| * theme, merges in HCM component overrides using arrays so that Emotion emits | ||
| * each entry as a separate CSS rule and the browser cascade (rather than JS |
There was a problem hiding this comment.
| * Follows the same signature as `responsiveFontSizes`: accepts a fully-created | |
| * theme, merges in HCM component overrides using arrays so that Emotion emits | |
| * each entry as a separate CSS rule and the browser cascade (rather than JS | |
| * Accepts a fully-created theme, merges in HCM component overrides using arrays so that Emotion emits | |
| * each entry as a separate CSS rule and the browser cascade (rather than JS |
No need to reference other utils here. We'll need to remember to update this comment if that other util changes in the future.
|
|
||
| The following tokens are available: | ||
|
|
||
| | Token | Default | Description | |
There was a problem hiding this comment.
Can we use the theme here and show the values for each color? We are already doing it for the regular palette. This table will need to be extracted in a Demo tough, so you can get access to the theme.
200607f to
44cdadd
Compare
| import Typography from '@mui/material/Typography'; | ||
| import { styled } from '@mui/material/styles'; | ||
|
|
||
| const tokens = [ |
There was a problem hiding this comment.
Ideally we should have extracted this out of API generated code. @dav-is it may be interesting to have a JSON generated for the theme (or parts of it) that we need to document. We could extract this object out of the theme object, but we would miss the description & the default values.
Added the changes (excluding the slider borders) to a separate theme object.
To be used like so:
Closes #48334
Closes #13174
Preview: https://deploy-preview-48319--material-ui.netlify.app/material-ui/customization/palette/#windows-high-contrast-mode
Improves support for Dark High Contrast by adding a new theme support. The theme support will merge HCL styles on top of an existing theme.
GrayTextfor border, placeholder and text.markfor border and text.Highlightfor border,HighlightTextfor text, andnoneforforcedColorAdjust.Colors were chosen based on this doc page.