Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): add spinner component #1583

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

olekbaran
Copy link

Spinner

This PR adds a Vercel style spinner component

image

@vercel
Copy link

vercel bot commented Sep 23, 2023

@olekbaran is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@shadcn shadcn added new component area: roadmap This looks great. We'll add it to the roadmap, review and merge. labels Oct 21, 2023
@shadcn shadcn added this to the next milestone Oct 21, 2023
@shadcn
Copy link
Collaborator

shadcn commented Nov 12, 2023

@olekbaran What do you think about using an svg icon from lucide-react and animate-spin?

import { Loader2 } from "lucide-react"

<Loader2 className="animate-spin" />

@shadcn shadcn added postpone: more info or changes requested maintainers asked a question or needs more info and removed area: roadmap This looks great. We'll add it to the roadmap, review and merge. labels Nov 12, 2023
@olekbaran
Copy link
Author

olekbaran commented Nov 12, 2023

@olekbaran What do you think about using an svg icon from lucide-react and animate-spin?

import { Loader2 } from "lucide-react"

<Loader2 className="animate-spin" />

@shadcn I think my loader is a better match for the components' style and more in line with your original idea. The whole library is designed with the Vercel style, so I made a loader that replicates it. But, the final decision is all yours.

Copy link

@lloydrichards lloydrichards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate you trying to keep the spinner in a similar style and not just adding an animate-spin to an icon (which is what i always do 😅). I think there are some things that could be improved though like removing the style prop or making the spinnerBars more composable or functional 🤷‍♂️

Though I think for more sophisticated animations like this, I would rather opt to use something like framer-motion rather then tailwind, but i'm sure a similar effect can be created regardless 👍

Comment on lines +14 to +27
const spinnerBars: SpinnerBar[] = [
{ animationDelay: -1.2, rotate: 0.0001 },
{ animationDelay: -1.1, rotate: 30 },
{ animationDelay: -1, rotate: 60 },
{ animationDelay: -0.9, rotate: 90 },
{ animationDelay: -0.8, rotate: 120 },
{ animationDelay: -0.7, rotate: 150 },
{ animationDelay: -0.6, rotate: 180 },
{ animationDelay: -0.5, rotate: 210 },
{ animationDelay: -0.4, rotate: 240 },
{ animationDelay: -0.3, rotate: 270 },
{ animationDelay: -0.2, rotate: 300 },
{ animationDelay: -0.1, rotate: 330 },
]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const spinnerBars: SpinnerBar[] = [
{ animationDelay: -1.2, rotate: 0.0001 },
{ animationDelay: -1.1, rotate: 30 },
{ animationDelay: -1, rotate: 60 },
{ animationDelay: -0.9, rotate: 90 },
{ animationDelay: -0.8, rotate: 120 },
{ animationDelay: -0.7, rotate: 150 },
{ animationDelay: -0.6, rotate: 180 },
{ animationDelay: -0.5, rotate: 210 },
{ animationDelay: -0.4, rotate: 240 },
{ animationDelay: -0.3, rotate: 270 },
{ animationDelay: -0.2, rotate: 300 },
{ animationDelay: -0.1, rotate: 330 },
]
const spinnerBars: SpinnerBar[] = Array.from({ length: 12 }, (_, index) => ({
animationDelay: -1.2 + 0.1 * index,
rotate: 30 * index
}));

Little cleaner implementations of the spinnerBars? Could also move it outside the context of the Component to save some performance 👍

Comment on lines +44 to +52
spinner: {
from: { opacity: '1' },
to: { opacity: '0.15' },
},
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
spinner: 'spinner 1.2s linear infinite',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this animation could be renames to something like fade-out since its only using opacity and doesnt describe any spinning animation. Will also make it more composable with other components 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking for a sec, something like this could work: spinner: 'rotate 1.2s infinite steps(12)', and then you can just stagger the opacity and rotate the icon 🤷‍♂️

Comment on lines +36 to +39
style={{
animationDelay: `${bar.animationDelay}s`,
transform: `rotate(${bar.rotate}deg) translate(146%)`,
}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm less of a fan of mixing style and className though I understand tailwinds limitations with dynamic values. Technically you could just return the classNames in your spinnerBars explicitly (ignoring my suggestion) and then tailwind would pick it up on compile 🤔😆

  const spinnerBars: SpinnerBar[] = [
    { animationDelay: "delay-[1.2s]", rotate: "rotate-[0deg]" },
    { animationDelay: "delay-[1.1s]", rotate: "rotate-[30deg]" },
    ...

Copy link
Author

@olekbaran olekbaran Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't do that, because delay class in tailwind stands for transition-delay CSS property, however I need animation-delay. To style this component only with className prop, I would need to add animation-delay as a custom tailwind class, but it will be so complicated to implement dynamic values for this class.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use Arbitray Properties: like [animation-delay: 1.2sec] 🤷‍♂️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but still think its better to rotate the icon using steps(12) rather then creating 12 individual animating divs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I'll give it a try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new component postpone: more info or changes requested maintainers asked a question or needs more info
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants