Skip to content

Minor fixes and improvements in the CAccordion and CNavGroup component #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(CAccordion): improve component accessibility
  • Loading branch information
mrholek committed Jan 7, 2025
commit 8c2827790d607a449aeafc821a80d7848a8438ba
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { CCollapse } from '../collapse/CCollapse'
const CAccordionBody = defineComponent({
name: 'CAccordionBody',
setup(_, { slots }) {
const id = inject('id')
const visible = inject('visible') as Ref<boolean>
return () =>
h(
CCollapse,
{ class: 'accordion-collapse', visible: visible.value },
{ class: 'accordion-collapse', id, visible: visible.value },
{
default: () => h('div', { class: ['accordion-body'] }, slots.default && slots.default()),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineComponent, h, inject, Ref } from 'vue'
const CAccordionButton = defineComponent({
name: 'CAccordionButton',
setup(_, { slots }) {
const id = inject('id') as string
const toggleVisibility = inject('toggleVisibility') as () => void
const visible = inject('visible') as Ref<boolean>

Expand All @@ -11,7 +12,8 @@ const CAccordionButton = defineComponent({
'button',
{
type: 'button',
'aria-expanded': !visible.value,
'aria-control': id,
'aria-expanded': visible.value,
class: ['accordion-button', { ['collapsed']: !visible.value }],
onClick: () => toggleVisibility(),
},
Expand Down
14 changes: 11 additions & 3 deletions packages/coreui-vue/src/components/accordion/CAccordionItem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { defineComponent, h, inject, provide, ref, watch, Ref } from 'vue'
import { defineComponent, h, inject, provide, ref, watch, Ref, useId } from 'vue'

const CAccordionItem = defineComponent({
name: 'CAccordionItem',
props: {
/**
* The id global attribute defines an identifier (ID) that must be unique in the whole document.
*/
id: String,
/**
* The item key.
*/
Expand All @@ -13,16 +17,20 @@ const CAccordionItem = defineComponent({
const alwaysOpen = inject('alwaysOpen') as boolean
const setActiveItemKey = inject('setActiveItemKey') as (key: number | string) => void

const itemKey = ref(props.itemKey ?? Math.random().toString(36).slice(2, 11))
const id = props.id ?? useId()
const itemKey = ref(props.itemKey ?? id)
const visible = ref(Boolean(activeItemKey.value === itemKey.value))

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

const toggleVisibility = () => {
visible.value = !visible.value
!alwaysOpen && visible && setActiveItemKey(itemKey.value)
if (!alwaysOpen && visible) {
setActiveItemKey(itemKey.value)
}
}

provide('id', id)
provide('visible', visible)
provide('toggleVisibility', toggleVisibility)

Expand Down
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