Style React applications with Styled Components and best practices
# CSS-in-JS with Styled Components for Google Antigravity
Master CSS-in-JS patterns using Styled Components in your Google Antigravity projects. This comprehensive guide covers component styling, theming, and performance optimization for modern React applications.
## Setting Up Styled Components
Configure Styled Components with proper TypeScript support:
```typescript
// src/styles/theme.ts
export const theme = {
colors: {
primary: "#6366f1",
secondary: "#8b5cf6",
background: "#0a0a0a",
surface: "#1a1a1a",
text: "#ffffff",
textMuted: "#a1a1aa",
success: "#22c55e",
warning: "#f59e0b",
error: "#ef4444",
},
spacing: {
xs: "0.25rem",
sm: "0.5rem",
md: "1rem",
lg: "1.5rem",
xl: "2rem",
xxl: "3rem",
},
borderRadius: {
sm: "0.25rem",
md: "0.5rem",
lg: "1rem",
full: "9999px",
},
shadows: {
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.2)",
},
transitions: {
fast: "150ms ease",
normal: "300ms ease",
slow: "500ms ease",
},
} as const;
export type Theme = typeof theme;
```
## Creating Styled Components
Build reusable styled components with props:
```typescript
// src/components/Button/Button.styled.ts
import styled, { css } from "styled-components";
interface ButtonProps {
variant?: "primary" | "secondary" | "outline" | "ghost";
size?: "sm" | "md" | "lg";
fullWidth?: boolean;
isLoading?: boolean;
}
const variants = {
primary: css`
background: linear-gradient(135deg, ${({ theme }) => theme.colors.primary}, ${({ theme }) => theme.colors.secondary});
color: white;
border: none;
&:hover:not(:disabled) {
transform: translateY(-2px);
box-shadow: ${({ theme }) => theme.shadows.lg};
}
`,
secondary: css`
background: ${({ theme }) => theme.colors.surface};
color: ${({ theme }) => theme.colors.text};
border: 1px solid ${({ theme }) => theme.colors.primary};
`,
outline: css`
background: transparent;
color: ${({ theme }) => theme.colors.primary};
border: 2px solid ${({ theme }) => theme.colors.primary};
`,
ghost: css`
background: transparent;
color: ${({ theme }) => theme.colors.text};
border: none;
&:hover {
background: ${({ theme }) => theme.colors.surface};
}
`,
};
const sizes = {
sm: css`
padding: ${({ theme }) => theme.spacing.xs} ${({ theme }) => theme.spacing.sm};
font-size: 0.875rem;
`,
md: css`
padding: ${({ theme }) => theme.spacing.sm} ${({ theme }) => theme.spacing.md};
font-size: 1rem;
`,
lg: css`
padding: ${({ theme }) => theme.spacing.md} ${({ theme }) => theme.spacing.lg};
font-size: 1.125rem;
`,
};
export const StyledButton = styled.button<ButtonProps>`
display: inline-flex;
align-items: center;
justify-content: center;
gap: ${({ theme }) => theme.spacing.sm};
border-radius: ${({ theme }) => theme.borderRadius.md};
font-weight: 600;
cursor: pointer;
transition: all ${({ theme }) => theme.transitions.fast};
width: ${({ fullWidth }) => (fullWidth ? "100%" : "auto")};
${({ variant = "primary" }) => variants[variant]}
${({ size = "md" }) => sizes[size]}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
${({ isLoading }) =>
isLoading &&
css`
pointer-events: none;
opacity: 0.7;
`}
`;
```
## Global Styles and Theme Provider
Set up global styles with Antigravity theming:
```typescript
// src/styles/GlobalStyles.ts
import { createGlobalStyle } from "styled-components";
export const GlobalStyles = createGlobalStyle`
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
scroll-behavior: smooth;
}
body {
font-family: "Inter", -apple-system, sans-serif;
background: ${({ theme }) => theme.colors.background};
color: ${({ theme }) => theme.colors.text};
line-height: 1.6;
-webkit-font-smoothing: antialiased;
}
a {
color: ${({ theme }) => theme.colors.primary};
text-decoration: none;
transition: color ${({ theme }) => theme.transitions.fast};
&:hover {
color: ${({ theme }) => theme.colors.secondary};
}
}
::selection {
background: ${({ theme }) => theme.colors.primary};
color: white;
}
`;
```
## Advanced Patterns
Implement polymorphic components and composition:
```typescript
// src/components/Card/Card.styled.ts
import styled from "styled-components";
export const Card = styled.div`
background: ${({ theme }) => theme.colors.surface};
border-radius: ${({ theme }) => theme.borderRadius.lg};
padding: ${({ theme }) => theme.spacing.lg};
box-shadow: ${({ theme }) => theme.shadows.md};
transition: all ${({ theme }) => theme.transitions.normal};
&:hover {
transform: translateY(-4px);
box-shadow: ${({ theme }) => theme.shadows.lg};
}
`;
export const CardHeader = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: ${({ theme }) => theme.spacing.md};
padding-bottom: ${({ theme }) => theme.spacing.md};
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
`;
export const CardTitle = styled.h3`
font-size: 1.25rem;
font-weight: 600;
background: linear-gradient(135deg, ${({ theme }) => theme.colors.primary}, ${({ theme }) => theme.colors.secondary});
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
`;
```
Google Antigravity leverages these patterns to generate consistent, maintainable styling code across your entire application.This CSS-in-JS prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their css-in-js implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For CSS-in-JS projects, consider mentioning your framework version, coding style, and any specific libraries you're using.