Skip to content

Commit

Permalink
feat: enhance the createTheme function
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Nov 26, 2024
1 parent 67fd9bd commit 754d9f5
Show file tree
Hide file tree
Showing 49 changed files with 254 additions and 245 deletions.
10 changes: 3 additions & 7 deletions packages/react-docs/pages/_app.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
ToastManager,
TonicProvider,
colorStyle as defaultColorStyle,
createTheme,
theme,
createTheme, // For theme customization (introduced in v2.5.0)
useTheme,
} from '@tonic-ui/react';
import {
Expand Down Expand Up @@ -51,7 +50,8 @@ const EmotionCacheProvider = ({
};

const App = (props) => {
const customTheme = useConst(() => createTheme(theme, {
const customTheme = useConst(() => createTheme({
cssVariables: true, // Enable CSS variables replacement
components: {
// Set default props for specific components
//
Expand All @@ -64,10 +64,6 @@ const App = (props) => {
// },
// ```
},
config: {
// Enable CSS variables replacement
useCSSVariables: true,
},
}));
const [initialColorMode, setColorMode] = useState(null);
const router = useRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import React from 'react';
describe('Code', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true, // Enable CSS variables replacement
};
const { container } = render((
<Code>Hello World</Code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ const isColorCode = value => String(value).startsWith('#') || String(value).star
const App = () => {
const theme = useTheme();
const [colorMode] = useColorMode();
const cssVariableMap = theme.__cssVariableMap;
const borderColor = {
const borderColor = {
dark: 'gray:60',
light: 'gray:30',
}[colorMode];
light: 'gray:30',
}[colorMode];
const tokenColor = {
dark: 'red:50',
light: 'red:60',
}[colorMode];

return (
<Box fontFamily="mono">
{Object.entries(cssVariableMap).map(([name, value]) => {
{Object.entries(theme?.vars).map(([name, value]) => {
if (!name.startsWith('--')) {
return null;
}

return (
<Flex key={name} columnGap="2x">
<Text color={tokenColor}>{name}:</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
Tonic UI supports converting theme tokens defined in the theme to CSS variables. You can enable this behavior by configuring the theme as shown below:

```jsx
import { TonicProvider, theme } from '@tonic-ui/react';

// Enable CSS variables
theme.config.useCSSVariables = true;

// Pass `theme` to the `TonicProvider`
<TonicProvider theme={theme} />
// Use `createTheme` to provide custom configuration
<TonicProvider
theme={createTheme({ // `createTheme` was introduced in v2.5.0
cssVariables: true, // Enable CSS variables replacement
})}
/>
```

For example, consider a theme object that looks like this:

```js disabled
```js
const theme = {
colors: {
'gray:10': '#fafafa',
Expand Down
53 changes: 9 additions & 44 deletions packages/react-docs/pages/getting-started/usage/index.page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,14 @@ import React from 'react';
import {
Box,
TonicProvider, // Provides theme and context for Tonic UI components
colorStyle, // Default color style object
createTheme, // For theme customization (introduced in v2.5.0)
theme, // Default theme configuration
} from '@tonic-ui/react';

const customTheme = createTheme(theme, {
config: {
// Enable CSS variables replacement
useCSSVariables: true,
},
components: {
// Set default props for specific components
//
// Example:
// ```
// 'ToastCloseButton': {
// defaultProps: {
// 'aria-label': 'Close toast',
// },
// },
// ```
},
});

function App(props) {
return (
<TonicProvider
colorMode={{
defaultValue: 'dark', // One of: 'dark', 'light'
}}
colorStyle={{
defaultValue: colorStyle, // optional
}}
theme={customTheme} // optional
useCSSBaseline={true} // If `true`, apply CSS reset and base styles
>
<Box {...props} />
Expand All @@ -67,17 +41,13 @@ import {
TonicProvider, // Provides theme and context for Tonic UI components
colorStyle, // Default color style object
createTheme, // For theme customization (introduced in v2.5.0)
theme, // Default theme configuration
useColorMode, // Hook to manage color mode (e.g., light/dark)
useColorStyle, // Hook to manage color style
useTheme, // Hook to access the theme object
} from '@tonic-ui/react';

const customTheme = createTheme(theme, {
config: {
// Enable CSS variables replacement
useCSSVariables: true,
},
const customTheme = createTheme({
cssVariables: true, // Enable CSS variables replacement
components: {
// Set default props for specific components
//
Expand All @@ -99,9 +69,9 @@ function App(props) {
defaultValue: 'dark', // One of: 'dark', 'light'
}}
colorStyle={{
defaultValue: colorStyle, // optional
defaultValue: colorStyle,
}}
theme={customTheme} // optional
theme={customTheme}
useCSSBaseline={true} // If `true`, apply CSS reset and base styles
>
<ToastManager
Expand Down Expand Up @@ -218,10 +188,8 @@ Tonic UI supports converting theme tokens defined in the theme to CSS variables.

```jsx
<TonicProvider
theme={merge(theme, {
config: {
useCSSVariables: true,
},
theme={createTheme({
cssVariables: true, // Enable CSS variables replacement
})}
/>
```
Expand Down Expand Up @@ -325,15 +293,12 @@ Override the theme object and pass it to the `<ThemeProvider>` component.
```js
import {
createTheme,
theme,
createTheme, // For theme customization (introduced in v2.5.0)
} from '@tonic-ui/react';

// Let's say you want to add more colors
const customTheme = createTheme(theme, {
config: {
useCSSVariables: true,
},
const customTheme = createTheme({
cssVariables: true, // Enable CSS variables replacement
colors: {
brand: {
90: "#1a365d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ console.log(colorStyle.chart.classic.colors[0]);

To use previously defined `severity` and `chart` colors, you need to pass a custom color style to `TonicProvider`. Follow the setup below to use previous `severity` and `chart` colors:

```jsx disabled
```jsx
import React from 'react';
import { Box, TonicProvider, colorStyle } from '@tonic-ui/react';
import { merge } from '@tonic-ui/utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ To use the responsive values, you can use either the array or object syntax.

Let's say you have a `Box` component that looks like this:

```jsx disabled
```jsx
<Box backgroundColor="red:50" width="50%">
This is a box
</Box>
```

To make the `width` responsive, you can use the array syntax as follows:

```jsx disabled
```jsx
<Box backgroundColor="red:50" width={["25%","50%","75%","100%"]}>
This is a box
</Box>
Expand All @@ -45,15 +45,15 @@ You can also use the object syntax to define responsive values. It's also the re

Let's say you have a `Text` component that looks like this:

```jsx disabled
```jsx
<Text fontSize={32}>
This is a text
</Text>
```

To make the `fontSize` responsive, you can use the object syntax as follows:

```jsx disabled
```jsx
<Text fontSize={{ _: 24, md: 32, lg: 40, xl: 48 }}>
This is a text
</Text>
Expand All @@ -72,7 +72,7 @@ Here is how to interpret the object syntax:

Given the following example:

```jsx disabled
```jsx
<Box width={{ _: '100%', sm: '50%', md: '25%' }}>
This is a box
</Box>
Expand Down
13 changes: 8 additions & 5 deletions packages/react-docs/pages/theme/breakpoints/index.page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ For more control over the breakpoints, you can define custom breakpoints for you

To do this, add a `breakpoints` array with additional aliases (e.g. `sm`, `md`, `lg`, `xl`, and `2xl`) to your theme.

```jsx disabled
```jsx
// 1. Import the theme
import { ThemeProvider, createTheme, theme } from '@tonic-ui/react';
import {
TonicProvider, // Provides theme and context for Tonic UI components
createTheme, // For theme customization (introduced in v2.5.0)
} from '@tonic-ui/react';

// 2. Update the breakpoints
const breakpoints = [
Expand All @@ -59,16 +62,16 @@ breakpoints.xl = breakpoints[3];
breakpoints['2xl'] = breakpoints[4];

// 3. Extend the theme
const customTheme = createTheme(theme, {
const customTheme = createTheme({
breakpoints,
});

// 4. Pass the custom theme to the theme provider
function App() {
return (
<ThemeProvider theme={customTheme}>
<TonicProvider theme={customTheme}>
{children}
</ThemeProvider>
</TonicProvider>
);
}
```
8 changes: 2 additions & 6 deletions packages/react-docs/sandbox/create-react-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
TonicProvider,
colorStyle,
createTheme,
theme,
useColorMode,
useColorStyle,
useTheme,
Expand Down Expand Up @@ -74,11 +73,8 @@ const customColorStyle = merge(colorStyle, {
},
});
const customTheme = createTheme(theme, {
config: {
// Enable CSS variables replacement
useCSSVariables: true,
},
const customTheme = createTheme({
cssVariables: true, // Enable CSS variables replacement
components: {
// Set default props for specific components
//
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/accordion/__tests__/Accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React from 'react';
describe('Accordion', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<Accordion rowGap={1}>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/badge/__tests__/Badge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from 'react';
describe('Badge', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<Flex columnGap="4x">
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/button/__tests__/ButtonBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React from 'react';
describe('ButtonBase', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/checkbox/__tests__/Checkbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { useEffect, useRef } from 'react';
describe('Checkbox', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/code/__tests__/Code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from 'react';
describe('Code', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<Code data-testid="Code">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('DatePicker', () => {
it('should render correctly', async () => {
const user = userEvent.setup();
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const defaultValue = new Date('2024-08-01');
const inputFormat = 'yyyy-MM-dd';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/divider/__tests__/Divider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React from 'react';
describe('Divider', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/drawer/__tests__/Drawer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import React, { useCallback, useRef, useState } from 'react';
describe('Drawer', () => {
it('should render correctly', async () => {
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { baseElement } = render((
<Drawer
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/input/__tests__/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Input', () => {
const sizes = ['sm', 'md', 'lg'];
const variants = ['outline', 'filled', 'flush', 'unstyled'];
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/input/__tests__/InputControl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('InputControl', () => {
const sizes = ['sm', 'md', 'lg'];
const variants = ['outline', 'filled', 'flush', 'unstyled'];
const renderOptions = {
useCSSVariables: true,
cssVariables: true,
};
const { container } = render((
<>
Expand Down
Loading

0 comments on commit 754d9f5

Please sign in to comment.