Skip to content

Commit e9d413c

Browse files
committed
refactor(CProgress): add a more accessible structure
1 parent 1878cb0 commit e9d413c

File tree

5 files changed

+209
-135
lines changed

5 files changed

+209
-135
lines changed

packages/coreui-vue/src/components/progress/CProgress.ts

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,61 @@
1-
import { defineComponent, h } from 'vue'
1+
import { defineComponent, h, inject } from 'vue'
22

33
import { CProgressBar } from './CProgressBar'
4+
import { Color } from '../../props'
45

56
const CProgress = defineComponent({
67
name: 'CProgress',
78
props: {
9+
/**
10+
* Use to animate the stripes right to left via CSS3 animations.
11+
*/
12+
animated: Boolean,
13+
/**
14+
* Sets the color context of the component to one of CoreUI’s themed colors.
15+
*
16+
* @values 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light'
17+
*/
18+
color: Color,
819
/**
920
* Sets the height of the component. If you set that value the inner `<CProgressBar>` will automatically resize accordingly.
1021
*/
1122
height: Number,
23+
/**
24+
* A string of all className you want applied to the <CProgressBar/> component.
25+
*
26+
* @since 5.0.0-alpha.1
27+
*/
28+
progressBarClassName: String,
1229
/**
1330
* Makes progress bar thinner.
1431
*/
1532
thin: Boolean,
33+
/**
34+
* The percent to progress the ProgressBar.
35+
*/
36+
value: {
37+
type: Number,
38+
default: 0,
39+
},
40+
/**
41+
* Set the progress bar variant to optional striped.
42+
*
43+
* @values 'striped'
44+
*/
45+
variant: {
46+
type: String,
47+
validator: (value: string) => {
48+
return value === 'striped'
49+
},
50+
},
1651
/**
1752
* Change the default color to white.
1853
*/
1954
white: Boolean,
20-
...CProgressBar.props,
2155
},
2256
setup(props, { slots }) {
57+
const stacked = inject('stacked', false) as boolean
58+
2359
return () =>
2460
h(
2561
'div',
@@ -31,20 +67,42 @@ const CProgress = defineComponent({
3167
'progress-white': props.white,
3268
},
3369
],
34-
...(props.height, { style: `height: ${props.height}px` }),
70+
style: {
71+
...(props.height ? { height: `${props.height}px` } : {}),
72+
...(stacked ? { width: `${props.value}%` } : {}),
73+
},
74+
...(props.value !== undefined && {
75+
role: 'progressbar',
76+
'aria-valuenow': props.value,
77+
'aria-valuemin': 0,
78+
'aria-valuemax': 100,
79+
}),
3580
},
36-
props.value
37-
? h(
81+
// @ts-expect-error name is defined in component
82+
slots.default && slots.default().some((vnode) => vnode.type.name === 'CProgressBar')
83+
? slots.default().map((vnode) => {
84+
// @ts-expect-error name is defined in component
85+
if (vnode.type.name === 'CProgressBar') {
86+
return h(vnode, {
87+
...(props.animated && { animated: props.animated }),
88+
...(props.color && { color: props.color }),
89+
...(props.value && { value: props.value }),
90+
...(props.variant && { variant: props.variant }),
91+
})
92+
}
93+
return vnode
94+
})
95+
: h(
3896
CProgressBar,
3997
{
40-
value: props.value,
98+
...(props.progressBarClassName && { class: props.progressBarClassName }),
4199
animated: props.animated,
42100
color: props.color,
101+
value: props.value,
43102
variant: props.variant,
44103
},
45-
slots.default && slots.default(),
46-
)
47-
: slots.default && slots.default(),
104+
() => slots.default && slots.default(),
105+
),
48106
)
49107
},
50108
})

packages/coreui-vue/src/components/progress/CProgressBar.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, h } from 'vue'
1+
import { defineComponent, h, inject } from 'vue'
22

33
import { Color } from '../../props'
44

@@ -35,6 +35,8 @@ const CProgressBar = defineComponent({
3535
},
3636
},
3737
setup(props, { slots }) {
38+
const stacked = inject('stacked', false) as boolean
39+
3840
return () =>
3941
h(
4042
'div',
@@ -47,11 +49,7 @@ const CProgressBar = defineComponent({
4749
['progress-bar-animated']: props.animated,
4850
},
4951
],
50-
role: 'progressbar',
51-
style: `width: ${props.value}%`,
52-
'aria-valuenow': props.value,
53-
'aria-valuemin': '0',
54-
'aria-valuemax': '100',
52+
...(!stacked && { style: { width: `${props.value}%` } }),
5553
},
5654
slots.default && slots.default(),
5755
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineComponent, h, provide } from 'vue'
2+
3+
const CProgressStacked = defineComponent({
4+
name: 'CProgressStacked',
5+
props: {},
6+
setup(_, { slots }) {
7+
provide('stacked', true)
8+
return () =>
9+
h(
10+
'div',
11+
{
12+
class: 'progress-stacked',
13+
},
14+
slots.default && slots.default(),
15+
)
16+
},
17+
})
18+
19+
export { CProgressStacked }
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { App } from 'vue'
22
import { CProgress } from './CProgress'
33
import { CProgressBar } from './CProgressBar'
4+
import { CProgressStacked } from './CProgressStacked'
45

56
const CProgressPlugin = {
67
install: (app: App): void => {
78
app.component(CProgress.name, CProgress)
89
app.component(CProgressBar.name, CProgressBar)
10+
app.component(CProgressStacked.name, CProgressStacked)
911
},
1012
}
1113

12-
export { CProgressPlugin, CProgress, CProgressBar }
14+
export { CProgressPlugin, CProgress, CProgressBar, CProgressStacked }

0 commit comments

Comments
 (0)