Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
How to add Chakra UI Animations Chakra UI

Chakra UI Animations

  • September 17, 2022September 17, 2022
  • by Coder Kai

Chakra UI is becoming very popular as an HTML styling framework that allows you to style components in a more React way rather than css. One of the problems that people face is how to add animations to the React components using Chakra UI Animations.

Chakra UI has a parameter called animation for the <Box> and other logical containers such as <Flex>, <Container>

If you look at the animation parameter it is the same as the CSS property animation

animation?: Token<CSS.Property.Animation>;

But animations that you have done in CSS has a keyframe attribute declared in their scope. For example

.slidein {
  animation-duration: 3s;
  animation-name: slidein;
  animation-iteration-count: 3;
  animation-direction: alternate;
}

@keyframes slidein {
  from {
    margin-left: 100%;
    width: 300%;
  }

  to {
    margin-left: 0%;
    width: 100%;
  }
}

Adding Chakra UI animations into a component

Like the above example, Chakra UI also has a method to define keyframe as a separate variable and inject into the scope of the component. For that, we need to import keyframe from Chakra framework.

import { keyframes, usePrefersReducedMotion } from '@chakra-ui/react'

We also have imported one extra one. That is usePrefersReducedMotion hook. It refers the x css property which provides animation preference of the user

The prefers-reduced-motion CSS media feature is used to detect if the user has requested that the system minimize the amount of non-essential motion it uses.

https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion

So if the user has disabled animation to save resources, this variable will stop the animation.

Following is an example of using an animation

import { Image, keyframes, usePrefersReducedMotion } from '@chakra-ui/react'
import logo from './logo.svg'

const spin = keyframes`
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
`

const Example() => {
  const prefersReducedMotion = usePrefersReducedMotion()

  const animation = prefersReducedMotion
    ? undefined
    : `${spin} infinite 20s linear`

  return <Image animation={animation} src={logo} {...props} />
}

spin variable that is globally declared and used as a formatted string.

Can you send animation-* css variables?

Currently, Chakra UI doesn’t support other variables individually. But you can pass all to the animation parameter as following

/* duration | easing-function | delay | iteration-count | direction |
   fill-mode | play-state | name */
animation={`3s ease-in 1s 2 reverse both paused ${slidein}`}

/* name | duration | easing-function | delay */
animation={`${slidein} 3s linear 1s`}

/* name | duration */
animation={`${slidein} 3s`}

Reference:

https://developer.mozilla.org/en-US/docs/Web/CSS/animation
https://chakra-ui.com/docs/hooks/use-prefers-reduced-motion

How to copy files from S3 to PostgreSQL RDS
04. Javascript Guide Booleans and Equality
Coder Kai
A humble developer
Animations Chakra UI Chakra UI Animations React.js Animations

Related articles

Multiple refs for an array of React Elements
Using multiple refs for an…
Immutable and Mutable Values in Javascript
07. Immutable and Mutable Values…
wrapper objects in javascript
06. Wrapper objects in Javascript
globals undefined and null values in javascript
05 Global, null and undefined…
Javascript Booleans and Equality
04. Javascript Guide Booleans and…
SSL Websocket using Nginx Proxy
SSL Websocket proxy with Nginx…
Change python version correctly
Python is not setting correct…
optimize React.js load time
How to optimize React.js app…
Multiple refs for an array of React Elements
How to use IntersectionObserver to…
Multiple refs for an array of React Elements
How to dismiss dropdowns when…
Javascript guide Strings
03. Javascript Guide – Strings
How to fetch data with useEffect
How to fetch data with…
add styles to stripe elements
How to add styles to…
Typescript
How to use Typescript with…
how to optimize react-native map view
How to optimize react-native map…
debounce with react hooks
Avoid multiple clicks using debounce…
Numbers inJavascript
02. Javascript Guide – Numbers
Introduction to Javascript
01. Javascript Guide – Introduction…
Nginx Load Balancer with docker…
Create React App Using npx

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • binance account on Minimum supported Gradle version is 7.0. Current version is 6.5
  • binance us registracija on How to add styles to stripe elements without using CardElement
  • binance Registrera dig on Higher order component wrappers using React.js and reusable components.

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress