Skip to content

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

feat: Add Gauge Circle component and documentation (#101)

Luis Tenorio36de0f62a01f
diff --git a/config/docs.ts b/config/docs.ts
index 3096f19a..254693ae 100644
--- a/config/docs.ts
+++ b/config/docs.ts
@@ -95,6 +95,12 @@ export const docsConfig: DocsConfig = {
items: [],
label: "New",
},
+ {
+ title: "Gauge Circle",
+ href: `/docs/components/gauge-circle`,
+ items: [],
+ label: "New",
+ }
],
},
{
diff --git a/content/docs/components/gauge-circle.mdx b/content/docs/components/gauge-circle.mdx
new file mode 100644
index 00000000..49913006
--- /dev/null
+++ b/content/docs/components/gauge-circle.mdx
@@ -0,0 +1,34 @@
+---
+title: Gauge Circle
+date: 2024-05-28
+description: Gauge Circle is a component that displays a circular gauge with a percentage value.
+author: luis-codex
+published: true
+---
+
+<ComponentPreview name="gauge-circle-demo" />
+
+<Steps>
+
+### Installation
+
+Copy and paste the following code into your project.
+
+<ComponentSource name="gauge-circle" />
+
+</Steps>
+
+## Props
+
+| Prop | Type | Description | Default |
+| ------------------- | ------ | --------------------------------------------- | ------- |
+| className | string | The class name to be applied to the component | |
+| max | number | The maximum value of the gauge | 100 |
+| min | number | The minimum value of the gauge | 0 |
+| value | number | The current value of the gauge | 0 |
+| gaugePrimaryColor | string | The primary color of the gauge | |
+| gaugeSecondaryColor | string | The secondary color of the gauge | |
+
+## Credits
+
+- Credit to [@luis-code](https://luis-code.vercel.app/)
diff --git a/registry/components/example/gauge-circle-demo.tsx b/registry/components/example/gauge-circle-demo.tsx
new file mode 100644
index 00000000..b7283da0
--- /dev/null
+++ b/registry/components/example/gauge-circle-demo.tsx
@@ -0,0 +1,30 @@
+"use client";
+
+import GaugeCircle from "@/registry/components/magicui/gauge-circle";
+import { useEffect, useState } from "react";
+
+export default function GaugeCircleDemo() {
+ const [value, setValue] = useState(0);
+
+ useEffect(() => {
+ const handleIncrement = (prev: number) => {
+ if (prev === 100) {
+ return 0;
+ }
+ return prev + 10;
+ };
+ setValue(handleIncrement);
+ const interval = setInterval(() => setValue(handleIncrement), 2000);
+ return () => clearInterval(interval);
+ }, []);
+
+ return (
+ <GaugeCircle
+ max={100}
+ min={0}
+ value={value}
+ gaugePrimaryColor="rgb(79 70 229)"
+ gaugeSecondaryColor="rgba(0, 0, 0, 0.1)"
+ />
+ );
+}
diff --git a/registry/components/magicui/gauge-circle.tsx b/registry/components/magicui/gauge-circle.tsx
new file mode 100644
index 00000000..cf4d195a
--- /dev/null
+++ b/registry/components/magicui/gauge-circle.tsx
@@ -0,0 +1,108 @@
+import { cn } from "@/lib/utils";
+
+interface Props {
+ max: number;
+ value: number;
+ min: number;
+ gaugePrimaryColor: string;
+ gaugeSecondaryColor: string;
+ className?: string;
+}
+
+export default function GaugeCircle({
+ max = 100,
+ min = 0,
+ value = 0,
+ gaugePrimaryColor,
+ gaugeSecondaryColor,
+ className,
+}: Props) {
+ const circumference = 2 * Math.PI * 45;
+ const percentPx = circumference / 100;
+ const currentPercent = ((value - min) / (max - min)) * 100;
+
+ return (
+ <div
+ className={cn("relative h-40 w-40 text-2xl font-semibold", className)}
+ style={
+ {
+ "--circle-size": "100px",
+ "--circumference": circumference,
+ "--percent-to-px": `${percentPx}px`,
+ "--gap-percent": "5",
+ "--offset-factor": "0",
+ "--transition-length": "1s",
+ "--transition-step": "200ms",
+ "--delay": "0s",
+ "--percent-to-deg": "3.6deg",
+ transform: "translateZ(0)",
+ } as React.CSSProperties
+ }
+ >
+ <svg
+ fill="none"
+ className="h-full w-full"
+ strokeWidth="2"
+ viewBox="0 0 100 100"
+ >
+ {currentPercent <= 90 && currentPercent >= 0 && (
+ <circle
+ cx="50"
+ cy="50"
+ r="45"
+ strokeWidth="10"
+ strokeDashoffset="0"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ className=" opacity-100"
+ style={
+ {
+ stroke: gaugeSecondaryColor,
+ "--stroke-percent": 90 - currentPercent,
+ "--offset-factor-secondary": "calc(1 - var(--offset-factor))",
+ strokeDasharray:
+ "calc(var(--stroke-percent) * var(--percent-to-px)) var(--circumference)",
+ transform:
+ "rotate(calc(1turn - 90deg - (var(--gap-percent) * var(--percent-to-deg) * var(--offset-factor-secondary)))) scaleY(-1)",
+ transition: "all var(--transition-length) ease var(--delay)",
+ transformOrigin:
+ "calc(var(--circle-size) / 2) calc(var(--circle-size) / 2)",
+ } as React.CSSProperties
+ }
+ />
+ )}
+ <circle
+ cx="50"
+ cy="50"
+ r="45"
+ strokeWidth="10"
+ strokeDashoffset="0"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ className="opacity-100"
+ style={
+ {
+ stroke: gaugePrimaryColor,
+ "--stroke-percent": currentPercent,
+ strokeDasharray:
+ "calc(var(--stroke-percent) * var(--percent-to-px)) var(--circumference)",
+ transition:
+ "var(--transition-length) ease var(--delay),stroke var(--transition-length) ease var(--delay)",
+ transitionProperty: "stroke-dasharray,transform",
+ transform:
+ "rotate(calc(-90deg + var(--gap-percent) * var(--offset-factor) * var(--percent-to-deg)))",
+ transformOrigin:
+ "calc(var(--circle-size) / 2) calc(var(--circle-size) / 2)",
+ } as React.CSSProperties
+ }
+ />
+ </svg>
+ <span
+ data-current-value={currentPercent}
+ className="duration-[var(--transition-length)] delay-[var(--delay)] absolute inset-0 m-auto h-fit w-fit ease-linear animate-in fade-in"
+ >
+ {currentPercent}
+ </span>
+ </div>
+ );
+}
diff --git a/registry/index.tsx b/registry/index.tsx
index 0fac1487..ffd67918 100644
--- a/registry/index.tsx
+++ b/registry/index.tsx
@@ -227,6 +227,11 @@ const ui: Registry = {
type: "components:ui",
files: ["registry/components/magicui/shine-border.tsx"],
},
+ "gauge-circle": {
+ name: "gauge-circle",
+ type: "components:ui",
+ files: ["registry/components/magicui/gauge-circle.tsx"],
+ },
};
const example: Registry = {
@@ -778,20 +783,12 @@ const example: Registry = {
() => import("@/registry/components/example/box-reveal-demo"),
),
},
- "shine-border-demo": {
- name: "shine-border-demo",
- type: "components:example",
- files: ["registry/components/example/shine-border-demo.tsx"],
- component: React.lazy(
- () => import("@/registry/components/example/shine-border-demo"),
- ),
- },
- "shine-border-demo-2": {
- name: "shine-border-demo-2",
+ "gauge-circle-demo": {
+ name: "gauge-circle-demo",
type: "components:example",
- files: ["registry/components/example/shine-border-demo-2.tsx"],
+ files: ["registry/components/example/gauge-circle-demo.tsx"],
component: React.lazy(
- () => import("@/registry/components/example/shine-border-demo-2"),
+ () => import("@/registry/components/example/gauge-circle-demo"),
),
},
};