Skip to content

Keyframes #14

Closed
Closed

Description

@mxstbr

We need to get keyframes to work, maybe something like this?

styled.div`
@keyframes {
  0% { color: transparent; }
  100% { color: awesome; }
}
`;

Activity

  1. geelen commented on Sep 13, 2016

    @geelen
    Member

    Hmm. How about:

    import styled, { keyframes } from "styled-components"
    
    const animationName = keyframes`
      0% { color: transparent; }
      100% { color: radboats; }
    `
    
    const Div = styled.div`
      animation: ${animationName} 0.3s 0s both;
    `
  2. mxstbr commented on Sep 13, 2016

    @mxstbr
    MemberAuthor

    I like this as an underlying primitive and exposed API point, but same as with media queries – the user shouldn't have to be aware of the stuff we do to make their lives easier. Writing @keyframes into your string should just work, just like @media.

  3. geelen commented on Sep 13, 2016

    @geelen
    Member

    How do you attach the keyframes to the element though? Just have css-modules like name mangling?

    styled.div`
    animation: local-name 0.3 0s both;
    @keyframes local-name {
      0% { color: transparent; }
      100% { color: awesome; }
    }
    `;

    Looks a bit awkward don't you think?

  4. mxstbr commented on Sep 13, 2016

    @mxstbr
    MemberAuthor

    Ah I forgot you have to define more than just @keyframes, dang. Warming to your idea now!

  5. geelen commented on Sep 21, 2016

    @geelen
    Member

    So, my example above looks really bad to me, because it breaks the metaphor for styled.tag:

    const Div = styled.div`
      /* any css here */
    `

    is the exact equivalent of this (this example could be useful in the docs in future)

    .class-ac32cf84 {
      /* any css here */
    }
    const Div = ({children, className, ...props}) => (
      <div class={`class-ac32cf84 ${className}`} {...props}>
        {children}
      </div>
    )

    right? We're allowing nesting and @media and & to define extra classnames which feels good because it works just like Sass. But defining @keyframes within a definition doesn't really make any sense:

    .class-ac32cf84 {
      animation: local-name 0.3 0s both;
      @keyframes local-name {
        0% { color: transparent; }
        100% { color: awesome; }
      }
    }

    That doesn't really make any sense. In Sass or in any PostCSS or anything, keyframes are always global.

    I'm thinking we should instead do this:

    const LocalName = styled.keyframes`
        0% { color: transparent; }
        100% { color: awesome; }
    `
    const Div = styled.keyframes`
      animation: ${LocalName} 0.3s 0s both;
    `

    Then we can also do something like styled.global for injecting global styles, since we wanted to have that capability as well, right?

  6. haikyuu commented on Dec 1, 2016

    @haikyuu
    Contributor

    Bonus point, you can use

    function rotationBuilder(degree) {
      const rotation = keyframes`
        0% {
          transform: rotate(0deg);
        }
        25% {
          transform: rotate(-${degree}deg);
        }
        50% {
          transform: rotate(0deg);
          opacity: .9
        }
        75% {
          transform: rotate(${degree}deg);
        }
      `;
      return rotation;
    }
    const StyledDiv = styled.div`
      animation: ${rotationBuilder(10)} 1s linear infinite;
    `

    Which is totally awesome.

  7. added a commit that references this issue on May 28, 2017
    35c9909
  8. ljsherlock commented on Jan 3, 2020

    @ljsherlock

    I wanted to add the following for anyone that is, like I was, trying to find a way to make keyframes work with a styled component created via the object method.

    let FieldMarker: any = styled('div')(
      props => ({
        position: 'absolute',
        width: props.theme.space[6],
        height: props.theme.space[6],
        left: `${props.x}px`,
        top: `${props.y}px`,
        '&:after': {
          position: 'absolute',
          left: 0,
          right: 0,
          top: 0,
          bottom: 0,
          margin: 'auto',
          transform: 'scale(0, 0)',
          backgroundColor: props.active && 'rgba(34, 203, 233, .2)',
          borderRadius: '50%',
          content:'""',
          height: '3.5em',
          width: '3.5em',
        }
      })
    )
    
    FieldMarker = styled(FieldMarker)`
      &:after {
        animation: ${css`${markPulse} 3s ease-out infinite;`}
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions