1.7.2 • Published 4 years ago

styled-tools v1.7.2

Weekly downloads
32,952
License
MIT
Repository
github
Last release
4 years ago

styled-tools 💅

NPM version NPM downloads Dependencies Build Status Coverage Status

Useful interpolated functions for styled-components 💅, emotion 👩‍🎤, JSS and other CSS-in-JS libraries.

Install

npm:

npm i styled-tools

Yarn:

yarn add styled-tools

Usage

import styled, { css } from "styled-components";
import { prop, ifProp, switchProp } from "styled-tools";

const Button = styled.button`
  color: ${prop("color", "red")};
  font-size: ${ifProp({ size: "large" }, "20px", "14px")};
  background-color: ${switchProp("theme", {
    dark: "blue", 
    darker: "mediumblue", 
    darkest: "darkblue" 
  })};
`;

// renders with color: blue
<Button color="blue" />

// renders with color: red
<Button />

// renders with font-size: 20px
<Button size="large" />

// renders with background-color: mediumblue
<Button theme="darker" />

A more complex example:

const Button = styled.button`
  color: ${prop("theme.colors.white", "#fff")};
  font-size: ${ifProp({ size: "large" }, prop("theme.sizes.lg", "20px"), prop("theme.sizes.md", "14px"))};
  background-color: ${prop("theme.colors.black", "#000")};
  
  ${switchProp("kind", {
    dark: css`
      background-color: ${prop("theme.colors.blue", "blue")};
      border: 1px solid ${prop("theme.colors.blue", "blue")};
    `,
    darker: css`
      background-color: ${prop("theme.colors.mediumblue", "mediumblue")};
      border: 1px solid ${prop("theme.colors.mediumblue", "mediumblue")};
    `,
    darkest: css`
      background-color: ${prop("theme.colors.darkblue", "darkblue")};
      border: 1px solid ${prop("theme.colors.darkblue", "darkblue")};
    `
  })}
  
  ${ifProp("disabled", css`
    background-color: ${prop("theme.colors.gray", "#999")};
    border-color: ${prop("theme.colors.gray", "#999")};
    pointer-events: none;
  `)}
`;

API

Table of Contents

prop

Returns the value of props[path] or defaultValue

Parameters

Examples

import styled from "styled-components";
import { prop } from "styled-tools";

const Button = styled.button`
  color: ${prop("color", "red")};
`;

Returns PropsFn

theme

Same as prop, except that it returns props.theme[path] instead of props[path].

Parameters

Examples

import styled from "styled-components";
import { theme } from "styled-tools";

const Button = styled.button`
 color: ${theme("button.color", "red")};
`;

palette

Returns props.theme.palette[key || props.palette][tone || props.tone || 0] or defaultValue.

Parameters

  • keyOrTone (string | number)
  • toneOrDefaultValue any
  • defaultValue any

Examples

import styled, { ThemeProvider } from "styled-components";
import { palette } from "styled-tools";

const theme = {
  palette: {
    primary: ['#1976d2', '#2196f3', '#71bcf7', '#c2e2fb'],
    secondary: ['#c2185b', '#e91e63', '#f06292', '#f8bbd0']
  }
};

const Button = styled.button`
  color: ${palette(1)};                    // props.theme.palette[props.palette][1]
  color: ${palette("primary", 1)};         // props.theme.palette.primary[1]
  color: ${palette("primary")};            // props.theme.palette.primary[props.tone || 0]
  color: ${palette("primary", -1)};        // props.theme.palette.primary[3]
  color: ${palette("primary", 10)};        // props.theme.palette.primary[3]
  color: ${palette("primary", -10)};       // props.theme.palette.primary[0]
  color: ${palette("primary", 0, "red")};  // props.theme.palette.primary[0] || red
`;

<ThemeProvider theme={theme}>
  <Button palette="secondary" />
</ThemeProvider>

ifProp

Returns pass if prop is truthy. Otherwise returns fail

Parameters

Examples

import styled from "styled-components";
import { ifProp, palette } from "styled-tools";

const Button = styled.button`
  background-color: ${ifProp("transparent", "transparent", palette(0))};
  color: ${ifProp(["transparent", "accent"], palette("secondary"))};
  font-size: ${ifProp({ size: "large" }, "20px", ifProp({ size: "medium" }, "16px", "12px"))};
`;

Returns PropsFn

ifNotProp

Returns pass if prop is falsy. Otherwise returns fail

Parameters

Examples

import styled from "styled-components";
import { ifNotProp } from "styled-tools";

const Button = styled.button`
  font-size: ${ifNotProp("large", "20px", "30px")};
`;

Returns PropsFn

withProp

Calls a function passing properties values as arguments.

Parameters

Examples

// example with polished
import styled from "styled-components";
import { darken } from "polished";
import { withProp, prop } from "styled-tools";

const Button = styled.button`
  border-color: ${withProp(prop("theme.primaryColor", "blue"), darken(0.5))};
  font-size: ${withProp("theme.size", size => `${size + 1}px`)};
  background: ${withProp(["foo", "bar"], (foo, bar) => `${foo}${bar}`)};
`;

Returns PropsFn

switchProp

Switches on a given prop. Returns the value or function for a given prop value. Third parameter is default value.

Parameters

Examples

import styled, { css } from "styled-components";
import { switchProp, prop } from "styled-tools";

const Button = styled.button`
  font-size: ${switchProp(prop("size", "medium"), {
    small: prop("theme.sizes.sm", "12px"),
    medium: prop("theme.sizes.md", "16px"),
    large: prop("theme.sizes.lg", "20px")
  }, prop("theme.sizes.md", "16px"))};
  ${switchProp("theme.kind", {
    light: css`
      color: LightBlue;
    `,
    dark: css`
      color: DarkBlue;
    `
  }, css`color: black;`)}
`;

<Button size="large" theme={{ kind: "light" }} />

Returns PropsFn

Types

Needle

A Needle is used to map the props to a value. This can either be done with a path string "theme.size.sm" or with a function (props) => props.theme.size.sm (these two examples are equivalent).

All of styled-tools can be used as Needles making it possible to do composition between functions. ie ifProp(theme("dark"), "black", "white")

Type: (string | Function)

License

MIT © Diego Haz

nodegen-utils@steroids/styled42dd8860-c724testing-citricteespace-adminstorybook-example-library@acuity-brands/uiux.el.map-attribution-modal@acuity-brands/uiux.facade.application-menu@acuity-brands/uiux.facade.application-menu-mobile@acuity-brands/uiux.facade.confirmation-modal@acuity-brands/uiux.facade.divider@acuity-brands/facade-box@acuity-brands/facade-checkbox@acuity-brands/facade-icon@acuity-brands/facade-loading-circle@acuity-brands/facade-modal@acuity-brands/facade-text@acuity-brands/facade-textfield@acuity-brands/facade-tooltip@acuity-brands/uiux.el.building-level-selector@acuity-brands/uiux.facade.side-menu-mobile@acuity-brands/uiux.facade.tabs@acuity-brands/uiux.facade.text-link@acuity-brands/uiux.facade.you-are-here@acuity-brands/uiux.facade.icon-button@acuity-brands/uiux.facade.loadingcircle@acuity-brands/uiux.facade.popover@acuity-brands/uiux.facade.row-item@acuity-brands/uiux.facade.textarea@acuity-brands/uiux.facade.tile@acuity-brands/uiux.facade.toggle-switch@everything-registry/sub-chunk-2841gngen-uihyperflex-uiharry-reporterm0mnnrhv6eencapsuledrew-i-kitfluid-layouteil-admin-ui-componentsfannypackfannypack-v5galacoemg-text-editorolx-react-gridmakeup-componentsphlo-component-libraryprey-stashreact-atomic-comprcl-react-components-libreact-canvas-annotationreact-linear-gradient-buttonsdk-common-componentssanarflix-webreasreakit-theme-defaultreactarsreactliteroderic@diogobiz/components@dmartss/style-wrapboldr-uibumbag-updatebumbagtest-citrictock-react-kittracksuitstyled-gelstyled-mdlstyled-themecitricbst-components@krustyc/react-checkbox@locus-labs/mod-badge@locus-labs/mod-box@locus-labs/mod-footer@locus-labs/mod-header@locus-labs/mod-icon@locus-labs/mod-location-marker@locus-labs/mod-map-legend@locus-labs/mod-offscreen-indicator@locus-labs/mod-pin@locus-labs/mod-qr-code-card@locus-labs/mod-qr-code-window@locus-labs/vms-autocomplete@locus-labs/vms-badge@locus-labs/vms-banner@locus-labs/vms-box@locus-labs/vms-breadcrumbs@locus-labs/vms-checkbox@locus-labs/vms-pin@locus-labs/vms-primary-button@locus-labs/vms-primary-circle-button@locus-labs/vms-radio@locus-labs/vms-secondary-button@locus-labs/vms-segmented-control@locus-labs/vms-select@locus-labs/vms-slider-control@locus-labs/vms-stepper@locus-labs/vms-tab-menu
1.7.2

4 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago