Skip to content

Instantly share code, notes, and snippets.

@ktmud
Created September 13, 2021 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktmud/26f18af0dcc170cd011781c8c57808c4 to your computer and use it in GitHub Desktop.
Save ktmud/26f18af0dcc170cd011781c8c57808c4 to your computer and use it in GitHub Desktop.
CSS Loading Indicator Three Flashing Dots in styled-jsx
export type FlashingDotsProps = {
/**
* Size of each dot
*/
dotSize?: number;
/**
* Animation duration.
*/
duration?: number;
/**
* Color of the dots.
*/
color?: string;
width?: number;
height?: number;
};
/**
* Flashing dots loading indicator.
*/
export default function FlashingDots({
width = 30,
height = 24,
dotSize = 5,
duration = 0.5,
color = '#9880ff',
}: FlashingDotsProps) {
return (
<>
<div>
<i />
</div>
<style jsx>
{`
i {
position: relative;
animation: infinite linear alternate;
}
i::before,
i::after {
content: '';
display: inline-block;
position: absolute;
top: 0;
animation: infinite alternate;
}
`}
</style>
<style jsx>
{`
@keyframes dotFlashing {
0% {
opacity: 1;
}
50%,
100% {
opacity: 0.4;
}
}
div {
width: ${width}px;
height: ${height}px;
display: flex;
align-items: center;
justify-content: center;
}
i,
i::before,
i::after {
animation-name: dotFlashing;
color: ${color};
background-color: ${color};
width: ${dotSize}px;
height: ${dotSize}px;
border-radius: ${dotSize / 2}px;
animation-duration: ${duration}s;
animation-delay: ${duration / 2}s;
}
i::before {
animation-delay: 0s;
left: -${dotSize * 1.4}px;
}
i::after {
animation-delay: ${duration}s;
left: ${dotSize * 1.4}px;
}
`}
</style>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment