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

VariantProps not working in a component library #190

Closed
srijans38 opened this issue Aug 9, 2023 · 1 comment
Closed

VariantProps not working in a component library #190

srijans38 opened this issue Aug 9, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@srijans38
Copy link

srijans38 commented Aug 9, 2023

Describe the bug
I am creating a component library for internal org use. For this I have a few components that use cva but when consuming these components in applications, the types from VariantProps are not reflected. Interestingly, the types work perfectly when consuming the component directly in storybook within the lib.

Here is a sample component:

const button = cva(['some-styles'], {
  variants: {
    intent: {
      ....
    },
  }
});

interface ButtonProps
  extends VariantProps<typeof button>,
    React.ButtonHTMLAttributes<HTMLButtonElement> {
  text: string;
  icon?: React.ReactNode;
  iconPosition?: 'before' | 'after';
}

export function Button({
  text,
  className,
  intent,
  weight,
  icon,
  iconPosition = 'before',
  ...props
}: ButtonProps) {
    ....
}

And, here is the declaration file output by tsc:

/// <reference types="react" />
import { VariantProps } from 'class-variance-authority';
declare const button: (props?: ({
    intent?: "primary" | "danger" | "outline" | null | undefined;
    weight?: "semibold" | "medium" | null | undefined;
} & import("class-variance-authority/dist/types").ClassProp) | undefined) => string;
interface ButtonProps extends VariantProps<typeof button>, React.ButtonHTMLAttributes<HTMLButtonElement> {
    text: string;
    icon?: React.ReactNode;
    iconPosition?: 'before' | 'after';
}
export declare function Button({ text, className, intent, weight, icon, iconPosition, ...props }: ButtonProps): import("react/jsx-runtime").JSX.Element;
export { };

What I suspect is happening here is that typescript is not taking the exports field for "./types" in package.json into account because manually editing the .d.ts file as below seems to work:

declare const button: (props?: ({
    intent?: "primary" | "danger" | "outline" | null | undefined;
    weight?: "semibold" | "medium" | null | undefined;
} & import("class-variance-authority/types").ClassProp) | undefined) => string;

So using import("class-variance-authority/types").ClassProp instead of import("class-variance-authority/dist/types").ClassProp

package.json:

"./types": {
"types": "./dist/types.d.ts"
}

Is there anything I can do to resolve this?

@srijans38 srijans38 added the bug Something isn't working label Aug 9, 2023
@joe-bell
Copy link
Owner

joe-bell commented Aug 9, 2023

Appreciate you taking the time to write this up, but I’m going to consider this as a duplicate of #143

Will make sure to tackle in cva@1.0

@joe-bell joe-bell closed this as not planned Won't fix, can't repro, duplicate, stale Aug 9, 2023
Repository owner locked as resolved and limited conversation to collaborators Aug 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants