Shadcn UI Source Code Examples
If you're a developer seeking to enhance your understanding and usage of shadcn UI library with examples, you've come to the right place. Shadcn UI is a collection of beautiful, provably production-ready React UI components that are composable, customizable, and accessible. Let's delve into some practical examples and explore their source code.
Before we dive into the examples, ensure you have Node.js and npm installed. Then, install shadcn UI via npm using the command: `npm install @shadcn/ui`. Now, let's get started with our first main topic: components.
Shadcn UI Components
Shadcn UI offers a wide array of components, ranging from buttons and cards to modals and accordions. Each component is designed with simplicity and extensibility in mind.
Let's explore two essential components: the Button and the Card.
Button
The Button component is a fundamental part of any UI library. Shadcn UI's Button component is simple, customizable, and supports different variants like primary, secondary, and destructive.
Here's an example of a primary button:
<Button>Button</Button>
The source code for the Button component in shadcn UI can be found at https://github.com/shadcn-ui/ui/blob/main/src/components/button.tsx. It's well-documented and easy to understand.
Card
Here's a simple Card example:
<Card> <CardHeader>Card Header</CardHeader> <CardContent>Card Content</CardContent> <CardFooter>Card Footer</CardFooter> </Card>
The Card component's source code can be found at https://github.com/shadcn-ui/ui/blob/main/src/components/card.tsx.
Theming and Styling
Shadcn UI supports theming out of the box, making it easy to customize the look and feel of your application. It also supports the use of class names and ids for specific styling.
Let's explore two aspects of theming: color and typography.
Color
Shadcn UI uses the tailwindcss color palette, providing a wide range of colors for your components. You can customize these colors using the theme.extend function in your theme file.
Here's an example of extending the theme to include a new color:
{
extend: {
colors: {
'my-color': '#F00',
},
},
}
The color is now available for use in your components: `bg-my-color`.
Typography
Shadcn UI supports customizing fonts and font sizes using the theme.fontFamily and theme.fontSize properties. You can also use the `@tailwindcss/typography` plugin for advanced typography customization.
Here's how you can extend the default font families:
{
extend: {
fontFamily: {
'my-font': ['rial', 'sans-serif'],
},
},
}
Now, you can use the new font family in your components: `font-my-font`.
The shadcn UI library is designed to be intuitive and extensible, helping you build beautiful, responsive user interfaces quickly. Whether you're a seasoned developer or just starting, shadcn UI's examples and source code provide valuable insights into creating production-ready React components. Happy coding!