Skip to content
Merged
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
15 changes: 11 additions & 4 deletions apps/www/content/docs/components/icon-cloud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ import { IconCloud } from "@/components/ui/icon-cloud"

## Props

| Prop | Type | Default | Description |
| -------- | ------------------- | ------- | ------------------------------------------ |
| `icons` | `React.ReactNode[]` | `[]` | Array of icons to render in the cloud |
| `images` | `string[]` | `[]` | Array of image URLs to render in the cloud |
| Prop | Type | Default | Description |
| ------------- | ------------------- | ------- | ------------------------------------------ |
| `icons` | `React.ReactNode[]` | `[]` | Array of icons to render in the cloud |
| `images` | `string[]` | `[]` | Array of image URLs to render in the cloud |
| `showControl` | `boolean` | `true` | Show play/pause control button |

## Accessibility

The cloud rotates automatically, so it ships with a play/pause control by default to satisfy [WCAG 2.1 SC 2.2.2 (Pause, Stop, Hide)](https://www.w3.org/WAI/WCAG22/Understanding/pause-stop-hide.html).

Rotation also pauses automatically when the user has `prefers-reduced-motion: reduce` enabled. Setting `showControl={false}` removes the only way to resume it, so opt out only when your layout provides its own control.
86 changes: 70 additions & 16 deletions apps/www/public/llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10300,8 +10300,11 @@ Description: An interactive 3D tag cloud component
"use client"

import React, { useEffect, useRef, useState } from "react"
import { Pause, Play } from "lucide-react"
import { renderToString } from "react-dom/server"

import { Button } from "@/components/ui/button"

interface Icon {
x: number
y: number
Expand All @@ -10314,16 +10317,22 @@ interface Icon {
interface IconCloudProps {
icons?: React.ReactNode[]
images?: string[]
showControl?: boolean
}

function easeOutCubic(t: number): number {
return 1 - Math.pow(1 - t, 3)
}

export function IconCloud({ icons, images }: IconCloudProps) {
export function IconCloud({
icons,
images,
showControl = true,
}: IconCloudProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const [iconPositions, setIconPositions] = useState<Icon[]>([])
const [isDragging, setIsDragging] = useState(false)
const [isPaused, setIsPaused] = useState(false)
const [lastMousePos, setLastMousePos] = useState({ x: 0, y: 0 })
const [mousePos, setMousePos] = useState({ x: 0, y: 0 })
const [targetRotation, setTargetRotation] = useState<{
Expand All @@ -10340,6 +10349,21 @@ export function IconCloud({ icons, images }: IconCloudProps) {
const iconCanvasesRef = useRef<HTMLCanvasElement[]>([])
const imagesLoadedRef = useRef<boolean[]>([])

// Pause animation if user prefers reduced motion
useEffect(() => {
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)")
if (mediaQuery.matches) {
setIsPaused(true)
}

const handleChange = (e: MediaQueryListEvent) => {
setIsPaused(e.matches)
}

mediaQuery.addEventListener("change", handleChange)
return () => mediaQuery.removeEventListener("change", handleChange)
}, [])

// Create icon canvases once when icons/images change
useEffect(() => {
if (!icons && !images) return
Expand Down Expand Up @@ -10541,7 +10565,7 @@ export function IconCloud({ icons, images }: IconCloudProps) {
if (progress >= 1) {
setTargetRotation(null)
}
} else if (!isDragging) {
} else if (!isDragging && !isPaused) {
rotationRef.current = {
x: rotationRef.current.x + (dy / canvas.height) * speed,
y: rotationRef.current.y + (dx / canvas.width) * speed,
Expand Down Expand Up @@ -10592,7 +10616,16 @@ export function IconCloud({ icons, images }: IconCloudProps) {

ctx.restore()
})
animationFrameRef.current = requestAnimationFrame(animate)

const hasPendingAssets =
Boolean(icons || images) &&
!imagesLoadedRef.current.every((loaded) => loaded)
const shouldContinue =
!isPaused || isDragging || targetRotation !== null || hasPendingAssets

if (shouldContinue) {
animationFrameRef.current = requestAnimationFrame(animate)
}
}

animate()
Expand All @@ -10603,21 +10636,42 @@ export function IconCloud({ icons, images }: IconCloudProps) {
cancelAnimationFrame(animationFrameRef.current)
}
}
}, [icons, images, iconPositions, isDragging, mousePos, targetRotation])
}, [
icons,
images,
iconPositions,
isDragging,
isPaused,
mousePos,
targetRotation,
])

return (
<canvas
ref={canvasRef}
width={400}
height={400}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
className="rounded-lg"
aria-label="Interactive 3D Icon Cloud"
role="img"
/>
<div className="relative inline-block">
<canvas
ref={canvasRef}
width={400}
height={400}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
className="rounded-lg"
aria-label="Interactive 3D Icon Cloud"
role="img"
/>
{showControl && (
<Button
variant="outline"
size="icon"
onClick={() => setIsPaused(!isPaused)}
aria-label={isPaused ? "Play Animation" : "Pause Animation"}
className="absolute top-2 right-2"
>
{isPaused ? <Play size={16} /> : <Pause size={16} />}
</Button>
)}
</div>
)
}

Expand Down
9 changes: 7 additions & 2 deletions apps/www/public/r/icon-cloud.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion apps/www/public/r/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,12 @@
"type": "registry:ui",
"title": "Icon Cloud",
"description": "An interactive 3D tag cloud component",
"dependencies": [],
"dependencies": [
"lucide-react"
],
"registryDependencies": [
"button"
],
"files": [
{
"path": "registry/magicui/icon-cloud.tsx",
Expand Down
7 changes: 6 additions & 1 deletion apps/www/public/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,12 @@
"type": "registry:ui",
"title": "Icon Cloud",
"description": "An interactive 3D tag cloud component",
"dependencies": [],
"dependencies": [
"lucide-react"
],
"registryDependencies": [
"button"
],
"files": [
{
"path": "registry/magicui/icon-cloud.tsx",
Expand Down
7 changes: 6 additions & 1 deletion apps/www/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,12 @@
"type": "registry:ui",
"title": "Icon Cloud",
"description": "An interactive 3D tag cloud component",
"dependencies": [],
"dependencies": [
"lucide-react"
],
"registryDependencies": [
"button"
],
"files": [
{
"path": "registry/magicui/icon-cloud.tsx",
Expand Down
2 changes: 1 addition & 1 deletion apps/www/registry/__index__.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ export const Index: Record<string, any> = {
name: "icon-cloud",
description: "An interactive 3D tag cloud component",
type: "registry:ui",
registryDependencies: undefined,
registryDependencies: ["button"],
files: [{
path: "registry/magicui/icon-cloud.tsx",
type: "registry:ui",
Expand Down
86 changes: 70 additions & 16 deletions apps/www/registry/magicui/icon-cloud.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"use client"

import React, { useEffect, useRef, useState } from "react"
import { Pause, Play } from "lucide-react"
import { renderToString } from "react-dom/server"

import { Button } from "@/components/ui/button"

interface Icon {
x: number
y: number
Expand All @@ -15,16 +18,22 @@ interface Icon {
interface IconCloudProps {
icons?: React.ReactNode[]
images?: string[]
showControl?: boolean
}

function easeOutCubic(t: number): number {
return 1 - Math.pow(1 - t, 3)
}

export function IconCloud({ icons, images }: IconCloudProps) {
export function IconCloud({
icons,
images,
showControl = true,
}: IconCloudProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const [iconPositions, setIconPositions] = useState<Icon[]>([])
const [isDragging, setIsDragging] = useState(false)
const [isPaused, setIsPaused] = useState(false)
const [lastMousePos, setLastMousePos] = useState({ x: 0, y: 0 })
const [mousePos, setMousePos] = useState({ x: 0, y: 0 })
const [targetRotation, setTargetRotation] = useState<{
Expand All @@ -41,6 +50,21 @@ export function IconCloud({ icons, images }: IconCloudProps) {
const iconCanvasesRef = useRef<HTMLCanvasElement[]>([])
const imagesLoadedRef = useRef<boolean[]>([])

// Pause animation if user prefers reduced motion
useEffect(() => {
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)")
if (mediaQuery.matches) {
setIsPaused(true)
}

const handleChange = (e: MediaQueryListEvent) => {
setIsPaused(e.matches)
}

mediaQuery.addEventListener("change", handleChange)
return () => mediaQuery.removeEventListener("change", handleChange)
}, [])

// Create icon canvases once when icons/images change
useEffect(() => {
if (!icons && !images) return
Expand Down Expand Up @@ -242,7 +266,7 @@ export function IconCloud({ icons, images }: IconCloudProps) {
if (progress >= 1) {
setTargetRotation(null)
}
} else if (!isDragging) {
} else if (!isDragging && !isPaused) {
rotationRef.current = {
x: rotationRef.current.x + (dy / canvas.height) * speed,
y: rotationRef.current.y + (dx / canvas.width) * speed,
Expand Down Expand Up @@ -293,7 +317,16 @@ export function IconCloud({ icons, images }: IconCloudProps) {

ctx.restore()
})
animationFrameRef.current = requestAnimationFrame(animate)

const hasPendingAssets =
Boolean(icons || images) &&
!imagesLoadedRef.current.every((loaded) => loaded)
const shouldContinue =
!isPaused || isDragging || targetRotation !== null || hasPendingAssets

if (shouldContinue) {
animationFrameRef.current = requestAnimationFrame(animate)
}
}

animate()
Expand All @@ -304,20 +337,41 @@ export function IconCloud({ icons, images }: IconCloudProps) {
cancelAnimationFrame(animationFrameRef.current)
}
}
}, [icons, images, iconPositions, isDragging, mousePos, targetRotation])
}, [
icons,
images,
iconPositions,
isDragging,
isPaused,
mousePos,
targetRotation,
])

return (
<canvas
ref={canvasRef}
width={400}
height={400}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
className="rounded-lg"
aria-label="Interactive 3D Icon Cloud"
role="img"
/>
<div className="relative inline-block">
<canvas
ref={canvasRef}
width={400}
height={400}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
className="rounded-lg"
aria-label="Interactive 3D Icon Cloud"
role="img"
/>
{showControl && (
<Button
variant="outline"
size="icon"
onClick={() => setIsPaused(!isPaused)}
aria-label={isPaused ? "Play Animation" : "Pause Animation"}
className="absolute top-2 right-2"
>
{isPaused ? <Play size={16} /> : <Pause size={16} />}
</Button>
)}
</div>
)
}
3 changes: 2 additions & 1 deletion apps/www/registry/registry-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,8 @@ export const ui: Registry["items"] = [
type: "registry:ui",
title: "Icon Cloud",
description: "An interactive 3D tag cloud component",
dependencies: [],
dependencies: ["lucide-react"],
registryDependencies: ["button"],
files: [
{
path: "magicui/icon-cloud.tsx",
Expand Down
Loading