Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Jan 19, 2025
1 parent fd6d3a2 commit cc7c431
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { HTMLAttributes } from "svelte/elements";
import { FOCUSABLE_ATTRIBUTE } from "../internal/get-floating-focus-element.js";

const ACTIVE_KEY = "active";
const SELECTED_KEY = "selected";
Expand Down Expand Up @@ -44,7 +45,10 @@ function mergeProps<Key extends keyof ElementProps>(
}

return {
...(elementKey === "floating" && { tabindex: -1 }),
...(elementKey === "floating" && {
tabindex: -1,
[FOCUSABLE_ATTRIBUTE]: "",
}),
...domUserProps,
...propsList
.map((value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export function getFloatingFocusElement(
// This indicates the floating element is acting as a positioning wrapper, and
// so focus should be managed on the child element with the event handlers and
// aria props.
return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE)
const res = floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE)
? floatingElement
: floatingElement.querySelector(`[${FOCUSABLE_ATTRIBUTE}]`) ||
floatingElement;
floatingElement;
console.log(res);
return res as HTMLElement;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script lang="ts">
import FloatingFocusManager from "../../../../src/components/floating-focus-manager/floating-focus-manager.svelte";
import {
useDismiss,
useFloating,
useInteractions,
} from "../../../../src/index.js";
let {
open,
onOpenChange,
}: { open: boolean; onOpenChange: (v: boolean) => void } = $props();
const f = useFloating({
open,
onOpenChange,
});
const ints = useInteractions([useDismiss(f.context)]);
</script>

<FloatingFocusManager context={f.context}>
<div bind:this={f.floating} {...ints.getFloatingProps()}>
<button data-testid="child-reference"> child reference </button>
</div>
</FloatingFocusManager>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script lang="ts">
import FloatingFocusManager from "../../../../src/components/floating-focus-manager/floating-focus-manager.svelte";
import {
useClick,
useDismiss,
useFloating,
useInteractions,
} from "../../../../src/index.js";
import ConnectedDrawer from "./connected-drawer.svelte";
let open = $state(false);
let isDrawerOpen = $state(false);
const f = useFloating({
open: () => open,
onOpenChange: (v) => {
open = v;
},
});
const ints = useInteractions([useClick(f.context), useDismiss(f.context)]);
</script>

<button
bind:this={f.reference}
data-testid="parent-reference"
{...ints.getReferenceProps()}>ref</button>
{#if open}
<FloatingFocusManager context={f.context}>
<div bind:this={f.floating} {...ints.getFloatingProps()}>
Parent Floating
<button
data-testid="parent-floating-reference"
onclick={() => {
isDrawerOpen = true;
open = false;
}}>parent floating ref</button>
</div>
</FloatingFocusManager>
{/if}
{#if isDrawerOpen}
<ConnectedDrawer
open={isDrawerOpen}
onOpenChange={(v) => (isDrawerOpen = v)} />
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script lang="ts">
import FloatingFocusManager from "../../../../src/components/floating-focus-manager/floating-focus-manager.svelte";
import {
useFloating,
useInteractions,
useRole,
} from "../../../../src/index.js";
let open = $state(false);
const f = useFloating({
open: () => open,
onOpenChange: (v) => {
open = v;
},
});
const ints = useInteractions([useRole(f.context)]);
</script>

<button
bind:this={f.reference}
{...ints.getReferenceProps({
onclick: () => (open = !open),
})}>
ref
</button>

{#if open}
<FloatingFocusManager context={f.context}>
<div bind:this={f.floating} data-testid="outer">
<div {...ints.getFloatingProps()} data-testid="inner"></div>
</div>
</FloatingFocusManager>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<script lang="ts">
import FloatingFocusManager from "../../../../src/components/floating-focus-manager/floating-focus-manager.svelte";
import {
useClick,
useFloating,
useInteractions,
} from "../../../../src/index.js";
let { restoreFocus = true }: { restoreFocus?: boolean } = $props();
let open = $state(false);
let removedIndex = $state(0);
const f = useFloating({
open: () => open,
onOpenChange: (v) => {
open = v;
},
});
const ints = useInteractions([useClick(f.context)]);
</script>

<button
bind:this={f.reference}
{...ints.getReferenceProps()}
data-testid="reference">ref</button>
{#if open}
<FloatingFocusManager context={f.context} initialFocus={1} {restoreFocus}>
<div
bind:this={f.floating}
{...ints.getFloatingProps()}
data-testid="floating">
{#if removedIndex < 3}
<button onclick={() => (removedIndex = removedIndex + 1)}>
three
</button>
{/if}
{#if removedIndex < 1}
<button onclick={() => (removedIndex = removedIndex + 1)}>
one
</button>
{/if}
{#if removedIndex < 2}
<button onclick={() => (removedIndex = removedIndex + 1)}>
two
</button>
{/if}
</div>
</FloatingFocusManager>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script lang="ts">
import FloatingFocusManager from "../../../../src/components/floating-focus-manager/floating-focus-manager.svelte";
import {
useClick,
useDismiss,
useFloating,
useInteractions,
useRole,
} from "../../../../src/index.js";
let open = $state(false);
const f = useFloating({
open: () => open,
onOpenChange: (v) => {
open = v;
},
});
const ints = useInteractions([
useRole(f.context),
useDismiss(f.context),
useClick(f.context),
]);
</script>

<div class="App">
<!-- svelte-ignore a11y_role_has_required_aria_props -->
<input
bind:this={f.reference}
{...ints.getReferenceProps()}
data-testid="input"
role="combobox" />
{#if open}
<FloatingFocusManager context={f.context}>
<div
bind:this={f.floating}
style={f.floatingStyles}
{...ints.getFloatingProps()}>
<button>one</button>
<button>two</button>
</div>
</FloatingFocusManager>
{/if}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script lang="ts">
import FloatingFocusManager from "../../../../src/components/floating-focus-manager/floating-focus-manager.svelte";
import FloatingPortal from "../../../../src/components/floating-portal/floating-portal.svelte";
import {
useClick,
useDismiss,
useFloating,
useInteractions,
useRole,
} from "../../../../src/index.js";
let open = $state(false);
const f = useFloating({
open: () => open,
onOpenChange: (v) => {
open = v;
},
});
const ints = useInteractions([
useRole(f.context),
useDismiss(f.context),
useClick(f.context),
]);
</script>

<div class="App">
<!-- svelte-ignore a11y_role_has_required_aria_props -->
<input
bind:this={f.reference}
{...ints.getReferenceProps()}
data-testid="input"
role="combobox" />
{#if open}
<FloatingPortal>
<FloatingFocusManager
context={f.context}
initialFocus={-1}
modal={false}>
<div
bind:this={f.floating}
style={f.floatingStyles}
{...ints.getFloatingProps()}>
<button>one</button>
<button>two</button>
</div>
</FloatingFocusManager>
</FloatingPortal>
{/if}
<button>outside</button>
</div>
Loading

0 comments on commit cc7c431

Please sign in to comment.