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

Controlled openFocus & closeFocus props #663

Merged
merged 14 commits into from
Oct 25, 2023

Conversation

huntabyte
Copy link
Member

@huntabyte huntabyte commented Oct 24, 2023

This PR introduces two new props, openFocus, and closeFocus to a few of the builders where you might want to override how we handle focus on open and close.

They accept a FocusProp type, which looks like this:

export type FocusTarget = string | HTMLElement | SVGElement | null;
export type FocusProp = FocusTarget | ((defaultEl?: HTMLElement) => FocusTarget);

Whatever is returned from this function will be focused instead of our default behavior. If null is passed, we don't focus on anything and expect you to handle it outside of the builder altogether.

We provide the defaultEl argument as a fallback mechanism, which could be implemented like so:

const customOpenFocus: FocusProp = (defaultEl) => {
 	const customElToFocus = document.getElementById('focusMe')
	if (!customElToFocus) {
		return defaultEl
	}
	return customElToFocus
}

In this scenario, if we aren't able to select our custom element, we'll fall back to the default, ensuring we keep the a11y box checked.

The following builders have both the openFocus and closeFocus props:

  • Dialog
  • Popover

These builders only have the closeFocus:

  • Dropdown Menu
  • Menubar (createMenu returned from the Menubar)
  • Context Menu

This PR also removes the need for the disableTriggerRefocus prop added to the menus in a previous PR.

@changeset-bot
Copy link

changeset-bot bot commented Oct 24, 2023

🦋 Changeset detected

Latest commit: f8f6d89

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@melt-ui/svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel vercel bot temporarily deployed to Preview – melt-ui-storybook October 24, 2023 18:40 Inactive
@vercel vercel bot temporarily deployed to Preview – melt-ui October 24, 2023 19:21 Inactive
src/lib/builders/dialog/create.ts Outdated Show resolved Hide resolved
src/lib/builders/dialog/create.ts Outdated Show resolved Hide resolved
src/lib/builders/menu/create.ts Outdated Show resolved Hide resolved
src/lib/builders/menu/create.ts Outdated Show resolved Hide resolved
src/lib/builders/popover/create.ts Outdated Show resolved Hide resolved
src/lib/builders/popover/create.ts Outdated Show resolved Hide resolved
src/lib/internal/helpers/focus.ts Outdated Show resolved Hide resolved
src/lib/internal/helpers/rovingFocus.ts Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – melt-ui-storybook October 25, 2023 13:38 Inactive
@vercel vercel bot temporarily deployed to Preview – melt-ui October 25, 2023 13:41 Inactive
@vercel vercel bot temporarily deployed to Preview – melt-ui-storybook October 25, 2023 15:33 Inactive
@vercel vercel bot temporarily deployed to Preview – melt-ui October 25, 2023 15:42 Inactive
@vercel vercel bot temporarily deployed to Preview – melt-ui October 25, 2023 15:59 Inactive
@vercel vercel bot temporarily deployed to Preview – melt-ui-storybook October 25, 2023 16:01 Inactive
@huntabyte huntabyte requested a review from TGlide October 25, 2023 16:14
@TGlide TGlide merged commit 1364800 into melt-ui:develop Oct 25, 2023
2 of 3 checks passed
@github-actions github-actions bot mentioned this pull request Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants