项目作者: ianva

项目描述 :
A tool to do pattern matching for props of style-components, just like write styles using css selectors.
高级语言: JavaScript
项目地址: git://github.com/ianva/style-cond.git
创建时间: 2020-05-25T12:53:29Z
项目社区:https://github.com/ianva/style-cond

开源协议:

下载


Style-cond

A tool to do pattern matching for props of style-components, just like write styles using css selectors.

Install

npm:

  1. npm i style-cond

yarn:

  1. yarn add style-cond

Usage

Matching specify props

  1. export const AudioPlay = styled.div<{ isHidden: boolean; isAudioPlayComplete: boolean; }>`
  2. border-radius: 3px;
  3. box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
  4. ${styleCond({
  5. isHidden: css`
  6. display: none;
  7. `,
  8. isAudioPlayComplete: css`
  9. opacity: 0.3;
  10. `
  11. })}
  12. `;

Matching value of props

  1. styled.button<{size: "s" | "m" | "l" | "xl"; hasBorder: boolean; index:number }>`
  2. font-size: 14px;
  3. border-radius: 12px;
  4. border: 1px solid #c00;
  5. ${styleCond(
  6. size: {
  7. s: css`
  8. padding: 0 8px;
  9. text-align: center;
  10. `,
  11. m: css`
  12. padding: 0 10px;
  13. `,
  14. l: css`
  15. padding: 0 12px;
  16. `,
  17. xl: css`
  18. padding: 0 14px;
  19. `
  20. },
  21. hasBorder: {
  22. false: css`
  23. border: 0;
  24. `
  25. },
  26. index: {
  27. 1: {
  28. font-size: 16px
  29. }
  30. 2: {
  31. font-size: 18px
  32. }
  33. }
  34. )}
  35. `

Passing props

  1. export const NavigationButton = styled.button<variant: "primary" | "default"; isDisabled: boolean>`
  2. ${styleCond((props) => ({
  3. variant: {
  4. primary: css`
  5. background-color: #ebab45;
  6. ${!props.isDisabled && css`
  7. &:hover {
  8. background-color: #dea612;
  9. }
  10. `}
  11. `,
  12. default: css`
  13. background-color: #ffffff;
  14. ${!props.isDisabled && css`
  15. &:hover {
  16. background-color: rgba(0, 0, 0, 0.03);
  17. }
  18. `}
  19. `
  20. }
  21. }))}
  22. `

Matching value by specify condition

  1. export const Gap = styled.input<{ isFocused: boolean; isInputted: boolean; isInteractive: boolean }>`
  2. ${styleCond((props)=>({
  3. isInputted: [
  4. (value) => value && !props.isFocused,
  5. css`
  6. border-color: #f5bb23;
  7. background-color: #f5bb23;
  8. `
  9. ],
  10. isInteractive: [
  11. (value) => value === false,
  12. css`
  13. &:hover {
  14. box-shadow: none;
  15. }
  16. `]
  17. }))}
  18. `;

Complex conditions

  1. export const Foo = styled.div<{ max:number }>`
  2. color: black;
  3. ${styleCond({
  4. max: [
  5. [
  6. value => value > 100,
  7. css`
  8. color: red;
  9. `
  10. ],
  11. [
  12. value => value > 50,
  13. css`
  14. color: green;
  15. `
  16. ],
  17. {
  18. 1: css`
  19. color: blueviolet;
  20. `,
  21. 2: css`
  22. color: pink;
  23. `
  24. }
  25. ]
  26. })}
  27. `;