feat: add ProgressBar ProgressCircle and Meter
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
provide,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import {
|
||||
progressCircleVariants,
|
||||
type ProgressCircleVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import {
|
||||
assignIndicatorLabelIds,
|
||||
indicatorContextKey,
|
||||
useIndicatorLabelAssociation,
|
||||
useIndicatorValue,
|
||||
type IndicatorFormatOptions,
|
||||
} from "../progress/value";
|
||||
import {
|
||||
PROGRESS_CIRCLE_CENTER,
|
||||
PROGRESS_CIRCLE_CIRCUMFERENCE,
|
||||
PROGRESS_CIRCLE_RADIUS,
|
||||
PROGRESS_CIRCLE_STROKE_WIDTH,
|
||||
} from "./constants";
|
||||
|
||||
export default defineComponent({
|
||||
name: "ProgressCircle",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
value: { type: Number, default: 0 },
|
||||
minValue: { type: Number, default: 0 },
|
||||
maxValue: { type: Number, default: 100 },
|
||||
isIndeterminate: { type: Boolean, default: false },
|
||||
valueLabel: {
|
||||
type: [String, Number] as PropType<string | number>,
|
||||
default: undefined,
|
||||
},
|
||||
formatOptions: {
|
||||
type: Object as PropType<IndicatorFormatOptions>,
|
||||
default: undefined,
|
||||
},
|
||||
size: {
|
||||
type: String as PropType<ProgressCircleVariants["size"]>,
|
||||
default: "md",
|
||||
},
|
||||
color: {
|
||||
type: String as PropType<ProgressCircleVariants["color"]>,
|
||||
default: "accent",
|
||||
},
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const { clampedValue, percentage, valueText } = useIndicatorValue(
|
||||
() => props,
|
||||
);
|
||||
const { baseId, resolveAriaLabelledby } = useIndicatorLabelAssociation();
|
||||
|
||||
const slotClasses = computed(() => {
|
||||
const s = progressCircleVariants({
|
||||
color: props.color,
|
||||
size: props.size,
|
||||
});
|
||||
return {
|
||||
base: s.base(),
|
||||
fillCircle: s.fillCircle(),
|
||||
track: s.track(),
|
||||
trackCircle: s.trackCircle(),
|
||||
};
|
||||
});
|
||||
|
||||
const state = computed(() => ({
|
||||
percentage: percentage.value,
|
||||
valueText: valueText.value,
|
||||
isIndeterminate: props.isIndeterminate,
|
||||
}));
|
||||
|
||||
provide(indicatorContextKey, { slots: slotClasses, state });
|
||||
|
||||
return () => {
|
||||
const rawChildren = slots.default?.(state.value) ?? [];
|
||||
const { nodes, ids } = assignIndicatorLabelIds(
|
||||
Array.isArray(rawChildren) ? rawChildren : [rawChildren],
|
||||
baseId,
|
||||
);
|
||||
const role =
|
||||
typeof attrs.role === "string" && attrs.role
|
||||
? attrs.role
|
||||
: "progressbar";
|
||||
|
||||
const rootProps: Record<string, unknown> = {
|
||||
...attrs,
|
||||
class: [slotClasses.value.base, attrs.class],
|
||||
role,
|
||||
"aria-valuemin": props.minValue,
|
||||
"aria-valuemax": props.maxValue,
|
||||
"aria-valuenow": props.isIndeterminate ? undefined : clampedValue.value,
|
||||
"aria-valuetext": props.isIndeterminate ? undefined : valueText.value,
|
||||
"data-slot": "progress-circle",
|
||||
};
|
||||
const labelledby = resolveAriaLabelledby(attrs, ids);
|
||||
if (labelledby !== undefined) rootProps["aria-labelledby"] = labelledby;
|
||||
return h("div", rootProps, nodes);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -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>
|
||||
@@ -0,0 +1,30 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, useAttrs } from "vue";
|
||||
import { indicatorContextKey } from "../progress/value";
|
||||
import { PROGRESS_CIRCLE_CENTER } from "./constants";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(indicatorContextKey, undefined);
|
||||
|
||||
const rootClass = computed(() => ctx?.slots.value.track);
|
||||
|
||||
const viewBox = computed(() => {
|
||||
const explicit = attrs.viewBox;
|
||||
if (explicit != null && explicit !== "") return explicit as string;
|
||||
return `0 0 ${PROGRESS_CIRCLE_CENTER * 2} ${PROGRESS_CIRCLE_CENTER * 2}`;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<svg
|
||||
v-bind="attrs"
|
||||
:class="rootClass"
|
||||
:viewBox="viewBox"
|
||||
fill="none"
|
||||
data-slot="progress-circle-track"
|
||||
>
|
||||
<slot />
|
||||
</svg>
|
||||
</template>
|
||||
@@ -0,0 +1,43 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, useAttrs } from "vue";
|
||||
import { indicatorContextKey } from "../progress/value";
|
||||
import {
|
||||
PROGRESS_CIRCLE_CENTER,
|
||||
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.trackCircle);
|
||||
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),
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<circle
|
||||
v-bind="attrs"
|
||||
:class="rootClass"
|
||||
:cx="cx"
|
||||
:cy="cy"
|
||||
:r="r"
|
||||
:stroke-width="strokeWidth"
|
||||
data-slot="progress-circle-track-circle"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,7 @@
|
||||
/** Circle geometry matching the fixed HeroUI baseline (viewBox 0 0 36 36). */
|
||||
export const PROGRESS_CIRCLE_CENTER = 18;
|
||||
export const PROGRESS_CIRCLE_STROKE_WIDTH = 4;
|
||||
export const PROGRESS_CIRCLE_RADIUS =
|
||||
PROGRESS_CIRCLE_CENTER - PROGRESS_CIRCLE_STROKE_WIDTH / 2;
|
||||
export const PROGRESS_CIRCLE_CIRCUMFERENCE =
|
||||
2 * Math.PI * PROGRESS_CIRCLE_RADIUS;
|
||||
Reference in New Issue
Block a user