|
| 1 | +import type { |
| 2 | + CnOptions, |
| 3 | + CnReturn, |
| 4 | + createTV as TailwindVariantsCreateTV, |
| 5 | + tv as TailwindVariantsTv, |
| 6 | + TV, |
| 7 | + TVConfig, |
| 8 | + TWMConfig, |
| 9 | + TWMergeConfig, |
| 10 | +} from 'tailwind-variants' |
| 11 | +import type { CreateOptions } from './types' |
| 12 | +import { defaultConfig, cnBase as tailwindVariantsCnBase } from 'tailwind-variants' |
| 13 | +// @ts-expect-error - upstream package does not ship type definitions for this path. |
| 14 | +import { c as createTailwindVariantsFactory } from 'tailwind-variants/dist/chunk-IFWU2MEM.js' |
| 15 | +import { resolveTransformers } from './core/transformers' |
| 16 | +import { create as createTailwindMergeRuntime } from './v4' |
| 17 | + |
| 18 | +type TailwindVariantsCn = <T extends CnOptions>(...classes: T) => (config?: TWMConfig) => CnReturn |
| 19 | + |
| 20 | +type TailwindVariantsFactory = ( |
| 21 | + cn: TailwindVariantsCn, |
| 22 | +) => { |
| 23 | + tv: typeof TailwindVariantsTv |
| 24 | + createTV: typeof TailwindVariantsCreateTV |
| 25 | +} |
| 26 | + |
| 27 | +function createVariantsRuntime(options?: CreateOptions) { |
| 28 | + const transformers = resolveTransformers(options) |
| 29 | + const tailwindMergeRuntime = createTailwindMergeRuntime(options) |
| 30 | + |
| 31 | + let cachedConfig: TWMConfig['twMergeConfig'] |
| 32 | + let cachedMergeFn = tailwindMergeRuntime.twMerge |
| 33 | + |
| 34 | + const getMergeFn = (config?: TWMConfig['twMergeConfig']) => { |
| 35 | + if (!config) { |
| 36 | + return tailwindMergeRuntime.twMerge |
| 37 | + } |
| 38 | + |
| 39 | + if (config === cachedConfig) { |
| 40 | + return cachedMergeFn |
| 41 | + } |
| 42 | + |
| 43 | + cachedConfig = config |
| 44 | + cachedMergeFn = tailwindMergeRuntime.extendTailwindMerge(config) |
| 45 | + return cachedMergeFn |
| 46 | + } |
| 47 | + |
| 48 | + const cn: TailwindVariantsCn = (...classes) => { |
| 49 | + const aggregated = tailwindVariantsCnBase(...classes) |
| 50 | + |
| 51 | + return (config?: TWMConfig) => { |
| 52 | + if (aggregated == null) { |
| 53 | + return aggregated |
| 54 | + } |
| 55 | + |
| 56 | + const shouldMerge = config?.twMerge ?? true |
| 57 | + if (!shouldMerge) { |
| 58 | + const normalized = transformers.unescape(aggregated) |
| 59 | + return transformers.escape(normalized) |
| 60 | + } |
| 61 | + |
| 62 | + const mergeFn = getMergeFn(config?.twMergeConfig) |
| 63 | + return mergeFn(aggregated) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const factory = (createTailwindVariantsFactory as TailwindVariantsFactory)(cn) |
| 68 | + |
| 69 | + const cnBase = (...classes: Parameters<typeof tailwindVariantsCnBase>) => { |
| 70 | + const aggregated = tailwindVariantsCnBase(...classes) |
| 71 | + if (aggregated == null) { |
| 72 | + return aggregated |
| 73 | + } |
| 74 | + |
| 75 | + const normalized = transformers.unescape(aggregated) |
| 76 | + return transformers.escape(normalized) |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + cnBase, |
| 81 | + cn, |
| 82 | + tv: factory.tv as TV, |
| 83 | + createTV: factory.createTV, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function create(options?: CreateOptions) { |
| 88 | + return createVariantsRuntime(options) |
| 89 | +} |
| 90 | + |
| 91 | +const variantsRuntime = create() |
| 92 | + |
| 93 | +const { |
| 94 | + cnBase, |
| 95 | + cn, |
| 96 | + tv, |
| 97 | + createTV, |
| 98 | +} = variantsRuntime |
| 99 | + |
| 100 | +export { |
| 101 | + cn, |
| 102 | + cnBase, |
| 103 | + create, |
| 104 | + createTV, |
| 105 | + defaultConfig, |
| 106 | + tv, |
| 107 | +} |
| 108 | + |
| 109 | +export type { |
| 110 | + CnOptions, |
| 111 | + CnReturn, |
| 112 | + TV, |
| 113 | + TVConfig, |
| 114 | + TWMConfig, |
| 115 | + TWMergeConfig, |
| 116 | +} |
0 commit comments