Kurs JavaScript i React · Moduł 7: Stylowanie i animacje
Projekt: Kosmiczny Panel Kontroli Misji
W tej lekcji4
Gratulacje, Nawigatorze! Dotarłeś do końca tego modułu i nadszedł czas, aby połączyć wszystkie poznane techniki stylowania w jeden, kompleksowy projekt. Twoim zadaniem jest stworzenie Kosmicznego Panelu Kontroli Misji - interaktywnego dashboardu, który wykorzysta wiedzę z całego modułu.
Cel projektu
Zbudujesz panel kontrolny statku kosmicznego, który łączy:
- Styled Components z dynamicznym themingiem
- Animacje CSS z
@keyframes - Responsywny layout z CSS Grid i Flexbox
- Theming (tryb jasny/ciemny)
- Interaktywne elementy z efektami hover i transition
Architektura projektu
Projekt składa się z kilku kluczowych sekcji:
1. System motywów (Theming)
Wykorzystujemy ThemeProvider ze styled-components, aby zarządzać kolorami i stylami w całej aplikacji:
const darkTheme = {
bg: '#0a0a1a',
surface: '#12122a',
accent: '#00ff88',
text: '#e0e0e0',
danger: '#ff4444',
warning: '#ffaa00'
};
const lightTheme = {
bg: '#f0f4f8',
surface: '#ffffff',
accent: '#0066ff',
text: '#1a1a2e',
danger: '#cc0000',
warning: '#cc8800'
};2. Animacje CSS
Panel wykorzystuje kilka typów animacji, które poznałeś w module:
/* Pulsowanie statusu */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
/* Wjazd z przesunięciem */
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Obracanie radaru */
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}3. Styled Components z propsami
Dynamiczne stylowanie komponentów na podstawie przekazanych propsów:
const StatusBadge = styled.span`
padding: 4px 12px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
background: ${props => props.active ? '#00ff8833' : '#ff444433'};
color: ${props => props.active ? '#00ff88' : '#ff4444'};
animation: pulse 2s infinite;
`;
const Card = styled.div`
background: ${props => props.theme.surface};
border: 1px solid ${props => props.theme.accent}33;
border-radius: 12px;
padding: 20px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
&:hover {
transform: translateY(-4px);
box-shadow: 0 8px 25px rgba(0, 255, 136, 0.15);
}
`;4. Responsywny layout z CSS Grid
Panel automatycznie dostosowuje się do szerokości ekranu:
.dashboard-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
padding: 20px;
}
@media (max-width: 768px) {
.dashboard-grid {
grid-template-columns: 1fr;
}
}5. Kompletna struktura komponentu
Główny komponent App łączy wszystkie elementy:
function App() {
const [isDark, setIsDark] = useState(true);
return (
<ThemeProvider theme={isDark ? darkTheme : lightTheme}>
<GlobalStyle />
<Header>
<Title>Mission Control</Title>
<ThemeToggle onClick={() => setIsDark(!isDark)}>
{isDark ? 'Jasny' : 'Ciemny'}
</ThemeToggle>
</Header>
<DashboardGrid>
<StatusCard systems={systemsData} />
<RadarCard />
<MissionsCard missions={missionsData} />
<CrewCard crew={crewData} />
</DashboardGrid>
</ThemeProvider>
);
}Kluczowe techniki zastosowane w projekcie
| Technika | Zastosowanie w projekcie |
|---|---|
| Styled Components | Wszystkie komponenty UI |
| ThemeProvider | Przełączanie dark/light mode |
@keyframes | Pulsowanie statusów, obrót radaru, wjazd kart |
| CSS Grid | Layout dashboardu |
| Flexbox | Układ wewnętrzny kart |
transition | Efekty hover na kartach i przyciskach |
| Props dynamiczne | Kolory statusów, warunkowe style |
| Responsywność | auto-fit i minmax w gridzie |
Podsumowanie modułu
W tym module poznałeś pełne spektrum technik stylowania w React:
- Klasyczny CSS i CSS Modules - fundament stylowania z izolacją klas
- Styled Components - CSS-in-JS z dynamicznymi propsami i themingiem
- SCSS - preprocesor z zmiennymi, mixinami i zagnieżdżaniem
- Tailwind CSS - podejście utility-first dla szybkiego prototypowania
- Frameworki UI - gotowe biblioteki komponentów (MUI, Ant Design, Chakra)
- Design Systems - budowanie spójnych systemów projektowych
- React Transition Group - animowanie cyklu życia komponentów
- Animacje CSS -
@keyframes,transitioni zaawansowane efekty
Każda z tych technik ma swoje miejsce w arsenale kosmicznego programisty. Najlepsi inżynierowie potrafią łączyć je w spójny system - dokładnie tak, jak w tym projekcie.
Wypróbuj panel poniżej i eksperymentuj z kodem - zmień kolory motywu, dodaj nowe animacje lub rozbuduj sekcje dashboardu!
Kod do tej lekcji: App.jsx
import React, { useState } from 'react';
import styled, { keyframes, css, ThemeProvider, createGlobalStyle } from 'styled-components';
// ===== THEMES =====
const darkTheme = {
bg: '#0a0a1a',
surface: '#12122a',
surfaceHover: '#1a1a3a',
accent: '#00ff88',
accentDim: 'rgba(0,255,136,0.15)',
text: '#e0e0e0',
textSecondary: '#8888aa',
danger: '#ff4444',
warning: '#ffaa00',
border: 'rgba(0,255,136,0.2)',
};
const lightTheme = {
bg: '#f0f4f8',
surface: '#ffffff',
surfaceHover: '#e8ecf0',
accent: '#0066ff',
accentDim: 'rgba(0,102,255,0.1)',
text: '#1a1a2e',
textSecondary: '#666688',
danger: '#cc0000',
warning: '#cc8800',
border: 'rgba(0,102,255,0.2)',
};
// ===== GLOBAL STYLES =====
const GlobalStyle = createGlobalStyle`
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', Tahoma, sans-serif;
background: ${props => props.theme.bg};
color: ${props => props.theme.text};
transition: background 0.3s ease, color 0.3s ease;
}
`;
// ===== ANIMATIONS =====
const pulse = keyframes`
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
`;
const slideIn = keyframes`
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
`;
const rotate = keyframes`
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
`;
const glow = keyframes`
0%, 100% { box-shadow: 0 0 5px ${() => 'rgba(0,255,136,0.3)'}; }
50% { box-shadow: 0 0 20px ${() => 'rgba(0,255,136,0.6)'}; }
`;
// ===== LAYOUT COMPONENTS =====
const Header = styled.header`
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 24px;
background: ${p => p.theme.surface};
border-bottom: 1px solid ${p => p.theme.border};
`;
const Title = styled.h1`
font-size: 20px;
color: ${p => p.theme.accent};
letter-spacing: 2px;
text-transform: uppercase;
`;
const ThemeToggle = styled.button`
background: ${p => p.theme.accentDim};
border: 1px solid ${p => p.theme.accent};
color: ${p => p.theme.accent};
padding: 8px 16px;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
transition: all 0.3s ease;
&:hover {
background: ${p => p.theme.accent};
color: ${p => p.theme.bg};
}
`;
const DashboardGrid = styled.div`
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
padding: 24px;
`;
const Card = styled.div`
background: ${p => p.theme.surface};
border: 1px solid ${p => p.theme.border};
border-radius: 12px;
padding: 20px;
animation: ${slideIn} 0.5s ease ${p => p.delay || '0s'} both;
transition: transform 0.3s ease, box-shadow 0.3s ease;
&:hover {
transform: translateY(-4px);
box-shadow: 0 8px 25px ${p => p.theme.accentDim};
}
`;
const CardTitle = styled.h3`
font-size: 14px;
text-transform: uppercase;
letter-spacing: 1px;
color: ${p => p.theme.accent};
margin-bottom: 16px;
display: flex;
align-items: center;
gap: 8px;
`;
// ===== STATUS COMPONENTS =====
const StatusBadge = styled.span`
display: inline-block;
padding: 3px 10px;
border-radius: 12px;
font-size: 11px;
font-weight: bold;
background: ${p => p.active ? 'rgba(0,255,136,0.15)' : 'rgba(255,68,68,0.15)'};
color: ${p => p.active ? '#00ff88' : '#ff4444'};
animation: ${pulse} 2s infinite;
`;
const SystemRow = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid ${p => p.theme.border};
&:last-child { border-bottom: none; }
`;
// ===== RADAR COMPONENTS =====
const RadarWrapper = styled.div`
width: 120px;
height: 120px;
border-radius: 50%;
border: 2px solid ${p => p.theme.accent};
position: relative;
margin: 0 auto 16px;
overflow: hidden;
background: radial-gradient(circle, ${p => p.theme.accentDim} 0%, transparent 70%);
`;
const RadarSweep = styled.div`
position: absolute;
top: 0; left: 50%;
width: 2px; height: 50%;
background: ${p => p.theme.accent};
transform-origin: bottom center;
animation: ${rotate} 3s linear infinite;
`;
const RadarDot = styled.div`
position: absolute;
width: 6px; height: 6px;
background: ${p => p.theme.accent};
border-radius: 50%;
top: ${p => p.y}%;
left: ${p => p.x}%;
animation: ${pulse} 1.5s infinite;
`;
// ===== PROGRESS BAR =====
const ProgressOuter = styled.div`
width: 100%;
height: 6px;
background: ${p => p.theme.border};
border-radius: 3px;
margin-top: 6px;
`;
const ProgressInner = styled.div`
height: 100%;
width: ${p => p.value || 0}%;
background: ${p =>
p.value > 75 ? '#00ff88' :
p.value > 40 ? '#ffaa00' : '#ff4444'};
border-radius: 3px;
transition: width 0.5s ease;
`;
// ===== CREW AVATAR =====
const AvatarCircle = styled.div`
width: 36px;
height: 36px;
border-radius: 50%;
background: ${p => p.color || p.theme.accent};
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
font-weight: bold;
color: #fff;
`;
const CrewRow = styled.div`
display: flex;
align-items: center;
gap: 12px;
padding: 8px 0;
`;
const CrewInfo = styled.div`
flex: 1;
& span { display: block; font-size: 12px; color: ${p => p.theme.textSecondary}; }
`;
// ===== DATA =====
const systems = [
{ name: 'Silniki warp', active: true },
{ name: 'Osłony energetyczne', active: true },
{ name: 'Systemy nawigacji', active: true },
{ name: 'Komunikacja dalekiego zasięgu', active: false },
{ name: 'Podtrzymywanie życia', active: true },
];
const missions = [
{ name: 'Eksploracja Nebuli X-7', progress: 87 },
{ name: 'Transport zaopatrzenia', progress: 45 },
{ name: 'Mapowanie asteroid', progress: 62 },
{ name: 'Naprawa stacji relay', progress: 15 },
];
const crew = [
{ name: 'Kpt. Nova', role: 'Dowódca', color: '#e74c3c', initial: 'N' },
{ name: 'Lt. Orion', role: 'Nawigator', color: '#3498db', initial: 'O' },
{ name: 'Dr. Stella', role: 'Medyk', color: '#9b59b6', initial: 'S' },
{ name: 'Ing. Vega', role: 'Inżynier', color: '#2ecc71', initial: 'V' },
];
// ===== APP =====
function App() {
const [isDark, setIsDark] = useState(true);
const theme = isDark ? darkTheme : lightTheme;
return (
<ThemeProvider theme={theme}>
<GlobalStyle />
<Header>
<Title>Mission Control</Title>
<ThemeToggle onClick={() => setIsDark(!isDark)}>
{isDark ? 'Light Mode' : 'Dark Mode'}
</ThemeToggle>
</Header>
<DashboardGrid>
{/* Systems Status */}
<Card delay="0s">
<CardTitle>Systemy statku</CardTitle>
{systems.map((s, i) => (
<SystemRow key={i}>
<span>{s.name}</span>
<StatusBadge active={s.active}>
{s.active ? 'ONLINE' : 'OFFLINE'}
</StatusBadge>
</SystemRow>
))}
</Card>
{/* Radar */}
<Card delay="0.1s">
<CardTitle>Radar</CardTitle>
<RadarWrapper>
<RadarSweep />
<RadarDot x={30} y={25} />
<RadarDot x={65} y={40} />
<RadarDot x={45} y={70} />
</RadarWrapper>
<p style={{ textAlign: 'center', fontSize: 13, opacity: 0.7 }}>
3 obiekty w zasięgu skanera
</p>
</Card>
{/* Missions Progress */}
<Card delay="0.2s">
<CardTitle>Misje</CardTitle>
{missions.map((m, i) => (
<div key={i} style={{ marginBottom: 12 }}>
<div style={{ display: 'flex', justifyContent: 'space-between', fontSize: 13 }}>
<span>{m.name}</span>
<span style={{ fontWeight: 'bold' }}>{m.progress}%</span>
</div>
<ProgressOuter>
<ProgressInner value={m.progress} />
</ProgressOuter>
</div>
))}
</Card>
{/* Crew */}
<Card delay="0.3s">
<CardTitle>Załoga</CardTitle>
{crew.map((c, i) => (
<CrewRow key={i}>
<AvatarCircle color={c.color}>{c.initial}</AvatarCircle>
<CrewInfo>
<strong>{c.name}</strong>
<span>{c.role}</span>
</CrewInfo>
<StatusBadge active={true}>Na służbie</StatusBadge>
</CrewRow>
))}
</Card>
</DashboardGrid>
</ThemeProvider>
);
}
export default App;Hasła z dokumentacji w tej lekcji
Widzisz błąd w tej lekcji?
Zadania praktyczne w grze
- Edytor kodu
Zbuduj kompleksowy Design System zawierający: styled-components dla component-level styling, CSS-in-JS patterns, theme provider z dark/light modes, responsive design z breakpoints, CSS Grid i Flexbox layouts, animations z @keyframes, typography system, color tokens, spacing system, oraz accessibility features (focus states, ARIA). Stwórz design system godny najbardziej zaawansowanych interfejsów Deep Space Nine!