Part-2. Add Dart sass to project
<= BACK TO PART 1 … GO TO PART 3 (HOOKS) =>
Workflow
- Install Dart Sass npm package
- Open commandline at the root of the project (Your project root is the same folder where your package.json file is.)
- use command
npm install sass
- Create an example component
- Create a new folder in
./components
. Name the folder toexample
- Create a new file
someExampleComponent.tsx
in./components/example
- Create a new file inside a
./components/example
and name it tosomeExampleComponent.module.scss
- Open the someExampleComponent.tsx file and use your
fcr
code snippet and save.- see docs/snippets
- Import style file, placing
import styles from './someExampleComponent.module.scss'
to the top of the someExampleComponent.tsx - Replace all HTML code in your example components return statement with the HTML code at the end of this file.
- You can see the
className={styles.container}
part indiv
element, it is a style reference to the style class of the sass file you imported.
- You can see the
- Paste the style class from end of this file to your components sass style file (.scss).
- Now open App.tsx file and import this example component.
import SomeExampleComponent from './components/example/someExampleComponent'
- Now you can use this new component, just like HTML!
- in App.tsx, place
<SomeExampleComponent />
between the div tags.
- in App.tsx, place
- Create a new folder in
- Now you can check the results, run command
npm run start
again. - Remember to add, commit & push your project to the GitHub.
HTML
<div className={styles.container}>
<h1>Header</h1>
<p>My supercool component</p>
</div>
CSS / SCSS
.container {
width: 100%;
height: 450px;
background: #1abc9c;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
h1 {
font-size: 52px;
color: azure;
text-shadow: 2px 2px #7860ff;
}
p {
font-weight: 700;
font-size: 25px;
color: rgb(206, 255, 255);
background: rgb(255, 0, 200);
padding: 10px;
}
}