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

idea: function helpers or generics for stacked modifiers #388

Open
bstro opened this issue Mar 8, 2022 · 2 comments
Open

idea: function helpers or generics for stacked modifiers #388

bstro opened this issue Mar 8, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@bstro
Copy link
Contributor

bstro commented Mar 8, 2022

Somewhat like the introduction of function helpers like TW.{backgroundColor,opacity,…}, I'm wondering if it might make sense to apply a similar pattern to variants. This could address the limitation where one can only use tailwindcss-classnames with one modifier like T.backgroundColor('active:bg-red-500'), where something with more modifiers like T.backgroundColor('active:hover:bg-red-500)` would not typecheck.

Moreover, it might help reduce the amount of types overall. I'm not sure if this would improve TS performance but it seems like it might?

Example of API:

    import { TW, M } from "tailwindcss-classnames";

    const classname = classnames(
        TW.backgroundColor(M("active", "hover", "active:hover:bg-red-500"))
    );

However, it seems a bit unnecessarily to duplicate the active and hover in the final argument, so perhaps its worth considering that the M function could apply those for the end-user:
M("active", "hover", "bg-red-500") would produce a string like active:hover:bg-red-500

Or perhaps a purely type-based approach:

const classname = classnames(
  TW.backgroundColor<'active', 'hover'>("active:hover:bg-red-500")
);

Would love to hear your thoughts.

@bstro bstro added the enhancement New feature or request label Mar 8, 2022
@bstro bstro changed the title idea: function helpers for stacked modifiers idea: function helpers or generics for stacked modifiers Mar 8, 2022
@muhammadsammy
Copy link
Owner

Well thought. I think the generics approach looks better 👍
Sorry for the late response, I've been very busy.

@bstro
Copy link
Contributor Author

bstro commented May 13, 2022

No problem! I dug into this a little bit more and uncovered a complication that I wanted to document here. I realized that the generic type will have to generate permutations for every possible order of classes. For instance, the generic for the following backgroundColor<['dark', '2xl']>("bg-red-500") would have to generate the following permutations dark:2xl:bg-red-500, 2xl:dark:bg-red-500, since we do not know what order the user might choose. Makes the DX a little less clean, but I still think this idea could be an improvement overall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants