Shadcn UI Nextjs Tutorial for Next.js 15 and Beyond

Author

Reads 223

Close-Up Shot of a Screen of a Smartphone
Credit: pexels.com, Close-Up Shot of a Screen of a Smartphone

Shadcn UI Next.js Tutorial is a comprehensive guide for building robust and scalable user interfaces with Next.js 15 and beyond. This tutorial is designed to help developers master the art of building fast, secure, and maintainable applications.

The tutorial starts with a basic setup, where you learn how to create a new Next.js project using the `create-next-app` command. This is a crucial step in getting started with Next.js 15.

Next, you'll learn how to install Shadcn UI, a popular UI library for Next.js, and start building your first components. Shadcn UI provides a wide range of pre-built components, including buttons, forms, and navigation bars.

By following this tutorial, you'll gain hands-on experience in building complex user interfaces with Next.js 15 and Shadcn UI. The tutorial covers topics such as routing, internationalization, and accessibility, ensuring that your applications meet the highest standards of quality and usability.

For more insights, see: Nextjs Client Side Cookies

Components

To add a component from Shadcn, you'll need to follow these steps. Go to the Shadcn docs and click on the component you want to use.

For another approach, see: Nextjs Shadcn Template

Credit: youtube.com, This UI component library is mind-blowing

The command to add a component to your project can be found on the component page. For example, to add the Button component, you'll run the command `npx shadcn-ui@latest add button` in your terminal.

You'll need to modify the command slightly if you use a different package manager. After running the command, you can import the component in the file where you want to use it.

Broaden your view: How to Add Img to Nextjs

Installing Next.js

To install Next.js, you need to choose the framework you're currently using. Next.js is the framework used in this article, so select that option.

You'll need to initialize a new Next.js project to get started. This will create a new project directory with two new directories: components and lib.

Next.js projects typically have these directories, which will be added to your project when you initialize it. The components directory is where you'll store reusable UI components.

To set up Next.js, you'll need to install it and configure your project. You can do this by initializing a new Next.js project, which will create the necessary directories and files.

With Next.js installed, you'll be able to create and manage your UI components in the components directory. This is where the magic happens, so make sure you understand how to use it.

If this caught your attention, see: Nextjs Ui

Using Next.js 15

Credit: youtube.com, Form Components in Next.js 15 Are About to Get a Whole Lot Better!

If you're upgrading to Next.js 15, you'll want to know about the status of React 19 support for your dependencies.

The upgrade process can be tracked using a table created by the developer, which lists the shadcn/ui dependencies and their React 19 support status.

Here are the packages that already work with React 19: radix-ui, lucide-react, class-variance-authority, tailwindcss-animate, embla-carousel-react, recharts, react-hook-form, react-resizable-panels, sonner, input-otp, and vaul.

The react-day-picker package works with React 19, but requires a flag for npm.

The @radix-ui/react-icons package is still in progress, with a PR in place to upgrade its React 19 support.

Take a look at this: React Next Js

Adding a Component

Adding a component to your project can be a straightforward process. To start, you'll want to go to the Shadcn docs, where you can find the component you're interested in using.

Click on the component you want to use, and you'll see the command you should run to add it to your project. This command is usually specific to the component, so make sure to follow the instructions carefully.

Credit: youtube.com, MASTER Figma Components in 10 Minutes (Everything You Need To Know)

For example, to add the Button component from Shadcn, you can follow these steps:

  1. Go to the Button component page.
  2. Run the command `npx shadcn-ui@latest add button` in your terminal.
  3. Import the button component in the file where you want to use it.

Remember to modify the command if you use a different package manager.

Button Shadow Styling

Shadcn components come with default styling that looks good and has hover effects, so you don't need to add any extra styling to use them.

You can customize the styling of Shadcn components, including the Button component, by adding or removing variants that you won't use. This is done by opening the component's source code in the components/ui directory.

The Button component has multiple variants, including default, destructive, outline, secondary, ghost, and link, as well as different sizes to choose from. You can add your own custom variants to the component by pasting in the styling for the custom button.

One of the variants I added was called myButton, which is the custom variant I created by pasting in the styling from our earlier example. This custom variant can be used in your project just like any other variant.

You can use the custom myButton variant in your project by importing the Button component and then selecting the myButton variant when using it.

Discover more: Next Js Custom Server

Feed Item Card

Credit: youtube.com, Material UI Tutorial #11 - Card Components

The Feed Item Card is a crucial component in our example app, responsible for rendering feed items in a visually appealing way. It takes two props: item (the feed item) and knockFeed.

This component does a lot of heavy lifting, extracting different blocks attached to the feed item, like the body, which is the message template we created as part of our workflow. It also loops through the item's actors and creates the heading.

To render the date, the Feed Item Card uses the toLocaleDateString function and the item.insertedAt property. This function helps format the date in a user-friendly way.

The Feed Item Card also handles message engagement statuses by conditionally rendering different buttons based on whether the item has been read or not. This is achieved by passing the knockFeed client into the component, allowing us to attach onClick handlers to specific methods on knockFeed.

Here's a breakdown of the actions the Feed Item Card performs:

  1. Extracts blocks attached to the feed item, like the body.
  2. Loops through the item's actors and creates the heading.
  3. Renders the date using toLocaleDateString and item.insertedAt.
  4. Handles message engagement statuses by rendering different buttons.

Package Managers

Credit: youtube.com, What are Package Managers? NPM, APT, Chocolatey, Homebrew and Many More

You can use pnpm, bun, or yarn to install shadcn/ui. No flags are needed.

Follow the instructions in the installation guide for the chosen package manager.

To resolve peer dependency issues, you can run the command with a flag.

The process for adding components is the same regardless of the package manager used.

One Answer

In Next.js, you can use the @ symbol to import components and libraries from specific directories. For example, @/components/whatever will look for the component at ROOT => src/components/whatever. This is useful for organizing your code and avoiding conflicts with other imports.

If you're using a config that's not set up correctly, you might end up looking for imports in the wrong place. For instance, if you write import { cn } from "@/lib/utils, it will look for it at ./@/components/ui/button/@/lib/utils, which is not found.

This can be frustrating, especially if you're not sure where to look. But with the right config and understanding of how imports work in Next.js, you can avoid these issues and keep your code organized and efficient.

Here's a quick rundown of how to set up your imports correctly:

  1. Use the @ symbol to import components and libraries from specific directories.
  2. Make sure your config is set up to look for imports in the right place.

Data Fetching

Credit: youtube.com, Using shadcn/ui with NextJs 15 and React 19

You can use the useState hook to create local state variables for your component. This is exactly what we did in the ActivityFeed component.

To fetch data, you need to make network calls using the knockFeed client. We used the knockFeed client to load the initial state of our feed and listen for updates.

The useEffect hook is used to ask the knockFeed client to listen for real-time updates. Inside the useEffect hook, we're asking the knockFeed client to listen for real-time updates.

You need to create a function to fetch the feed initially. We created a function to fetch the feed initially.

To reconcile updates with our local state, we need to add event listeners to the knockFeed. The Knock feed does a good job of keeping track of its own internal state.

Readers also liked: Next Js Fetch Data

Features

Shadcn UI Next.js is a powerful tool that offers a wide range of features to enhance your development experience.

Credit: youtube.com, Shadcn UI Crash Course #1 - Introduction & Setup

It provides a set of pre-built UI components, including buttons, forms, and navigation menus, which can be easily integrated into your Next.js application.

These components are highly customizable, allowing you to tailor their appearance and behavior to suit your project's needs.

With Shadcn UI Next.js, you can also take advantage of its robust layout system, which enables you to create complex and responsive layouts with ease.

The library includes a variety of layout components, such as Grid and Flexbox, that can be used to build custom layouts.

Shadcn UI Next.js also offers a range of utility functions that can be used to simplify common development tasks, such as handling forms and validating user input.

These utility functions are designed to be easy to use and provide a lot of functionality out of the box.

By leveraging Shadcn UI Next.js's features, you can streamline your development process and focus on building high-quality applications.

Expand your knowledge: Next Js Development Company

Oscar Hettinger

Writer

Oscar Hettinger is a skilled writer with a passion for crafting informative and engaging content. With a keen eye for detail, he has established himself as a go-to expert in the tech industry, covering topics such as cloud storage and productivity tools. His work has been featured in various online publications, where he has shared his insights on Google Drive subtitle management and other related topics.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.