Attract Men or Women

Digging Into the Preview Loading Animation in WordPress

WordPress shipped the Block Editor (aka Gutenberg) back in version 5.0 and with it came a snazzy new post preview screen that shows the WordPress logo drawing itself while the preview loads.

That's what you get when saving a post draft and clicking the "Preview" button in the editor. How'd they make it? I had a tough time viewing source for the page because the preview loads up pretty quickly, but I did see that SVG is being used for the WordPress logo. That's all I really needed because my mind instantly went back to something Chris wrote in 2014 that uses uses the stroke-dasharray and stroke-dashoffset properties to create the same effect.

This is the example Chris shared in that post:

See the Pen bGyoz by Chris Coyier (@chriscoyier) on CodePen.

I've since been able to get my hands on the CSS to confirm that the WordPress drawing is indeed using the same technique, but I'll share how my mind broke things out while I was trying to reverse-engineer it.

We're working with a straight-up inlined SVG

The neat thing about the WordPress version is that we're working with two SVG paths instead of one. That means we have two parts that appear to be drawing at the same time. Here's the SVG which is inlined in the HTML. We've got that "Generating preview" text as well, which can live outside the SVG.



Generating preview...

The first path is an ellipse that acts as a border around the second path, which is the shape of the WordPress logo. It's probably a good idea to give each path a class — especially if this isn't the only element on the page — but I decided to leave things class-less since this is the only element in the demo. We can select both paths together in CSS or use pseudo-selectors (e.g. path:nth-child(2)) to select them individually in this instance.

There are a few other housekeeping things happening in there. For example, the SVG gets attributes to make it more accessible, such as identifying it as an image, hiding it from screen readers, and preventing it from being focused.

We need to lightly style the SVG

Very, very light styling. We need a stroke since there is no fill color on the paths. Otherwise we'll be looking at a whole lot of nothing. Well, an invisible shape, but essentially a nothing burger.

svg {
  stroke: #555;
  stroke-width: 0.5;
  width: 250px;
}

That gives us the outline for both paths. The nice thing about the stroke-width property is that it accepts decimal values, so we get to make the lines a little thinner. The drawing sorta looks like it's drawn in pencil that way.

The width is pretty arbitrary here, but it's important because the stroke-dasharray and stroke-dashoffset properties we'll be working with rely on it. If those property values are smaller than the size of the SVG, then the drawing will stop short of completing. If it's too large, then the speed of the drawing becomes too fast.

Now that we know our width and can see the path strokes, we can set stroke-dasharray and stroke-dashoffset accordingly.

svg path {
  stroke-dasharray: 300;
  stroke-dashoffset: 300;
}

A little larger than the SVG and lots of space between the dashes, which should be just about right. Those values can be adjusted to tailor the animation to your liking.

The rest is merely using Chris' technique

The drawing is a CSS animation using one keyframe. If we start the stroke-dashoffset at zero, then the paths will be invisible on initial load and grow to the 300 value we set earlier when the animation reaches 100%. Again, we set the offset at 300 so that the stroke dashes and the spaces between them will extend beyond the SVG to cover the entire thing.

All the magic is a mere five lines of code:

@keyframes draw {
  0% {
    stroke-dashoffset: 0;
  }
}

Name the animation whatever you'd like. We can get away with only defining the animation at 0% since 100% is implicit.

Oh! And we've gotta attach the animation to the paths, so:

svg path {
  animation: draw 2s ease-out infinite alternate;
  stroke-dasharray: 300;
  stroke-dashoffset: 300;
}

You can definitely tweak those values as well to speed thing up or down. Easing gives the animation that slight pulsing effect where it starts and ends a little slower than the middle of the movement.

All together now!

See the Pen
WordPress Preview Loading State
by Geoff Graham (@geoffgraham)
on CodePen.

I mentioned it earlier, but I was eventually able to snatch the source code from the actual implementation and it's pretty darn close, using the same principles.

The post Digging Into the Preview Loading Animation in WordPress appeared first on CSS-Tricks.

WordPress shipped the Block Editor (aka Gutenberg) back in version 5.0 and with it came a snazzy new post preview screen that shows the WordPress logo drawing itself while the preview loads.

That's what you get when saving a post draft and clicking the "Preview" button in the editor. How'd they make it? I had a tough time viewing source for the page because the preview loads up pretty quickly, but I did see that SVG is being used … Read article

The post Digging Into the Preview Loading Animation in WordPress appeared first on CSS-Tricks.

[Collection]Article, css animation, loader, SVG animation, WordPress
Digging Into the Preview Loading Animation in WordPress Digging Into the Preview Loading Animation in WordPress Reviewed by Duos on October 25, 2019 Rating: 5

No comments:

Law Of Attraction : Quantum Manifest

Powered by Blogger.