-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0ae40b
commit eb2e8f5
Showing
5 changed files
with
202 additions
and
16 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
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,103 @@ | ||
import clsx from "clsx"; | ||
import type { Component, ComponentProps } from "solid-js"; | ||
import { mergeProps, splitProps } from "solid-js"; | ||
|
||
type Size = "xs" | "sm" | "md" | "lg" | "xl"; | ||
|
||
const sizes: Record<Size, { radius: number; strokeWidth: number }> = { | ||
xs: { radius: 15, strokeWidth: 3 }, | ||
sm: { radius: 19, strokeWidth: 4 }, | ||
md: { radius: 32, strokeWidth: 6 }, | ||
lg: { radius: 52, strokeWidth: 8 }, | ||
xl: { radius: 80, strokeWidth: 10 }, | ||
}; | ||
|
||
type ProgressCircleProps = ComponentProps<"div"> & { | ||
value?: number; | ||
size?: Size; | ||
radius?: number; | ||
strokeWidth?: number; | ||
showAnimation?: boolean; | ||
}; | ||
|
||
const ProgressCircle: Component<ProgressCircleProps> = (rawProps) => { | ||
const props = mergeProps( | ||
{ size: "md" as Size, showAnimation: true }, | ||
rawProps, | ||
); | ||
const [local, others] = splitProps(props, [ | ||
"class", | ||
"children", | ||
"value", | ||
"size", | ||
"radius", | ||
"strokeWidth", | ||
"showAnimation", | ||
]); | ||
|
||
const value = () => getLimitedValue(local.value); | ||
const radius = () => local.radius ?? sizes[local.size].radius; | ||
const strokeWidth = () => local.strokeWidth ?? sizes[local.size].strokeWidth; | ||
const normalizedRadius = () => radius() - strokeWidth() / 2; | ||
const circumference = () => normalizedRadius() * 2 * Math.PI; | ||
const strokeDashoffset = () => (value() / 100) * circumference(); | ||
const offset = () => circumference() - strokeDashoffset(); | ||
|
||
return ( | ||
<div | ||
class={clsx("flex flex-col items-center justify-center", local.class)} | ||
{...others} | ||
> | ||
<svg | ||
width={radius() * 2} | ||
height={radius() * 2} | ||
viewBox={`0 0 ${radius() * 2} ${radius() * 2}`} | ||
class="-rotate-90" | ||
> | ||
<title>Progress Circle</title> | ||
<circle | ||
r={normalizedRadius()} | ||
cx={radius()} | ||
cy={radius()} | ||
stroke-width={strokeWidth()} | ||
fill="transparent" | ||
stroke="" | ||
stroke-linecap="round" | ||
class="stroke-secondary transition-colors ease-linear" | ||
/> | ||
{value() >= 0 ? ( | ||
<circle | ||
r={normalizedRadius()} | ||
cx={radius()} | ||
cy={radius()} | ||
stroke-width={strokeWidth()} | ||
stroke-dasharray={`${circumference()} ${circumference()}`} | ||
stroke-dashoffset={offset()} | ||
fill="transparent" | ||
stroke="" | ||
stroke-linecap="round" | ||
class={clsx( | ||
"stroke-primary transition-colors ease-linear", | ||
local.showAnimation | ||
? "transition-all duration-300 ease-in-out" | ||
: "", | ||
)} | ||
/> | ||
) : null} | ||
</svg> | ||
<div class="absolute flex">{local.children}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
function getLimitedValue(input: number | undefined) { | ||
if (input === undefined) { | ||
return 0; | ||
// biome-ignore lint/style/noUselessElse: | ||
} else if (input > 100) { | ||
return 100; | ||
} | ||
return input; | ||
} | ||
|
||
export { ProgressCircle }; |