@makolabs/ripple 0.0.1-dev.16 → 0.0.1-dev.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { cn } from '../../helper/cls.js';
|
|
3
3
|
import { Color, Size } from '../../variants.js';
|
|
4
|
-
import type { DividedProgressProps, VariantColors } from '../../index.js';
|
|
4
|
+
import type { DividedProgressProps, ProgressSegment, VariantColors } from '../../index.js';
|
|
5
5
|
|
|
6
6
|
let {
|
|
7
7
|
segments,
|
|
@@ -14,20 +14,28 @@
|
|
|
14
14
|
labelClass = ''
|
|
15
15
|
}: DividedProgressProps = $props();
|
|
16
16
|
|
|
17
|
-
// Calculate
|
|
18
|
-
const totalValue = $derived(segments
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
// Calculate derived values
|
|
18
|
+
const totalValue = $derived(calculateTotalValue(segments));
|
|
19
|
+
const segmentPercentages = $derived(calculateSegmentPercentages(segments, max));
|
|
20
|
+
|
|
21
|
+
// Function composition - each function has a single responsibility
|
|
22
|
+
function calculateTotalValue(segments: ProgressSegment[]): number {
|
|
23
|
+
return segments.reduce((sum, segment) => sum + segment.value, 0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function calculatePercentage(value: number, max: number): number {
|
|
27
|
+
return Math.min(Math.round((value / max) * 100), 100);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function calculateSegmentPercentages(segments: ProgressSegment[], max: number) {
|
|
31
|
+
return segments.map(segment => ({
|
|
23
32
|
...segment,
|
|
24
|
-
percentage:
|
|
25
|
-
}))
|
|
26
|
-
|
|
33
|
+
percentage: calculatePercentage(segment.value, max)
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
);
|
|
37
|
+
// Class compositions
|
|
38
|
+
const containerClass = $derived(cn('relative w-full', className));
|
|
31
39
|
|
|
32
40
|
const progressClass = $derived(
|
|
33
41
|
cn('w-full rounded-full bg-gray-200 flex overflow-hidden', {
|
|
@@ -39,37 +47,28 @@
|
|
|
39
47
|
})
|
|
40
48
|
);
|
|
41
49
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
};
|
|
50
|
+
function getColorClass(color: VariantColors): string {
|
|
51
|
+
const colorMap = {
|
|
52
|
+
[Color.PRIMARY]: 'bg-primary-600',
|
|
53
|
+
[Color.SECONDARY]: 'bg-secondary-600',
|
|
54
|
+
[Color.SUCCESS]: 'bg-success-600',
|
|
55
|
+
[Color.DANGER]: 'bg-danger-600',
|
|
56
|
+
[Color.WARNING]: 'bg-warning-600',
|
|
57
|
+
[Color.INFO]: 'bg-info-600',
|
|
58
|
+
[Color.DEFAULT]: 'bg-gray-600'
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return colorMap[color] || 'bg-primary-600';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getSizeTextClass(size: string): string {
|
|
65
|
+
if (size === Size.XS || size === Size.SM) return 'text-xs';
|
|
66
|
+
if (size === Size.BASE) return 'text-sm';
|
|
67
|
+
return 'text-base'; // For Size.LG, Size.XL, and any other sizes
|
|
68
|
+
}
|
|
62
69
|
|
|
63
70
|
const labelTextClass = $derived(
|
|
64
|
-
cn(
|
|
65
|
-
'text-gray-500 mt-1',
|
|
66
|
-
{
|
|
67
|
-
'text-xs': size === Size.XS || size === Size.SM,
|
|
68
|
-
'text-sm': size === Size.BASE,
|
|
69
|
-
'text-base': size === Size.LG || size === Size.XL
|
|
70
|
-
},
|
|
71
|
-
labelClass
|
|
72
|
-
)
|
|
71
|
+
cn('text-gray-500 mt-1', getSizeTextClass(size), labelClass)
|
|
73
72
|
);
|
|
74
73
|
</script>
|
|
75
74
|
|
|
@@ -81,7 +80,7 @@
|
|
|
81
80
|
aria-valuemin="0"
|
|
82
81
|
aria-valuemax={max}
|
|
83
82
|
>
|
|
84
|
-
{#each segmentPercentages as segment
|
|
83
|
+
{#each segmentPercentages as segment}
|
|
85
84
|
{#if segment.percentage > 0}
|
|
86
85
|
<div
|
|
87
86
|
class={cn(getColorClass(segment.color), barClass)}
|
|
@@ -94,7 +93,7 @@
|
|
|
94
93
|
|
|
95
94
|
{#if showLabels || showValues}
|
|
96
95
|
<div class="flex justify-between mt-1">
|
|
97
|
-
{#each segmentPercentages as segment
|
|
96
|
+
{#each segmentPercentages as segment}
|
|
98
97
|
{#if segment.percentage > 0}
|
|
99
98
|
<div class={labelTextClass}>
|
|
100
99
|
{#if showLabels && segment.label}
|