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 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/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 the `post-megadropdown` by focusing on the first element when it is opened with the Enter key and returning the focus to the trigger when it is closed.
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 @@ -26,6 +26,12 @@ export class PostMegadropdownTrigger {
*/
private slottedButton: HTMLButtonElement | null = null;

/**
* Tracks whether this trigger's dropdown was expanded before a state change.
* Used to determine if this trigger should handle focus when its dropdown closes.
*/
private wasExpanded: boolean = false;

/**
* Watch for changes to the `for` property to validate its type and ensure it is a string.
* @param forValue - The new value of the `for` property.
Expand All @@ -50,13 +56,29 @@ 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();

// Check if the mega dropdown attached to the trigger is expanded or not
document.addEventListener('postToggleMegadropdown', (event: CustomEvent) => {
if ((event.target as HTMLPostMegadropdownElement).id === this.for) {
this.ariaExpanded = event.detail;

if (this.wasExpanded && !this.ariaExpanded) {
setTimeout(() => this.slottedButton?.focus(), 100);
}
this.wasExpanded = this.ariaExpanded;

if (this.slottedButton) {
this.slottedButton.setAttribute('aria-expanded', this.ariaExpanded.toString());
}
Expand All @@ -69,6 +91,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 @@ -83,11 +83,16 @@ export class PostMegadropdown {
*/
@Method()
async hide() {
this.postToggleMegadropdown.emit(false);
this.animationClass = 'slide-out';
PostMegadropdown.activeDropdown = null;
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 All @@ -112,7 +117,7 @@ export class PostMegadropdown {
if (this.animationClass === 'slide-out') {
this.isVisible = false;
this.animationClass = null;
this.postToggleMegadropdown.emit(this.isVisible);
PostMegadropdown.activeDropdown = null;
this.removeOutsideClickListener();
}
}
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