Lab 06: CSS-in-JS
Overview
Step 1: CSS Modules — Compile-Time Scoping
/* Button.module.css */
.button {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
border: none;
border-radius: 6px;
font-size: 0.875rem;
font-weight: 600;
cursor: pointer;
transition: filter 0.15s ease;
}
.button:hover { filter: brightness(1.1); }
.button:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.primary { background: var(--color-primary); color: white; }
.secondary { background: var(--color-surface-2); color: var(--color-text); }
.danger { background: var(--color-danger); color: white; }
.small { padding: 0.25rem 0.75rem; font-size: 0.75rem; }
.large { padding: 0.75rem 1.5rem; font-size: 1rem; }
/* Composes: inherit styles from another class */
.icon-button {
composes: button; /* inherit base button */
width: 2.5rem;
height: 2.5rem;
padding: 0;
border-radius: 50%;
}Step 2: Vanilla Extract — Zero-Runtime Type-Safe CSS
Step 3: styled-components — Runtime CSS-in-JS
Step 4: Tailwind CSS — Utility-First
Step 5: Comparison Table
Step 6: Bundle Size Strategies
Step 7: Choosing the Right Approach
Step 8: Capstone — CSS Modules Class Name Hashing
Summary
Approach
Runtime
Type Safety
Best For
Last updated
