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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/www/__registry__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ export const Index: Record<string, any> = {
component: React.lazy(() => import("@/registry/default/ui/slider")),
files: ["registry/default/ui/slider.tsx"],
},
"spinner": {
name: "spinner",
type: "components:ui",
registryDependencies: undefined,
component: React.lazy(() => import("@/registry/default/ui/spinner")),
files: ["registry/default/ui/spinner.tsx"],
},
"switch": {
name: "switch",
type: "components:ui",
Expand Down Expand Up @@ -775,6 +782,13 @@ export const Index: Record<string, any> = {
component: React.lazy(() => import("@/registry/default/example/slider-demo")),
files: ["registry/default/example/slider-demo.tsx"],
},
"spinner-demo": {
name: "spinner-demo",
type: "components:example",
registryDependencies: ["spinner"],
component: React.lazy(() => import("@/registry/default/example/spinner-demo")),
files: ["registry/default/example/spinner-demo.tsx"],
},
"switch-demo": {
name: "switch-demo",
type: "components:example",
Expand Down Expand Up @@ -1294,6 +1308,13 @@ export const Index: Record<string, any> = {
component: React.lazy(() => import("@/registry/new-york/ui/slider")),
files: ["registry/new-york/ui/slider.tsx"],
},
"spinner": {
name: "spinner",
type: "components:ui",
registryDependencies: undefined,
component: React.lazy(() => import("@/registry/new-york/ui/spinner")),
files: ["registry/new-york/ui/spinner.tsx"],
},
"switch": {
name: "switch",
type: "components:ui",
Expand Down Expand Up @@ -1854,6 +1875,13 @@ export const Index: Record<string, any> = {
component: React.lazy(() => import("@/registry/new-york/example/slider-demo")),
files: ["registry/new-york/example/slider-demo.tsx"],
},
"spinner-demo": {
name: "spinner-demo",
type: "components:example",
registryDependencies: ["spinner"],
component: React.lazy(() => import("@/registry/new-york/example/spinner-demo")),
files: ["registry/new-york/example/spinner-demo.tsx"],
},
"switch-demo": {
name: "switch-demo",
type: "components:example",
Expand Down
5 changes: 5 additions & 0 deletions apps/www/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ export const docsConfig: DocsConfig = {
href: "/docs/components/slider",
items: [],
},
{
title: "Spinner",
href: "/docs/components/spinner",
items: [],
},
{
title: "Switch",
href: "/docs/components/switch",
Expand Down
102 changes: 102 additions & 0 deletions apps/www/content/docs/components/spinner.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
title: Spinner
description: Indicates an action running in the background.
component: true
---

<ComponentPreview name="spinner-demo" />

## Installation

<Tabs defaultValue="cli">

<TabsList>
<TabsTrigger value="cli">CLI</TabsTrigger>
<TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

<Steps>

<Step>Run the following command:</Step>

```bash
npx shadcn-ui@latest add spinner
```

<Step>Update `tailwind.config.js`</Step>

Add the following animations to your `tailwind.config.js` file:

```js title="tailwind.config.js" {5-13}
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
keyframes: {
spinner: {
from: { opacity: "1" },
to: { opacity: "0.15" },
},
},
animation: {
spinner: "spinner 1.2s linear infinite",
},
},
},
}
```

</Steps>

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="spinner" />

<Step>Update the import paths to match your project setup.</Step>

<Step>Update `tailwind.config.js`</Step>

Add the following animations to your `tailwind.config.js` file:

```js title="tailwind.config.js" {5-13}
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
keyframes: {
spinner: {
from: { opacity: "1" },
to: { opacity: "0.15" },
},
},
animation: {
spinner: "spinner 1.2s linear infinite",
},
},
},
}
```

</Steps>

</TabsContent>

</Tabs>

## Usage

```tsx
import { Spinner } from "@/components/ui/spinner"
```

```tsx
<Spinner />
```
5 changes: 5 additions & 0 deletions apps/www/content/docs/installation/laravel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,15 @@ export default {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: 0 },
},
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",
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions apps/www/content/docs/installation/manual.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ module.exports = {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: "0" },
},
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",
},
},
},
Expand Down
11 changes: 11 additions & 0 deletions apps/www/pages/api/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@
],
"type": "ui"
},
{
"name": "spinner",
"files": [
{
"name": "spinner.tsx",
"dir": "components/ui",
"content": "import * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface SpinnerBar {\n animationDelay: number\n rotate: number\n}\n\nconst Spinner = React.forwardRef<\n HTMLDivElement,\n React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => {\n const spinnerBars: SpinnerBar[] = [\n { animationDelay: -1.2, rotate: 0.0001 },\n { animationDelay: -1.1, rotate: 30 },\n { animationDelay: -1, rotate: 60 },\n { animationDelay: -0.9, rotate: 90 },\n { animationDelay: -0.8, rotate: 120 },\n { animationDelay: -0.7, rotate: 150 },\n { animationDelay: -0.6, rotate: 180 },\n { animationDelay: -0.5, rotate: 210 },\n { animationDelay: -0.4, rotate: 240 },\n { animationDelay: -0.3, rotate: 270 },\n { animationDelay: -0.2, rotate: 300 },\n { animationDelay: -0.1, rotate: 330 },\n ]\n\n return (\n <div ref={ref} className={cn(\"h-5 w-5\", className)} {...props}>\n <div className=\"relative left-1/2 top-1/2 h-full\">\n {spinnerBars.map((bar) => (\n <div\n key={bar.rotate}\n className=\"absolute left-[-10%] top-[-3.9%] h-[8%] w-[24%] animate-spinner rounded-md bg-muted-foreground\"\n style={{\n animationDelay: `${bar.animationDelay}s`,\n transform: `rotate(${bar.rotate}deg) translate(146%)`,\n }}\n />\n ))}\n </div>\n </div>\n )\n})\nSpinner.displayName = \"Spinner\"\n\nexport { Spinner }\n"
}
],
"type": "ui"
},
{
"name": "switch",
"dependencies": ["@radix-ui/react-switch"],
Expand Down
7 changes: 7 additions & 0 deletions apps/www/public/registry/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@
],
"type": "components:ui"
},
{
"name": "spinner",
"files": [
"ui/spinner.tsx"
],
"type": "components:ui"
},
{
"name": "switch",
"dependencies": [
Expand Down
10 changes: 10 additions & 0 deletions apps/www/public/registry/styles/default/spinner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "spinner",
"files": [
{
"name": "spinner.tsx",
"content": "import * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface SpinnerBar {\n animationDelay: number\n rotate: number\n}\n\nconst Spinner = React.forwardRef<\n HTMLDivElement,\n React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => {\n const spinnerBars: SpinnerBar[] = [\n { animationDelay: -1.2, rotate: 0.0001 },\n { animationDelay: -1.1, rotate: 30 },\n { animationDelay: -1, rotate: 60 },\n { animationDelay: -0.9, rotate: 90 },\n { animationDelay: -0.8, rotate: 120 },\n { animationDelay: -0.7, rotate: 150 },\n { animationDelay: -0.6, rotate: 180 },\n { animationDelay: -0.5, rotate: 210 },\n { animationDelay: -0.4, rotate: 240 },\n { animationDelay: -0.3, rotate: 270 },\n { animationDelay: -0.2, rotate: 300 },\n { animationDelay: -0.1, rotate: 330 },\n ]\n\n return (\n <div ref={ref} className={cn(\"h-5 w-5\", className)} {...props}>\n <div className=\"relative left-1/2 top-1/2 h-full\">\n {spinnerBars.map((bar) => (\n <div\n key={bar.rotate}\n className=\"absolute left-[-10%] top-[-3.9%] h-[8%] w-[24%] animate-spinner rounded-md bg-muted-foreground\"\n style={{\n animationDelay: `${bar.animationDelay}s`,\n transform: `rotate(${bar.rotate}deg) translate(146%)`,\n }}\n />\n ))}\n </div>\n </div>\n )\n})\nSpinner.displayName = \"Spinner\"\n\nexport { Spinner }\n"
}
],
"type": "components:ui"
}
10 changes: 10 additions & 0 deletions apps/www/public/registry/styles/new-york/spinner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "spinner",
"files": [
{
"name": "spinner.tsx",
"content": "import * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface SpinnerBar {\n animationDelay: number\n rotate: number\n}\n\nconst Spinner = React.forwardRef<\n HTMLDivElement,\n React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => {\n const spinnerBars: SpinnerBar[] = [\n { animationDelay: -1.2, rotate: 0.0001 },\n { animationDelay: -1.1, rotate: 30 },\n { animationDelay: -1, rotate: 60 },\n { animationDelay: -0.9, rotate: 90 },\n { animationDelay: -0.8, rotate: 120 },\n { animationDelay: -0.7, rotate: 150 },\n { animationDelay: -0.6, rotate: 180 },\n { animationDelay: -0.5, rotate: 210 },\n { animationDelay: -0.4, rotate: 240 },\n { animationDelay: -0.3, rotate: 270 },\n { animationDelay: -0.2, rotate: 300 },\n { animationDelay: -0.1, rotate: 330 },\n ]\n\n return (\n <div ref={ref} className={cn(\"h-5 w-5\", className)} {...props}>\n <div className=\"relative left-1/2 top-1/2 h-full\">\n {spinnerBars.map((bar) => (\n <div\n key={bar.rotate}\n className=\"absolute left-[-10%] top-[-3.9%] h-[8%] w-[24%] animate-spinner rounded-md bg-muted-foreground\"\n style={{\n animationDelay: `${bar.animationDelay}s`,\n transform: `rotate(${bar.rotate}deg) translate(146%)`,\n }}\n />\n ))}\n </div>\n </div>\n )\n})\nSpinner.displayName = \"Spinner\"\n\nexport { Spinner }\n"
}
],
"type": "components:ui"
}
5 changes: 5 additions & 0 deletions apps/www/registry/default/example/spinner-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Spinner } from "@/registry/default/ui/spinner"

