Files
NapCatQQ/.pnpm-store/v3/files/57/ef2daafa8404a13364e0678e8c5b199432bd7cccc60ca8e4b6b9757f8b8cc8bbf138829682ab6c398c0ea6ac09fd986a5fe20b32d4df03ed026ba0b7f29168
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

114 lines
3.4 KiB
Plaintext

import { OmitUndefined, ClassValue, StringToBoolean, ClassProp } from 'tailwind-variants';
import { JSXElementConstructor, ForwardRefExoticComponent, PropsWithoutRef, RefAttributes } from 'react';
type SlotsClassValue<S> = {
[K in keyof S]?: ClassValue;
};
type Variants<S> = {
[K: string]: {[P: string]: S extends undefined ? ClassValue : SlotsClassValue<S>};
};
type ComponentProps<C> = C extends JSXElementConstructor<infer P> ? P : never;
type ComponentSlots<CP> = CP extends {classNames?: infer S} ? S : undefined;
type ValidateSubtype<T, U> = OmitUndefined<T> extends U ? "true" : "false";
type GetSuggestedValues<S> = S extends undefined ? ClassValue : SlotsClassValue<S>;
type SuggestedVariants<CP, S> = {
[K in keyof CP]?: ValidateSubtype<CP[K], string> extends "true"
? {[K2 in CP[K]]?: GetSuggestedValues<S>}
: ValidateSubtype<CP[K], boolean> extends "true"
? {
true?: GetSuggestedValues<S>;
false?: GetSuggestedValues<S>;
}
: never;
};
type ComposeVariants<CP, S> = SuggestedVariants<CP, S> | Variants<S>;
type VariantValue<V, SV> = {
[K in keyof V | keyof SV]?:
| (K extends keyof V ? StringToBoolean<keyof V[K]> : never)
| (K extends keyof V ? StringToBoolean<keyof V[K]>[] : never)
| (K extends keyof SV
? ValidateSubtype<SV[K], object> extends "true"
? keyof OmitUndefined<SV[K]>
: never
: never);
};
type DefaultVariants<V, SV> = VariantValue<V, SV>;
type CompoundVariants<V, SV> = Array<VariantValue<V, SV> & ClassProp<ClassValue>>;
type Options = {
/**
* Whether to merge the class names with `tailwind-merge` library.
* It avoids to have duplicate tailwind classes. (Recommended)
* @see https://github.com/dcastil/tailwind-merge/blob/v1.8.1/README.md
* @default true
*/
twMerge?: boolean;
/**
* The config object for `tailwind-merge` library.
* @see https://github.com/dcastil/tailwind-merge/blob/v1.8.1/docs/configuration.md
*/
twMergeConfig?: any;
};
type ExtendVariantProps = {
variants?: Record<string, Record<string, string>>;
defaultVariants?: Record<string, string>;
compoundVariants?: Array<Record<string, boolean | string | Record<string, string>>>;
};
type ExtendVariantWithSlotsProps = {
variants?: Record<string, Record<string, string | Record<string, string>>>;
defaultVariants?: Record<string, string>;
compoundVariants?: Array<Record<string, boolean | string | Record<string, string>>>;
};
type InferRef<C> =
C extends ForwardRefExoticComponent<RefAttributes<infer R>>
? R
: C extends new (...args: any[]) => infer I
? I
: any;
type ExtendVariants = {
<
C extends JSXElementConstructor<any>,
CP extends ComponentProps<C>,
S extends ComponentSlots<CP>,
V extends ComposeVariants<CP, S>,
SV extends SuggestedVariants<CP, S>,
DV extends DefaultVariants<V, SV>,
CV extends CompoundVariants<V, SV>,
>(
BaseComponent: C,
styles: {
variants?: V;
defaultVariants?: DV;
compoundVariants?: CV;
slots?: S;
},
opts?: Options,
): ForwardRefExoticComponent<
PropsWithoutRef<
CP & {
[key in keyof V]?: StringToBoolean<keyof V[key]>;
}
> &
RefAttributes<InferRef<C>>
>;
};
// main function
declare const extendVariants: ExtendVariants;
export { type ExtendVariantProps, type ExtendVariantWithSlotsProps, type ExtendVariants, extendVariants };