1. Installing Carbon
Starting with our Vue CLI generated app, let’s install Carbon and begin using Carbon components. By the end you will have a Vue app that uses the UI Shell to navigate between pages.
- Fork, clone and branch
- Build and start
- Install Carbon
- Other dependencies
- 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-vue that we’ll use as a starting point for each step.
Fork
To begin, fork carbon-tutorial-vue using your GitHub account.
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-vue
Add upstream remote
Add a remote called
upstream
git remote add upstream git@github.com:carbon-design-system/carbon-tutorial-vue.git
Or, if you prefer to use HTTPS instead of SSH with your remotes:
git remote add upstream https://github.com/carbon-design-system/carbon-tutorial-vue.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-vue.git (fetch)upstream git@github.com:carbon-design-system/carbon-tutorial-vue.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 vue-step-1 upstream/vue-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 Vue app’s dependencies with:
yarn
After the dependencies are installed, you can start the app with:
yarn serve
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 tutorial.
OK. So we made a small change to the Vue CLI generated app replacing the HelloWorld component and replaced it with our own message and swapped out the favicon.
Install Carbon
Even though we installed existing dependencies, we’ve yet to install the Carbon packages.
- - component stylescarbon-components
- - Vue components@carbon/vue
- - Vue icons@carbon/icons-vue
Stop your development server with
CTRL-C
yarn add carbon-components @carbon/vue @carbon/icons-vue
Other dependencies
If you check out the file
package.json
- : Used to make routing in Vue apps simplervue-router
- : To ensure we produce well supported code.@vue/cli-plugin-babel
- : To allow us to catch potential errors.@vue/cli-plugin-eslint
- : To allow us to unit test our code.@vue/cli-plugin-unit-jest
- : To allow us to use the sass css precompiler.sass-loader
NOTE: We could have installed these separately but using the CLI to set this up for us ensures a good base config for these dependencies.
To avoid having to add the
~
node_modules
.env
SASS_PATH="node_modules"
For the Windows operating system, use:
SASS_PATH=./node_modules
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 serve
The app looks as it did before. Next, let’s add Carbon styling.
Import carbon-component styles
In the
src
styles
_carbon.scss
App.vue
<style lang="scss">@import "./styles/carbon";</style>
In
styles/_carbon.scss
@import 'carbon-components/scss/globals/scss/styles';
This will take a moment for the Sass to compile. Once compiled, you’ll notice that the Carbon base styling is applied (IBM Plex Sans font family, font size, weight, colors, etc.)
Because any change to
_carbon.scss
Next we’ll create a Carbon
Button
After the other imports in main.js and before the Vue instance is created add the following.
import CarbonComponentsVue from "@carbon/vue";Vue.use(CarbonComponentsVue);
This is a quick way to pull in all @carbon/vue components and register them for use in your project. Individual components can be imported to a project or component.
e.g. Instead of modifying src/main.js we could have added the following to src/App.vue:
<script>import { CvButton } from '@carbon/vue';export default {components: {CvButton,}};</script>
See the Carbon Vue Components documentation for other ways to load components from @carbon/vue components.
In this tutorial we will stick to importing all of the components at once so we can focus on our use of @carbon/vue.
Now open the
App.vue
Hello Carbon! Well, not quite yet. This is the starting point for the Carbon tutorial.
with:
<CvButton>Button</CvButton>
or
<cv-button>Button</cv-button>
Congratulations, you’ve imported your first component! You should see a Carbon styled button on the page.
NOTE: In this tutorial you can use either tag format. The Vue style guide recommend sticking to either Pascal or kebab case. The examples from here will use Pascal case for file and component names with kebab case in the HTML.
Add UI Shell
Next we’re going to create a Vue component called
TutorialHeader
src/components
src/components
TutorialHeader
src/components/TutorialHeader
src/components/TutorialHeader├──index.js└──TutorialHeader.vue
Import and export the header
In
src/components/TutorialHeader/index.js
TutorialHeader
import TutorialHeader from './TutorialHeader';export default TutorialHeader;
Lazyness - VSCode users only
If you are using VSCode then you might want to add the following snippet which will when you type ‘index’ generate an index file for you based on the folder name.
"index-file": {"prefix": "index","body": ["import ${TM_DIRECTORY/.*[\\/]//gm} from './${TM_DIRECTORY/.*[\\/]//gm}';","export { ","\t${TM_DIRECTORY/.*[\\/]//gm},","};","export default ${TM_DIRECTORY/.*[\\/]//gm}",""
You can also use the following to create a skeleton component. To use this one, start typing the word template in your template file and it will generate a file, making assumptions based on the component file name.
"Vue_Component": {"prefix": "template","body": ["<template>","\t$0","</template>","","<script>","export default {",
OK, back to using Carbon components. Let’s make use of our Carbon UI Shell components in
TutorialHeader.vue
<template><cv-header aria-label="Carbon tutorial"><cv-skip-to-content href="#main-content">Skip to content</cv-skip-to-content><cv-header-name href="/" prefix="IBM">Carbon Tutorial</cv-header-name><cv-header-nav>
Import icons
Now let’s import the icons from our
@carbon/icons-vue
TutorialHeader.vue
<script>import { Notification20, UserAvatar20, AppSwitcher20 } from '@carbon/icons-vue';export default {name: "TutorialHeader",components: { Notification20, UserAvatar20, AppSwitcher20 }};</script>
Then we need to add content to the
header-global
<template slot="header-global" />
With:
<template slot="header-global"><cv-header-global-action aria-label="Notifications"><notification-20 /></cv-header-global-action><cv-header-global-action aria-label="User avatar"><user-avatar-20 /></cv-header-global-action><cv-header-global-action aria-label="App switcher"><app-switcher-20 />
Render the header
Next we’ll render our UI Shell by importing our
TutorialHeader
CvContent
App.vue
<script>import TutorialHeader from "./components/TutorialHeader";export default {name: "App",components: {TutorialHeader}};
In addition to importing the
TutorialHeader
components
As our template currently just contains a
Button
<template><div id="app"><tutorial-header /><cv-content id="#main-content"><cv-button>Button</cv-button></cv-content></div></template>
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 views.
Since our app will have two pages, we’ll create two folders in
src/views
src/views├── LandingPage└── RepoPage
Create the following files in the
LandingPage
src/view/LandingPage├── index.js└── LandingPage.vue
Create the following files in the
RepoPage
src/view/RepoPage├── index.js└── RepoPage.vue
Import and export content pages
Starting with
LandingPage
src/view/LandingPage/index.js
import LandingPage from './LandingPage';export default LandingPage;
Next in
LandingPage.vue
<template><div>LANDING PAGE</div></template>
We’ll repeat this process with
RepoPage
In
src/view/RepoPage/index.js
RepoPage
import RepoPage from './RepoPage';export default RepoPage;
Then in
RepoPage.vue
<template><div>REPO PAGE</div></template>
Awesome! We’ve just created our content pages. Next thing we need to do is render them with our router.
Add routing
Lucky for you, as part of the Vue CLI project set up we added vue-router. This created the views folder and also added,
src/router.js
src/main.js
We need to adjust it by replacing the contents of
src/router.js
import Vue from 'vue';import Router from 'vue-router';import LandingPage from './views/LandingPage';Vue.use(Router);export default new Router({routes: [{
If you click on
Repositories
IBM Carbon Tutorial
Next we need to update
src/App.vue
In the template section remove the
<cv-button />
<router-view />
<cv-content id="#main-content"><router-view /></cv-content>
After that we need to do a couple quick fixes to the UI Shell to have it work with the vue-router.
In
src/components/TuturialHeader/TutorialHeader.vue
href
cv-header-name
cv-header-menu-item
to
a
router-link
<cv-header-name to="/" prefix="IBM">Carbon Tutorial</cv-header-name>
and
<cv-header-menu-item to="/repos">Repositories</cv-header-menu-item>
You should now have a working header that routes to different pages without full page reload!
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 vue-step-1
Pull request (PR)
Finally, visit carbon-tutorial-vue to “Compare & create pull request”. In doing so, make sure that you are comparing your repository’s
vue-step-1
base: vue-step-1