UNPKG

reka-ui

Version:

Vue port for Radix UI Primitives.

1 lines 6.74 kB
{"version":3,"file":"ProgressRoot.cjs","sources":["../../src/Progress/ProgressRoot.vue"],"sourcesContent":["<script lang=\"ts\">\nimport type { ComputedRef, Ref } from 'vue'\nimport type { PrimitiveProps } from '@/Primitive'\nimport { createContext, isNullish, useForwardExpose } from '@/shared'\n\nexport type ProgressRootEmits = {\n /** Event handler called when the progress value changes */\n 'update:modelValue': [value: string[] | undefined]\n /** Event handler called when the max value changes */\n 'update:max': [value: number]\n}\n\nexport interface ProgressRootProps extends PrimitiveProps {\n /** The progress value. Can be bind as `v-model`. */\n modelValue?: number | null\n /** The maximum progress value. */\n max?: number\n /**\n * A function to get the accessible label text representing the current value in a human-readable format.\n *\n * If not provided, the value label will be read as the numeric value as a percentage of the max value.\n */\n getValueLabel?: (value: number, max: number) => string\n}\n\nconst DEFAULT_MAX = 100\n\ninterface ProgressRootContext {\n modelValue?: Readonly<Ref<ProgressRootProps['modelValue']>>\n max: Readonly<Ref<number>>\n progressState: ComputedRef<ProgressState>\n}\n\nexport const [injectProgressRootContext, provideProgressRootContext]\n = createContext<ProgressRootContext>('ProgressRoot')\n\nexport type ProgressState = 'indeterminate' | 'loading' | 'complete'\n\nconst isNumber = (v: any): v is number => typeof v === 'number'\n\nfunction validateValue(value: any, max: number): number | null {\n const isValidValueError\n = isNullish(value)\n || (isNumber(value) && !Number.isNaN(value) && value <= max && value >= 0)\n\n if (isValidValueError)\n return value as null\n\n console.error(`Invalid prop \\`value\\` of value \\`${value}\\` supplied to \\`ProgressRoot\\`. The \\`value\\` prop must be:\n - a positive number\n - less than the value passed to \\`max\\` (or ${DEFAULT_MAX} if no \\`max\\` prop is set)\n - \\`null\\` or \\`undefined\\` if the progress is indeterminate.\n\nDefaulting to \\`null\\`.`)\n return null\n}\n\nfunction validateMax(max: number): number {\n const isValidMaxError = isNumber(max) && !Number.isNaN(max) && max > 0\n\n if (isValidMaxError)\n return max\n\n console.error(\n `Invalid prop \\`max\\` of value \\`${max}\\` supplied to \\`ProgressRoot\\`. Only numbers greater than 0 are valid max values. Defaulting to \\`${DEFAULT_MAX}\\`.`,\n )\n return DEFAULT_MAX\n}\n</script>\n\n<script setup lang=\"ts\">\nimport { useVModel } from '@vueuse/core'\nimport { computed, nextTick, watch } from 'vue'\nimport { Primitive } from '@/Primitive'\n\nconst props = withDefaults(defineProps<ProgressRootProps>(), {\n max: DEFAULT_MAX,\n getValueLabel: (value: number, max: number) =>\n `${Math.round((value / max) * DEFAULT_MAX)}%`,\n})\n\nconst emit = defineEmits<ProgressRootEmits>()\n\ndefineSlots<{\n default: (props: {\n /** Current input values */\n modelValue: typeof modelValue.value\n }) => any\n}>()\n\nuseForwardExpose()\nconst modelValue = useVModel(props, 'modelValue', emit, {\n passive: (props.modelValue === undefined) as false,\n})\n\nconst max = useVModel(props, 'max', emit, {\n passive: (props.max === undefined) as false,\n})\n\n// ------- Watch for correct values -------\nwatch(\n () => modelValue.value,\n async (value) => {\n const correctedValue = validateValue(value, props.max)\n if (correctedValue !== value) {\n await nextTick()\n modelValue.value = correctedValue\n }\n },\n { immediate: true },\n)\n\nwatch(\n () => props.max,\n (newMax) => {\n const correctedMax = validateMax(props.max)\n if (correctedMax !== newMax)\n max.value = correctedMax\n },\n { immediate: true },\n)\n// ------- End of watch for correct values -------\n\nconst progressState = computed<ProgressState>(() => {\n if (isNullish(modelValue.value))\n return 'indeterminate'\n if (modelValue.value === max.value)\n return 'complete'\n return 'loading'\n})\n\nprovideProgressRootContext({\n modelValue,\n max,\n progressState,\n})\n</script>\n\n<template>\n <Primitive\n :as-child=\"asChild\"\n :as=\"as\"\n :aria-valuemax=\"max\"\n :aria-valuemin=\"0\"\n :aria-valuenow=\"isNumber(modelValue) ? modelValue : undefined\"\n :aria-valuetext=\"getValueLabel(modelValue!, max)\"\n :aria-label=\"getValueLabel(modelValue!, max)\"\n role=\"progressbar\"\n :data-state=\"progressState\"\n :data-value=\"modelValue ?? undefined\"\n :data-max=\"max\"\n >\n <slot :model-value=\"modelValue\" />\n </Primitive>\n</template>\n"],"names":["createContext","isNullish","useForwardExpose","useVModel","watch","nextTick","computed"],"mappings":";;;;;;;;;AAyBA,MAAM,WAAc,GAAA,GAAA;AAQb,MAAM,CAAC,yBAAA,EAA2B,0BAA0B,CAAA,GAC/DA,mCAAmC,cAAc;AAIrD,MAAM,QAAW,GAAA,CAAC,CAAwB,KAAA,OAAO,CAAM,KAAA,QAAA;AAEvD,SAAS,aAAA,CAAc,OAAY,GAA4B,EAAA;AAC7D,EAAA,MAAM,iBACF,GAAAC,wBAAA,CAAU,KAAK,CAAA,IACb,SAAS,KAAK,CAAA,IAAK,CAAC,MAAA,CAAO,KAAM,CAAA,KAAK,CAAK,IAAA,KAAA,IAAS,OAAO,KAAS,IAAA,CAAA;AAE1E,EAAI,IAAA,iBAAA;AACF,IAAO,OAAA,KAAA;AAET,EAAQ,OAAA,CAAA,KAAA,CAAM,qCAAqC,KAAK,CAAA;AAAA;AAAA,8CAAA,EAEV,WAAW,CAAA;AAAA;;AAAA,uBAGnC,CAAA,CAAA;AACtB,EAAO,OAAA,IAAA;AACT;AAEA,SAAS,YAAY,GAAqB,EAAA;AACxC,EAAM,MAAA,eAAA,GAAkB,SAAS,GAAG,CAAA,IAAK,CAAC,MAAO,CAAA,KAAA,CAAM,GAAG,CAAA,IAAK,GAAM,GAAA,CAAA;AAErE,EAAI,IAAA,eAAA;AACF,IAAO,OAAA,GAAA;AAET,EAAQ,OAAA,CAAA,KAAA;AAAA,IACN,CAAA,gCAAA,EAAmC,GAAG,CAAA,mGAAA,EAAsG,WAAW,CAAA,GAAA;AAAA,GACzJ;AACA,EAAO,OAAA,WAAA;AACT;;;;;;;;;;;;AAQA,IAAA,MAAM,KAAQ,GAAA,OAAA;AAMd,IAAA,MAAM,IAAO,GAAA,MAAA;AASb,IAAiBC,wCAAA,EAAA;AACjB,IAAA,MAAM,UAAa,GAAAC,cAAA,CAAU,KAAO,EAAA,YAAA,EAA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