css-variants
Version:
Lightweight helpers to compose class names and inline styles using variants. Zero runtime deps, small bundle, and first-class TypeScript support.
53 lines (50 loc) • 2.08 kB
text/typescript
import { O as ObjectKeyArrayPicker, a as ObjectKeyPicker } from './types-BXKyjXhG.cjs';
import cx, { ClassValue } from './cx.cjs';
import 'csstype';
type ClassVariantRecord = Record<string, Record<string, ClassValue>>;
type ClassVariantExtendProps = {
className: ClassValue;
};
interface ClassVariantDefinition<T extends ClassVariantRecord | undefined> {
base?: ClassValue;
variants?: T;
compoundVariants?: (ObjectKeyArrayPicker<T> & ClassVariantExtendProps)[];
defaultVariants?: ObjectKeyPicker<T>;
classNameResolver?: typeof cx;
}
type ClassVariantFnProps<T extends ClassVariantRecord | undefined> = T extends undefined ? Partial<ClassVariantExtendProps> : ObjectKeyPicker<T> & Partial<ClassVariantExtendProps>;
type ClassVariantFn<T extends ClassVariantRecord | undefined> = (props?: ClassVariantFnProps<T>) => string;
type ClassVariantCreatorFn = <T extends ClassVariantRecord | undefined>(config: ClassVariantDefinition<T>) => ClassVariantFn<T>;
/**
* Creates a class variant function that combines base classes, variants, compound variants, and default variants.
*
* @template T - Type of the variant record
* @param config - Configuration object for creating class variants
* @returns A function that accepts variant props and returns a combined class string
*
* @example
* ```typescript
* const button = cv({
* base: 'px-4 py-2 rounded',
* variants: {
* color: {
* primary: 'bg-blue-500 text-white',
* secondary: 'bg-gray-500 text-white'
* },
* size: {
* sm: 'text-sm',
* lg: 'text-lg'
* }
* },
* defaultVariants: {
* color: 'primary',
* size: 'sm'
* }
* });
*
* button(); // => 'px-4 py-2 rounded bg-blue-500 text-white text-sm'
* button({ color: 'secondary' }); // => 'px-4 py-2 rounded bg-gray-500 text-white text-sm'
* ```
*/
declare const cv: ClassVariantCreatorFn;
export { type ClassVariantCreatorFn, type ClassVariantDefinition, type ClassVariantExtendProps, type ClassVariantFn, type ClassVariantFnProps, type ClassVariantRecord, cv, cv as default };