Skip to content

Latest commit

History

History
97 lines (75 loc) 路 2.81 KB

2021-05-19.md

File metadata and controls

97 lines (75 loc) 路 2.81 KB
publish_date
2021-05-19
  • Read through styled-components docs. Using a lot of JS-in-CSS at work so finally taking the time to understand how this library works...

    • Utalising prop within the styled components requires using string interpolation
      const Button = styled.button`
        /* Adapt the colors based on primary prop */
        background: ${props => props.primary ? "palevioletred" : "white"};
        color: ${props => props.primary ? "white" : "palevioletred"};
    
        font-size: 1em;
        margin: 1em;
        padding: 0.25em 1em;
        border: 2px solid palevioletred;
        border-radius: 3px;
      `;
    
      render(
        <div>
          <Button>Normal</Button>
          <Button primary>Primary</Button>
        </div>
      );e"};
        color: ${props => props.primary ? "white" : "palevioletred"};
    • styled components opens up polymorphic css! extend an existing styled component with styled()
    const TomatoButton = styled(Button)`
      color: tomato;
      border-color: tomato;
    `;
    // Create the keyframes
    const rotate = keyframes`
      from {
        transform: rotate(0deg);
      }
    
      to {
        transform: rotate(360deg);
      }
    `;
    
    // Here we create a component that will rotate everything we pass in over two seconds
    const Rotate = styled.div`
      display: inline-block;
      animation: ${rotate} 2s linear infinite;
      padding: 2rem 1rem;
      font-size: 1.2rem;
    `;
    
    render(<Rotate>&lt; 馃拝馃従 &gt;</Rotate>);
    const Thing = styled.div`
      color: blue;
    
      .something {
        border: 1px solid; // an element labeled ".something" inside <Thing>
        display: block;
      }
    `;
    
    render(
      <Thing>
        <label htmlFor="foo-button" className="something">
          Mystery button
        </label>
        <button id="foo-button">What do I do?</button>
      </Thing>
    );