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 uncheck “Copy the main branch only” so you can access all branches / steps of the tutorial.
Verify that your forked repository remotes are correct:
Your terminal should output something like this:
After the dependencies are installed, create a build with:
After the build and dependencies are installed, you can start the app with:
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.
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.
The app looks as it did before. Next, let’s prepare our app for a Sass build.
In src directory, rename globals.css as globals.scss and change the import in layout.js from global.css to globals.scss.
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 from Carbon to test that our dependencies are working properly. At the top of page.js, import the Button by adding the following:
We need use client since the Carbon components we use are all client components. In Next 13 pages are pulled in as children to layout files (see RootLayout src/app/layout.js) and these are always server side components.
In the Page component return, you can now replace:
with
Congratulations, you’ve imported your first component! You should see a Carbon styled button on the page.
Now let’s import the icons. In the TutorialHeader.js file, we need to import each individual icon we will use.
Then we need to add the HeaderGlobalAction component inside of the HeaderGlobalBar where we will add our icons. These represent actions in the header a user can make. Replace:
With:
Create a providers.js file within the app folder with the following content.
You should now see a styled UI Shell header and a button below it.
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 and optionally a layout.js and styling sheet.
Create the following files in the home folder:
Create the following files in the repos folder:
And we will add this into our root page:
We’ll repeat this process with RepoPage.
Navigate to the repos page by adding /repos at the end of your locally hosted site to see your repos page.
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 import in TutorialHeader.js:
We need to use the Link component instead of the default anchor elements to prevent full page reload when navigating to different pages in Next.js applications. To use Link, we wrap HeaderName component and pass through href elements to it:
Do the same with the components HeaderNavigation and HeaderSideNavItems that contain href="/repos", updating them to:
and the following:
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 to match the specs.
In providers.js we will add inline theming for our navigation. First, we need to import our new Theme component.
Then, we will wrap Theme around our header, and set the zoned theme using the theme prop, which accepts one of four strings: "white", "g10", "g90" or "g100".
We have one last thing to fix before we’re done. Because we changed the header theme to dark, the <HeaderGlobalAction> tooltips are now light instead of dark, and when you scroll the page, it blends into the content. To fix this, we’ll add some overriding styles in _tutorial-header.scss:
Continuous integration (CI) check
We have a ci-check script defined in package.json that verifies file formatting for files that have been touched since the last Git commit with a tool called Prettier. You’d typically also have that script run your test suite as part of your CI build. Go ahead and make sure everything looks good with:
Then, push to your repository:
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 into base: v11-next-step-1. Take notice of the Netlify bot that deploys a preview of your PR every time that you push new commits. These previews can be shared and viewed by anybody to assist the PR review process.