This project combines Tailwind CSS & Material UI and Sass in a structured form. Supports theming at the level of these 3 tools.
git clone https://github.com/Spelchure/ReactThemedLogin.git && cd ReactThemedLogin && npm start
theme: {
colors: {
...colors,
/* LIGHT THEME */
primary_fg: colors.neutral[900],
primary_fg_focus: colors.neutral[700],
primary_bg: colors.neutral[400],
/* Add more */
/* DARK THEME */
primary_fg_dark: colors.cyan[500],
primary_fg_focus_dark: colors.cyan[300],
primary_bg_dark: colors.neutral[800],
/* Add more */
},
extend: {},
},
NOTE: Don't forget the add darkMode: 'class' to tailwind.config.js. See: Tailwind CSS DarkMode
For accessing specified theme colors in tailwind.config.js include these colors in ThemeVariables.scss
/* DONT FORGET TO ADD TAILWIND IN YOUR SCSS FILES */
@tailwind base;
@tailwind components;
@tailwind utilities;
/* SETUP LIGHT THEME */
$primary-fg: theme('colors.primary_fg');
/* SETUP DARK THEME */
$primary-fg-dark: theme('colors.primary_fg_dark');
/* Export your colors */
:export {
light: {
primary-fg: $primary-fg; // can be accessed : light-primary-fg
}
dark: {
primary-fg: $primary-fg-dark; // can be access: dark-primary-fg
}
}
You can use ThemeVariables.scss to access global theme configuration.
@import "./path/to/ThemeVariables.scss";
Specify your class names and styles for both light and dark mode to use in components.
.LoginBox {
color: $primary-fg;
}
.LoginBoxDark {
color: $primary-fg-dark;
}
.Input {
@apply flex text-primary_fg /* Color variables in tailwind.config.js */
}
You can style your component giving tailwind css configuration in className:
<SomeComponent className="flex bg-primary_fg_focus m-3 pr-2 dark:bg-primary_fg_focus_dark" />
You can style your component in sass file. (Recommended)
/*SomeComponent.scss*/
@import "/path/to/ThemeVariables.scss";
.SomeComponent {
@apply bg-primary_fg; // apply tailwind
color: $primary-fg; // sass
}
.SomeComponentDark {
@apply bg-primary_fg_dark text-primary_fg;
}
/*SomeComponent.jsx*/
import './SomeComponent.scss';
export default const SomeComponent = () => {
const {theme} = useContext(ThemeContext);
return(
<div className={theme === 'dark' ? "SomeComponentDark" : "SomeComponent"}>
...
</div>
);
}
import ThemeVariabels from "/path/to/ThemeVariables.scss";
/* Styled button */
const StyledButton = styled(Button)(({theme}) => ({
backgroundColor: ThemeVariables[`${theme}-primary-fg`], /* Gets color from ThemeVariables.scss */
color: ThemeVariables[`${theme}-primary-bg`],
'&:hover': {
backgroundColor: ThemeVariables[`${theme}-primary-fg-focus`],
}
}));
export const ThemedButton = (props) => {
const {theme} = React.useContext(ThemeContext);
return ( <StyledButton theme={theme} {...props}/> ); // Return themed button.
}
npx create-react-app react-theme
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
npm install @mui/material @emotion/react @emotion/styled
npm install @mui/icons-material // Optional
npm install clsx // Optional
npm install sass
See ThemeContext.jsx
This project licensed under MIT License