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 ui dir #2266

Merged
merged 3 commits into from
Jan 2, 2024
Merged

feat(cli): add support for custom ui dir #2266

merged 3 commits into from
Jan 2, 2024

Conversation

shadcn
Copy link
Collaborator

@shadcn shadcn commented Jan 2, 2024

This PR adds support for custom dir/path for ui components. It builds on top of the existing schema, aliases and resolveImport.

Default schema

This will place components inside .[ROOT]/components/ui.

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils",
  }
}

Custom ui alias

This will place components inside .[ROOT]/primitives. Imports will be resolved automatically.

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils",
    "ui": "@/primitives" <---- adds components to `.[ROOT]/primitives`👈
  }
}

More examples

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "aliases": {
    "components": "~/components",
    "utils": "~/lib/utils",
    "ui": "~/app/ui" <---- adds components to `.[ROOT]/app/ui`👈
  }
}
{
  "$schema": "https://ui.shadcn.com/schema.json",
  "aliases": {
    "components": "#/components",
    "utils": "#/lib/utils",
    "ui": "#/components" <---- adds components to `.[ROOT]/components`👈
  }
}

Help me test this

  1. Add a ui alias to your components.json file.
  2. Run the following command to add new components: npx [email protected] add
  3. Verify that components are placed in the custom path and that imports are correctly updated.
  4. Comment to let me know.

What's an alias?

An alias is an import alias. You can think of it as How do I write imports for my components or utils?.

If you write import { Button } from "@/components/button" then @/components is your alias. import { AlertDialog } from "~/app/ui/alert-dialog" has alias ~/app/ui.

We use aliases instead of paths because it tells us more about your project setup. Using aliases, we can figure out components paths and automatically resolve imports when adding new components.

Copy link

vercel bot commented Jan 2, 2024

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

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
ui ⬜️ Ignored (Inspect) Visit Preview Jan 2, 2024 1:18pm

@shadcn shadcn added enhancement New feature or request area: cli labels Jan 2, 2024
Copy link
Contributor

github-actions bot commented Jan 2, 2024

A new prerelease is available for testing:

@ezeparziale
Copy link

I installed npx [email protected] init
I tested without ui parameter --> The components are saved in @/components/ui automatically
I tested with ui parameter set with @/primitives, when I ran npx [email protected] add alert I got the alert file in primitives folder. If the folder doesn't exist, it is created automatically.

Copy link

@billymoonz billymoonz left a comment

Choose a reason for hiding this comment

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

image

works as expected

@shadcn
Copy link
Collaborator Author

shadcn commented Jan 2, 2024

Merging. Thanks everyone.

@shadcn shadcn merged commit be580db into main Jan 2, 2024
8 checks passed
@shadcn shadcn deleted the feat/custom-ui branch January 2, 2024 18:32
@jonathanwilke
Copy link

Seems to be working for me!

@Bolaji06
Copy link

Bolaji06 commented Jan 2, 2024

Works perfectly fine components get saved automatically into the preferred path.

@tiavina-mika
Copy link

when it will be released?

@vagnermaltauro
Copy link

yes @tiavina-mika

kjxbyz pushed a commit to muse-ui/muse-ui that referenced this pull request Jun 7, 2024
* feat(cli): add support for custom ui dir

* docs(www): update docs for aliases.ui

* chore: add changeset
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

7 participants