Skip to content

Commit efa7d25

Browse files
authored
Merge pull request carbon-design-system#150 from abbeyhrt/chore-add-file-names-react-tutorial
chore(react tutorial): adds file paths to code blocks
2 parents 5e85438 + 4a6992f commit efa7d25

File tree

5 files changed

+104
-306
lines changed

5 files changed

+104
-306
lines changed

src/pages/tutorial/react/step-1.mdx

Lines changed: 28 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,13 @@ $ yarn add node-sass
129129

130130
To avoid having to add the `~` prefix when importing SCSS files from `node_modules`, create a `.env` file at the project root that contains:
131131

132-
##### .env
133-
134-
```bash
132+
```bash path=.env
135133
SASS_PATH="node_modules"
136134
```
137135

138136
For the Windows operating system, use:
139137

140-
##### .env
141-
142-
```bash
138+
```bash path=.env
143139
SASS_PATH=./node_modules
144140
```
145141

@@ -157,38 +153,30 @@ In the `src` directory, rename `index.css` as `index.scss`. Then in `index.js` u
157153

158154
In `index.scss`, import the Carbon styles by adding the following at the top of the file:
159155

160-
##### src/index.scss
161-
162-
```scss
156+
```scss path=src/index.scss
163157
@import 'carbon-components/scss/globals/scss/styles.scss';
164158
```
165159

166160
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.)
167161

168162
Because any change to `index.scss` will re-compile all of the Carbon Sass, create an `app.scss` file in the `src` directory and in `App.js`, import that new file.
169163

170-
##### src/App.js
171-
172-
```javascript
164+
```javascript path=src/App.js
173165
import './app.scss';
174166
```
175167

176168
_Note: To optimize our Sass compile time, we'll be adding app-specific styling to_ `app.scss` _and only modifying_ `index.scss` _when necessary._
177169

178170
Next we'll import a `Button` from Carbon to test that our dependencies are working properly. At the top of `App.js`, import the `Button` by adding the following:
179171

180-
##### src/App.js
181-
182-
```javascript
172+
```javascript path=src/App.js
183173
import { Button } from 'carbon-components-react';
184174
```
185175

186176
In the `App` component return, you can now replace:
187177

188-
##### src/App.js
189-
190178
<!-- prettier-ignore-start -->
191-
```html
179+
```html path=src/App.js
192180
<div>
193181
Hello Carbon! Well, not quite yet. This is the starting point for the Carbon tutorial.
194182
</div>
@@ -197,10 +185,8 @@ In the `App` component return, you can now replace:
197185

198186
with:
199187

200-
##### src/App.js
201-
202188
<!-- prettier-ignore-start -->
203-
```html
189+
```html path=src/App.js
204190
<Button>Button</Button>
205191
```
206192
<!-- prettier-ignore-end -->
@@ -224,9 +210,7 @@ src/components/TutorialHeader
224210

225211
In `index.scss` add the following feature-flag **above** the Carbon styles import like so:
226212

227-
##### src/index.scss
228-
229-
```scss
213+
```scss path=src/index.scss
230214
$feature-flags: (
231215
ui-shell: true,
232216
);
@@ -236,28 +220,22 @@ This is because our UI Shell is in experimental mode and the styles need to be m
236220

237221
Next, in `app.scss`, we'll import our `TutorialHeader` styles. Your file should now look like this:
238222

239-
##### src/app.scss
240-
241-
```scss
223+
```scss path=src/app.scss
242224
@import './components/TutorialHeader/tutorial-header.scss';
243225
```
244226

245227
### Import and export the header
246228

247229
In `src/components/TutorialHeader/index.js`, import and export our `TutorialHeader` component like so:
248230

249-
##### src/components/TutorialHeader/index.js
250-
251-
```javascript
231+
```javascript path=src/components/TutorialHeader/index.js
252232
import TutorialHeader from './TutorialHeader';
253233
export default TutorialHeader;
254234
```
255235

256236
Next we'll import our Carbon UI Shell components into `TutorialHeader.js`. Set up the file like so:
257237

