Styled-is Flag Utility for styled-components

1.3.0 · active · verified Sun Apr 19

styled-is is a utility library designed to simplify conditional styling within `styled-components` by providing declarative functions for checking component props. It offers functions like `is`, `isNot`, `isOr`, and `isSomeNot` for boolean prop checks, as well as `match` for value-based prop comparisons. The current stable version, 1.3.0, indicates a mature and stable project focused on a specific use case within the `styled-components` ecosystem. The library aims to enable cleaner and more readable style definitions, reducing the need for verbose ternary operators or complex prop destructuring directly within styled templates. It ships with TypeScript types, enhancing the developer experience in TypeScript projects by providing strong type checking and IDE autocompletion.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `styled-is` functions like `is`, `isNot`, `isOr`, and `match` to create conditionally styled `ThemedButton` components in a React application. It showcases various prop-based styling scenarios including simple booleans, combined booleans, value matching, and a dynamic compact size based on a prop, rendering several button variations.

import is, { isNot, isOr, match } from 'styled-is';
import styled from 'styled-components';
import React from 'react';
import { createRoot } from 'react-dom/client';

const ThemedButton = styled.button`
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  background-color: #eee;
  color: #333;

  ${is('primary')`
    background-color: #007bff;
    color: white;
  `};

  ${is('large')`
    padding: 15px 30px;
    font-size: 20px;
  `};

  ${isNot('disabled')`
    &:hover {
      opacity: 0.9;
    }
  `};

  ${isOr('primary', 'secondary')`
    font-weight: bold;
  `};

  ${match('variant', 'outline')`
    border: 2px solid currentColor;
    background-color: transparent;
  `};

  ${match('variant', 'text')`
    background-color: transparent;
    color: #007bff;
    padding: 5px 10px;
  `};

  ${(props) =>
    is('compact')(props) &&
    `
      padding: 5px 10px;
      font-size: 14px;
    `}
`;

function App() {
  return (
    <div>
      <ThemedButton>Default Button</ThemedButton>
      <ThemedButton primary>Primary Button</ThemedButton>
      <ThemedButton primary large>Primary Large Button</ThemedButton>
      <ThemedButton large disabled>Large Disabled Button</ThemedButton>
      <ThemedButton variant="outline" primary>Outline Primary</ThemedButton>
      <ThemedButton variant="text">Text Button</ThemedButton>
      <ThemedButton compact>Compact Button</ThemedButton>
      <ThemedButton primary compact>Primary Compact</ThemedButton>
    </div>
  );
}

const container = document.getElementById('root');
if (container) {
  const root = createRoot(container);
  root.render(<App />);
} else {
  console.error("Root element not found. Please ensure your HTML has a <div id='root'>.");
}

view raw JSON →