Skip to content

Commit

Permalink
feat: add createTV utility to create a tv function from a config (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton authored Oct 28, 2023
1 parent 9b8ae43 commit e07e421
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/__tests__/createTV.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component extends (...args: any) => any> = Omit<
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
removeExtraSpaces,
flatMergeArrays,
flatArray,
isBoolean,
} from "./utils.js";

export const defaultConfig = {
Expand Down Expand Up @@ -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);
};

0 comments on commit e07e421

Please sign in to comment.