Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#SIKKA-5131[in progress] added gauge and other components
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React from "react"; | ||
import { cn } from "../util"; | ||
|
||
type GaugeType = { | ||
value: number; // Value between 0 and 100 | ||
maxValue: number; // Value between 0 and 100 | ||
design?: "full-circle" | "half-circle"; | ||
}; | ||
|
||
export const Gauge: React.FC<GaugeType> = ({ | ||
value = 0, | ||
maxValue = 100, | ||
design = "half-circle", | ||
}) => { | ||
const radius = 16; | ||
const fullCircumference = 2 * Math.PI * radius; // Full circle circumference | ||
const halfCircumference = Math.PI * radius ; // Half circle circumference, updated | ||
const percentage = value / maxValue; | ||
const fullOffset = fullCircumference - percentage * fullCircumference; | ||
const halfOffset = halfCircumference - percentage * halfCircumference; // Updated offset for half-circle | ||
const strokeDasharray = percentage * halfCircumference; | ||
const strokeDashoffset = halfCircumference * (1 - percentage); | ||
|
||
const FullCircle = () => ( | ||
<svg className={"hawa-h-36 hawa-w-36"} viewBox="0 0 36 36"> | ||
<circle | ||
cx="18" | ||
cy="18" | ||
r={radius} | ||
strokeWidth="4" | ||
stroke="lightgray" | ||
fill="none" | ||
/> | ||
<circle | ||
cx="18" | ||
cy="18" | ||
r={radius} | ||
strokeWidth="4" | ||
stroke="#00f" | ||
fill="none" | ||
strokeDasharray={fullCircumference} | ||
strokeDashoffset={fullOffset} | ||
style={{ | ||
transition: "stroke-dashoffset 0.35s", | ||
transform: "rotate(180deg)", | ||
transformOrigin: "50% 50%", | ||
}} | ||
/> | ||
<text | ||
x="50%" | ||
y="50%" | ||
alignmentBaseline="middle" | ||
textAnchor="middle" | ||
fontSize="10" | ||
fill="#000" | ||
> | ||
{value} | ||
</text> | ||
</svg> | ||
); | ||
|
||
const HalfCircle = () => ( | ||
<svg className={"hawa-h-36 hawa-w-36"} viewBox="0 0 36 36"> | ||
<path | ||
d="M 4,18 A 14,14 0 0,1 32,18" | ||
strokeWidth="4" | ||
stroke="lightgray" | ||
fill="none" | ||
/> | ||
<path | ||
d="M 4,18 A 14,14 0 0,1 32,18" | ||
strokeWidth="4" | ||
stroke="#00f" | ||
fill="none" | ||
strokeDasharray={`${strokeDasharray}`} | ||
strokeDashoffset={`${strokeDashoffset}`} | ||
style={{ | ||
transition: "stroke-dashoffset 0.35s", | ||
transformOrigin: "50% 50%", | ||
}} | ||
/> | ||
<text | ||
x="50%" | ||
y="45%" | ||
alignmentBaseline="middle" | ||
textAnchor="middle" | ||
fontSize="8" | ||
fill="#000" | ||
> | ||
{value} | ||
</text> | ||
</svg> | ||
); | ||
return ( | ||
<div className="hawa-flex hawa-flex-row hawa-gap-x-3"> | ||
<div aria-label="Gauge" role="status"> | ||
{design === "full-circle" ? <FullCircle /> : <HalfCircle />} | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { Gauge } from "../../components"; | ||
import { ArgsTable, Story, Title } from "@storybook/blocks"; | ||
import { setLocale, t } from "../translations/i18n"; | ||
import { GaugeCircle } from "lucide-react"; | ||
|
||
const meta = { | ||
title: "Elements/Gauge", | ||
component: Gauge, | ||
parameters: { | ||
layout: "centered", | ||
docs: { | ||
page: () => ( | ||
<> | ||
<h1>{"<Gauge/>"}</h1> | ||
<ArgsTable /> | ||
</> | ||
), | ||
}, | ||
}, | ||
tags: ["autodocs"], | ||
} satisfies Meta<typeof Gauge>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Gauge>; | ||
|
||
const Template = (args: any, globals: any) => { | ||
const locale = globals.globals?.locale === "ar" ? "ar" : "en"; | ||
const direction = locale === "ar" ? "rtl" : "ltr"; | ||
setLocale(locale); | ||
|
||
return ( | ||
<div dir={direction} className="hawa-w-full hawa-h-52 "> | ||
<Gauge {...args} design="half-circle" /> | ||
<Gauge {...args} design="full-circle" /> | ||
</div> | ||
); | ||
}; | ||
export const Default: Story = { | ||
render: Template.bind({}), | ||
}; |