This video is available to students only

Building a News Site - Navigation

Create the page navigation for NEWSLY, a shadcn/ui news site

News Site - Navigation#

In this lesson, you are going to build the page navigation component using shadcn/ui NavigationMenu and Separator components.

Navigation

Start by installing the NavigationMenu and Separator using shadcn/ui CLI.

Run this command in your favorite terminal:

Categories Data#

The API you are going to fetch articles from is called newsapi.org, and it has a list of categories you can use.

Create a new components/categories.tsx file, and declare a new array with the categories labels and icons.

As mentioned before, you will use lucide.dev icons. Lucide packages are installed with the template you've used to create your app.

Add the following icon imports to your file:

Then, create the categories array:

The categories array is your data model for the categories component.

Rendering Categories#

Import shadcn/ui NavigationMenu component, and create a Categories component:

What's going on here?

  1. You have used the NavigationMenu and the NavigationMenuList components to wrap the menu items.

  2. Then, you've mapped through the categories and rendered a NavigationMenuItem for each.

  3. Inside the NavigationMenuItem, you've rendered the icon and label of the category.

Now you'd want to see it live. Go back to the header file and render the categories component after the H1 tag like this:

Try navigate to the app, and you should see this error:

Error

The NavigationMenu component uses react context inside of it, and you can only use createContext inside a client component (versus a server-side component).

In Next.js, client rendering is opt-in, meaning you have to explicitly decide what components React should render on the client.

Next.js Docs

To make the categories component client-side only, add this line at the file top:

Now go back to the site and the header should look like this:

Navigation

You've made good progress!

Category Button Design#

This lesson preview is part of the Sleek Next.JS Applications with shadcn/ui course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to Sleek Next.JS Applications with shadcn/ui, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Sleek Next.JS Applications with shadcn/ui