-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathprogress.tsx
More file actions
169 lines (150 loc) · 4.38 KB
/
Copy pathprogress.tsx
File metadata and controls
169 lines (150 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { CSSProperties, defineComponent, effect, reactive, ref, toRefs, watch } from 'vue';
import { ProgressProps, progressProps, ISvgData } from './progress-types';
import { useNamespace, middleNum } from '../../../devui/shared/utils';
import './progress.scss';
export default defineComponent({
name: 'DProgress',
props: progressProps,
setup(props: ProgressProps) {
const {
height,
percentage,
percentageText,
percentageTextPlacement,
percentageTextColor,
barBgColor,
isCircle,
strokeWidth,
showContent,
} = toRefs(props);
const normalPercentage = ref(0);
effect(() => {
normalPercentage.value = middleNum(percentage.value);
});
const data: ISvgData = reactive({
pathString: '',
trailPath: null,
strokePath: null,
});
const setCircleProgress = () => {
if (!isCircle) {
return;
}
const radius = 50 - strokeWidth.value / 2;
const beginPositionY = -radius;
const endPositionY = radius * -2;
data.pathString = `M 50,50 m 0,${beginPositionY}
a ${radius},${radius} 0 1 1 0,${-endPositionY}
a ${radius},${radius} 0 1 1 0,${endPositionY}`;
const len = Math.PI * 2 * radius;
data.trailPath = {
stroke: 'var(--devui-dividing-line, #dfe1e6)',
strokeDasharray: `${len}px ${len}px`,
strokeDashoffset: `0`,
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s',
};
data.strokePath = {
stroke: barBgColor || null,
strokeDasharray: `${(normalPercentage.value / 100) * len}px ${len}px`,
strokeDashoffset: `0`,
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s',
};
};
setCircleProgress();
watch(
[
height,
normalPercentage,
percentageText,
percentageTextPlacement,
percentageTextColor,
barBgColor,
isCircle,
strokeWidth,
showContent,
],
() => {
setCircleProgress();
}
);
return {
data,
normalPercentage,
};
},
render() {
const {
height,
normalPercentage,
percentageText,
percentageTextPlacement,
percentageTextColor,
barBgColor,
isCircle,
strokeWidth,
showContent,
data,
$slots,
} = this;
const ns = useNamespace('progress');
const isOutside = percentageTextPlacement === 'outside';
const isInsideBg = percentageTextPlacement === 'insideBg';
const createPercentageText = () => {
return (
<span
style={{
lineHeight: height,
color: percentageTextColor,
}}>
{percentageText}
</span>
);
};
const progressLine = (
<div class={ns.e('content')}>
<div
class={ns.e('line')}
style={{
height: height,
borderRadius: height,
}}>
<div
class={[ns.e('bar'), percentageTextPlacement]}
style={{
height: height,
borderRadius: height,
width: `${normalPercentage}%`,
backgroundColor: barBgColor,
}}>
{!isOutside && !isInsideBg ? createPercentageText() : null}
</div>
{isInsideBg ? createPercentageText() : null}
</div>
{isOutside && !!percentageText ? createPercentageText() : null}
</div>
);
const textElement = (
<span class={ns.e('circle-text')} style={{ color: percentageTextColor }}>
{normalPercentage}%
</span>
);
const progressCircle = (
<div class={ns.e('circle')}>
<svg class={ns.e('circle')} viewBox="0 0 100 100">
<path fill-opacity="0" stroke-width={strokeWidth} style={data.trailPath as CSSProperties} d={data.pathString} />
<path
d={data.pathString}
stroke-linecap="round"
fill-opacity="0"
stroke={barBgColor}
stroke-width={normalPercentage ? strokeWidth : 0}
style={data.strokePath as CSSProperties}
/>
</svg>
{showContent && $slots.default?.()}
{showContent && !$slots.default && textElement}
</div>
);
return <div class={ns.b()}>{!isCircle ? progressLine : progressCircle}</div>;
},
});