安装

  1. yarn add styled-components
  2. # TS还需安装类型声明
  3. yarn add @type/styled-components

样式化组件

支持类 scss 语法

  1. import styled from 'styled-components'
  2. const StyledHeader = styled.header`
  3. background: #343a40;
  4. padding: 14px 100px;
  5. display: flex;
  6. align-items: center;
  7. width: 100%;
  8. > img {
  9. height: 30px;
  10. }
  11. > nav > a {
  12. color: #9a9787;
  13. margin-left: 16px;
  14. padding: 0 4px;
  15. &:hover {
  16. color: #cccdb8;
  17. }
  18. &.active {
  19. position: relative;
  20. color: #cccdb8;
  21. }
  22. &.active::after {
  23. content: '';
  24. display: block;
  25. height: 2px;
  26. width: 100%;
  27. position: absolute;
  28. left: 0;
  29. top: 26px;
  30. background: #f082ac;
  31. }
  32. }
  33. `

动画 keyframes

  1. import styled, { keyframes } from 'styled-components'
  2. const wave = keyframes`
  3. 0% {width: 0; height: 0; opacity: 1;}
  4. 100% {width: 100px; height: 100px; opacity: 0;}
  5. `
  6. const StyledLoading = styled.div`
  7. position: relative;
  8. margin: 100px auto;
  9. &::before,
  10. &::after {
  11. content: '';
  12. position: absolute;
  13. left: 0;
  14. top: 0;
  15. bottom: 0;
  16. right: 0;
  17. margin: auto;
  18. background: #b4b8f9;
  19. border-radius: 50%;
  20. animation: ${wave} 1.5s infinite linear;
  21. }
  22. &::after {
  23. animation-delay: 0.75s;
  24. }
  25. `
  26. const Loading: React.FC = () => {
  27. return <StyledLoading />
  28. }
  29. export default Loading