-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
feat(cli): add support for custom Tailwind prefix transformer #770
Conversation
@Bekacru is attempting to deploy a commit to the shadcn-pro Team on Vercel. A member of the Team first needs to authorize it. |
hey @dan5py I know u guys are probably very busy but just wanna know if u guys have any taught or feedbacks on this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Bekacru, thank you for this great PR.
I see only one problem:
https://github.com/shadcn/ui/blob/eee7ce68798b9345cb969942cd279c3acc054ad6/apps/www/scripts/build-registry.ts#L255-L262
The classes applied in the css file are missing the prefix when it gets added, so they cannot be found.
Apart from that I think that it's really well done.
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Ignored Deployments
|
hi @dan5py thanks! Oh yeah, I just added that in with the latest commit, so it should be fixed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 @shadcn
I would really like this feature as the manual work on this is kinda difficult since am working on a monorepo based UI, Thanks |
I do need it as well, as my chrome extension I am building based on shadcn intervening on some of the websites which also built using tailwind. So I cannot release it without major manual rework. In practise I need styling not to clash... Could you guys have a look into mergins this PR as soon as please? Thanks! |
hey @shadcn you should review this when you get time, I'm happy to help if there is something that's holding this back. |
Oh we really need this too ! How could we help ? |
This is a needed feature for working in a mono repo setup. I am stuck for now without this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a non-code-owner, I think it looks good, few things I would have done different.
|
||
export const applyTwPrefixesCss = (css: string, twPrefix: string) => { | ||
const lines = css.split("\n") | ||
for (let line of lines) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const line?
packages/cli/src/commands/init.ts
Outdated
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty lines
) | ||
} | ||
if (value) { | ||
TODO://this is very hacky way find tailwind classes but for current components it works. Need to find a better way |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
meanwhile is there a better way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also comment looks messed up.
🙏🏼 |
Looking into this. |
Any news on this? Such a great feature to have. const twMerge = extendTailwindMerge({ Otherwise, can experience some conflicts for overiding default classes. |
Bump could definitely use this! Building an embedded chatbot and the host site's styles are leaking so this would prevent classname conflict. Should probably just implement a Shadow DOM instead. |
Any update on this? This is an important prerequisite to being able to safely use shadcn components in a monorepo and/or use them in distributed components that will be embedded in sites with their own (potentially different and conflicting) tailwind configurations. |
@@ -120,6 +123,12 @@ export async function promptForConfig( | |||
active: "yes", | |||
inactive: "no", | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this question should be moved down after tailwindConfig and if config exists, the default answer could be pre-populated based on config.
packages/cli/src/utils/templates.ts
Outdated
plugins: [require("tailwindcss-animate")], | ||
}` | ||
plugins: [require("tailwindcss-animate")], ${prefix ? `\n prefix: "${prefix}",` : ""} | ||
}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if the empty end line was removed or it was like this before, but I'd suggest to keep it.
Also I see "prefix" option to be defined closer to the top of the config and not the end (maybe I'm mistaking and haven't seen enough configs in the wild). I think it's more visual on the top.
Hey, I've written this function you can use in javascript to convert className to prefixed className until the cli will be updated : function addTwPrefix(string = '', initialPrefix = 'tw-') {
let finalString = ''
const all = string.split(/\s/g)
for (let str of all) {
let prefix = initialPrefix
const isDoubleDot = str.indexOf(':')
if (isDoubleDot > -1) {
let strToUpdate = str.split(':')
if (strToUpdate[1].startsWith('-')) {
prefix = `-${prefix}`
strToUpdate[1] = strToUpdate[1].slice(1)
}
const updatedClass = `${prefix}${strToUpdate[1]}`
finalString += ` ${strToUpdate[0]}:${updatedClass}`
} else {
if (str.startsWith('-')) {
prefix = `-${prefix}`
str = str.slice(1)
}
finalString += ` ${prefix}${str}`
}
}
return finalString
}
addTwPrefix('-mx-1 my-1 h-px bg-muted') Not sure if its optimal but it does the job. |
thank you very much ,bro. but tailwindcss do not support construct class names dynamically |
Yes I know, here the thing is to use node js to create your classes, instead of manually adding your prefix on each class. Copy paste the classes you want to convert, it's painfull but less than pointing your cursor in front of each class when the component is generated ps: i've updated the function on 12/14/2023 11h15 Paris Timezone |
# Conflicts: # packages/cli/src/commands/init.ts # packages/cli/src/utils/templates.ts # packages/cli/src/utils/transformers/index.ts
I'm comming back again with a greater solution for anyone struggling : https://github.com/Medaillek/shadcn-ui-tailwind-prefix/tree/main
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made some changes to how the transformer handles classes (including support for classes inside cva
and cn
). We're green!
Thanks for your work on this @Bekacru Appreciate it.
Great job @Bekacru |
my main goal was to add support for tailwind prefixes shadcn-ui/ui#770
…-ui#770) * feat(cli): add support for custom Tailwind prefix * fix(cli): add tw prefix on classes applied in the css file * feat(cli): add support for custom tailwind prefix * chore: add changeset * style(shadcn-ui): code format --------- Co-authored-by: shadcn <[email protected]>
#535
Hey there! It appears that this issue has not received any attention yet. I encountered a similar situation yesterday while working on a project, where I had to manually change the components' prefixes. Hence, it seems imperative to address this matter in some way.
This pr introduces a valuable addition to the CLI. It allows users to define a custom Tailwind prefixes.
The implementation updates both the configuration schema, the init command and the tailwind config templates. The configuration schema now includes a new optional field called "prefix." This will ensures that users who have already customized their configuration won't be required to make any further updates. And, the CLI will not throw any errors with existing configurations.
please take a moment to review these changes and provide your feedback.