export default function SpinnerDemo() {
return <Spinner />
}
48 changes: 48 additions & 0 deletions apps/www/registry/default/ui/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as React from "react"

import { cn } from "@/lib/utils"

interface SpinnerBar {
animationDelay: number
rotate: number
}

const Spinner = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
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 },
]
Comment on lines +14 to +27

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 👍


return (
<div ref={ref} className={cn("h-5 w-5", className)} {...props}>
<div className="relative left-1/2 top-1/2 h-full">
{spinnerBars.map((bar) => (
<div
key={bar.rotate}
className="absolute left-[-10%] top-[-3.9%] h-[8%] w-[24%] animate-spinner rounded-md bg-muted-foreground"
style={{
animationDelay: `${bar.animationDelay}s`,
transform: `rotate(${bar.rotate}deg) translate(146%)`,
}}
Comment on lines +36 to +39

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

/>
))}
</div>
</div>
)
})
Spinner.displayName = "Spinner"

export { Spinner }
5 changes: 5 additions & 0 deletions apps/www/registry/new-york/example/spinner-demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Spinner } from "@/registry/default/ui/spinner"

export default function SpinnerDemo() {
return <Spinner />
}
48 changes: 48 additions & 0 deletions apps/www/registry/new-york/ui/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as React from "react"

