How to use themes in your project
Enable usage of themes
Firstly, you should enable usage of themes in the config.
You can enable usage of themes for colors or effects now.
Set an array of allowedThemes and defaultTheme in Config -> styles
.
module.exports = {
...
allowedThemes: ['light','dark'],
defaultTheme: 'light',
styles: {
exportPath: './theme',
...
},
...
};
For more information to follow there
Launch the extractor.
After successfully launching of the extractor you see created files like that:
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/naming-convention */
// THIS FILE IS GENERATED AUTOMATICALLY. DON'T CHANGE IT.
export const DEFAULT_THEME = 'light';
export const THEMES = ['dark', 'light'];
export type Theme = (typeof THEMES)[number];
Install next-themes
package in your project
Generated files can be used with https://github.com/pacocoursey/next-themes (opens in a new tab).
next-themes
is a great solution for switching themes.
Include received themes in next-themes
Inside app.tsx
you should add following code:
import { THEMES, DEFAULT_THEME } from '@sh/ui/theme/themes-list';
<ThemeProvider attribute="data-theme" defaultTheme={DEFAULT_THEME} themes={THEMES}>
{children}
</ThemeProvider>
Include generated js files in tailwind.config.ts
import { colors} from './theme/colors/with-vars';
import { effects } from './theme/effects/with-vars';
/** @type {import('tailwindcss').Config} */
const config = {
...
theme: {
...
...effects,
colors: {
transparent: 'transparent',
current: 'currentColor',
...colors,
},
},
...
};
export default config;
Note: You should import with-vars.ts
file instead of index.ts
.
index.ts
contents real data from Figma and it's been left only for legacy code.
with-vars.ts
contents CSS variables for correctly working with themes.
Include generated CSS files in global-styles
.
You should include all CSS variables for each theme and a root theme.
In ui/organisms/global-styles/index.css
add following lines:
@import "@sh/ui/theme/colors/vars.css";
@import "@sh/ui/theme/colors/light/vars.css";
@import "@sh/ui/theme/colors/dark/vars.css";
@import "@sh/ui/theme/effects/vars.css";
@import "@sh/ui/theme/effects/light/vars.css";
@import "@sh/ui/theme/effects/dark/vars.css";
@tailwind base;
@tailwind components;
@tailwind utilities;
Finish.
Now you can use tailwind's classes as usually.