Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/5.x/docs/guides/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<DynamicColorTheme />
<DynamicColorTheme version="5.x" />

<br />

Expand Down
9 changes: 5 additions & 4 deletions docs/6.x/docs/guides/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,22 @@ 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:

<DynamicColorTheme />
<DynamicColorTheme version="6.x" />

<br />

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)

_Source: [Material You Color System](https://m3.material.io/styles/color/the-color-system/custom-colors)_

### 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';
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
166 changes: 137 additions & 29 deletions docs/src/components/DynamicColorTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string, string | Record<string, string>>;

type Preview = {
background: string;
color: string;
borderColor: string;
boxes: {
key: string;
background: string;
color: string;
gridColumn?: string;
}[];
};

type GeneratedThemes = Record<Mode, GeneratedTheme> & {
defaultSecondary: string;
defaultTertiary: string;
preview: Record<Mode, Preview>;
};

const defaultColor = '#663399';

const colorPalette = [
Expand Down Expand Up @@ -315,22 +352,84 @@ 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<string>(defaultColor);
const [secondary, setSecondary] = useState<string | undefined>();
const [tertiary, setTertiary] = useState<string | undefined>();
const [customColors, setCustomColors] = useState<CustomColorEntry[]>([]);

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 (
<div>
Expand All @@ -340,14 +439,14 @@ const DynamicColorTheme = () => {
<AdditionalColor
color={secondary}
setColor={setSecondary}
base={baseTheme.secondary}
base={themes.defaultSecondary}
>
Secondary
</AdditionalColor>
<AdditionalColor
color={tertiary}
setColor={setTertiary}
base={baseTheme.tertiary}
base={themes.defaultTertiary}
>
Tertiary
</AdditionalColor>
Expand All @@ -356,25 +455,41 @@ const DynamicColorTheme = () => {
customColors={customColors}
setCustomColors={setCustomColors}
/>

{version === '6.x' ? (
<tr>
<td>
<label htmlFor="color-picker-match-input-colors">
Match input colors
</label>
</td>
<td>
<Switch
id="color-picker-match-input-colors"
value={matchInputColors}
color="green"
onValueChange={() => setMatchInputColors(!matchInputColors)}
/>
</td>
</tr>
) : null}
</tbody>
</table>

<div
className="color-picker-preview"
style={{
//@ts-ignore
background: themes[darkMode].background,
//@ts-ignore
color: themes[darkMode].onBackground,
//@ts-ignore
borderColor: themes[darkMode].outlineVariant,
background: preview.background,
color: preview.color,
borderColor: preview.borderColor,
}}
>
<div className="color-picker-preview-header">
<h3>Preview</h3>
<div>
<p>Dark Mode:</p>
<Switch
id="color-picker-dark-mode"
value={isDark}
color="green"
onValueChange={() => setIsDark(!isDark)}
Expand All @@ -383,22 +498,15 @@ const DynamicColorTheme = () => {
</div>

<div className="color-picker-grid-column">
{getPreviewColors(themes[darkMode]).map(([key, color]) => {
const gridColumn = key === 'outline' ? 'span 2' : undefined;
return (
<div
key={key}
className="color-picker-preview-box"
style={{
backgroundColor: color,
color: getMatchingColor(key, themes[darkMode]),
gridColumn,
}}
>
{key}
</div>
);
})}
{preview.boxes.map(({ key, background, color, gridColumn }) => (
<div
key={key}
className="color-picker-preview-box"
style={{ backgroundColor: background, color, gridColumn }}
>
{key}
</div>
))}
</div>
</div>

Expand Down
11 changes: 4 additions & 7 deletions docs/src/components/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
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 (
<>
<input
checked={value}
onChange={onValueChange}
className="react-switch-checkbox"
id={`react-switch-new`}
id={id}
type="checkbox"
/>
<label
style={{ background }}
className="react-switch-label"
htmlFor={`react-switch-new`}
>
<label style={{ background }} className="react-switch-label" htmlFor={id}>
<span className={`react-switch-button`} />
</label>
</>
Expand Down
File renamed without changes.
Loading
Loading