Shadcn/ui - Perfect partner for tailwind
The hot combo for any next-js or react js app - tailwind with shadcn/ui.
Shadcn/ui Library
The shadcn/ui offers customizable tailwind components if you are in search for a quick components to be used in your nextjs or react project. Its a lightweight and modular collection of re-usable components who's unique point is that it only installs the code that user needs unlike other component based libraries e.g. bootstrap, which user to add the complete long list of code. Moreoever, it utlises tailwind and offers lots of customization. Another great feature is that for forms and input components, it makes use of Zod library and Reack-hooks form. Easy to setup, and easy to integrate, it defnilty can be a developer best tool if he preferes nextjs-tailwind in his stack.
Installation
After launching the nextjs project with tailwind enabled, the first command that needs to be added is:
This command wont add much but would create a root components folder whithin app directory. This component folder can act as any future components that user creates. When any other component is added, it will create a sub-folder by name of ui and place all related shadcn-ui components' code in that folder.
Now lets add a very basic component: Button.
For that, navigate to the button component and you will find the instructions to add button there.
Usage is pretty straight-forward: Just import the component and use it like any other JSX.
Now if you navigate to the components folder and open ui folder, you would locate code for the button component that you just installed. As a reference, following is the code that you would see.
Now starting from bottom there are three points of interest.
- The button component export button as well as button-variant. That means this button variant can also be used without using button itself, e.g. on link.
-
You can modify default styling by passing classname as prop as indicated by line 47 or you can add/sutract any tailwind class directly on line-8.
Moreover, you can also add/subtrack styling of different variants by directly changing the tailwind classes on those variant. You may also add your own variant as well. -
If you dont want to modify the component and believe in extracting changing from the source, you may notice that variant utilises some classes which picks value from the tailwind config which in turn picks the value from the css variables defined in globals.css.
You may make the changes in globals.css and just utlise the button as it is.