1. Installing Carbon
Starting from a base Create Next App, let’s install Carbon and begin using Carbon components. By the end you will have a Next.js app that uses the UI Shell to navigate between pages.
- Fork, clone and branch
- Build and start
- Install Carbon
- Install and build Sass
- Add UI Shell
- Create pages
- Add routing
- Submit pull request
Preview
A preview of what you will build:
Fork, clone and branch
This tutorial has an accompanying GitHub repository called carbon-tutorial-nextjs that we’ll use as a starting point for each step.
Fork
To begin, fork carbon-tutorial-nextjs using your GitHub account. Please note when forking you must untick “Copy the main branch only” so you can access all branches / steps of the tutorial.
Clone
Go to your forked repository, copy the SSH or HTTPS URL and in your terminal run the two commands to get the repository in your local file system and enter that directory.
git clone [your fork SSH/HTTPS]cd carbon-tutorial-nextjs
Add upstream remote
Add a remote called
upstream
SSH
git remote add upstream git@github.com:carbon-design-system/carbon-tutorial-nextjs.git
HTTPS
git remote add upstream https://github.com/carbon-design-system/carbon-tutorial-nextjs.git
Verify that your forked repository remotes are correct:
git remote -v
Your terminal should output something like this:
origin [your forked repo] (fetch)origin [your forked repo] (push)upstream git@github.com:carbon-design-system/carbon-tutorial-nextjs.git (fetch)upstream git@github.com:carbon-design-system/carbon-tutorial-nextjs.git (push)
Branch
Now that we have our repository set up, let’s check out the branch for this tutorial step’s starting point. Run the two commands:
git fetch upstreamgit checkout -b v11-next-step-1 upstream/v11-next-step-1
Build and start
We have the repository forked to your GitHub account, cloned down to your machine, and the starting branch checked out. Next, install the Next.js app’s dependencies with:
yarn
After the dependencies are installed, create a build with:
yarn build
After the build and dependencies are installed, you can start the app with:
yarn dev
This is a Next.js 13 app with a home page, its root layout and a global style sheet.
Your default browser should open up with an empty page that says:
Hello Carbon! Well, not quite yet. This is the starting point for the Carbon React tutorial.
Install Carbon
Even though we installed existing dependencies, we’ve yet to install our v11 Carbon package,
@carbon/react
Stop your development server with
CTRL-C
yarn add @carbon/react@1.33.0
Install and build Sass
We need to run a Sass build as the Carbon styles are authored in Sass, so run the following command to install
sass
yarn add sass@1.63.6
Then, start the app again. If your app’s currently running, you’ll need to restart it for the new environment variable to be used.
yarn dev
The app looks as it did before. Next, let’s prepare our app for a Sass build.
In
src
globals.css
globals.scss
layout.js
global.css
globals.scss
Import carbon-component styles
In
globals.scss
@use '@carbon/react';/// Remove overrides once Carbon bugs are fixed upstream./// Need grid option to not add page gutters at large viewports, to also use when nesting grids/// @link https://github.com/carbon-design-system/carbon/issues/2792@media (min-width: 42rem) {.cds--grid--no-gutter {padding-left: 1rem;padding-right: 1rem;
In Next.js 13 there is a global style sheet and then every page has it own, optional, style sheet.
Next, we’ll import a
Button
page.js
Button
'use client';import { Button } from '@carbon/react';
We need
use client
src/app/layout.js
In the
Page
<div>Hello Carbon! Well, not quite yet. This is the starting point for the CarbonReact tutorial.</div>
with:
<Button>Button</Button>
Congratulations, you’ve imported your first component! You should see a Carbon styled button on the page.
Add UI Shell
Next, we’re going to create a React component called
TutorialHeader
src
components
TutorialHeader
src/components/TutorialHeader
src/components/TutorialHeader├──_tutorial-header.scss└──TutorialHeader.js
Add UI Shell Sass
Next, in
globals.scss
TutorialHeader
@use '@/components/TutorialHeader/tutorial-header';
Import and export the header
Next we’ll import our Carbon UI Shell components into
TutorialHeader.js
import {Header,HeaderContainer,HeaderName,HeaderNavigation,HeaderMenuButton,HeaderMenuItem,HeaderGlobalBar,HeaderGlobalAction,
Import icons
First we will install the icons we will use in the header
yarn add @carbon/icons-react
Now let’s import the icons. In the
TutorialHeader.js
import { Switcher, Notification, UserAvatar } from '@carbon/icons-react';
Then we need to add the
HeaderGlobalAction
HeaderGlobalBar
<HeaderGlobalBar />
With:
<HeaderGlobalBar><HeaderGlobalActionaria-label="Notifications"tooltipAlignment="center"className="action-icons"><Notification size={20} /></HeaderGlobalAction><HeaderGlobalActionaria-label="User Avatar"
Render the header
Next we’ll render our UI Shell by importing our
TutorialHeader
Content
import './globals.scss';import { Providers } from './providers';export const metadata = {title: 'Carbon + Next13',description: 'IBM Carbon Tutorial with Next.js 13',};export default function RootLayout({ children }) {
Create a
providers.js
'use client';import TutorialHeader from '@/components/TutorialHeader/TutorialHeader';import { Content } from '@carbon/react';export function Providers({ children }) {return (<div><TutorialHeader />
You should now see a styled UI Shell header and a button below it.
Create pages
Next thing we need to do is create the files for our content. We already have a folder called
app
src
src/components
Since our app will have two pages, we’ll create two folders in
src/app
src/app├── home└── repos
Next.js uses these folders for page routing which is built into the framework, we do not need separate React routing. In each there is a
page.js
layout.js
Create the following files in the
home
src/app/home├── _landing-page.scss└── page.js
Create the following files in the
repos
src/app/repos├── _repo-page.scss└── page.js
Set up content Sass
Next, we’ll import our content Sass files in
globals.scss
@use '@/app/home/landing-page';@use '@/app/repos/repo-page';
Import and export content pages
Now that our stylesheets are set up, we need to create our pages’ components. Starting with
LandingPage
javascript path=src/app/home/page.js
`use client`;export default function LandingPage() {return <div>LANDING PAGE</div>;}
And we will add this into our root page:
import LandingPage from './home/page';export default function Page() {return <LandingPage />;}
We’ll repeat this process with
RepoPage
`use client`;export default function RepoPage() {return <div>REPO PAGE</div>;}
Navigate to the repos page by adding
/repos
Awesome! We’ve just created our content pages with automatic page routing courtesy of Next.js.
After that we need to do a couple of quick fixes to the UI Shell to work with Next.js links.
Add the
Link
TutorialHeader.js
import Link from 'next/link';
We need to use the
Link
Link
HeaderName
href
<Link href="/" passHref legacyBehavior><HeaderName prefix="IBM">Carbon Tutorial</HeaderName></Link>
Do the same with the components
HeaderNavigation
HeaderSideNavItems
href="/repos"
<HeaderNavigation aria-label="Carbon Tutorial"><Link href="/repos" passHref legacyBehavior><HeaderMenuItem>Repositories</HeaderMenuItem></Link></HeaderNavigation>
and the following:
<HeaderSideNavItems><Link href="/repos" passHref legacyBehavior><HeaderMenuItem>Repositories</HeaderMenuItem></Link></HeaderSideNavItems>
You should now have a working header that routes to different pages without full page reload! However, our page does not match the design specs. We need to change the header theme to
g100
In
providers.js
Theme
import { Content, Theme } from '@carbon/react';
Then, we will wrap
Theme
theme
"white"
"g10"
"g90"
"g100"
<div><Theme theme="g100"><TutorialHeader /></Theme><Content>{children}</Content></div>
We have one last thing to fix before we’re done. Because we changed the header theme to dark, the
<HeaderGlobalAction>
_tutorial-header.scss
@use '@carbon/react/scss/colors';// overriding header tooltip bg color// because the navigation is dark themed while the content is white// which means the dark theme tooltip bg blends into the white content bg.cds--header__global .cds--popover-content {background-color: colors.$gray-20;}
Submit pull request
We’re going to submit a pull request to verify completion of this tutorial step and demonstrate a couple of related concepts.
Continuous integration (CI) check
We have a
ci-check
package.json
yarn ci-check
Git commit and push
Before we can create a pull request, stage and commit all of your changes:
git add --all && git commit -m "feat(tutorial): complete step 1"
Then, push to your repository:
git push origin v11-next-step-1
Pull request (PR)
Finally, visit carbon-react-tutorial to “Compare & pull request”. In doing so, make sure that you are comparing to
v11-next-step-1
base: v11-next-step-1