Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): responsive variants for base when slots are present #202

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {expect, describe, test} from "@jest/globals";

import {tvTransformer, withTV} from "../transformer";
import {tv, cn} from "../index";

Check warning on line 6 in src/__tests__/transformer.test.ts

View workflow job for this annotation

GitHub Actions / pnpm (lint)

'cn' is defined but never used

type Mock = {
withTV: WithTV;
Expand Down Expand Up @@ -318,6 +319,78 @@
expect(result).toBe(expectedContent(sourceCode, transformedContent));
});

test("should return a transformed content (responsive default base with slot variant)", () => {
const sourceCode = `
import {tv} from "tailwind-variants";

const button = tv(
{
base: "w-fit",
slots: {
icon: "text-lg"
},
variants: {
size: {
sm: "w-[100px]",
md: "w-[200px]"
}
}
},
{
responsiveVariants: true
}
);
`;

const button = tv(
{
base: "w-fit",
slots: {
icon: "text-lg",
},
variants: {
size: {
sm: "w-[100px]",
md: "w-[200px]",
},
},
},
{
responsiveVariants: true,
},
);

const result = tvTransformer(sourceCode, defaultScreens);

const {base} = button({size: {initial: "sm", md: "md"}});

const transformedContent = [
{
size: {
sm: {
original: "w-[100px]",
sm: "sm:w-[100px]",
md: "md:w-[100px]",
lg: "lg:w-[100px]",
xl: "xl:w-[100px]",
"2xl": "2xl:w-[100px]",
},
md: {
original: "w-[200px]",
sm: "sm:w-[200px]",
md: "md:w-[200px]",
lg: "lg:w-[200px]",
xl: "xl:w-[200px]",
"2xl": "2xl:w-[200px]",
},
},
},
];

expect(base()).toBe("md:w-[200px] w-[100px]");
expect(result).toBe(expectedContent(sourceCode, transformedContent));
});

test("should return a transformed content (on-demand responsive variants - screens)", () => {
const sourceCode = `
import {tv} from "tailwind-variants";
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ export const tv = (options, configProp) => {
if (screenValues.length > 0) {
screenValues.push(value);

if (slotKey === "base") {
return screenValues.join(" ");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures line 266 is evaluated and returns the variantValue as a string

}

return screenValues;
}

Expand Down
Loading