258-
##### src/components/TutorialHeader/TutorialHeader.js
259-
260-
```javascript
238+
```javascript path=src/components/TutorialHeader/TutorialHeader.js
261239
import React from 'react';
262240
import {
263241
Header,
@@ -295,27 +273,21 @@ _Note: It's important that the_ `TutorialHeader` _returns the Carbon_ `Header` _
295273
296274
Now let's import the icons from our `@carbon/icons-react` elements package. In the `TutorialHeader.js` file, we need to import each individual icon we will use.
297275
298-
##### src/components/TutorialHeader/TutorialHeader.js
299-
300-
```javascript
276+
```javascript path=src/components/TutorialHeader/TutorialHeader.js
301277
import Notification20 from '@carbon/icons-react/lib/notification/20';
302278
import UserAvatar20 from '@carbon/icons-react/lib/user--avatar/20';
303279
import AppSwitcher20 from '@carbon/icons-react/lib/app-switcher/20';
304280
```
305281

306282
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:
307283

308-
##### src/components/TutorialHeader/TutorialHeader.js
309-
310-
```html
284+
```html path=src/components/TutorialHeader/TutorialHeader.js
311285
<HeaderGlobalBar />
312286
```
313287

314288
With:
315289

316-
##### src/components/TutorialHeader/TutorialHeader.js
317-
318-
```html
290+
```html path=src/components/TutorialHeader/TutorialHeader.js
319291
<HeaderGlobalBar>
320292
<HeaderGlobalAction aria-label="Notifications">
321293
<Notification20 />
@@ -333,9 +305,7 @@ With:
333305

334306
Next we'll render our UI Shell by importing our `TutorialHeader` component and `Content` into `App.js`. Your imports should look like this:
335307

336-
##### src/App.js
337-
338-
```javascript
308+
```javascript path=src/App.js
339309
import React, { Component } from 'react';
340310
import './app.scss';
341311
import { Button } from 'carbon-components-react';
@@ -345,9 +315,7 @@ import TutorialHeader from './components/TutorialHeader';
345315

346316
Our `return` currently just contains a `Button`. Let's update that to include our imported components. This should look like the following:
347317

348-
##### src/App.js
349-
350-
```javascript
318+
```javascript path=src/App.js
351319
class App extends Component {
352320
render() {
353321
return (
@@ -400,9 +368,7 @@ src/content/RepoPage
400368

401369
Next, we'll import our content Sass files in `app.scss`, like so:
402370

403-
##### src/app.scss
404-
405-
```scss
371+
```scss path=src/app.scss
406372
@import './components/TutorialHeader/tutorial-header.scss';
407373
@import './content/LandingPage/landing-page.scss';
408374
@import './content/RepoPage/repo-page.scss';
@@ -412,18 +378,14 @@ Next, we'll import our content Sass files in `app.scss`, like so:
412378

413379
Now that our stylesheets are set up, we need to create our pages' components. Starting with `LandingPage`, just like with our header, we need to export the component in `src/content/LandingPage/index.js` by adding:
414380

415-
##### src/content/LandingPage/index.js
416-
417-
```javascript
381+
```javascript path=src/content/LandingPage/index.js
418382
import LandingPage from './LandingPage';
419383
export default LandingPage;
420384
```
421385

422386
Next in `LandingPage.js` we'll create our component.
423387

424-
##### src/content/LandingPage/LandingPage.js
425-
426-
```javascript
388+
```javascript path=src/content/LandingPage/LandingPage.js
427389
import React from 'react';
428390

429391
const LandingPage = () => {
@@ -437,18 +399,14 @@ We'll repeat this process with `RepoPage`.
437399

438400
In `src/content/RepoPage/index.js`, import and export the `RepoPage` component like so:
439401

440-
##### src/content/RepoPage/index.js
441-
442-
```javascript
402+
```javascript path=src/content/RepoPage/index.js
443403
import RepoPage from './RepoPage';
444404
export default RepoPage;
445405
```
446406

447407
Then in `RepoPage.js` create the component.
448408

449-
##### src/content/RepoPage/RepoPage.js
450-
451-
```javascript
409+
```javascript path=src/content/RepoPage/RepoPage.js
452410
import React from 'react';
453411

454412
const RepoPage = () => {
@@ -471,19 +429,15 @@ $ yarn start
471429

472430
First, we need to wrap our app in the `Router` component. In the root `index.js`, add the import:
473431

474-
##### src/index.js
475-
476-
```javascript
432+
```javascript path=src/index.js
477433
import { HashRouter as Router } from 'react-router-dom';
478434
```
479435

480436
_Note: We're using_ `HashRouter` _instead of_ `BrowserRouter` _to simplify deployments in upcoming tutorial steps. Learn more about the React Router [here](https://reacttraining.com/react-router/web/api/BrowserRouter)._
481437

482438
Then, update the `render()` function to include the `Router`.
483439

484-
##### src/index.js
485-
486-
```javascript
440+
```javascript path=src/index.js
487441
ReactDOM.render(
488442
<Router>
489443
<App />
@@ -494,9 +448,7 @@ ReactDOM.render(
494448

495449
In order to render our content pages, we need to add the following imports in `App.js` below our existing imports.
496450

497-
##### src/App.js
498-
499-
```javascript
451+
```javascript path=src/App.js
500452
import { Route, Switch } from 'react-router-dom';
501453
import LandingPage from './content/LandingPage';
502454
import RepoPage from './content/RepoPage';
@@ -508,10 +460,8 @@ Next thing we need to is update what we're returning in `App.js` . We currently
508460

509461
Now inside `Content` we'll add the following:
510462

511-
##### src/App.js
512-
513463
<!-- prettier-ignore-start -->
514-
```html
464+
```html path=src/App.js
515465
<Switch>
516466
<Route exact path="/" component={LandingPage} />
517467
<Route path="/repos" component={RepoPage} />
@@ -523,27 +473,21 @@ After that we need to do a couple quick fixes to the UI Shell to have it work wi
523473
524474
Add the `Link` import in `TutorialHeader.js`:
525475
526-
##### src/components/TutorialHeader/TutorialHeader.js
527-
528-
```javascript
476+
```javascript path=src/components/TutorialHeader/TutorialHeader.js
529477
import { Link } from 'react-router-dom';
530478
```
531479

532480
We need to use the `Link` component instead of the default anchor elements to prevent full page reload when navigating to different pages with React Router. To use `Link`, update the `HeaderName` component to use the `element` prop and replace the `href` with `to`:
533481

534-
##### src/components/TutorialHeader/TutorialHeader.js
535-
536-
```javascript
482+
```javascript path=src/components/TutorialHeader/TutorialHeader.js
537483
<HeaderName element={Link} to="/" prefix="IBM">
538484
Carbon Tutorial
539485
</HeaderName>
540486
```
541487

542488
Do the same with the component that contains `href="/repos"`, updating to:
543489

544-
##### src/components/TutorialHeader/TutorialHeader.js
545-
546-
```javascript
490+
```javascript path=src/components/TutorialHeader/TutorialHeader.js
547491
<HeaderMenuItem element={Link} to="/repos">
548492
Repositories
549493
</HeaderMenuItem>

0 commit comments

Comments
 (0)