diff --git a/docs/5.x/docs/guides/theming.mdx b/docs/5.x/docs/guides/theming.mdx index d2d4392fe8..15275e785a 100644 --- a/docs/5.x/docs/guides/theming.mdx +++ b/docs/5.x/docs/guides/theming.mdx @@ -182,7 +182,7 @@ export default function Main() { Dynamic Color Themes allows for generating two color schemes lightScheme and darkScheme, based on the provided source color. Created schemes are following the Material Design 3 color system and covering colors structure from the Paper theme. User may generate these schemes using the following tool: - +
diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index d62fcfefef..4c64001638 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -219,13 +219,14 @@ export default function Main() { ## Creating dynamic theme colors Dynamic Color Themes allows for generating two color schemes lightScheme and darkScheme, based on the provided source color. -Created schemes are following the Material Design 3 color system and covering colors structure from the Paper theme. User may generate these schemes using the following tool: +Created schemes are built with Material's dynamic color scheme and contain every color role of the Paper theme. User may generate these schemes using the following tool: - +
Passed source color into the util is translated into tones to automatically provide the range of tones that map to color roles. +By default, the colors follow Material's default scheme, which tones down the chroma of the picked colors. Turn on **Match input colors** to keep the generated colors, such as `primaryContainer`, close to the picked colors, like the color match option of the [Material Theme Builder](https://material-foundation.github.io/material-theme-builder/). ![customColors](../../../public/screenshots/custom-colors.png) @@ -233,7 +234,7 @@ _Source: [Material You Color System](https://m3.material.io/styles/color/the-col ### Using schemes -Once we have copied the color schemes from the generated JSON above, we can use by passing it to the colors in `theme` object as below: +Once we have copied the color schemes from the generated JSON above, we can use by passing it to the colors in `theme` object as below. Use `LightTheme` with the light scheme and `DarkTheme` with the dark scheme: ```jsx import * as React from 'react'; @@ -242,7 +243,7 @@ import App from './src/App'; const theme = { ...LightTheme, - colors: yourGeneratedLightOrDarkScheme.colors, // Copy it from the color codes scheme and then use it here + colors: yourGeneratedLightScheme.colors, // Copy it from the color codes scheme and then use it here }; export default function Main() { diff --git a/docs/package.json b/docs/package.json index 50d5a6c934..dac4f98e26 100644 --- a/docs/package.json +++ b/docs/package.json @@ -14,7 +14,7 @@ "@callstack/react-theme-provider": "^3.0.9", "@callstack/rspress-preset": "0.6.6", "@callstack/rspress-theme": "0.6.6", - "@material/material-color-utilities": "0.2.4", + "@material/material-color-utilities": "0.3.0", "@react-native-vector-icons/material-design-icons": "^12.0.0", "@rspress/core": "2.0.14", "camelcase": "^7.0.1", diff --git a/docs/src/components/DynamicColorTheme.tsx b/docs/src/components/DynamicColorTheme.tsx index 1203992e7d..c20bb49413 100644 --- a/docs/src/components/DynamicColorTheme.tsx +++ b/docs/src/components/DynamicColorTheme.tsx @@ -10,8 +10,13 @@ import { getMatchingColor, getPreviewColors, hexThemeFromColor, - prepareThemes, -} from '../utils/themes'; + prepareThemes as prepareThemes5x, +} from '../utils/themes5x'; +import { + getDefaultAccentColors, + prepareThemes as prepareThemes6x, + previewColors, +} from '../utils/themes6x'; type ColorProps = { color?: string; @@ -32,6 +37,38 @@ type CustomColorEntry = { hex: string; }; +type Version = '5.x' | '6.x'; + +type ThemeInput = { + primary: string; + secondary?: string; + tertiary?: string; + custom: [string, string][]; + matchInputColors: boolean; +}; + +type Mode = 'light' | 'dark'; + +type GeneratedTheme = Record>; + +type Preview = { + background: string; + color: string; + borderColor: string; + boxes: { + key: string; + background: string; + color: string; + gridColumn?: string; + }[]; +}; + +type GeneratedThemes = Record & { + defaultSecondary: string; + defaultTertiary: string; + preview: Record; +}; + const defaultColor = '#663399'; const colorPalette = [ @@ -315,8 +352,69 @@ const CustomColor = ({ ); }; -const DynamicColorTheme = () => { +const generateThemes5x = (colors: ThemeInput): GeneratedThemes => { + const baseTheme = hexThemeFromColor(colors.primary); + const themes = prepareThemes5x(colors); + + const getPreview = (mode: Mode): Preview => { + const theme = themes[mode]; + // These roles come from the primary color only + const { background, onBackground, outlineVariant } = hexThemeFromColor( + colors.primary, + mode + ); + + return { + background, + color: onBackground, + borderColor: outlineVariant, + boxes: getPreviewColors(theme).map(([key, color]) => ({ + key, + background: color, + color: getMatchingColor(key, theme), + gridColumn: key === 'outline' ? 'span 2' : undefined, + })), + }; + }; + + return { + ...themes, + defaultSecondary: baseTheme.secondary, + defaultTertiary: baseTheme.tertiary, + preview: { light: getPreview('light'), dark: getPreview('dark') }, + }; +}; + +const generateThemes6x = (colors: ThemeInput): GeneratedThemes => { + const defaults = getDefaultAccentColors( + colors.primary, + colors.matchInputColors + ); + const themes = prepareThemes6x(colors); + + const getPreview = (theme: (typeof themes)[Mode]): Preview => ({ + background: theme.background, + color: theme.onBackground, + borderColor: theme.outlineVariant, + boxes: previewColors.map(([key, text, columns]) => ({ + key, + background: theme[key], + color: theme[text], + gridColumn: columns ? `span ${columns}` : undefined, + })), + }); + + return { + ...themes, + defaultSecondary: defaults.secondary, + defaultTertiary: defaults.tertiary, + preview: { light: getPreview(themes.light), dark: getPreview(themes.dark) }, + }; +}; + +const DynamicColorTheme = ({ version }: { version: Version }) => { const [isDark, setIsDark] = useState(false); + const [matchInputColors, setMatchInputColors] = useState(false); const [primary, setPrimary] = useState(defaultColor); const [secondary, setSecondary] = useState(); @@ -324,13 +422,14 @@ const DynamicColorTheme = () => { const [customColors, setCustomColors] = useState([]); const darkMode = isDark ? 'dark' : 'light'; - const baseTheme = hexThemeFromColor(primary); - const themes = prepareThemes({ + const themes = (version === '5.x' ? generateThemes5x : generateThemes6x)({ primary, secondary, tertiary, custom: customColors.map(({ name, hex }) => [name, hex]), + matchInputColors, }); + const preview = themes.preview[darkMode]; return (
@@ -340,14 +439,14 @@ const DynamicColorTheme = () => { Secondary Tertiary @@ -356,18 +455,33 @@ const DynamicColorTheme = () => { customColors={customColors} setCustomColors={setCustomColors} /> + + {version === '6.x' ? ( + + + + + + setMatchInputColors(!matchInputColors)} + /> + + + ) : null}
@@ -375,6 +489,7 @@ const DynamicColorTheme = () => {

Dark Mode:

setIsDark(!isDark)} @@ -383,22 +498,15 @@ const DynamicColorTheme = () => {
- {getPreviewColors(themes[darkMode]).map(([key, color]) => { - const gridColumn = key === 'outline' ? 'span 2' : undefined; - return ( -
- {key} -
- ); - })} + {preview.boxes.map(({ key, background, color, gridColumn }) => ( +
+ {key} +
+ ))}
diff --git a/docs/src/components/Switch.tsx b/docs/src/components/Switch.tsx index 30b7e62d03..1389cd0de2 100644 --- a/docs/src/components/Switch.tsx +++ b/docs/src/components/Switch.tsx @@ -1,10 +1,11 @@ interface SwitchProps { + id: string; value: boolean; onValueChange?: () => void; color: string; } -const Switch = ({ value, onValueChange, color }: SwitchProps) => { +const Switch = ({ id, value, onValueChange, color }: SwitchProps) => { const background = value ? color : 'none'; return ( <> @@ -12,14 +13,10 @@ const Switch = ({ value, onValueChange, color }: SwitchProps) => { checked={value} onChange={onValueChange} className="react-switch-checkbox" - id={`react-switch-new`} + id={id} type="checkbox" /> -