import { cn } from "@/lib/utils"

interface SpinnerBar {
animationDelay: number
rotate: number
}

const Spinner = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
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 },
]

return (
<div ref={ref} className={cn("h-5 w-5", className)} {...props}>
<div className="relative left-1/2 top-1/2 h-full">
{spinnerBars.map((bar) => (
<div
key={bar.rotate}
className="absolute left-[-10%] top-[-3.9%] h-[8%] w-[24%] animate-spinner rounded-md bg-muted-foreground"
style={{
animationDelay: `${bar.animationDelay}s`,
transform: `rotate(${bar.rotate}deg) translate(146%)`,
}}
/>
))}
</div>
</div>
)
})
Spinner.displayName = "Spinner"

export { Spinner }
11 changes: 11 additions & 0 deletions apps/www/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ const ui: Registry = [
dependencies: ["@radix-ui/react-slider"],
files: ["ui/slider.tsx"],
},
{
name: "spinner",
type: "components:ui",
files: ["ui/spinner.tsx"],
},
{
name: "switch",
type: "components:ui",
Expand Down Expand Up @@ -672,6 +677,12 @@ const example: Registry = [
registryDependencies: ["slider"],
files: ["example/slider-demo.tsx"],
},
{
name: "spinner-demo",
type: "components:example",
registryDependencies: ["spinner"],
files: ["example/spinner-demo.tsx"],
},
{
name: "switch-demo",
type: "components:example",
Expand Down
Loading