Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyframes with Inline Styles ReactJS

Tags:

html

css

reactjs

I'm trying to set the keyframes of a pulsate animation in ReactJS. I tried just setting the keyframes inside the inline style but that doesn't work.

My code

const NewRelpyheButton = ({style = {}, open, handleOPenSettings}) => {

    var bar = {
    color: '#000',
    padding: '1em 0',
    fontSize: '20px',
    textAlign: 'center',
    cursor: 'pointer',
    position: 'fixed',
    bottom: '0',
    width: '100%',
    zIndex: '10',
    animation: 'pulse 1.2s ease-in-out',
    animationIterationCount: 'infinite',
    }

    Object.assign(style, {});
    let openModal;
    if (open) {
        openModal = <Modal><NewRelpyhe/></Modal>
    }

    return (
        <div>
        {openModal}
        <Bar color='purple' style={bar} onClick={handleOpenSettings}>
            create a new relphye site
        </Bar></div>
    )
}

I'm trying to mimic this in css:

.element {
  width: 100%;
  height: 100%;
  animation: pulse 5s infinite;
}

@keyframes pulse {
  0% {
    background-color: #001F3F;
  }
  100% {
    background-color: #FF4136;
  }
}

html,
body {
  height: 100%;
}
like image 432
user3421197 Avatar asked May 25 '16 22:05

user3421197


People also ask

Are inline styles GOOD FOR React?

The official React documentation frowns upon the use of inline styling as a primary means of styling projects and recommends the use of the className attribute instead.

What is inline style React?

React lets you use "inline styles" to style your components; inline styles in React are just JavaScript objects that you can render in an element's style attribute. The properties of these style objects are just like the CSS property, but they are camel case ( borderRadius ) instead of kebab-case ( border-radius ).

How do you use animate CSS in React JS?

You can add it to your index. html (there is one in React apps in the public folder); import into another css file that's included somewhere in your app using a css @import ; or if your app is based on create-react-app, you can import it directly into a component file.


1 Answers

If you like to keep all your styling tightly coupled to your components, give Styled Components a go. They have a helper for keyframes

e.g.

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

const pulse = keyframes`
  from {
    background-color: #001F3F;
  }

  to {
    background-color: #FF4136;
  }
`

const Bar = styled.div`
  color: #000;
  padding: 1em 0;
  font-size: 20px,
  text-align: center;
  cursor: pointer;
  position: fixed;
  bottom: '0',
  width: 100%;
  z-index: 10;
  animation: ${pulse} 1.2s ease-in-out;
  animation-iteration-count: infinite;
`

Then use like so:

<Bar>I pulse</Bar>
like image 91
Will Stone Avatar answered Sep 19 '22 12:09

Will Stone