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

Modal should return the focus to the triggering element after close #1865

Closed
hugotox opened this issue Aug 14, 2023 · 7 comments
Closed

Modal should return the focus to the triggering element after close #1865

hugotox opened this issue Aug 14, 2023 · 7 comments
Labels
feature request Request a feature or introduce and update to the project.
Milestone

Comments

@hugotox
Copy link

hugotox commented Aug 14, 2023

Describe the feature in detail (code, mocks, or screenshots encouraged)

This is an accessibility requirement: when the modal is closed, the focus should return to the previously focused element, usually the element that triggered opening the modal.

What type of pull request would this be?

Enhancement

Provide relevant links or additional information.

WAI Example https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/examples/dialog/

MDN docs https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-modal

@hugotox hugotox added the feature request Request a feature or introduce and update to the project. label Aug 14, 2023
@endigo9740
Copy link
Contributor

endigo9740 commented Aug 14, 2023

Agreed, though we'll have to make considerations for how this plays out when multiple modals are queued from multiple sources. Not every modal has a trigger element on the page. For example, some modals may be generated programmatically, such as an error modal when a server call fails.

@aaroncrockett
Copy link
Contributor

I'd gladly take a look at this.

@aaroncrockett
Copy link
Contributor

In order to ensure the modal was triggered by an element in the DOM to return focus to, how do you feel about passing in the trigger element as setting?

In regards to what @endigo9740 brought, if a modal is triggered programmatically would we want to return focus to the last focus element, period? Would that be unexpected?

Here is what I worked out as a setting, which I can put into a PR if that seems the correct direction.

types.ts

export interface ModalSettings {
	/** Provide recommended trigger element, if one exists, to return focus when modal closes */
	triggerElement?: HTMLElement;
	function modalDemo(event: MouseEvent): void {
		const modal: ModalSettings = {
			type: 'alert',
			title: 'Hello Skeleton',
			body: 'This modal example includes a title, body, and image.',
			image: 'https://i.imgur.com/TykCy5e.gif',
			triggerElement: event.target as HTMLElement | undefined
		};
		modalStore.trigger(modal);
	}
		close: () =>
			update((mStore) => {
				if (mStore.length > 0) {
					const modal = mStore.shift();
					/**  Return focus to the element that triggered the modal. */
					if (modal?.triggerElement && document.body.contains(modal.triggerElement)) {
						modal.triggerElement.focus();
					}
				}
				return mStore;
			}),

@endigo9740
Copy link
Contributor

endigo9740 commented Oct 25, 2023

@aaroncrockett Yeah I think that'll be the only sane way to go about it. My only suggestion is to use trigger for the key name - the type is already define to support the type (HTML element) and JSDocs can clarify this as well.

Per the type - I'm going to ping @AdrianGonz97 and @ryceg to share their opinions. I'm not sure if we'll want to cast using as HTML and I don't believe undefined should be needed there. But they can confirm for sure.

@aaroncrockett
Copy link
Contributor

@endigo9740 Checking in with an update -- I will be able to look at this again this week probably -- before Friday and I will also look more into the types.

@doic
Copy link
Contributor

doic commented Nov 28, 2023

I'm curious about the status of this feature

@endigo9740
Copy link
Contributor

In an effort to prepare for Skeleton v3, we're consolidate some related issues down to a single ticket. This will ensure that we can see the full context of requests when the time comes to refactor and update this feature going forward. If you wish to add additional feedback or suggestions, please so here:

@endigo9740 endigo9740 modified the milestones: v3.0 (Next), v2.0 Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request a feature or introduce and update to the project.
Projects
None yet
Development

No branches or pull requests

4 participants