JavaScript and React course ยท Module 7: Styling and Animations
Sass (SCSS) - Cosmic Syntactic Superpower
In this lesson9
In the cosmos of React styling, Sass (Syntactically Awesome Style Sheets) is like a next-generation spaceship - it offers all the capabilities of standard CSS, but with additional powers that make styling more efficient, modular, and easier to maintain. Navigating through the React galaxy, it is worth mastering this technology that can significantly streamline the creation of advanced styles.
What is Sass/SCSS?
Sass is a CSS preprocessor that extends standard CSS with variables, nested rules, mixins, selector inheritance, and many other features. There are two Sass syntaxes:
- Indented syntax (.sass) - indentation-based, without curly braces and semicolons
- SCSS (.scss) - newer syntax that is a superset of CSS (every valid CSS is valid SCSS)
In this module, we will focus on SCSS, as it is more popular and easier to learn for people who know CSS. Think of SCSS as improved rocket fuel that retains all the properties of standard fuel but adds more power.
Setting Up Sass in a React Project
To use Sass in a React project, we first need to install the appropriate packages:
Create React App
If you are using Create React App, installation is simple:
npm install sassThat is all! CRA has built-in Sass support, so after installation you can simply create .scss/.sass files and import them into components.
Vite
In Vite projects, the process is similar:
npm install sassVite has native support for Sass, so no additional configuration is needed.
Other Configurations (e.g., Custom Webpack)
If you are creating your own configuration, you need:
npm install sass sass-loaderAnd add the appropriate configuration to the webpack.config.js file:
// Simplified webpack configuration
module.exports = {
//
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
'sass-loader',
],
},
],
},
};Using SCSS in a React Project
Once configured, you can create SCSS files and import them into components:
// SpacePanel.jsx
import React from 'react';
import './SpacePanel.scss'; // We import SCSS instead of CSS
function SpacePanel() {
return (
<div className="space-panel">
<h2 className="panel-title">Ship Control Panel</h2>
<div className="control-area">
<div className="button-group">
<button className="control-button primary">Start engines</button>
<button className="control-button secondary">Navigation</button>
</div>
<div className="status-display">Status: Ready for launch</div>
</div>
</div>
);
}
export default SpacePanel;Key SCSS Features
1. Variables
In a cosmic interface, we often use the same colors, margins, or fonts. SCSS variables allow us to define them once and use them repeatedly:
// variables.scss
$primary-color: #8a2be2; // Cosmic purple
$secondary-color: #4a90e2; // Space blue
$danger-color: #ff4d4f; // Red alert
$success-color: #52c41a; // Green "go"
$border-radius-small: 4px;
$border-radius-medium: 8px;
$border-radius-large: 16px;
$font-main: 'Space Grotesk', sans-serif;
$font-display: 'Nova Mono', monospace;
// Using variables
.control-button {
background-color: $secondary-color;
border-radius: $border-radius-medium;
font-family: $font-main;
&.primary {
background-color: $primary-color;
}
&.danger {
background-color: $danger-color;
}
}2. Nesting Selectors
Nesting selectors allows creating a more readable hierarchy that reflects the HTML structure:
.space-panel {
background-color: #1a1a2e;
padding: 20px;
border-radius: 10px;
.panel-title {
color: white;
font-size: 24px;
margin-bottom: 15px;
}
.control-area {
display: flex;
justify-content: space-between;
.button-group {
display: flex;
gap: 10px;
.control-button {
padding: 10px 15px;
// Other button styles...
}
}
.status-display {
background-color: rgba(0, 0, 0, 0.3);
padding: 10px;
border-radius: 5px;
}
}
}This notation is not only more readable but also helps avoid selector duplication and makes the structure more obvious.
3. The & (Ampersand) Selector
The ampersand (&) represents the current selector and is especially useful for:
Pseudo-classes and Pseudo-elements
.control-button {
background-color: #4a90e2;
transition: all 0.3s;
&:hover {
background-color: darken(#4a90e2, 10%);
transform: translateY(-2px);
}
&:active {
transform: translateY(1px);
}
&::before {
content: "โฒ";
margin-right: 5px;
}
}Class Modifiers
.control-button {
// Base styles...
&.primary {
background-color: #8a2be2;
}
&.secondary {
background-color: #4a90e2;
}
&.large {
padding: 12px 20px;
font-size: 18px;
}
}Combining Selectors
.space-theme {
.control-button {
// Styles for .space-theme .control-button
}
// Using & we can select different selector combinations
&__dark .control-button {
// Styles for .space-theme__dark .control-button
}
}4. Mixins - Reusable Code
Mixins are collections of CSS declarations that can be used multiple times in code. They are like reusable spaceship modules:
// mixins.scss
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
@mixin glass-effect {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);
}
@mixin button-variant($bg-color, $text-color) {
background-color: $bg-color;
color: $text-color;
&:hover {
background-color: darken($bg-color, 10%);
}
&:active {
background-color: darken($bg-color, 15%);
}
}Using mixins:
.control-panel {
@include glass-effect;
padding: 20px;
}
.button-container {
@include flex-center;
gap: 10px;
}
.launch-button {
@include button-variant(#ff4d4f, white);
padding: 10px 20px;
}
.navigation-button {
@include button-variant(#4a90e2, white);
padding: 8px 15px;
}5. Inheritance - @extend
Inheritance allows one class to inherit the properties of another, reducing code duplication:
.base-button {
padding: 10px 15px;
border: none;
border-radius: 4px;
font-weight: bold;
cursor: pointer;
transition: all 0.3s;
}
.primary-button {
@extend .base-button;
background-color: #8a2be2;
color: white;
}
.secondary-button {
@extend .base-button;
background-color: #4a90e2;
color: white;
}
.ghost-button {
@extend .base-button;
background-color: transparent;
color: #8a2be2;
border: 1px solid currentColor;
}6. Mathematical and Color Functions
SCSS offers many built-in functions for manipulating colors and values:
.panel {
// Mathematical operations
width: 100% - 40px;
padding: 10px * 2;
// Color functions
background-color: lighten(#1a1a2e, 10%);
border: 1px solid darken(#1a1a2e, 5%);
.warning-label {
color: transparentize(#ff4d4f, 0.2); // Adds transparency
}
.accent-element {
background-color: mix(#8a2be2, #4a90e2, 30%); // Mixes colors
}
}7. Importing and File Organization
SCSS enables splitting code into modular files, which is crucial for large projects. Files are loaded with the @use rule (the old @import has been deprecated since Dart Sass 1.80), and the as * suffix makes variables and mixins available without a namespace prefix:
// styles/variables.scss
$primary-color: #8a2be2;
// Other variables...
// styles/mixins.scss
@mixin flex-center { /* ... */ }
// Other mixins...
// styles/buttons.scss
@use 'variables' as *;
@use 'mixins' as *;
.button { /* ... */ }
// Button styles...
// styles/panels.scss
@use 'variables' as *;
@use 'mixins' as *;
.panel { /* ... */ }
// Panel styles...
// styles/main.scss
@use 'variables' as *;
@use 'mixins' as *;
@use 'buttons';
@use 'panels';
// We can import everything together or selectively in components8. Conditional Operators and Loops
SCSS also offers control structures known from programming languages:
// Conditions
$theme: 'dark';
.space-control {
@if $theme == 'dark' {
background-color: #121212;
color: white;
} @else {
background-color: #f5f5f5;
color: #333;
}
}
// Loops
$sizes: (
'small': 8px,
'medium': 16px,
'large': 24px,
'xlarge': 32px
);
@each $name, $size in $sizes {
.margin-#{$name} {
margin: $size;
}
.padding-#{$name} {
padding: $size;
}
}
// Generates:
// .margin-small { margin: 8px; }
// .padding-small { padding: 8px; }
// ...etc. for all sizesIntegrating SCSS with CSS Modules in React
SCSS can be perfectly combined with CSS Modules, creating a powerful styling solution:
// SpacePanel.module.scss
@use 'src/styles/variables' as *;
@use 'src/styles/mixins' as *;
.panel {
@include glass-effect;
background-color: $panel-bg-color;
.title {
color: $text-primary;
font-family: $font-display;
}
.controls {
display: flex;
gap: $spacing-md;
.button {
@include button-base;
&.primary {
@include button-variant($primary-color, white);
}
&.secondary {
@include button-variant($secondary-color, white);
}
}
}
}Usage in a component:
import React from 'react';
import styles from './SpacePanel.module.scss';
function SpacePanel() {
return (
<div className={styles.panel}>
<h2 className={styles.title}>Control Panel</h2>
<div className={styles.controls}>
<button className={`${styles.button} ${styles.primary}`}>
Launch
</button>
<button className={`${styles.button} ${styles.secondary}`}>
Cancel
</button>
</div>
</div>
);
}Best Practices for Using SCSS in React
1. File Structure
Organize your SCSS files in a way that reflects the component structure:
src/
โโโ styles/ # Global styles and assets
โ โโโ _variables.scss # Convention: files with underscore are for import only
โ โโโ _mixins.scss
โ โโโ _functions.scss
โ โโโ _reset.scss
โ โโโ global.scss # Main global styles file
โโโ components/
โ โโโ Button/
โ โ โโโ Button.jsx
โ โ โโโ Button.module.scss
โ โโโ Panel/
โ โ โโโ Panel.jsx
โ โ โโโ Panel.module.scss
โ โโโ
โโโ2. Naming Conventions
Use consistent naming conventions:
- BEM (Block, Element, Modifier) - for CSS classes
- camelCase - for SCSS variables and mixins
- PascalCase - for React components
- kebab-case - for SCSS files
// _variables.scss
$primaryColor: #8a2be2;
$secondaryColor: #4a90e2;
// Button.module.scss (using BEM)
.button {
// Block
background-color: $primaryColor;
&__icon {
// Element
margin-right: 8px;
}
&--large {
// Modifier
padding: 12px 24px;
}
}3. Avoid Excessive Nesting
Too deep nesting can lead to overly specific selectors, making them harder to override:
// Avoid:
.panel {
.content {
.section {
.title {
.icon {
// 5 levels of nesting!
color: red;
}
}
}
}
}
// Better:
.panel {
// General panel styles
}
.panel-content {
// Content styles
}
.section-title-icon {
color: red;
}4. Use Variables for Repeating Values
// _spacing.scss
$spacing-xs: 4px;
$spacing-sm: 8px;
$spacing-md: 16px;
$spacing-lg: 24px;
$spacing-xl: 32px;
// _colors.scss
$blue-100: #e3f2fd;
$blue-200: #bbdefb;
//
$blue-900: #0d47a1;
$color-primary: $blue-500;
$color-secondary: $purple-500;5. Create System Mixins
@mixin media-breakpoint-up($breakpoint) {
@if $breakpoint == sm {
@media (min-width: 576px) { @content; }
} @else if $breakpoint == md {
@media (min-width: 768px) { @content; }
} @else if $breakpoint == lg {
@media (min-width: 992px) { @content; }
} @else if $breakpoint == xl {
@media (min-width: 1200px) { @content; }
}
}
// Usage:
.responsive-element {
font-size: 14px;
@include media-breakpoint-up(md) {
font-size: 16px;
}
@include media-breakpoint-up(lg) {
font-size: 18px;
}
}Practical Example: Spaceship Control Panel
Let's see how to apply SCSS to create an interactive control panel:
// _variables.scss
$primary-color: #7b2cbf;
$secondary-color: #3a86ff;
$success-color: #06d6a0;
$warning-color: #ffd166;
$danger-color: #ef476f;
$dark-bg: #1a1a2e;
$panel-bg: #252941;
$text-light: #e6e6ff;
$border-radius-sm: 4px;
$border-radius-md: 8px;
$border-radius-lg: 12px;
$transition-fast: 0.2s ease;
$transition-normal: 0.3s ease;
// _mixins.scss
@mixin glass-morphism {
background: rgba(37, 41, 65, 0.8);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
@mixin button-glow($color) {
box-shadow: 0 0 15px rgba($color, 0.5);
&:hover {
box-shadow: 0 0 25px rgba($color, 0.7);
}
}
// SpaceControlPanel.module.scss
@use 'src/styles/variables' as *;
@use 'src/styles/mixins' as *;
.controlPanel {
@include glass-morphism;
padding: 24px;
border-radius: $border-radius-lg;
color: $text-light;
.panelHeader {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 24px;
padding-bottom: 16px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
.title {
font-size: 28px;
font-weight: 600;
&::before {
content: "โฒ";
margin-right: 10px;
}
}
.status {
padding: 6px 12px;
border-radius: $border-radius-sm;
font-size: 14px;
font-weight: 500;
&.ready {
background-color: rgba($success-color, 0.2);
color: $success-color;
}
&.standby {
background-color: rgba($warning-color, 0.2);
color: $warning-color;
}
&.error {
background-color: rgba($danger-color, 0.2);
color: $danger-color;
}
}
}
.controlGrid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
.controlSection {
background-color: rgba(0, 0, 0, 0.2);
border-radius: $border-radius-md;
padding: 16px;
.sectionTitle {
font-size: 18px;
margin-bottom: 16px;
color: rgba($text-light, 0.8);
}
.buttonGroup {
display: flex;
flex-direction: column;
gap: 12px;
}
}
}
.button {
padding: 12px 16px;
border: none;
border-radius: $border-radius-sm;
font-weight: 500;
letter-spacing: 0.5px;
cursor: pointer;
transition: all $transition-normal;
position: relative;
overflow: hidden;
&::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(
90deg,
transparent,
rgba(255, 255, 255, 0.2),
transparent
);
transition: all 0.6s;
}
&:hover::before {
left: 100%;
}
&.primary {
background-color: $primary-color;
color: white;
@include button-glow($primary-color);
}
&.secondary {
background-color: $secondary-color;
color: white;
@include button-glow($secondary-color);
}
&.success {
background-color: $success-color;
color: black;
@include button-glow($success-color);
}
&.warning {
background-color: $warning-color;
color: black;
@include button-glow($warning-color);
}
&.danger {
background-color: $danger-color;
color: white;
@include button-glow($danger-color);
}
&.disabled {
opacity: 0.5;
cursor: not-allowed;
pointer-events: none;
}
}
.monitorDisplay {
margin-top: 24px;
padding: 16px;
background-color: rgba(0, 0, 0, 0.4);
border-radius: $border-radius-md;
font-family: monospace;
height: 120px;
overflow-y: auto;
.logLine {
margin: 4px 0;
font-size: 14px;
&.info {
color: $secondary-color;
}
&.success {
color: $success-color;
}
&.warning {
color: $warning-color;
}
&.error {
color: $danger-color;
}
&::before {
content: "> ";
}
}
}
}
// Usage in a React component
import React, { useState } from 'react';
import styles from './SpaceControlPanel.module.scss';
function SpaceControlPanel() {
const [status, setStatus] = useState('standby');
const [logs, setLogs] = useState([
{ type: 'info', text: 'System initialized' },
{ type: 'info', text: 'Waiting for commands' }
]);
const addLog = (text, type = 'info') => {
setLogs(prev => [...prev, { type, text }]);
};
const initiateSequence = () => {
setStatus('ready');
addLog('Launch sequence initiated', 'success');
};
const abortSequence = () => {
setStatus('standby');
addLog('Launch sequence aborted', 'warning');
};
const triggerAlert = () => {
setStatus('error');
addLog('ALERT: Critical system failure detected', 'error');
};
return (
<div className={styles.controlPanel}>
<div className={styles.panelHeader}>
<h2 className={styles.title}>Cosmic Control Panel</h2>
<div className={`${styles.status} ${styles[status]}`}>
Status: {status === 'standby' ? 'Standby' :
status === 'ready' ? 'Ready' : 'Error'}
</div>
</div>
<div className={styles.controlGrid}>
<div className={styles.controlSection}>
<h3 className={styles.sectionTitle}>Launch Control</h3>
<div className={styles.buttonGroup}>
<button
className={`${styles.button} ${styles.success}`}
onClick={initiateSequence}
>
Initiate Launch
</button>
<button
className={`${styles.button} ${styles.warning}`}
onClick={abortSequence}
>
Abort Sequence
</button>
</div>
</div>
<div className={styles.controlSection}>
<h3 className={styles.sectionTitle}>Onboard Systems</h3>
<div className={styles.buttonGroup}>
<button
className={`${styles.button} ${styles.primary}`}
onClick={() => addLog('Navigation systems online', 'info')}
>
Navigation System
</button>
<button
className={`${styles.button} ${styles.secondary}`}
onClick={() => addLog('Life support activated', 'info')}
>
Life Support
</button>
</div>
</div>
<div className={styles.controlSection}>
<h3 className={styles.sectionTitle}>Safety</h3>
<div className={styles.buttonGroup}>
<button
className={`${styles.button} ${styles.danger}`}
onClick={triggerAlert}
>
Emergency Alert
</button>
<button
className={`${styles.button} ${styles.disabled}`}
>
Evacuation Protocol
</button>
</div>
</div>
</div>
<div className={styles.monitorDisplay}>
{logs.map((log, index) => (
<div key={index} className={`${styles.logLine} ${styles[log.type]}`}>
{log.text}
</div>
))}
</div>
</div>
);
}
export default SpaceControlPanel;Sass vs CSS-in-JS: When to Use Which?
Advantages of SCSS
- Performance - CSS is generated during build time, not at runtime
- Familiar syntax - very similar to standard CSS
- Tool support - full support for developer tools (DevTools)
- CSS extraction - styles can be extracted into separate CSS files
- Smaller bundle size - does not require additional JS libraries
When to Choose SCSS?
- In larger projects with complex styles
- When you prefer separation of logic and style
- When performance is critical
- When working with designers who use traditional CSS/SCSS
- When the team has experience with SCSS
Summary
Sass (SCSS) is a powerful tool that significantly extends the capabilities of CSS while remaining close to its original syntax. In the React ecosystem, SCSS integrates well with both the modular approach (CSS Modules) and other styling techniques.
Thanks to variables, mixins, nesting, and other features, SCSS allows creating more modular, easier-to-maintain CSS code, which is especially important in larger React applications.
Like any advanced spaceship, SCSS requires some time to master, but the investment in learning quickly pays off in the form of more efficient work and better organized code. For React applications, where managing components and their styles is crucial, SCSS is one of the best styling options, offering an excellent balance between power and simplicity.
Code for this lesson: App.jsx
import React, { useState } from 'react';
import styled, { ThemeProvider, createGlobalStyle, keyframes } from 'styled-components';
// Theme definitions
const themes = {
space: {
primary: '#00d4ff',
secondary: '#ffd700',
background: 'linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%)',
cardBg: 'rgba(255, 255, 255, 0.05)',
text: '#ffffff',
textSecondary: '#a0a0a0',
},
nebula: {
primary: '#ff6b9d',
secondary: '#c44569',
background: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%)',
cardBg: 'rgba(255, 107, 157, 0.1)',
text: '#ffffff',
textSecondary: '#b0b0b0',
},
solar: {
primary: '#ffa502',
secondary: '#ff6348',
background: 'linear-gradient(135deg, #2c2c54 0%, #474787 50%, #706fd3 100%)',
cardBg: 'rgba(255, 165, 2, 0.1)',
text: '#ffffff',
textSecondary: '#c0c0c0',
},
};
// Global styles
const GlobalStyle = createGlobalStyle`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
min-height: 100vh;
}
`;
// Animations
const float = keyframes`
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
`;
const pulse = keyframes`
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
`;
// Styled components
const AppContainer = styled.div`
min-height: 100vh;
background: ${props => props.theme.background};
padding: 30px;
color: ${props => props.theme.text};
transition: all 0.5s ease;
`;
const Header = styled.header`
text-align: center;
margin-bottom: 40px;
`;
const Title = styled.h1`
font-size: 2.5em;
color: ${props => props.theme.primary};
text-shadow: 0 0 20px ${props => props.theme.primary}40;
animation: ${float} 3s ease-in-out infinite;
`;
const Subtitle = styled.p`
color: ${props => props.theme.textSecondary};
margin-top: 10px;
`;
const ThemeSelector = styled.div`
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 40px;
`;
const ThemeButton = styled.button`
padding: 12px 24px;
border: 2px solid ${props => props.active ? props.theme.primary : 'transparent'};
border-radius: 8px;
background: ${props => props.theme.cardBg};
color: ${props => props.theme.text};
cursor: pointer;
font-weight: bold;
transition: all 0.3s ease;
&:hover {
background: ${props => props.theme.primary}30;
transform: translateY(-2px);
}
`;
const CardsGrid = styled.div`
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 25px;
max-width: 1200px;
margin: 0 auto;
`;
const Card = styled.div`
background: ${props => props.theme.cardBg};
border-radius: 15px;
padding: 25px;
border: 1px solid ${props => props.theme.primary}30;
transition: all 0.3s ease;
&:hover {
transform: translateY(-5px);
box-shadow: 0 10px 30px ${props => props.theme.primary}20;
}
`;
const CardTitle = styled.h3`
color: ${props => props.theme.secondary};
margin-bottom: 15px;
font-size: 1.4em;
`;
const CardText = styled.p`
color: ${props => props.theme.textSecondary};
line-height: 1.6;
margin-bottom: 20px;
`;
const Button = styled.button`
padding: 10px 20px;
background: ${props => props.variant === 'primary'
? props.theme.primary
: 'transparent'};
color: ${props => props.variant === 'primary'
? '#000'
: props.theme.primary};
border: 2px solid ${props => props.theme.primary};
border-radius: 6px;
cursor: pointer;
font-weight: bold;
transition: all 0.3s ease;
&:hover {
background: ${props => props.theme.primary};
color: #000;
transform: scale(1.05);
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`;
const StatusBadge = styled.span`
display: inline-block;
padding: 4px 12px;
border-radius: 20px;
font-size: 0.85em;
font-weight: bold;
background: ${props => {
switch(props.status) {
case 'active': return 'rgba(76, 175, 80, 0.2)';
case 'pending': return 'rgba(255, 152, 0, 0.2)';
case 'inactive': return 'rgba(244, 67, 54, 0.2)';
default: return 'rgba(255, 255, 255, 0.1)';
}
}};
color: ${props => {
switch(props.status) {
case 'active': return '#4CAF50';
case 'pending': return '#ff9800';
case 'inactive': return '#f44336';
default: return '#fff';
}
}};
animation: ${props => props.status === 'active' ? pulse : 'none'} 2s infinite;
`;
const Footer = styled.footer`
text-align: center;
margin-top: 50px;
color: ${props => props.theme.textSecondary};
padding: 20px;
border-top: 1px solid ${props => props.theme.primary}20;
`;
// Data components
const missions = [
{
id: 1,
name: 'Mars Exploration',
description: 'Research mission on the surface of the Red Planet',
status: 'active',
},
{
id: 2,
name: 'Orbital Station',
description: 'Building a new space station in Earth orbit',
status: 'pending',
},
{
id: 3,
name: 'Jupiter Probe',
description: 'Studying the atmosphere of the largest planet in the Solar System',
status: 'inactive',
},
];
function App() {
const [currentTheme, setCurrentTheme] = useState('space');
return (
<ThemeProvider theme={themes[currentTheme]}>
<GlobalStyle />
<AppContainer>
<Header>
<Title>Styled Components Demo</Title>
<Subtitle>Dynamic styling with themes in React</Subtitle>
</Header>
<ThemeSelector>
{Object.keys(themes).map(themeName => (
<ThemeButton
key={themeName}
active={currentTheme === themeName}
onClick={() => setCurrentTheme(themeName)}
>
{themeName.charAt(0).toUpperCase() + themeName.slice(1)}
</ThemeButton>
))}
</ThemeSelector>
<CardsGrid>
{missions.map(mission => (
<Card key={mission.id}>
<CardTitle>{mission.name}</CardTitle>
<StatusBadge status={mission.status}>
{mission.status}
</StatusBadge>
<CardText>{mission.description}</CardText>
<Button variant="primary">Details</Button>
</Card>
))}
</CardsGrid>
<Footer>
<p>Styled-components allows writing CSS in JavaScript</p>
<p>with full support for themes and dynamic styles</p>
</Footer>
</AppContainer>
</ThemeProvider>
);
}
export default App;Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which SCSS syntax is compatible with CSS?
2. What does the & operator mean in nested SCSS selectors?
These are 2 of 5 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Horizontal ordering
Arrange the SCSS installation command in a React project:
- Click in order
Arrange the SCSS variable definition:
- Code editor
SCSS Variable System
- Code editor
SCSS Nesting and the & Operator
- Click in order
Arrange the mixin definition in SCSS:
- Vertical ordering
Arrange the steps for adding SCSS to a React project:
- Vertical ordering
Arrange the elements of the @extend directive in SCSS from left to right:
- Code editor
SCSS Mixins for Components
- Vertical ordering
Arrange a nested SCSS selector with the hover pseudo-class:
- Code editor
SCSS Functions
- Click in order
Arrange the BEM syntax in SCSS from block to modifier:
- Vertical ordering
Order the SCSS compilation stages:
- Code editor
CSS Modules with SCSS
Tasks after this lesson
- Put in orderInstalling SCSS
- QuestionSCSS vs Sass
- Click in orderVariables in SCSS
- Coding taskPractice: SCSS Variables
- QuestionNesting Selectors in SCSS
- Coding taskPractice: SCSS Nesting
- Click in orderMixins in SCSS
- Put in orderSteps to Configure SCSS in React
- QuestionMixins in SCSS
- Put in order@extend Directive in SCSS
- Coding taskPractice: SCSS Mixins
- Put in orderNesting in SCSS
- QuestionSCSS Nesting Best Practices
- QuestionImport Directive in SCSS
- Coding taskPractice: SCSS Functions
- Click in orderBEM Naming Methodology
- Put in orderSCSS Compilation Order
- Coding taskPractice: CSS Modules + SCSS