Skip to content
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

fix(focus): fix focus order in post-megadropdown component #4379

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/quick-suits-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components': patch
---

Fixed focus order in `post-megadropdown` component.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ post-popovercontainer {

.megadropdown {
padding: 31px 40px 40px;
display: flex;
flex-direction: column;

@include media.max(lg) {
padding: 16px 32px 24px;
Expand Down Expand Up @@ -92,6 +94,7 @@ post-popovercontainer {

a {
text-decoration: none;
display: none;
font-weight: inherit;
border-bottom: 0;
border-top: 0;
Expand Down Expand Up @@ -207,6 +210,7 @@ post-popovercontainer {
}

@include media.max(lg) {
order: -1;
display: block;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getFocusableChildren } from '@/utils/get-focusable-children';
import { Component, Element, Event, EventEmitter, h, Host, Method, State } from '@stencil/core';

@Component({
Expand Down Expand Up @@ -53,11 +54,28 @@ export class PostMegadropdown {
*/
@Method()
async show(target: HTMLElement) {
if (this.popoverRef) {
await this.popoverRef.show(target);
this.animationClass = 'slide-in';
} else {
if (!this.popoverRef) {
console.error('show: popoverRef is null or undefined');
return;
}

await this.popoverRef.show(target);
this.animationClass = 'slide-in';

const megadropdownItems = this.getFocusableElementsInMegadropdown();

if (megadropdownItems.length > 0) {
const visibleItem = megadropdownItems.find(
(item) => window.getComputedStyle(item).display !== 'none'
);

if (visibleItem) {
visibleItem.focus();
} else {
console.warn('No visible focusable items found in the megadropdown.');
}
} else {
console.warn('No focusable items found in the megadropdown.');
}
}

Expand Down Expand Up @@ -88,6 +106,18 @@ export class PostMegadropdown {
}
}

private getFocusableElementsInMegadropdown(): HTMLElement[] {
const megadropdownContainer = this.host.querySelector('.megadropdown');

const allChildren = Array.from(megadropdownContainer.querySelectorAll('*'));
return allChildren
.flatMap(el => Array.from(getFocusableChildren(el)))
.filter(child =>
!child.classList.contains('back-button') &&
!child.classList.contains('close-button')
);
}

render() {
return (
<Host>
Expand All @@ -98,16 +128,16 @@ export class PostMegadropdown {
ref={el => (this.popoverRef = el)}
>
<div class="megadropdown" onFocusout={e => this.handleFocusout(e)}>
<slot name="megadropdown-title"></slot>
leagrdv marked this conversation as resolved.
Show resolved Hide resolved
<div class="megadropdown-content">
<slot></slot>
</div>
<div onClick={() => this.handleBackButtonClick()} class="back-button">
<slot name="back-button"></slot>
</div>
<div onClick={() => this.handleCloseButtonClick()} class="close-button">
<slot name="close-button"></slot>
</div>
<slot name="megadropdown-title"></slot>
<div class="megadropdown-content">
<slot></slot>
</div>
</div>
</post-popovercontainer>
</Host>
Expand Down
Loading