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

feat(use-focus): adds focusWithin flag to useFocus hook #164

Open
wants to merge 1 commit 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
22 changes: 20 additions & 2 deletions packages/floating-ui-svelte/src/hooks/use-focus.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getWindow, isElement, isHTMLElement } from "@floating-ui/utils/dom";
import type { HTMLAttributes } from "svelte/elements";
import {
activeElement,
contains,
Expand All @@ -25,6 +26,12 @@ interface UseFocusOptions {
* @default true
*/
visibleOnly?: boolean;
/**
* Whether the open state should change when focusing within the trigger element.
*
* @default false
*/
focusWithin?: boolean;
}

function useFocus(context: FloatingContext, options: UseFocusOptions = {}) {
Expand All @@ -35,7 +42,11 @@ function useFocus(context: FloatingContext, options: UseFocusOptions = {}) {
elements: { reference, floating },
} = $derived(context);

const { enabled = true, visibleOnly = true } = $derived(options);
const {
enabled = true,
visibleOnly = true,
focusWithin = false,
} = $derived(options);

let blockFocus = false;
let timeout = -1;
Expand Down Expand Up @@ -101,7 +112,7 @@ function useFocus(context: FloatingContext, options: UseFocusOptions = {}) {
if (!enabled) {
return {};
}
return {
const handlers: HTMLAttributes<HTMLElement> = {
onpointerdown: (event: PointerEvent) => {
if (isVirtualPointerEvent(event)) return;
keyboardModality = false;
Expand Down Expand Up @@ -174,6 +185,13 @@ function useFocus(context: FloatingContext, options: UseFocusOptions = {}) {
});
},
};

if (focusWithin) {
handlers.onfocusin = handlers.onfocus;
handlers.onfocusout = handlers.onblur;
}

return handlers;
},
};
}
Expand Down
42 changes: 42 additions & 0 deletions packages/floating-ui-svelte/test/hooks/use-focus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { fireEvent, render, screen } from "@testing-library/svelte";
import { userEvent } from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { useFloating, useFocus, useId } from "../../src/index.js";
import { withRunes } from "../internal/with-runes.svelte.js";
import App from "./wrapper-components/use-focus.svelte";

/**
Expand Down Expand Up @@ -102,3 +104,43 @@ describe.skip("useFocus", () => {
});
});
});

describe("focusWithin", () => {
function createElements(): { reference: HTMLElement; floating: HTMLElement } {
const reference = document.createElement("div");
const floating = document.createElement("div");
reference.id = useId();
floating.id = useId();
return { reference, floating };
}

it(
"Should not output onfocusin or onfocusout event handlers",
withRunes(() => {
expect.assertions(2);

const elements = createElements();
const floating = useFloating({ elements });
const focus = useFocus(floating.context);
const handlers = focus.reference;

expect(handlers).not.toHaveProperty("onfocusin");
expect(handlers).not.toHaveProperty("onfocusout");
}),
);

it(
"Should output onfocusin or onfocusout event handlers",
withRunes(() => {
expect.assertions(2);

const elements = createElements();
const floating = useFloating({ elements });
const focus = useFocus(floating.context, { focusWithin: true });
const handlers = focus.reference;

expect(handlers).toHaveProperty("onfocusin");
expect(handlers).toHaveProperty("onfocusout");
}),
);
});