项目作者: diegohaz

项目描述 :
Useful interpolated functions for CSS-in-JS
高级语言: JavaScript
项目地址: git://github.com/diegohaz/styled-tools.git
创建时间: 2017-01-20T06:17:10Z
项目社区:https://github.com/diegohaz/styled-tools

开源协议:MIT License

下载


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:

  1. npm i styled-tools

Yarn:

  1. yarn add styled-tools

Usage

  1. import styled, { css } from "styled-components";
  2. import { prop, ifProp, switchProp } from "styled-tools";
  3. const Button = styled.button`
  4. color: ${prop("color", "red")};
  5. font-size: ${ifProp({ size: "large" }, "20px", "14px")};
  6. background-color: ${switchProp("theme", {
  7. dark: "blue",
  8. darker: "mediumblue",
  9. darkest: "darkblue"
  10. })};
  11. `;
  12. // renders with color: blue
  13. <Button color="blue" ></Button>
  14. // renders with color: red
  15. <Button ></Button>
  16. // renders with font-size: 20px
  17. <Button size="large" ></Button>
  18. // renders with background-color: mediumblue
  19. <Button theme="darker" ></Button>

A more complex example:

  1. const Button = styled.button`
  2. color: ${prop("theme.colors.white", "#fff")};
  3. font-size: ${ifProp({ size: "large" }, prop("theme.sizes.lg", "20px"), prop("theme.sizes.md", "14px"))};
  4. background-color: ${prop("theme.colors.black", "#000")};
  5. ${switchProp("kind", {
  6. dark: css`
  7. background-color: ${prop("theme.colors.blue", "blue")};
  8. border: 1px solid ${prop("theme.colors.blue", "blue")};
  9. `,
  10. darker: css`
  11. background-color: ${prop("theme.colors.mediumblue", "mediumblue")};
  12. border: 1px solid ${prop("theme.colors.mediumblue", "mediumblue")};
  13. `,
  14. darkest: css`
  15. background-color: ${prop("theme.colors.darkblue", "darkblue")};
  16. border: 1px solid ${prop("theme.colors.darkblue", "darkblue")};
  17. `
  18. })}
  19. ${ifProp("disabled", css`
  20. background-color: ${prop("theme.colors.gray", "#999")};
  21. border-color: ${prop("theme.colors.gray", "#999")};
  22. pointer-events: none;
  23. `)}
  24. `;

API

Table of Contents

prop

Returns the value of props[path] or defaultValue

Parameters

  • path string
  • defaultValue any

Examples

  1. import styled from "styled-components";
  2. import { prop } from "styled-tools";
  3. const Button = styled.button`
  4. color: ${prop("color", "red")};
  5. `;

Returns PropsFn

theme

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

Parameters

  • path string
  • defaultValue any

Examples

  1. import styled from "styled-components";
  2. import { theme } from "styled-tools";
  3. const Button = styled.button`
  4. color: ${theme("button.color", "red")};
  5. `;

palette

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

Parameters

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

Examples

  1. import styled, { ThemeProvider } from "styled-components";
  2. import { palette } from "styled-tools";
  3. const theme = {
  4. palette: {
  5. primary: ['#1976d2', '#2196f3', '#71bcf7', '#c2e2fb'],
  6. secondary: ['#c2185b', '#e91e63', '#f06292', '#f8bbd0']
  7. }
  8. };
  9. const Button = styled.button`
  10. color: ${palette(1)}; // props.theme.palette[props.palette][1]
  11. color: ${palette("primary", 1)}; // props.theme.palette.primary[1]
  12. color: ${palette("primary")}; // props.theme.palette.primary[props.tone || 0]
  13. color: ${palette("primary", -1)}; // props.theme.palette.primary[3]
  14. color: ${palette("primary", 10)}; // props.theme.palette.primary[3]
  15. color: ${palette("primary", -10)}; // props.theme.palette.primary[0]
  16. color: ${palette("primary", 0, "red")}; // props.theme.palette.primary[0] || red
  17. `;
  18. <ThemeProvider theme={theme}>
  19. <Button palette="secondary" ></Button>
  20. </ThemeProvider>

ifProp

Returns pass if prop is truthy. Otherwise returns fail

Parameters

Examples

  1. import styled from "styled-components";
  2. import { ifProp, palette } from "styled-tools";
  3. const Button = styled.button`
  4. background-color: ${ifProp("transparent", "transparent", palette(0))};
  5. color: ${ifProp(["transparent", "accent"], palette("secondary"))};
  6. font-size: ${ifProp({ size: "large" }, "20px", ifProp({ size: "medium" }, "16px", "12px"))};
  7. `;

Returns PropsFn

ifNotProp

Returns pass if prop is falsy. Otherwise returns fail

Parameters

Examples

  1. import styled from "styled-components";
  2. import { ifNotProp } from "styled-tools";
  3. const Button = styled.button`
  4. font-size: ${ifNotProp("large", "20px", "30px")};
  5. `;

Returns PropsFn

withProp

Calls a function passing properties values as arguments.

Parameters

Examples

  1. // example with polished
  2. import styled from "styled-components";
  3. import { darken } from "polished";
  4. import { withProp, prop } from "styled-tools";
  5. const Button = styled.button`
  6. border-color: ${withProp(prop("theme.primaryColor", "blue"), darken(0.5))};
  7. font-size: ${withProp("theme.size", size => `${size + 1}px`)};
  8. background: ${withProp(["foo", "bar"], (foo, bar) => `${foo}${bar}`)};
  9. `;

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

  1. import styled, { css } from "styled-components";
  2. import { switchProp, prop } from "styled-tools";
  3. const Button = styled.button`
  4. font-size: ${switchProp(prop("size", "medium"), {
  5. small: prop("theme.sizes.sm", "12px"),
  6. medium: prop("theme.sizes.md", "16px"),
  7. large: prop("theme.sizes.lg", "20px")
  8. }, prop("theme.sizes.md", "16px"))};
  9. ${switchProp("theme.kind", {
  10. light: css`
  11. color: LightBlue;
  12. `,
  13. dark: css`
  14. color: DarkBlue;
  15. `
  16. }, css`color: black;`)}
  17. `;
  18. <Button size="large" theme={{ kind: "light" }} ></Button>

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