1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-09-14 03:35:32 -04:00

loading spinner component's styles were excessively redundant

This commit is contained in:
2022-01-12 11:57:07 -05:00
parent 12d0959629
commit 3dfe85d5f6

View File

@@ -1,28 +1,23 @@
import { memo } from "react"; import { memo } from "react";
import css from "styled-jsx/css";
type Props = { type Props = {
boxes?: number; width: number; // of entire container, in pixels
timing?: number; boxes?: number; // total number of boxes (default: 3)
width: number; timing?: number; // staggered timing between each box's pulse, in seconds (default: 0.1s)
}; };
const Loading = ({ boxes = 3, timing = 0.1, width }: Props) => { const Loading = ({ width, boxes = 3, timing = 0.1 }: Props) => {
// each box is just an empty div // each box is just an empty div
const divs = []; const divs = [];
// allow a custom number of pulsing boxes (defaults to 3) // box styles (extracted here so they're not defined for each individual box)
for (let i = 0; i < boxes; i++) { const { className: boxClassName, styles: boxStyles } = css.resolve`
// width of each box correlates with number of boxes (with a little padding)
// each individual box's animation has a staggered start in corresponding order
divs.push(
<div key={i}>
<style jsx>{`
div { div {
display: inline-block; display: inline-block;
width: ${width / (boxes + 1)}px; width: ${width / (boxes + 1)}px;
height: 100%; height: 100%;
animation: loading 1.5s infinite ease-in-out both; animation: loading 1.5s infinite ease-in-out both;
animation-delay: ${i * timing}s;
background-color: var(--medium-light); background-color: var(--medium-light);
} }
@@ -36,14 +31,24 @@ const Loading = ({ boxes = 3, timing = 0.1, width }: Props) => {
transform: scale(0.6); transform: scale(0.6);
} }
} }
`}</style> `;
</div>
// allow a custom number of pulsing boxes (defaults to 3)
for (let i = 0; i < boxes; i++) {
// width of each box correlates with number of boxes (with a little padding)
// each individual box's animation has a staggered start in corresponding order
divs.push(
<>
<div key={i} className={boxClassName} style={{ animationDelay: `${i * timing}s` }} />
{boxStyles}
</>
); );
} }
return ( return (
<div> <div>
{divs} {divs}
<style jsx>{` <style jsx>{`
div { div {
width: ${width}px; width: ${width}px;