-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
feat(VBreadcrumbs): improve accessibility and add menu when collapsed #22358
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
base: dev
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <template> | ||
| <v-breadcrumbs :items="items" :total-visible="3" collapse-in-menu> | ||
| </v-breadcrumbs> | ||
| </template> | ||
|
|
||
| <script setup> | ||
| const items = [ | ||
| { | ||
| title: 'Dashboard', | ||
| disabled: false, | ||
| href: 'breadcrumbs_dashboard', | ||
| }, | ||
| { | ||
| title: 'Link 1', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_1', | ||
| }, | ||
| { | ||
| title: 'Link 2', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_2', | ||
| }, | ||
| { | ||
| title: 'Link 3', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_3', | ||
| }, | ||
| { | ||
| title: 'Link 4', | ||
| disabled: true, | ||
| href: 'breadcrumbs_link_4', | ||
| }, | ||
| ] | ||
| </script> | ||
|
|
||
| <script> | ||
| export default { | ||
| data: () => ({ | ||
| items: [ | ||
| { | ||
| title: 'Dashboard', | ||
| disabled: false, | ||
| href: 'breadcrumbs_dashboard', | ||
| }, | ||
| { | ||
| title: 'Link 1', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_1', | ||
| }, | ||
| { | ||
| title: 'Link 2', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_2', | ||
| }, | ||
| { | ||
| title: 'Link 3', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_3', | ||
| }, | ||
| { | ||
| title: 'Link 4', | ||
| disabled: true, | ||
| href: 'breadcrumbs_link_4', | ||
| }, | ||
| ], | ||
| }), | ||
| } | ||
| </script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <template> | ||
| <v-breadcrumbs :items="items" :total-visible="3"> | ||
| </v-breadcrumbs> | ||
| </template> | ||
|
|
||
| <script setup> | ||
| const items = [ | ||
| { | ||
| title: 'Dashboard', | ||
| disabled: false, | ||
| href: 'breadcrumbs_dashboard', | ||
| }, | ||
| { | ||
| title: 'Link 1', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_1', | ||
| }, | ||
| { | ||
| title: 'Link 2', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_2', | ||
| }, | ||
| { | ||
| title: 'Link 3', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_3', | ||
| }, | ||
| { | ||
| title: 'Link 4', | ||
| disabled: true, | ||
| href: 'breadcrumbs_link_4', | ||
| }, | ||
| ] | ||
| </script> | ||
|
|
||
| <script> | ||
| export default { | ||
| data: () => ({ | ||
| items: [ | ||
| { | ||
| title: 'Dashboard', | ||
| disabled: false, | ||
| href: 'breadcrumbs_dashboard', | ||
| }, | ||
| { | ||
| title: 'Link 1', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_1', | ||
| }, | ||
| { | ||
| title: 'Link 2', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_2', | ||
| }, | ||
| { | ||
| title: 'Link 3', | ||
| disabled: false, | ||
| href: 'breadcrumbs_link_3', | ||
| }, | ||
| { | ||
| title: 'Link 4', | ||
| disabled: true, | ||
| href: 'breadcrumbs_link_4', | ||
| }, | ||
| ], | ||
| }), | ||
| } | ||
| </script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <template> | ||
| <v-app> | ||
| <v-container> | ||
| <v-breadcrumbs :items="items" :total-visible="3" /> | ||
| <v-breadcrumbs :items="items" :total-visible="3" collapse-in-menu /> | ||
| </v-container> | ||
| </v-app> | ||
| </template> | ||
|
|
||
| <script> | ||
| export default { | ||
| name: 'Playground', | ||
| data: () => ({ | ||
| items: Array.from({ length: 4 }, (k, v) => ({ title: `Link ${v + 1}`, href: `breadcrumbs_link_${v + 1}` })), | ||
| }), | ||
| } | ||
| </script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ import './VBreadcrumbs.sass' | |
| // Components | ||
| import { VBreadcrumbsDivider } from './VBreadcrumbsDivider' | ||
| import { VBreadcrumbsItem } from './VBreadcrumbsItem' | ||
| import { VIcon } from '../VIcon' | ||
| import { VList, VListItem, VListItemTitle } from '../VList' | ||
| import { VMenu } from '../VMenu' | ||
| import { VDefaultsProvider } from '@/components/VDefaultsProvider' | ||
| import { VIcon } from '@/components/VIcon' | ||
|
|
||
| // Composables | ||
| import { useBackgroundColor } from '@/composables/color' | ||
|
|
@@ -17,8 +19,8 @@ import { makeRoundedProps, useRounded } from '@/composables/rounded' | |
| import { makeTagProps } from '@/composables/tag' | ||
|
|
||
| // Utilities | ||
| import { computed, toRef } from 'vue' | ||
| import { genericComponent, isObject, propsFactory, useRender } from '@/util' | ||
| import { computed, ref, toRef, watch } from 'vue' | ||
| import { genericComponent, isObject, noop, propsFactory, useRender } from '@/util' | ||
|
|
||
| // Types | ||
| import type { PropType } from 'vue' | ||
|
|
@@ -36,23 +38,29 @@ export const makeVBreadcrumbsProps = propsFactory({ | |
| activeClass: String, | ||
| activeColor: String, | ||
| bgColor: String, | ||
| collapseInMenu: Boolean, | ||
| color: String, | ||
| disabled: Boolean, | ||
| divider: { | ||
| type: String, | ||
| default: '/', | ||
| }, | ||
| ellipsis: { | ||
| type: String, | ||
| default: '...', | ||
| }, | ||
| icon: IconValue, | ||
| items: { | ||
| type: Array as PropType<readonly BreadcrumbItem[]>, | ||
| default: () => ([]), | ||
| }, | ||
| itemProps: Boolean, | ||
| totalVisible: Number, | ||
|
|
||
| ...makeComponentProps(), | ||
| ...makeDensityProps(), | ||
| ...makeRoundedProps(), | ||
| ...makeTagProps({ tag: 'ul' }), | ||
| ...makeTagProps({ tag: 'nav' }), | ||
| }, 'VBreadcrumbs') | ||
|
|
||
| export const VBreadcrumbs = genericComponent<new <T extends BreadcrumbItem>( | ||
|
|
@@ -91,12 +99,23 @@ export const VBreadcrumbs = genericComponent<new <T extends BreadcrumbItem>( | |
| const items = computed(() => props.items.map(item => { | ||
| return typeof item === 'string' ? { item: { title: item }, raw: item } : { item, raw: item } | ||
| })) | ||
| const showEllipsis = toRef(() => props.totalVisible ? items.value.length > props.totalVisible : false) | ||
| const enableEllipsis = ref(showEllipsis.value) | ||
|
|
||
| const onClickEllipsis = () => { | ||
| enableEllipsis.value = false | ||
| } | ||
|
|
||
| watch(showEllipsis, (value: boolean) => { | ||
| enableEllipsis.value = value | ||
| }) | ||
|
|
||
| useRender(() => { | ||
| const hasPrepend = !!(slots.prepend || props.icon) | ||
|
|
||
| return ( | ||
| <props.tag | ||
| aria-label="breadcrumbs" | ||
| class={[ | ||
| 'v-breadcrumbs', | ||
| backgroundColorClasses.value, | ||
|
|
@@ -109,6 +128,7 @@ export const VBreadcrumbs = genericComponent<new <T extends BreadcrumbItem>( | |
| props.style, | ||
| ]} | ||
| > | ||
| <ol> | ||
| { hasPrepend && ( | ||
| <li key="prepend" class="v-breadcrumbs__prepend"> | ||
| { !slots.prepend ? ( | ||
|
|
@@ -133,7 +153,7 @@ export const VBreadcrumbs = genericComponent<new <T extends BreadcrumbItem>( | |
| </li> | ||
| )} | ||
|
|
||
| { items.value.map(({ item, raw }, index, array) => ( | ||
| { !enableEllipsis.value && items.value.map(({ item, raw }, index, array) => ( | ||
| <> | ||
| { slots.item?.({ item, index }) ?? ( | ||
| <VBreadcrumbsItem | ||
|
|
@@ -157,7 +177,68 @@ export const VBreadcrumbs = genericComponent<new <T extends BreadcrumbItem>( | |
| </> | ||
| ))} | ||
|
|
||
| { enableEllipsis.value && ( | ||
| <> | ||
| { (() => { | ||
| const { item } = items.value[0] | ||
| return ( | ||
| <> | ||
| { slots.item?.({ item, index: 0 }) ?? ( | ||
| <VBreadcrumbsItem | ||
| disabled={ false } | ||
| { ...(typeof item === 'string' ? { title: item } : item) } | ||
| /> | ||
| )} | ||
| </> | ||
| ) | ||
| })()} | ||
|
|
||
| <VBreadcrumbsDivider /> | ||
|
|
||
| <VBreadcrumbsItem | ||
| tabindex="0" | ||
| onClick={ props.collapseInMenu ? noop : onClickEllipsis } | ||
| class="v-breadcrumbs-item--ellipsis" | ||
| > | ||
| { props.ellipsis } | ||
| { props.collapseInMenu ? ( | ||
| <VMenu activator="parent"> | ||
| {{ | ||
| default: () => ( | ||
| <VList> | ||
|
||
| { items.value.slice(1, items.value.length - 1).map(({ item }, index) => ( | ||
| <VListItem key={ index } value={ index } component="a" href={ 'href' in item ? item.href : undefined }> | ||
|
||
| <VListItemTitle>{ item.title }</VListItemTitle> | ||
| </VListItem> | ||
| ))} | ||
| </VList> | ||
| ), | ||
| }} | ||
| </VMenu> | ||
| ) : null } | ||
| </VBreadcrumbsItem> | ||
|
|
||
| <VBreadcrumbsDivider /> | ||
|
|
||
| { (() => { | ||
| const lastIndex = items.value.length - 1 | ||
| const { item } = items.value[lastIndex] | ||
| return ( | ||
| <> | ||
| { slots.item?.({ item, index: lastIndex }) ?? ( | ||
| <VBreadcrumbsItem | ||
| disabled | ||
| { ...(typeof item === 'string' ? { title: item } : item) } | ||
| /> | ||
| )} | ||
| </> | ||
| ) | ||
| })()} | ||
| </> | ||
| )} | ||
|
|
||
| { slots.default?.() } | ||
| </ol> | ||
| </props.tag> | ||
| ) | ||
| }) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prevents the limit from being responsive. Let's say I resize the window and the limit changes, it won't be reflected until the component or page is fully refreshed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree. I added a watcher on showEllipsis to handle the responsive behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we leave it like this, it will only support on/off change while someone might want to change the limit based on the available width (e.g. `:total-visible='$vuetify.display.smAndUp ? 4 : 2').
BTW. it might be a personal preference, but I feel like those variables names should swap:
showEllipsis»ellipsisEnabledenableEllipsis»showEllipsisorhasEllipsisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll renamed it