css-variants
Version:
A lightweight, flexible API for managing CSS class variants.
57 lines (56 loc) • 2.22 kB
TypeScript
import { ObjectKeyPicker, ObjectKeyArrayPicker, PartialRecord } from './utils/types';
import { cx, ClassValue } from './cx';
export type SlotClassRecord<S extends string> = PartialRecord<S, ClassValue>;
export type SlotClassVariantRecord<S extends string> = Record<string, Record<string, SlotClassRecord<S>>>;
export type SlotClassVariantExtendProps<S extends string> = {
classNames: SlotClassRecord<S>;
};
export interface SlotClassVariantDefinition<S extends string, T extends SlotClassVariantRecord<S> | undefined> {
slots: S[];
base?: SlotClassRecord<S>;
variants?: T;
compoundVariants?: (ObjectKeyArrayPicker<T> & SlotClassVariantExtendProps<S>)[];
defaultVariants?: ObjectKeyPicker<T>;
classNameResolver?: typeof cx;
}
export type SlotClassVariantFnProps<S extends string, T extends SlotClassVariantRecord<S> | undefined> = T extends undefined ? Partial<SlotClassVariantExtendProps<S>> : ObjectKeyPicker<T> & Partial<SlotClassVariantExtendProps<S>>;
export type SlotClassVariantFn<S extends string, T extends SlotClassVariantRecord<S> | undefined> = (props?: SlotClassVariantFnProps<S, T>) => Record<S, string>;
export type SlotClassVariantCreatorFn = <S extends string, T extends SlotClassVariantRecord<S> | undefined>(config: SlotClassVariantDefinition<S, T>) => SlotClassVariantFn<S, T>;
/**
* Creates a slot-based class variant function that manages class names for multiple slots with variants.
*
* @param config - Configuration object for creating the variant function
* @returns A function that accepts variant props and returns class names for each slot
*
* @example
* ```typescript
* const button = scv({
* slots: ['root', 'icon'],
* base: {
* root: 'btn',
* icon: 'btn-icon'
* },
* variants: {
* size: {
* sm: {
* root: 'btn-sm',
* icon: 'icon-sm'
* },
* lg: {
* root: 'btn-lg',
* icon: 'icon-lg'
* }
* }
* },
* defaultVariants: {
* size: 'sm'
* }
* })
*
* // Usage
* const classes = button({ size: 'lg' })
* // Result: { root: 'btn btn-lg', icon: 'btn-icon icon-lg' }
* ```
*/
export declare const scv: SlotClassVariantCreatorFn;
export default scv;