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

Mismatched variant nesting generates [object Object] in class #165

Open
Cael6 opened this issue Feb 26, 2024 · 2 comments
Open

Mismatched variant nesting generates [object Object] in class #165

Cael6 opened this issue Feb 26, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Cael6
Copy link

Cael6 commented Feb 26, 2024

Describe the bug
When using the extend capabilities, if a variation's value does not match the shape of the extended tv result, it will add [object Object] to the class list rather than calculate the value.

For example:


const styles = tv({
  slots: {
    base: "button",
    other: "other",
  },
  variants: {
    variation: {
      blue: {
        base: "blue",
        other: "other-blue",
      },
    },
  },
});

const extendedStyles = tv({
  extend: styles,
  variants: {
    variation: {
      blue: "extended-blue",
    },
  },
});

styles uses an object to apply variations to variation, but extendedStyles uses a string to apply to just the base.

To Reproduce
Steps to reproduce the behavior:

  1. Go to codesandbox example
  2. Inspect Button's class

Expected behavior
I expect that both blue and extended-blue are applied to Button's class.

Screenshots
image

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Chrome
  • Version: 121.0.6167.184 (Official Build) (arm64)

Additional context
I'm using TypeScript 5.3.3, but on previous versions of TypeScript I believe it would simply omit [object Object] instead of adding in the desired class.

@Cael6 Cael6 added the bug Something isn't working label Feb 26, 2024
@thefalked
Copy link
Contributor

For me it would make sense to use like this:

const styles = tv({
  slots: {
    base: "button",
    other: "other",
  },
  variants: {
    variation: {
      blue: {
        base: "blue",
        other: "other-blue",
      },
    },
  },
});

const extendedStyles = tv({
  extend: styles,
  variants: {
    variation: {
      blue: {
        base: "extended-blue",
      },
    },
  },
});

or if you want to add the blue: "extended-blue" it would apply to all slots, not just the base one.

keep in mind that base is the name of the slot not the "base" for all slots.

@Cael6
Copy link
Author

Cael6 commented Jun 10, 2024

Yes, it would work if you matched the structure between styles and extendedStyles, but it should also work with my example as well.

Consider a case where styles is defined in a library and extendedStyles is defined in an application using that library. The types allow you to define extendedStyles as I did in the example, so it ends up not working as expected because the original blue class from styles does not get applied.

It is a very difficult bug to track down if you're not aware of the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants