Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/nine-apes-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/system-rsc": major
Copy link
Member

Choose a reason for hiding this comment

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

use patch instead of major

---

fix(system-rsc): correct type inference in extendVariants and CompoundVariants
63 changes: 61 additions & 2 deletions packages/core/system-rsc/__tests__/extend-variants.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ExtendVariantProps} from "../src/extend-variants";
import type {ExtendVariantProps, ExtendVariantWithSlotsProps} from "../src/extend-variants";

import React from "react";
import {render, screen} from "@testing-library/react";
Expand Down Expand Up @@ -37,7 +37,7 @@ const createExtendNoSlotsComponent = (styles: ExtendVariantProps = {}) =>
],
});

const createExtendSlotsComponent = () =>
const createExtendSlotsComponent = (styles: ExtendVariantWithSlotsProps = {}) =>
extendVariants(Card, {
variants: {
shadow: {
Expand Down Expand Up @@ -73,6 +73,19 @@ const createExtendSlotsComponent = () =>
shadow: "xl",
radius: "xl",
},
compoundVariants: styles?.compoundVariants ?? [
{
shadow: "none",
radius: "none",
class: "rounded-sm",
},
{
shadow: "none",
class: {
header: "scale-75",
},
},
],
});

describe("extendVariants function - no slots", () => {
Expand Down Expand Up @@ -253,4 +266,50 @@ describe("extendVariants function - with slots", () => {
expect(baseEl).toHaveClass("shadow-xs");
expect(headerEl).toHaveClass("rounded-none");
});

test("should include the compound variant styles - extended", () => {
const Card2 = createExtendSlotsComponent();

const {getByTestId} = render(
<Card2 radius="none" shadow="none">
Card Content
</Card2>,
);

const baseEl = getByTestId("base");
const headerEl = getByTestId("header");

expect(baseEl).toHaveClass("rounded-sm");
expect(headerEl).toHaveClass("scale-75");
});

test("should include the compound variant styles - original", () => {
const Card2 = createExtendSlotsComponent({
compoundVariants: [
{
shadow: "none",
radius: "sm",
class: "rounded-xl",
},
{
radius: "sm",
class: {
header: "scale-150",
},
},
],
});

const {getByTestId} = render(
<Card2 radius="sm" shadow="none">
Card Content
</Card2>,
);

const baseEl = getByTestId("base");
const headerEl = getByTestId("header");

expect(baseEl).toHaveClass("rounded-xl");
expect(headerEl).toHaveClass("scale-150");
});
});
26 changes: 10 additions & 16 deletions packages/core/system-rsc/src/extend-variants.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import type {ClassValue, StringToBoolean, OmitUndefined, ClassProp} from "tailwind-variants";
import type {
ForwardRefExoticComponent,
JSXElementConstructor,
PropsWithoutRef,
RefAttributes,
} from "react";
import type {ForwardRefExoticComponent, JSXElementConstructor, RefAttributes} from "react";

type SlotsClassValue<S> = {
[K in keyof S]?: ClassValue;
};

type Variants<S> = {
[K: string]: {[P: string]: S extends undefined ? ClassValue : SlotsClassValue<S>};
[K: string]: {[P: string]: GetSuggestedValues<S>};
};

type ComponentProps<C> = C extends JSXElementConstructor<infer P> ? P : never;
Expand All @@ -20,7 +15,7 @@ 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 GetSuggestedValues<S> = ClassValue | (S extends undefined ? never : SlotsClassValue<S>);

type SuggestedVariants<CP, S> = {
[K in keyof CP]?: ValidateSubtype<CP[K], string> extends "true"
Expand Down Expand Up @@ -48,7 +43,7 @@ type VariantValue<V, SV> = {

type DefaultVariants<V, SV> = VariantValue<V, SV>;

type CompoundVariants<V, SV> = Array<VariantValue<V, SV> & ClassProp<ClassValue>>;
type CompoundVariants<V, SV, S> = Array<VariantValue<V, SV> & ClassProp<GetSuggestedValues<S>>>;

type Options = {
/**
Expand Down Expand Up @@ -92,7 +87,7 @@ export type ExtendVariants = {
V extends ComposeVariants<CP, S>,
SV extends SuggestedVariants<CP, S>,
DV extends DefaultVariants<V, SV>,
CV extends CompoundVariants<V, SV>,
CV extends CompoundVariants<V, SV, ComponentSlots<CP>>,
>(
BaseComponent: C,
styles: {
Expand All @@ -103,12 +98,11 @@ export type ExtendVariants = {
},
opts?: Options,
): ForwardRefExoticComponent<
PropsWithoutRef<
CP & {
[key in keyof V]?: StringToBoolean<keyof V[key]>;
}
> &
RefAttributes<InferRef<C>>
{
[key in Exclude<keyof CP | keyof V, "ref">]?:
| (key extends keyof CP ? CP[key] : never)
| (key extends keyof V ? StringToBoolean<keyof NonNullable<V[key]>> : never);
} & RefAttributes<InferRef<C>>
>;
};

Expand Down
Loading