Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

refactor: refactor magic card component

Dillion Vermad7dad653b077
diff --git a/content/docs/components/magic-card.mdx b/content/docs/components/magic-card.mdx
index 4a74d0f1..f92d6a13 100644
--- a/content/docs/components/magic-card.mdx
+++ b/content/docs/components/magic-card.mdx
@@ -1,10 +1,9 @@
---
title: Magic Card
-date: 2023-07-02
+date: 2024-07-07
description: A spotlight effect that follows your mouse cursor and highlights borders on hover.
author: dillionverma
published: true
-video: https://cdn.magicui.design/magic-card.mp4
---
<ComponentPreview name="magic-card-demo" />
@@ -41,35 +40,18 @@ npx magicui-cli add magic-card
</Tabs>
-## Examples
-
-### Radial Gradient border
-
-<ComponentPreview name="magic-card-gradient" />
-
## Props
-### MagicContainer
-
-| Prop name | Type | Default | Description |
-| --------- | --------- | ------- | --------------------------------------- |
-| children | ReactNode | - | The children to render |
-| className | string | - | The className to apply to the container |
-
### MagicCard
-| Prop name | Type | Default | Description |
-| -------------- | --------- | ---------------------- | ---------------------------------- |
-| children | ReactNode | - | The children to render |
-| className | string | - | The className to apply to the card |
-| maskSize | number | 200 | The size of the spotlight mask |
-| borderColor | string | rgba(120,119,198) | The color of the border |
-| borderWidth | number | 1 | The width of the border |
-| borderRadius | number | 16 | The radius of the border |
-| spotlight | boolean | true | Whether to show the spotlight |
-| spotlightColor | string | rgba(120,119,198,0.08) | The color of the spotlight |
-| isolated | boolean | true | Whether to isolate the card |
-| background | string | rgba(120,119,198, 0.2) | The background of the card |
+| Prop name | Type | Default | Description |
+| -------------------- | --------------- | --------- | ------------------------------------------- |
+| children | React.ReactNode | - | The content to be rendered inside the card |
+| className | string | - | Additional CSS classes to apply to the card |
+| gradientSize | number | 200 | Size of the gradient effect |
+| gradientColor | string | "#262626" | Color of the gradient effect |
+| gradientOpacity | number | - | Opacity of the gradient effect |
+| gradientTransparency | number | 80 | Transparency level of the gradient effect |
## Credits
diff --git a/content/pages/home.mdx b/content/pages/home.mdx
index c564de5e..6b8ec8f4 100644
--- a/content/pages/home.mdx
+++ b/content/pages/home.mdx
@@ -1,9 +1,6 @@
---
title: Components
-date: 2024-01-01
description: A collection of components that are used to build the landing page of a website.
-author: dillionverma
-published: true
---
<ComponentPreview name="bento-demo" preview />
diff --git a/registry/components/example/magic-card-demo.tsx b/registry/components/example/magic-card-demo.tsx
index dd9697d4..af23b955 100644
--- a/registry/components/example/magic-card-demo.tsx
+++ b/registry/components/example/magic-card-demo.tsx
@@ -1,30 +1,27 @@
-import { MagicCard, MagicContainer } from "@/components/magicui/magic-card";
+import { useTheme } from "next-themes";
+
+import { MagicCard } from "@/components/magicui/magic-card";
export default function MagicCardDemo() {
+ const { theme } = useTheme();
return (
- <MagicContainer
+ <div
className={
"flex h-[500px] w-full flex-col gap-4 lg:h-[250px] lg:flex-row"
}
>
- <MagicCard className="flex w-full cursor-pointer flex-col items-center justify-center overflow-hidden p-20 shadow-2xl">
- <p className="z-10 whitespace-nowrap text-4xl font-medium text-gray-800 dark:text-gray-200">
- Magic
- </p>
- <div className="pointer-events-none absolute inset-0 h-full bg-[radial-gradient(circle_at_50%_120%,rgba(120,119,198,0.3),rgba(255,255,255,0))]" />
- </MagicCard>
- <MagicCard className="flex w-full cursor-pointer flex-col items-center justify-center overflow-hidden p-20 shadow-2xl">
- <p className="z-10 whitespace-nowrap text-4xl font-medium text-gray-800 dark:text-gray-200">
- Card
- </p>
- <div className="pointer-events-none absolute inset-0 h-full bg-[radial-gradient(circle_at_50%_120%,rgba(120,119,198,0.3),rgba(255,255,255,0))]" />
+ <MagicCard
+ className="cursor-pointer flex-col items-center justify-center shadow-2xl whitespace-nowrap text-4xl"
+ gradientColor={theme === "dark" ? "#262626" : "#D9D9D9"}
+ >
+ Magic
</MagicCard>
- <MagicCard className="flex w-full cursor-pointer flex-col items-center justify-center overflow-hidden p-20 shadow-2xl">
- <p className="z-10 whitespace-nowrap text-4xl font-medium text-gray-800 dark:text-gray-200">
- Demo
- </p>
- <div className="pointer-events-none absolute inset-0 h-full bg-[radial-gradient(circle_at_50%_120%,rgba(120,119,198,0.3),rgba(255,255,255,0))]" />
+ <MagicCard
+ className="cursor-pointer flex-col items-center justify-center shadow-2xl whitespace-nowrap text-4xl"
+ gradientColor={theme === "dark" ? "#262626" : "#D9D9D9"}
+ >
+ Card
</MagicCard>
- </MagicContainer>
+ </div>
);
}
diff --git a/registry/components/example/magic-card-gradient.tsx b/registry/components/example/magic-card-gradient.tsx
deleted file mode 100644
index deb7a34f..00000000
--- a/registry/components/example/magic-card-gradient.tsx
+++ /dev/null
@@ -1,33 +0,0 @@
-import { MagicCard, MagicContainer } from "@/components/magicui/magic-card";
-
-export default function MagicCardGradient() {
- return (
- <MagicContainer
- className={
- "flex h-[500px] w-full flex-col gap-4 lg:h-[250px] lg:flex-row"
- }
- >
- <MagicCard
- borderWidth={3}
- className="flex w-full cursor-pointer flex-col items-center justify-center overflow-hidden bg-[radial-gradient(var(--mask-size)_circle_at_var(--mouse-x)_var(--mouse-y),#ffaa40_0,#9c40ff_50%,transparent_100%)] p-20 shadow-2xl"
- >
- <p className="z-10 whitespace-nowrap text-4xl font-medium text-gray-800 dark:text-gray-200">
- Magic
- </p>
- <div className="pointer-events-none absolute inset-0 h-full bg-[radial-gradient(circle_at_50%_120%,rgba(120,119,198,0.3),rgba(255,255,255,0))]" />
- </MagicCard>
- <MagicCard className="flex w-full cursor-pointer flex-col items-center justify-center overflow-hidden bg-[radial-gradient(var(--mask-size)_circle_at_var(--mouse-x)_var(--mouse-y),#ffaa40_0,#9c40ff_50%,transparent_100%)] p-20 shadow-2xl">
- <p className="z-10 whitespace-nowrap text-4xl font-medium text-gray-800 dark:text-gray-200">
- Card
- </p>
- <div className="pointer-events-none absolute inset-0 h-full bg-[radial-gradient(circle_at_50%_120%,rgba(120,119,198,0.3),rgba(255,255,255,0))]" />
- </MagicCard>
- <MagicCard className="flex w-full cursor-pointer flex-col items-center justify-center overflow-hidden bg-[radial-gradient(var(--mask-size)_circle_at_var(--mouse-x)_var(--mouse-y),#ffaa40_0,#9c40ff_50%,transparent_100%)] p-20 shadow-2xl">
- <p className="z-10 whitespace-nowrap text-4xl font-medium text-gray-800 dark:text-gray-200">
- Demo
- </p>
- <div className="pointer-events-none absolute inset-0 h-full bg-[radial-gradient(circle_at_50%_120%,rgba(120,119,198,0.3),rgba(255,255,255,0))]" />
- </MagicCard>
- </MagicContainer>
- );
-}
diff --git a/registry/components/magicui/magic-card.tsx b/registry/components/magicui/magic-card.tsx
index 9b951c23..27973001 100644
--- a/registry/components/magicui/magic-card.tsx
+++ b/registry/components/magicui/magic-card.tsx
@@ -1,220 +1,51 @@
"use client";
-import {
- CSSProperties,
- ReactElement,
- ReactNode,
- useEffect,
- useRef,
- useState,
-} from "react";
+import React from "react";
+import { motion, useMotionTemplate, useMotionValue } from "framer-motion";
import { cn } from "@/lib/utils";
-interface MousePosition {
- x: number;
- y: number;
-}
-
-function useMousePosition(): MousePosition {
- const [mousePosition, setMousePosition] = useState<MousePosition>({
- x: 0,
- y: 0,
- });
-
- useEffect(() => {
- const handleMouseMove = (event: globalThis.MouseEvent) => {
- setMousePosition({ x: event.clientX, y: event.clientY });
- };
-
- window.addEventListener("mousemove", handleMouseMove);
-
- return () => {
- window.removeEventListener("mousemove", handleMouseMove);
- };
- }, []);
-
- return mousePosition;
-}
-
-interface MagicContainerProps {
- children?: ReactNode;
- className?: any;
-}
-
-const MagicContainer = ({ children, className }: MagicContainerProps) => {
- const containerRef = useRef<HTMLDivElement>(null);
- const mousePosition = useMousePosition();
- const mouse = useRef<{ x: number; y: number }>({ x: 0, y: 0 });
- const containerSize = useRef<{ w: number; h: number }>({ w: 0, h: 0 });
- const [boxes, setBoxes] = useState<Array<HTMLElement>>([]);
-
- useEffect(() => {
- init();
- containerRef.current &&
- setBoxes(
- Array.from(containerRef.current.children).map(
- (el) => el as HTMLElement,
- ),
- );
- }, []);
-
- useEffect(() => {
- init();
- window.addEventListener("resize", init);
-
- return () => {
- window.removeEventListener("resize", init);
- };
- }, [setBoxes]);
-
- useEffect(() => {
- onMouseMove();
- }, [mousePosition]);
-
- const init = () => {
- if (containerRef.current) {
- containerSize.current.w = containerRef.current.offsetWidth;
- containerSize.current.h = containerRef.current.offsetHeight;
- }
- };
-
- const onMouseMove = () => {
- if (containerRef.current) {
- const rect = containerRef.current.getBoundingClientRect();
- const { w, h } = containerSize.current;
- const x = mousePosition.x - rect.left;
- const y = mousePosition.y - rect.top;
- const inside = x < w && x > 0 && y < h && y > 0;
-
- mouse.current.x = x;
- mouse.current.y = y;
- boxes.forEach((box) => {
- const boxX =
- -(box.getBoundingClientRect().left - rect.left) + mouse.current.x;
- const boxY =
- -(box.getBoundingClientRect().top - rect.top) + mouse.current.y;
- box.style.setProperty("--mouse-x", `${boxX}px`);
- box.style.setProperty("--mouse-y", `${boxY}px`);
-
- if (inside) {
- box.style.setProperty("--opacity", `1`);
- } else {
- box.style.setProperty("--opacity", `0`);
- }
- });
- }
- };
-
- return (
- <div className={cn("h-full w-full", className)} ref={containerRef}>
- {children}
- </div>
- );
-};
-
-interface MagicCardProps {
- /**
- * @default <div />
- * @type ReactElement
- * @description
- * The component to be rendered as the card
- * */
- as?: ReactElement;
- /**
- * @default ""
- * @type string
- * @description
- * The className of the card
- */
+export interface MagicCardProps {
+ children: React.ReactNode;
className?: string;
-
- /**
- * @default ""
- * @type ReactNode
- * @description
- * The children of the card
- * */
- children?: ReactNode;
-
- /**
- * @default 600
- * @type number
- * @description
- * The size of the spotlight effect in pixels
- * */
- size?: number;
-
- /**
- * @default true
- * @type boolean
- * @description
- * Whether to show the spotlight
- * */
- spotlight?: boolean;
-
- /**
- * @default "rgba(255,255,255,0.03)"
- * @type string
- * @description
- * The color of the spotlight
- * */
- spotlightColor?: string;
-
- /**
- * @default true
- * @type boolean
- * @description
- * Whether to isolate the card which is being hovered
- * */
- isolated?: boolean;
-
- /**
- * @default "rgba(255,255,255,0.03)"
- * @type string
- * @description
- * The background of the card
- * */
- background?: string;
-
- [key: string]: any;
+ gradientSize?: number;
+ gradientColor?: string;
+ gradientOpacity?: number;
+ gradientTransparency?: number;
}
-const MagicCard: React.FC<MagicCardProps> = ({
- className,
+export function MagicCard({
children,
- size = 600,
- spotlight = true,
- borderColor = "hsl(0 0% 98%)",
- isolated = true,
- ...props
-}) => {
+ className = "",
+ gradientSize = 200,
+ gradientColor = "#262626",
+ gradientTransparency = 80,
+}: MagicCardProps) {
+ const mouseX = useMotionValue(0);
+ const mouseY = useMotionValue(0);
+
return (
<div
- style={
- {
- "--mask-size": `${size}px`,
- "--border-color": `${borderColor}`,
- } as CSSProperties
- }
+ onMouseMove={(e) => {
+ const { left, top } = e.currentTarget.getBoundingClientRect();
+
+ mouseX.set(e.clientX - left);
+ mouseY.set(e.clientY - top);
+ }}
className={cn(
- "relative z-0 h-full w-full rounded-2xl p-6",
- "bg-gray-300 dark:bg-gray-700",
- "bg-[radial-gradient(var(--mask-size)_circle_at_var(--mouse-x)_var(--mouse-y),var(--border-color),transparent_100%)]",
+ "group relative flex size-full overflow-hidden rounded-xl bg-neutral-100 dark:bg-neutral-900 border text-black dark:text-white",
className,
)}
- {...props}
>
- {children}
-
- {/* Background */}
- <div
- className={
- "absolute inset-[1px] -z-20 rounded-2xl bg-white dark:bg-black/95"
- }
+ <div className="relative z-10">{children}</div>
+ <motion.div
+ className="pointer-events-none absolute -inset-px rounded-xl opacity-0 transition duration-300 group-hover:opacity-100"
+ style={{
+ background: useMotionTemplate`
+ radial-gradient(${gradientSize}px circle at ${mouseX}px ${mouseY}px, ${gradientColor}, transparent ${gradientTransparency}%)
+ `,
+ }}
/>
</div>
);
-};
-
-export { MagicCard, MagicContainer };
+}
diff --git a/registry/index.tsx b/registry/index.tsx
index 7fa1ed90..1805daed 100644
--- a/registry/index.tsx
+++ b/registry/index.tsx
@@ -269,15 +269,6 @@ const example: Registry = {
() => import("@/registry/components/example/neon-gradient-card-demo"),
),
},
- "magic-card-gradient": {
- name: "magic-card-gradient",
- type: "components:example",
- registryDependencies: ["magic-card"],
- files: ["registry/components/example/magic-card-gradient.tsx"],
- component: React.lazy(
- () => import("@/registry/components/example/magic-card-gradient"),
- ),
- },
"meteors-demo": {
name: "meteors-demo",
type: "components:example",