From e07e42179aedcdedd998888e2124c1a9666d7179 Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Sat, 28 Oct 2023 10:04:44 -0500 Subject: [PATCH] feat: add `createTV` utility to create a `tv` function from a config (#121) --- src/__tests__/createTV.test.ts | 20 ++++++++++++++++++++ src/index.d.ts | 2 ++ src/index.js | 5 ++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/createTV.test.ts diff --git a/src/__tests__/createTV.test.ts b/src/__tests__/createTV.test.ts new file mode 100644 index 0000000..c7cded2 --- /dev/null +++ b/src/__tests__/createTV.test.ts @@ -0,0 +1,20 @@ +import {createTV} from "../index"; + +describe("createTV", () => { + test("should use config in tv calls", () => { + const tv = createTV({twMerge: false}); + const h1 = tv({base: "text-3xl font-bold text-blue-400 text-xl text-blue-200"}); + + expect(h1()).toBe("text-3xl font-bold text-blue-400 text-xl text-blue-200"); + }); + + test("should override config", () => { + const tv = createTV({twMerge: false}); + const h1 = tv( + {base: "text-3xl font-bold text-blue-400 text-xl text-blue-200"}, + {twMerge: true}, + ); + + expect(h1()).toBe("font-bold text-xl text-blue-200"); + }); +}); diff --git a/src/index.d.ts b/src/index.d.ts index 9a5cd71..c226fc6 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -296,6 +296,8 @@ export type TV = { // main function export declare const tv: TV; +export declare function createTV(config: TVConfig): TV; + export declare const defaultConfig: TVConfig; export type VariantProps any> = Omit< diff --git a/src/index.js b/src/index.js index c093140..98abf48 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,6 @@ import { removeExtraSpaces, flatMergeArrays, flatArray, - isBoolean, } from "./utils.js"; export const defaultConfig = { @@ -427,3 +426,7 @@ export const tv = (options, configProp) => { return component; }; + +export const createTV = (configProp) => { + return (options, config) => tv(options, config ? mergeObjects(configProp, config) : configProp); +};