feat: add ProgressBar ProgressCircle and Meter

This commit is contained in:
maoyingjie
2026-10-22 14:33:15 +08:00
parent e4d817ce55
commit 73bd7da5fa
66 changed files with 4143 additions and 10 deletions
@@ -0,0 +1,100 @@
<script lang="ts">
import {
computed,
defineComponent,
h,
provide,
useAttrs,
type PropType,
type StyleValue,
} from "vue";
import {
progressBarVariants,
type ProgressBarVariants,
} from "@heroui-vue/styles";
import {
assignIndicatorLabelIds,
indicatorContextKey,
useIndicatorLabelAssociation,
useIndicatorValue,
type IndicatorFormatOptions,
} from "../progress/value";
export default defineComponent({
name: "ProgressBar",
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<ProgressBarVariants["size"]>,
default: "md",
},
color: {
type: String as PropType<ProgressBarVariants["color"]>,
default: "accent",
},
},
setup(props, { slots }) {
const attrs = useAttrs();
const { clampedValue, percentage, valueText } = useIndicatorValue(
() => props,
);
const { baseId, resolveAriaLabelledby } = useIndicatorLabelAssociation();
const slotClasses = computed(() => {
const s = progressBarVariants({ color: props.color, size: props.size });
return {
base: s.base(),
fill: s.fill(),
output: s.output(),
track: s.track(),
};
});
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-bar",
};
const labelledby = resolveAriaLabelledby(attrs, ids);
if (labelledby !== undefined) rootProps["aria-labelledby"] = labelledby;
return h("div", rootProps, nodes);
};
},
});
</script>
@@ -0,0 +1,30 @@
<script setup lang="ts">
import { computed, inject, useAttrs, type StyleValue } from "vue";
import { indicatorContextKey } from "../progress/value";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(indicatorContextKey, undefined);
const rootClass = computed(() => ctx?.slots.value.fill);
const rootStyle = computed<StyleValue>(() => {
const width =
ctx?.state.value.isIndeterminate || ctx?.state.value.percentage == null
? undefined
: `${ctx.state.value.percentage}%`;
return [{ width }, attrs.style];
});
</script>
<template>
<div
v-bind="attrs"
:class="rootClass"
:style="rootStyle"
data-slot="progress-bar-fill"
>
<slot />
</div>
</template>
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { computed, inject, useAttrs } from "vue";
import { indicatorContextKey } from "../progress/value";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(indicatorContextKey, undefined);
const rootClass = computed(() => ctx?.slots.value.output);
</script>
<template>
<span v-bind="attrs" :class="rootClass" data-slot="progress-bar-output">
<slot>{{ ctx?.state.value.valueText }}</slot>
</span>
</template>
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { computed, inject, useAttrs } from "vue";
import { indicatorContextKey } from "../progress/value";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(indicatorContextKey, undefined);
const rootClass = computed(() => ctx?.slots.value.track);
</script>
<template>
<div v-bind="attrs" :class="rootClass" data-slot="progress-bar-track">
<slot />
</div>
</template>