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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 11 additions & 3 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy