Skip to content

Instantly share code, notes, and snippets.

@NuroDev
Last active July 8, 2022 09:54
Show Gist options
  • Save NuroDev/b25e2381e2ec07de8ba9b5a870953b08 to your computer and use it in GitHub Desktop.
Save NuroDev/b25e2381e2ec07de8ba9b5a870953b08 to your computer and use it in GitHub Desktop.
πŸ‘©β€πŸŽ¨ TypeTheme ─ Theme based class names styling using Tailwind CSS
import clsx from 'clsx';
type Theme<TThemeType extends string> = {
[TType in TThemeType]: string;
} & {
default: string;
};
export const getTheme = <TThemeType extends string>(
theme: TThemeType | 'default' = 'default',
classes: Theme<TThemeType>
): string => classes[theme];
type MyCustomTheme = 'primary' | 'secondary';
interface ExampleButtonProps {
theme?: MyCustomTheme;
}
export function ExampleButton({ theme }: ExampleButtonProps) {
return (
<div
className={clsx(
// Base styles
'w-full h-8',
// Get the class names from an optionally defined theme
getTheme<MyCustomTheme>(theme, {
primary: 'bg-blue-500 text-white',
secondary: 'bg-purple-500 text-purple-100',
default: 'bg-gray-900 text-gray-100',
})
)}
>
<h1>Hello World</h1>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment