question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing props.theme in a keyframes object

See original GitHub issue

Hey!

I would love to be able to access my theme in a keyframes object. I am making a div’s background-color cycle through the various brand colors, but currently seem to have to hard-code the hex values.

My use case is something like this:


import styled from 'styled-components';
import { keyframes } from 'styled-components';

const colorize = keyframes`
    0% {
        background-color: palevioletred;
    }

    100% {
        background-color: papayawhip;
    }
`;

const Wrapper = styled.div`
    animation: ${colorize} 4s ease-in-out infinite;
`;

It would be great if I could write background-color: ${props => props.theme.brandColorOne};

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
k15acommented, May 29, 2017

Duplicate of https://github.com/styled-components/styled-components/issues/397

import styled from 'styled-components';
import { keyframes } from 'styled-components';

const colorize = props => keyframes`
    0% {
        background-color: ${props.theme.brandColorOne};
    }

    100% {
        background-color: ${props.theme.brandColorTwo};
    }
`;

const Wrapper = styled.div`
    animation: ${colorize} 4s ease-in-out infinite;
`;
0reactions
ckknightcommented, Jun 24, 2019
type MyTheme = { ... };
export function themedKeyframes(
  fn: (theme: MyTheme) => Keyframes,
): (props: { theme: MyTheme }) => Keyframes {
  const keyframeCache =
    typeof WeakMap === 'undefined' ? new Map() : new WeakMap();
  return ({ theme }) => {
    let result = keyframeCache.get(theme);
    if (!result) {
      result = fn(theme);
      keyframeCache.set(theme, result);
    }
    return result;
  };
}

Usage:

const myAnimation = themedKeyframes(theme => keyframes`
  from: { color: ${theme.colors.RED}; }
  to: { color: ${theme.colors.BLUE}; }
`);
const MyDiv = styled.div`
  animation: 1s ${myAnimation};
`;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing props/theme in keyframes · Issue #397 - GitHub
It was really hard to work out a way to access props in animation keyframes to make animations themeable. Eventually I came across...
Read more >
How to pass props to keyframes in styled-component with react?
I have following code and I want to pass value of y from react component to moveVertically keyframe ...
Read more >
Keyframes - Theme UI
To directly set animationName using object syntax, append .toString() to the animation variable. This workaround is not needed when using the variable inside...
Read more >
Styled-Components - Pass Props / Theme to keyframes?
Coding example for the question Styled-Components - Pass Props / Theme to keyframes? ... import styled from 'styled-components'; import { keyframes } from ......
Read more >
React Styled Components — Animation and Theming - Medium
We can theme by exporting using the ThemeProvider component. ... which takes the theme prop to get the styles from the baseTheme object....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found