DEV Community

Cover image for Create a simple spinner with reactjs + styled-components
0xkoji
0xkoji

Posted on

1

Create a simple spinner with reactjs + styled-components

In this post, I will introduce a simple spinner that uses reactjs + styled-components.

Spinner
https://vdxkbn.csb.app/

[props]

interface SpinnerProps {
  width?: string;
  height?: string;
  borderWidth?: string;
  borderColor?: string;
  duration?: number;
}
Enter fullscreen mode Exit fullscreen mode

Actually, the main part is CSS (styled-components).
Create an animation and passing it to a created style, SpinnerBody.

The structure is quite simple.
width, height, and boarderWidth can be number or string | number. I use string because I would like to pass the value with rem or px.

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

const SpinnerBody = styled.div<SpinnerProps>`
  height: ${(p) => (p.height ? p.height : "4rem")};
  width: ${(p) => (p.width ? p.width : "4rem")};
  border: ${(p) => (p.borderWidth ? p.borderWidth : "4px")} solid #d1d5db;
  border-top-color: ${(p) => (p.borderColor ? p.borderColor : "#3b82f6")};
  border-radius: 50%;
  animation: ${spinnerAnimation}
    ${(p) => (p.duration ? `${p.duration}ms` : "800ms")} linear infinite;
`;

export const Spinner = (props: SpinnerProps) => {
  return <SpinnerBody {...props} />;
};

Enter fullscreen mode Exit fullscreen mode

Codesandbox
https://codesandbox.io/s/spinner-with-reactjs-vdxkbn?file=/src/components/Spinner.tsx

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay