Styled Components Breakpoint Utilities

3.0.0-preview.20 · active · verified Tue Apr 21

styled-components-breakpoint provides utility functions for integrating responsive design breakpoints directly into styled-components using CSS-in-JS. Currently in a 3.0.0-preview.20 release, it offers two primary modes of operation: themeable mixins that leverage styled-components' ThemeProvider to define breakpoints globally, and static mixin factories for scenarios where breakpoints do not need to be dynamic or themed. The library simplifies the creation of media queries, allowing developers to define styles that adapt across different screen sizes (e.g., mobile, tablet, desktop) using an intuitive API like breakpoint('md'). It is designed to complement other related packages such as styled-components-spacing and styled-components-grid, promoting a consistent approach to responsive layouts within styled-components projects. While a formal release cadence isn't specified, its preview status indicates ongoing development towards a stable v3.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up `styled-components-breakpoint` with a `ThemeProvider` for custom breakpoints, applying responsive styles using the `breakpoint` mixin and mapping values with the `map` mixin.

import React from 'react';
import ReactDOM from 'react-dom/client';
import styled, { ThemeProvider } from 'styled-components';
import breakpoint, { map } from 'styled-components-breakpoint';

// Define a theme with custom breakpoints
const theme = {
  breakpoints: {
    xs: 0,
    sm: 576,
    md: 768,
    lg: 992,
    xl: 1200,
  },
};

// Define a styled component using the breakpoint and map utilities
const ResponsiveBox = styled.div`
  padding: 1rem;
  text-align: center;
  font-family: sans-serif;
  
  // Base style for all sizes
  background-color: lightblue;
  color: black;
  font-size: 14px;

  // Styles from 'md' breakpoint onwards
  ${breakpoint('md')`
    background-color: lightgreen;
    font-size: 18px;
    padding: 2rem;
  `}

  // Styles from 'lg' breakpoint onwards
  ${breakpoint('lg')`
    background-color: lightcoral;
    font-size: 22px;
    padding: 3rem;
  `}

  // Map colors based on breakpoints
  ${map(
    { xs: 'crimson', sm: 'darkblue', md: 'purple', lg: 'darkgreen', xl: 'black' },
    color => `color: ${color};`
  )}
`;

function App() {
  return (
    <ThemeProvider theme={theme}>
      <ResponsiveBox>
        <h1>Responsive Text Example</h1>
        <p>
          This box changes its background color, font size, padding, and text color
          based on the viewport width using styled-components-breakpoint.
        </p>
        <p>Try resizing your browser window!</p>
      </ResponsiveBox>
    </ThemeProvider>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root') ?? document.createElement('div'));
root.render(<App />);

view raw JSON →