classname-theme
is a lightweight library that allows you to configure and generate themes for components using Tailwind classes quickly. It’s designed to provide flexibility in managing variants, sizes, states, and more for component customization.
Installation To install the library, use npm:
npm install classname-theme
The main function of the library is defineTheme, which lets you define a theme and then generate classes easily for your components.
defineTheme
:import { defineTheme } from 'classname-theme';
const simpleTheme = {
initial: { variant: 'primary' },
variants: {
primary: 'bg-blue-500 text-white',
secondary: 'bg-gray-500 text-white',
},
sizes: {
small: 'text-sm',
large: 'text-lg',
},
};
const cnt = defineTheme(simpleTheme);
// Using the default 'primary' variant
cnt(); // 'bg-blue-500 text-white'
// Specifying a variant
cnt({ variant: 'secondary' }); // 'bg-gray-500 text-white'
Themes are objects that allow you to define variants, sizes, states, flags, and options in a flexible way. Here are the main keys you can use:
You can customize the generated classes using several parameters:
// Defining a more complex theme
const myTheme = defineTheme({
variants: {
primary: 'bg-green-500 text-white',
secondary: 'bg-yellow-500 text-black',
},
sizes: {
medium: 'text-md',
large: 'text-xl',
},
states: {
loading: 'animate-pulse',
ok: '',
},
flags: {
rounded: 'rounded-full',
},
});
// Uso
cnt({
variant: 'secondary',
size: 'large',
flags: ['rounded'],
state: 'loading',
});
// Generates: 'bg-yellow-500 text-black text-xl rounded-full animate-pulse'
You can extend your theme to add more customization for specific parts of a component using the extensions key:
const complexTheme = defineTheme({
variants: {
primary: 'bg-blue-600',
},
extensions: {
button: {
sizes: {
small: 'py-2 px-4',
large: 'py-4 px-8',
},
},
},
});
// Generate classes for a specific button
cnt({ size: 'large', ext: 'button' }); // 'py-4 px-8'
This project is licensed under the MIT License.