Skip to content

Commit 8c28277

Browse files
committed
refactor(CAccordion): improve component accessibility
1 parent 342ba88 commit 8c28277

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

packages/coreui-vue/src/components/accordion/CAccordionBody.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { CCollapse } from '../collapse/CCollapse'
44
const CAccordionBody = defineComponent({
55
name: 'CAccordionBody',
66
setup(_, { slots }) {
7+
const id = inject('id')
78
const visible = inject('visible') as Ref<boolean>
89
return () =>
910
h(
1011
CCollapse,
11-
{ class: 'accordion-collapse', visible: visible.value },
12+
{ class: 'accordion-collapse', id, visible: visible.value },
1213
{
1314
default: () => h('div', { class: ['accordion-body'] }, slots.default && slots.default()),
1415
},

packages/coreui-vue/src/components/accordion/CAccordionButton.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineComponent, h, inject, Ref } from 'vue'
33
const CAccordionButton = defineComponent({
44
name: 'CAccordionButton',
55
setup(_, { slots }) {
6+
const id = inject('id') as string
67
const toggleVisibility = inject('toggleVisibility') as () => void
78
const visible = inject('visible') as Ref<boolean>
89

@@ -11,7 +12,8 @@ const CAccordionButton = defineComponent({
1112
'button',
1213
{
1314
type: 'button',
14-
'aria-expanded': !visible.value,
15+
'aria-control': id,
16+
'aria-expanded': visible.value,
1517
class: ['accordion-button', { ['collapsed']: !visible.value }],
1618
onClick: () => toggleVisibility(),
1719
},

packages/coreui-vue/src/components/accordion/CAccordionItem.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { defineComponent, h, inject, provide, ref, watch, Ref } from 'vue'
1+
import { defineComponent, h, inject, provide, ref, watch, Ref, useId } from 'vue'
22

33
const CAccordionItem = defineComponent({
44
name: 'CAccordionItem',
55
props: {
6+
/**
7+
* The id global attribute defines an identifier (ID) that must be unique in the whole document.
8+
*/
9+
id: String,
610
/**
711
* The item key.
812
*/
@@ -13,16 +17,20 @@ const CAccordionItem = defineComponent({
1317
const alwaysOpen = inject('alwaysOpen') as boolean
1418
const setActiveItemKey = inject('setActiveItemKey') as (key: number | string) => void
1519

16-
const itemKey = ref(props.itemKey ?? Math.random().toString(36).slice(2, 11))
20+
const id = props.id ?? useId()
21+
const itemKey = ref(props.itemKey ?? id)
1722
const visible = ref(Boolean(activeItemKey.value === itemKey.value))
1823

1924
watch(activeItemKey, () => (visible.value = Boolean(activeItemKey.value === itemKey.value)))
2025

2126
const toggleVisibility = () => {
2227
visible.value = !visible.value
23-
!alwaysOpen && visible && setActiveItemKey(itemKey.value)
28+
if (!alwaysOpen && visible) {
29+
setActiveItemKey(itemKey.value)
30+
}
2431
}
2532

33+
provide('id', id)
2634
provide('visible', visible)
2735
provide('toggleVisibility', toggleVisibility)
2836

0 commit comments

Comments
 (0)