styled-components-best-practices
styled-components best practices for CSS-in-JS development in React applications
What this skill does
# styled-components Best Practices
You are an expert in styled-components, CSS-in-JS patterns, and React component styling.
## Key Principles
- Write component-scoped styles that avoid global CSS conflicts
- Leverage the full power of JavaScript for dynamic styling
- Keep styled components small, focused, and reusable
- Prioritize performance with proper memoization and SSR support
## Basic Setup
### Installation
```bash
npm install styled-components
npm install -D @types/styled-components # For TypeScript
```
### Basic Usage
```tsx
import styled from 'styled-components';
const Button = styled.button`
display: inline-flex;
align-items: center;
justify-content: center;
padding: 8px 16px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.3s ease;
&:hover {
background-color: #2980b9;
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`;
// Usage
function App() {
return <Button>Click me</Button>;
}
```
## Project Structure
### File Organization
```
src/
├── components/
│ ├── Button/
│ │ ├── Button.tsx
│ │ ├── Button.styles.ts # Styled components
│ │ ├── Button.types.ts # TypeScript types
│ │ └── index.ts # Re-exports
│ ├── Card/
│ │ ├── Card.tsx
│ │ ├── Card.styles.ts
│ │ └── index.ts
│ └── index.ts
├── styles/
│ ├── theme.ts # Theme definition
│ ├── GlobalStyles.ts # Global styles
│ ├── mixins.ts # Reusable style mixins
│ └── index.ts
└── App.tsx
```
### Component Style File
```tsx
// Button.styles.ts
import styled, { css } from 'styled-components';
import type { ButtonProps } from './Button.types';
export const StyledButton = styled.button<Pick<ButtonProps, 'variant' | 'size'>>`
display: inline-flex;
align-items: center;
justify-content: center;
border: none;
border-radius: ${({ theme }) => theme.borderRadius.md};
font-family: inherit;
font-weight: ${({ theme }) => theme.fontWeight.medium};
cursor: pointer;
transition: all ${({ theme }) => theme.transition.base};
${({ size, theme }) => {
switch (size) {
case 'small':
return css`
padding: ${theme.spacing.xs} ${theme.spacing.sm};
font-size: ${theme.fontSize.small};
`;
case 'large':
return css`
padding: ${theme.spacing.md} ${theme.spacing.lg};
font-size: ${theme.fontSize.large};
`;
default:
return css`
padding: ${theme.spacing.sm} ${theme.spacing.md};
font-size: ${theme.fontSize.base};
`;
}
}}
${({ variant, theme }) => {
switch (variant) {
case 'secondary':
return css`
background-color: transparent;
color: ${theme.colors.primary};
border: 2px solid ${theme.colors.primary};
&:hover:not(:disabled) {
background-color: ${theme.colors.primary};
color: white;
}
`;
case 'danger':
return css`
background-color: ${theme.colors.error};
color: white;
&:hover:not(:disabled) {
background-color: ${theme.colors.errorDark};
}
`;
default:
return css`
background-color: ${theme.colors.primary};
color: white;
&:hover:not(:disabled) {
background-color: ${theme.colors.primaryDark};
}
`;
}
}}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`;
export const ButtonIcon = styled.span`
display: inline-flex;
margin-right: ${({ theme }) => theme.spacing.xs};
`;
```
## Theming
### Theme Definition
```tsx
// styles/theme.ts
export const theme = {
colors: {
primary: '#3498db',
primaryLight: '#5dade2',
primaryDark: '#2980b9',
secondary: '#2ecc71',
secondaryLight: '#58d68d',
secondaryDark: '#27ae60',
error: '#e74c3c',
errorLight: '#ec7063',
errorDark: '#c0392b',
warning: '#f39c12',
success: '#27ae60',
info: '#17a2b8',
text: '#333333',
textMuted: '#666666',
textLight: '#999999',
background: '#ffffff',
backgroundAlt: '#f8f9fa',
border: '#e0e0e0',
borderDark: '#cccccc',
},
spacing: {
xs: '4px',
sm: '8px',
md: '16px',
lg: '24px',
xl: '32px',
xxl: '48px',
},
fontSize: {
xs: '0.75rem',
small: '0.875rem',
base: '1rem',
large: '1.25rem',
xl: '1.5rem',
xxl: '2rem',
xxxl: '2.5rem',
},
fontWeight: {
normal: 400,
medium: 500,
semibold: 600,
bold: 700,
},
fontFamily: {
base: "'Helvetica Neue', Arial, sans-serif",
heading: "'Georgia', serif",
mono: "'Consolas', monospace",
},
lineHeight: {
tight: 1.2,
base: 1.5,
relaxed: 1.75,
},
borderRadius: {
sm: '2px',
md: '4px',
lg: '8px',
xl: '16px',
pill: '50px',
circle: '50%',
},
shadow: {
sm: '0 1px 2px rgba(0, 0, 0, 0.05)',
md: '0 4px 6px rgba(0, 0, 0, 0.1)',
lg: '0 10px 15px rgba(0, 0, 0, 0.1)',
xl: '0 20px 25px rgba(0, 0, 0, 0.15)',
},
transition: {
fast: '0.15s ease',
base: '0.3s ease',
slow: '0.5s ease',
},
breakpoints: {
sm: '576px',
md: '768px',
lg: '992px',
xl: '1200px',
xxl: '1400px',
},
zIndex: {
dropdown: 1000,
sticky: 1020,
fixed: 1030,
modalBackdrop: 1040,
modal: 1050,
popover: 1060,
tooltip: 1070,
},
} as const;
export type Theme = typeof theme;
```
### TypeScript Theme Typing
```tsx
// styles/styled.d.ts
import 'styled-components';
import type { Theme } from './theme';
declare module 'styled-components' {
export interface DefaultTheme extends Theme {}
}
```
### Theme Provider Setup
```tsx
// App.tsx
import { ThemeProvider } from 'styled-components';
import { theme } from './styles/theme';
import { GlobalStyles } from './styles/GlobalStyles';
function App() {
return (
<ThemeProvider theme={theme}>
<GlobalStyles />
{/* App content */}
</ThemeProvider>
);
}
```
## Global Styles
```tsx
// styles/GlobalStyles.ts
import { createGlobalStyle } from 'styled-components';
export const GlobalStyles = createGlobalStyle`
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
font-size: 16px;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
margin: 0;
padding: 0;
font-family: ${({ theme }) => theme.fontFamily.base};
font-size: ${({ theme }) => theme.fontSize.base};
line-height: ${({ theme }) => theme.lineHeight.base};
color: ${({ theme }) => theme.colors.text};
background-color: ${({ theme }) => theme.colors.background};
}
h1, h2, h3, h4, h5, h6 {
font-family: ${({ theme }) => theme.fontFamily.heading};
font-weight: ${({ theme }) => theme.fontWeight.bold};
line-height: ${({ theme }) => theme.lineHeight.tight};
margin-top: 0;
margin-bottom: ${({ theme }) => theme.spacing.md};
}
p {
margin-top: 0;
margin-bottom: ${({ theme }) => theme.spacing.md};
}
a {
color: ${({ theme }) => theme.colors.primary};
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
button {
font-family: inherit;
}
img {
max-width: 100%;
height: auto;
}
/* Focus styles for accessibility */
:focus-visible {
outline: 2px solid ${({ theme }) => theme.colors.primary};
outline-offset: 2px;
}
`;
```
## Dynamic Styling
### Props-Based Styling
```tsx
import styled, { css } from 'styled-components';
interface CardProps {
$elevated?: boolean;
$variant?: 'default' | 'outlined' | 'filled';
}
const Card = styled.div<CardProps>`
border-radius: ${({ theme }) => theme.borderRadius.lg};
padding: ${({ theme }) => theme.spacing.md};
transition: box-shadow ${({ theme }) => theme.transition.base};
${({ $variant, theRelated in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.