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 25, 2024
1 parent 67fd9bd commit 7e74c2a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 72 deletions.
13 changes: 6 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,11 @@ const EmotionCacheProvider = ({
};

const App = (props) => {
const customTheme = useConst(() => createTheme(theme, {
const customTheme = useConst(() => createTheme({
config: {
// Enable CSS variables replacement
useCSSVariables: true,
},
components: {
// Set default props for specific components
//
Expand All @@ -64,10 +67,6 @@ const App = (props) => {
// },
// ```
},
config: {
// Enable CSS variables replacement
useCSSVariables: true,
},
}));
const [initialColorMode, setColorMode] = useState(null);
const router = useRouter();
Expand Down
36 changes: 4 additions & 32 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,13 +41,12 @@ 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, {
const customTheme = createTheme({
config: {
// Enable CSS variables replacement
useCSSVariables: true,
Expand Down Expand Up @@ -218,7 +191,7 @@ Tonic UI supports converting theme tokens defined in the theme to CSS variables.

```jsx
<TonicProvider
theme={merge(theme, {
theme={createTheme({
config: {
useCSSVariables: true,
},
Expand Down Expand Up @@ -325,12 +298,11 @@ 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, {
const customTheme = createTheme({
config: {
useCSSVariables: true,
},
Expand Down
11 changes: 7 additions & 4 deletions packages/react-docs/pages/theme/breakpoints/index.page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ To do this, add a `breakpoints` array with additional aliases (e.g. `sm`, `md`,

```jsx disabled
// 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>
);
}
```
3 changes: 1 addition & 2 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,7 +73,7 @@ const customColorStyle = merge(colorStyle, {
},
});
const customTheme = createTheme(theme, {
const customTheme = createTheme({
config: {
// Enable CSS variables replacement
useCSSVariables: true,
Expand Down
37 changes: 12 additions & 25 deletions packages/react/src/theme/createTheme.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import tonicTheme from '@tonic-ui/theme';
import { merge } from '@tonic-ui/utils';
import { ensurePlainObject } from 'ensure-type';
import createCSSVariableMap from './utils/createCSSVariableMap';

const defaultCSSVariablePrefix = 'tonic';

const cssVariableScales = [
'borders',
'breakpoints',
'colors',
'fonts',
'fontSizes',
'fontWeights',
'letterSpacings',
'lineHeights',
'outlines',
'radii',
'shadows',
'sizes',
'space',
'zIndices',
];
const cssVariableScales = Object.keys(tonicTheme);

const createTheme = (options = {}, ...args) => {
// Merge provided options with default configurations
let theme = merge(options, {
config: {
prefix: defaultCSSVariablePrefix,
useCSSVariables: false,
let theme = merge(
{
...tonicTheme,
config: {
prefix: defaultCSSVariablePrefix,
useCSSVariables: false,
},
components: {},
},
});

// Ensure the components field is initialized
theme.components = theme.components ?? {};
options,
);

// Merge additional arguments into the theme
theme = args.reduce((acc, arg) => merge(acc, arg), theme);
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/theme/theme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tonicTheme from '@tonic-ui/theme';
import createTheme from './createTheme';

const theme = createTheme(tonicTheme);
const theme = createTheme();

export default theme;
1 change: 1 addition & 0 deletions packages/theme/src/createTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const createTheme = (unit) => {
'px': _px(foundation),
}[unit] ?? foundation;

// TODO: Consider adding `Object.freeze(theme)` in a future major release
return theme;
};

Expand Down

0 comments on commit 7e74c2a

Please sign in to comment.