Galleria: keyboard support correction for quick navigation elements

pull/4925/head
tugcekucukoglu 2023-12-02 10:02:01 +03:00
parent 3978ab2d16
commit bdb697fed6
1 changed files with 72 additions and 3 deletions

View File

@ -14,12 +14,11 @@
<component v-if="templates.caption" :is="templates.caption" :item="activeItem" /> <component v-if="templates.caption" :is="templates.caption" :item="activeItem" />
</div> </div>
</div> </div>
<ul v-if="showIndicators" :class="cx('indicators')" v-bind="ptm('indicators')"> <ul v-if="showIndicators" ref="indicatorContent" :class="cx('indicators')" v-bind="ptm('indicators')">
<li <li
v-for="(item, index) of value" v-for="(item, index) of value"
:key="`p-galleria-indicator-${index}`" :key="`p-galleria-indicator-${index}`"
:class="cx('indicator', { index })" :class="cx('indicator', { index })"
tabindex="0"
:aria-label="ariaPageLabel(index + 1)" :aria-label="ariaPageLabel(index + 1)"
:aria-selected="activeIndex === index" :aria-selected="activeIndex === index"
:aria-controls="id + '_item_' + index" :aria-controls="id + '_item_' + index"
@ -29,7 +28,7 @@
v-bind="ptm('indicator', getIndicatorPTOptions(index))" v-bind="ptm('indicator', getIndicatorPTOptions(index))"
:data-p-highlight="isIndicatorItemActive(index)" :data-p-highlight="isIndicatorItemActive(index)"
> >
<button v-if="!templates['indicator']" type="button" tabindex="-1" :class="cx('indicatorButton')" v-bind="ptm('indicatorButton', getIndicatorPTOptions(index))"></button> <button v-if="!templates['indicator']" type="button" :tabindex="activeIndex === index ? '0' : '-1'" :class="cx('indicatorButton')" v-bind="ptm('indicatorButton', getIndicatorPTOptions(index))"></button>
<component v-if="templates.indicator" :is="templates.indicator" :index="index" /> <component v-if="templates.indicator" :is="templates.indicator" :index="index" />
</li> </li>
</ul> </ul>
@ -41,6 +40,7 @@ import BaseComponent from 'primevue/basecomponent';
import ChevronLeftIcon from 'primevue/icons/chevronleft'; import ChevronLeftIcon from 'primevue/icons/chevronleft';
import ChevronRightIcon from 'primevue/icons/chevronright'; import ChevronRightIcon from 'primevue/icons/chevronright';
import Ripple from 'primevue/ripple'; import Ripple from 'primevue/ripple';
import { DomHandler } from 'primevue/utils';
export default { export default {
name: 'GalleriaItem', name: 'GalleriaItem',
@ -156,8 +156,32 @@ export default {
event.preventDefault(); event.preventDefault();
break; break;
case 'ArrowRight':
this.onRightKey();
break;
case 'ArrowLeft':
this.onLeftKey();
break;
case 'Home':
this.onHomeKey();
event.preventDefault();
break;
case 'End':
this.onEndKey();
event.preventDefault();
break;
case 'Tab':
this.onTabKey();
break;
case 'ArrowDown': case 'ArrowDown':
case 'ArrowUp': case 'ArrowUp':
case 'PageUp':
case 'PageDown':
event.preventDefault(); event.preventDefault();
break; break;
@ -165,6 +189,51 @@ export default {
break; break;
} }
}, },
onRightKey() {
const indicators = [...DomHandler.find(this.$refs.indicatorContent, '[data-pc-section="indicator"]')];
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, activeIndex + 1 === indicators.length ? indicators.length - 1 : activeIndex + 1);
},
onLeftKey() {
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, activeIndex - 1 <= 0 ? 0 : activeIndex - 1);
},
onHomeKey() {
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, 0);
},
onEndKey() {
const indicators = [...DomHandler.find(this.$refs.indicatorContent, '[data-pc-section="indicator"]')];
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, indicators.length - 1);
},
onTabKey() {
const indicators = [...DomHandler.find(this.$refs.indicatorContent, '[data-pc-section="indicator"]')];
const highlightedIndex = indicators.findIndex((ind) => DomHandler.getAttribute(ind, 'data-p-highlight') === true);
const activeIndicator = DomHandler.findSingle(this.$refs.indicatorContent, '[data-pc-section="indicator"] > button[tabindex="0"]');
const activeIndex = indicators.findIndex((ind) => ind === activeIndicator.parentElement);
indicators[activeIndex].children[0].tabIndex = '-1';
indicators[highlightedIndex].children[0].tabIndex = '0';
},
findFocusedIndicatorIndex() {
const indicators = [...DomHandler.find(this.$refs.indicatorContent, '[data-pc-section="indicator"]')];
const activeIndicator = DomHandler.findSingle(this.$refs.indicatorContent, '[data-pc-section="indicator"] > button[tabindex="0"]');
return indicators.findIndex((ind) => ind === activeIndicator.parentElement);
},
changedFocusedIndicator(prevInd, nextInd) {
const indicators = [...DomHandler.find(this.$refs.indicatorContent, '[data-pc-section="indicator"]')];
indicators[prevInd].children[0].tabIndex = '-1';
indicators[nextInd].children[0].tabIndex = '0';
indicators[nextInd].children[0].focus();
},
isIndicatorItemActive(index) { isIndicatorItemActive(index) {
return this.activeIndex === index; return this.activeIndex === index;
}, },