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

chore(header): impoved focus behaviour of megadropdown #4625

Merged
merged 4 commits into from
Feb 18, 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
5 changes: 5 additions & 0 deletions .changeset/blue-eyes-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components': patch
---

Improved keyboard navigation for `post-megadropdown` by auto-focusing first element when opened with Enter key and returning focus to trigger when closed.
schaertim marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export namespace Components {
interface PostMainnavigation {
}
interface PostMegadropdown {
"focusFirst": () => Promise<void>;
/**
* Hides the dropdown with an animation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export class PostMegadropdownTrigger {
}
}

private handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Enter') {
event.preventDefault();
this.handleToggle();
if (this.megadropdown && !this.ariaExpanded) {
setTimeout(() => this.megadropdown.focusFirst(), 100);
}
}
};

componentDidLoad() {
this.validateControlFor();

Expand All @@ -69,6 +79,7 @@ export class PostMegadropdownTrigger {
this.slottedButton.addEventListener('click', () => {
this.handleToggle();
});
this.slottedButton.addEventListener('keydown', this.handleKeyDown);
} else {
console.warn('No button found within post-megadropdown-trigger');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class PostMegadropdown {
private firstFocusableEl: HTMLElement | null;
private lastFocusableEl: HTMLElement | null;

private triggerElement: HTMLPostMegadropdownTriggerElement | null = null;

@State() device: DEVICE_SIZE;

@Element() host: HTMLPostMegadropdownElement;
Expand Down Expand Up @@ -75,6 +77,9 @@ export class PostMegadropdown {
PostMegadropdown.activeDropdown = this;
this.postToggleMegadropdown.emit(this.isVisible);
this.addOutsideClickListener();
this.triggerElement = document.querySelector(
schaertim marked this conversation as resolved.
Show resolved Hide resolved
`post-megadropdown-trigger[for="${this.host.id}"]`,
);
}

/**
Expand All @@ -86,6 +91,11 @@ export class PostMegadropdown {
this.host.removeEventListener('keydown', e => this.keyboardHandler(e));
}

@Method()
async focusFirst() {
this.firstFocusableEl?.focus();
}

connectedCallback() {
this.header = this.host.closest('post-header');
if (this.header) {
Expand Down Expand Up @@ -166,6 +176,13 @@ export class PostMegadropdown {
}
}

private handleBackClick() {
this.hide();
if (this.triggerElement) {
setTimeout(() => this.triggerElement.querySelector('button')?.focus(), 100);
}
}

render() {
const containerStyle = this.isVisible ? {} : { display: 'none' };

Expand All @@ -177,7 +194,7 @@ export class PostMegadropdown {
onAnimationEnd={() => this.handleAnimationEnd()}
>
<div class="megadropdown">
<div onClick={() => this.hide()} class="back-button">
<div onClick={() => this.handleBackClick()} class="back-button">
<slot name="back-button"></slot>
</div>
<div onClick={() => this.hide()} class="close-button">
Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/components/post-megadropdown/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

## Methods

### `focusFirst() => Promise<void>`



#### Returns

Type: `Promise<void>`



### `hide() => Promise<void>`

Hides the dropdown with an animation.
Expand Down
Loading