Sitemap
codebro.io

codebro.io

♡ playing with web technologies. Especially anything graphics, game or education

React animations / Framer Motion — Part I

Making a laser sight button — hover/tap

2 min readNov 22, 2019

--

The end result:

Press enter or click to view image in full size
Our laser sight button (image)
Our laser sight button (code)

Fire and forget made easy

One great thing about Framer Motion is that it makes the useful stuff easy. Hover and tap animations are some of the most ubiquitous on the web and are a great way to show the user that they are interacting with your application’s UI.

Hover

The whileHover property allows us to add an animation when our user hovers our component.

<motion.div
whileHover={{ backgroundColor: "#424242" }}
/>

Tap

The whileTap property allows us to add an animation when a user taps or clicks on our component.

<motion.div
whileTap={{ backgroundColor: "#212121" }}
/>

The above can also be achieved fairly easily using CSS pseudo-selectors such as :hover and :active along with transition. So… Why is this better than plain old CSS?

Accuracy

They are much more customizable and run smoother than their CSS counterparts.

Get codebro.io’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, all of CSS’s transition animations are timed, wheras with Framer Motion you can have timed or spring (no set time because based on physics) animations.

Intention

Using “hover” and “tap” seem better suited to the modern world of web where about 50% is accessed via touch device.

CSS seems to have been primarily implemented for non-touch devices and have added touch (tap) interactions as an afterthought.

As an example :hover interactions are triggered by touch and mouse hover and :active interactions are triggered by touch and mouse click, which is a little confusing and not the easiest to fine-tune.

Readability

Another nice feature of Framer Motion is that it uses shortcuts for transforms eg/ x, scale, rotate.

Personally, I will often set a variable such asconst interaction={opacity: 0.9} and use this within my component to create some nice consistent behaviour for my UI between mouse and touch devices.

<motion.div
whileHover={interaction}
whileTap={interaction}
/>

NB both of these attributes can also be passed down the component tree using “variants” which we will discuss in another post

In the next post we will finish our button by implementing the mouse animations.

--

--

codebro.io
codebro.io

Written by codebro.io

♡ playing with web technologies. Especially anything graphics, game or education