css-variants
Version:
A lightweight, flexible API for managing CSS class variants.
52 lines (51 loc) • 2.19 kB
TypeScript
import { ObjectKeyPicker, ObjectKeyArrayPicker, PartialRecord, CssProperties } from './utils/types';
export type SlotStyleRecord<S extends string> = PartialRecord<S, CssProperties>;
export type SlotStyleVariantRecord<S extends string> = Record<string, Record<string, SlotStyleRecord<S>>>;
export type SlotStyleVariantExtendProps<S extends string> = {
styles: SlotStyleRecord<S>;
};
export interface SlotStyleVariantDefinition<S extends string, T extends SlotStyleVariantRecord<S> | undefined> {
slots: S[];
base?: SlotStyleRecord<S>;
variants?: T;
compoundVariants?: (ObjectKeyArrayPicker<T> & SlotStyleVariantExtendProps<S>)[];
defaultVariants?: ObjectKeyPicker<T>;
}
export type SlotStyleVariantFnProps<S extends string, T extends SlotStyleVariantRecord<S> | undefined> = T extends undefined ? Partial<SlotStyleVariantExtendProps<S>> : ObjectKeyPicker<T> & Partial<SlotStyleVariantExtendProps<S>>;
export type SlotStyleVariantFn<S extends string, T extends SlotStyleVariantRecord<S> | undefined> = (props?: SlotStyleVariantFnProps<S, T>) => Record<S, CssProperties>;
export type SlotStyleVariantCreatorFn = <S extends string, T extends SlotStyleVariantRecord<S> | undefined>(config: SlotStyleVariantDefinition<S, T>) => SlotStyleVariantFn<S, T>;
/**
* Creates a slot-based style variant function that composes CSS properties based on variants and compound variants.
*
* @param config - Configuration object for creating style variants
* @returns A function that accepts variant props and returns composed styles for each slot
*
* @example
* ```ts
* const buttonStyles = csv({
* slots: ['root', 'icon'],
* base: {
* root: { padding: '8px' },
* icon: { size: '16px' }
* },
* variants: {
* size: {
* small: {
* root: { padding: '4px' },
* icon: { size: '12px' }
* },
* large: {
* root: { padding: '12px' },
* icon: { size: '20px' }
* }
* }
* }
* });
*
* // Usage
* const styles = buttonStyles({ size: 'small' });
* // => { root: { padding: '4px' }, icon: { size: '12px' } }
* ```
*/
export declare const ssv: SlotStyleVariantCreatorFn;
export default ssv;