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

feat(cli): add support for custom Tailwind prefix transformer #770

Merged
merged 6 commits into from
Dec 21, 2023

Conversation

Bekacru
Copy link
Contributor

@Bekacru Bekacru commented Jun 29, 2023

#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.

@vercel
Copy link

vercel bot commented Jun 29, 2023

@Bekacru is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@Bekacru
Copy link
Contributor Author

Bekacru commented Jun 30, 2023

hey @dan5py I know u guys are probably very busy but just wanna know if u guys have any taught or feedbacks on this

Copy link
Contributor

@dan5py dan5py left a 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.

@vercel
Copy link

vercel bot commented Jun 30, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
next-template ⬜️ Ignored (Inspect) Dec 21, 2023 9:21pm
ui ⬜️ Ignored (Inspect) Visit Preview Dec 21, 2023 9:21pm

@Bekacru
Copy link
Contributor Author

Bekacru commented Jun 30, 2023

hi @dan5py thanks! Oh yeah, I just added that in with the latest commit, so it should be fixed now.

Copy link
Contributor

@dan5py dan5py left a comment

Choose a reason for hiding this comment

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

👍 @shadcn

@bim-oulabi
Copy link

I would really like this feature as the manual work on this is kinda difficult since am working on a monorepo based UI,

Thanks

@firebotQL
Copy link

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!

@Bekacru
Copy link
Contributor Author

Bekacru commented Jul 13, 2023

hey @shadcn you should review this when you get time, I'm happy to help if there is something that's holding this back.

@yovanoc
Copy link

yovanoc commented Jul 28, 2023

Oh we really need this too ! How could we help ?

@Pipe-Runner
Copy link

This is a needed feature for working in a mono repo setup. I am stuck for now without this.

Copy link

@m10rten m10rten left a 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) {
Copy link

Choose a reason for hiding this comment

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

const line?

Comment on lines 89 to 90


Copy link

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
Copy link

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?

Copy link

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.

@GiancarlosIO
Copy link

🙏🏼

@shadcn shadcn added enhancement New feature or request area: cli labels Sep 20, 2023
@shadcn shadcn self-assigned this Sep 20, 2023
@shadcn
Copy link
Collaborator

shadcn commented Sep 20, 2023

Looking into this.

@tecoad
Copy link

tecoad commented Sep 27, 2023

Looking into this.

Any news on this? Such a great feature to have.
I just noticed the cn function at utils.ts also would have to be updated so twMerge can receive also the prefix to work as intended:

const twMerge = extendTailwindMerge({
prefix: 'wdg-',
})

Otherwise, can experience some conflicts for overiding default classes.

@HowieG
Copy link

HowieG commented Oct 10, 2023

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.

@wadefletch
Copy link

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",
},
{
Copy link

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.

plugins: [require("tailwindcss-animate")],
}`
plugins: [require("tailwindcss-animate")], ${prefix ? `\n prefix: "${prefix}",` : ""}
}`
Copy link

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.

@Medaillek
Copy link

Medaillek commented Dec 11, 2023

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.

@othorizon
Copy link

othorizon commented Dec 14, 2023

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 = '', prefix = 'tw-') {
      let finalString = ''
      const all = string.split(/\s/g)
      for (let str of all) {
        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
    }

Not sure if its optimal but it does the job.

thank you very much ,bro. but tailwindcss do not support construct class names dynamically

@Medaillek
Copy link

Medaillek commented Dec 14, 2023

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 = '', prefix = 'tw-') {
      let finalString = ''
      const all = string.split(/\s/g)
      for (let str of all) {
        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
    }

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
@Medaillek
Copy link

I'm comming back again with a greater solution for anyone struggling : https://github.com/Medaillek/shadcn-ui-tailwind-prefix/tree/main

  • Copy paste the code in the .cjs file
  • Update const prefix = "tw-"
  • Update the path to your shadcn-ui components directory
  • run it using nodeJS

Copy link
Collaborator

@shadcn shadcn left a 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.

@shadcn shadcn merged commit 4fb98d5 into shadcn-ui:main Dec 21, 2023
6 checks passed
@Kinfe123
Copy link

Great job @Bekacru

kmiterror added a commit to kmiterror/shadcn-solid that referenced this pull request Jan 31, 2024
my main goal was to add support for tailwind prefixes
shadcn-ui/ui#770
kjxbyz pushed a commit to muse-ui/muse-ui that referenced this pull request Jun 7, 2024
…-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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cli enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet