项目作者: rmariuzzo

项目描述 :
Shorted theme references for Styled Components.
高级语言: TypeScript
项目地址: git://github.com/rmariuzzo/shorted-theme.git
创建时间: 2020-09-18T02:38:10Z
项目社区:https://github.com/rmariuzzo/shorted-theme

开源协议:MIT License

下载


" class="reference-link">shorted-theme - Shorted theme references for Styled Components.

Comparison: before and after




Features

Installation

  1. npm i shorted-theme

Usage

  1. import styled from 'styled-components'
  2. import shorted from 'shorted-theme'
  3. const theme = {
  4. colors: {
  5. primary: 'red',
  6. secondary: 'blue'
  7. },
  8. fontFamily: '"Roboto", sans-serif',
  9. fontSizes: {
  10. small: 12,
  11. regular: 14,
  12. large: 18
  13. }
  14. }
  15. const t = shorted(theme) // 👈 short your theme then use everywhere.
  16. const Button = styled.button`
  17. color: ${t.colors.primary};
  18. color: ${t.colors.primary};
  19. font-family: ${t.fontFamily};
  20. font-size: ${t.fontSizes.regular};
  21. `

TypeScript Support

shorted-theme use all your typings.

Suggestion

You can destructure the shorted theme object. That way you can reference direct children properties instead of a root object.

  1. const { colors, fontFamily, fontSizes } = shorted(theme)
  2. const Button = styled.button`
  3. color: ${colors.primary};
  4. color: ${colors.primary};
  5. font-family: ${fontFamily};
  6. font-size: ${fontSizes.regular};
  7. `

Limitations

shorted-theme is useful for writing less when referencing theme values. However, it can’t be used in expressions. For example:

  1. // THIS WILL NOT WORK
  2. const Button = styled.button`
  3. padding: ${t.padding.default * 10};
  4. `

Instead you should use the function expression:

  1. const Button = styled.button`
  2. padding: ${(props) => props.theme.padding.default * 10};
  3. `

Development

  1. Clone this repository.
  2. Install dependencies: npm i.
  3. Run it locally: npm start or ./src/bin.js

Tests

  1. npm run test

Releases

Releases are triggered by npm version and handled by GitHub Actions.





Made with ♥ by @rmariuzzo