feat: add ProgressBar ProgressCircle and Meter
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, useAttrs } from "vue";
|
||||
import { indicatorContextKey } from "../progress/value";
|
||||
import {
|
||||
PROGRESS_CIRCLE_CENTER,
|
||||
PROGRESS_CIRCLE_CIRCUMFERENCE,
|
||||
PROGRESS_CIRCLE_RADIUS,
|
||||
PROGRESS_CIRCLE_STROKE_WIDTH,
|
||||
} from "./constants";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(indicatorContextKey, undefined);
|
||||
|
||||
function pick(
|
||||
camel: string,
|
||||
kebab: string,
|
||||
fallback: string | number,
|
||||
): string | number {
|
||||
const value = attrs[camel] ?? attrs[kebab];
|
||||
return value == null || value === "" ? fallback : (value as string | number);
|
||||
}
|
||||
|
||||
const rootClass = computed(() => ctx?.slots.value.fillCircle);
|
||||
|
||||
const cx = computed(() => pick("cx", "cx", PROGRESS_CIRCLE_CENTER));
|
||||
const cy = computed(() => pick("cy", "cy", PROGRESS_CIRCLE_CENTER));
|
||||
const r = computed(() => pick("r", "r", PROGRESS_CIRCLE_RADIUS));
|
||||
const strokeWidth = computed(() =>
|
||||
pick("strokeWidth", "stroke-width", PROGRESS_CIRCLE_STROKE_WIDTH),
|
||||
);
|
||||
const strokeDasharray = computed(() =>
|
||||
pick("strokeDasharray", "stroke-dasharray", PROGRESS_CIRCLE_CIRCUMFERENCE),
|
||||
);
|
||||
|
||||
const strokeDashoffset = computed(() => {
|
||||
const explicit = attrs.strokeDashoffset ?? attrs["stroke-dashoffset"];
|
||||
if (explicit != null && explicit !== "") return explicit as string | number;
|
||||
const state = ctx?.state.value;
|
||||
if (!state || state.isIndeterminate || state.percentage == null) {
|
||||
return PROGRESS_CIRCLE_CIRCUMFERENCE * 0.75;
|
||||
}
|
||||
return (
|
||||
PROGRESS_CIRCLE_CIRCUMFERENCE -
|
||||
(state.percentage / 100) * PROGRESS_CIRCLE_CIRCUMFERENCE
|
||||
);
|
||||
});
|
||||
|
||||
const linecap = computed(() => {
|
||||
const value = attrs.strokeLinecap ?? attrs["stroke-linecap"];
|
||||
return (value ?? "round") as "butt" | "round" | "square" | "inherit";
|
||||
});
|
||||
|
||||
const transform = computed(() => {
|
||||
const explicit = attrs.transform;
|
||||
if (explicit != null && explicit !== "") return explicit as string;
|
||||
return `rotate(-90 ${PROGRESS_CIRCLE_CENTER} ${PROGRESS_CIRCLE_CENTER})`;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<circle
|
||||
v-bind="attrs"
|
||||
:class="rootClass"
|
||||
:cx="cx"
|
||||
:cy="cy"
|
||||
:r="r"
|
||||
:stroke-width="strokeWidth"
|
||||
:stroke-dasharray="strokeDasharray"
|
||||
:stroke-dashoffset="strokeDashoffset"
|
||||
:stroke-linecap="linecap"
|
||||
:transform="transform"
|
||||
data-slot="progress-circle-fill-circle"
|
||||
/>
|
||||
</template>
|
||||
Reference in New Issue
Block a user