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(web): keyboard access for search dropdown, combobox fixes #8079

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions web/src/lib/components/shared-components/combobox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
};

const onClear = () => {
deactivate();
ben-basten marked this conversation as resolved.
Show resolved Hide resolved
selectedOption = undefined;
searchQuery = '';
dispatch('select', selectedOption);
Expand All @@ -110,12 +111,7 @@
<label class="text-sm text-black dark:text-white" class:sr-only={hideLabel} for={inputId}>{label}</label>
<div
class="relative w-full dark:text-gray-300 text-gray-700 text-base"
use:clickOutside={{ onOutclick: deactivate }}
on:focusout={(e) => {
if (e.relatedTarget instanceof Node && !e.currentTarget.contains(e.relatedTarget)) {
deactivate();
}
}}
use:clickOutside={{ onOutclick: deactivate, onFocusOut: deactivate }}
>
<div>
{#if isActive}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
}}
/>

<div class="w-full relative" use:clickOutside={{ onOutclick: onFocusOut }}>
<div class="w-full relative" use:clickOutside={{ onOutclick: onFocusOut, onFocusOut }}>
<form
draggable="false"
autocomplete="off"
Expand Down Expand Up @@ -122,11 +122,11 @@
? 'rounded-t-3xl border border-gray-200 bg-white dark:border-gray-800'
: 'rounded-3xl border border-transparent bg-gray-200'}"
placeholder="Search your photos"
required
ben-basten marked this conversation as resolved.
Show resolved Hide resolved
pattern="^(?!m:$).*$"
bind:value
bind:this={input}
on:click={onFocusIn}
on:focus={onFocusIn}
disabled={showFilter}
use:shortcut={{
shortcut: { key: 'Escape' },
Expand Down
11 changes: 10 additions & 1 deletion web/src/lib/utils/click-outside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ interface Attributes {
interface Options {
onOutclick?: () => void;
onEscape?: () => void;
onFocusOut?: () => void;
}

export function clickOutside(node: HTMLElement, options: Options = {}): ActionReturn<void, Attributes> {
const { onOutclick, onEscape } = options;
const { onOutclick, onEscape, onFocusOut } = options;

const handleClick = (event: MouseEvent) => {
const targetNode = event.target as Node | null;
Expand Down Expand Up @@ -42,13 +43,21 @@ export function clickOutside(node: HTMLElement, options: Options = {}): ActionRe
}
};

const handleFocusOut = (event: FocusEvent) => {
if (onFocusOut && event.relatedTarget instanceof Node && !node.contains(event.relatedTarget as Node)) {
onFocusOut();
ben-basten marked this conversation as resolved.
Show resolved Hide resolved
}
};

document.addEventListener('click', handleClick, true);
document.addEventListener('keydown', handleKey, true);
node.addEventListener('focusout', handleFocusOut);

return {
destroy() {
document.removeEventListener('click', handleClick, true);
document.removeEventListener('keydown', handleKey, true);
node.removeEventListener('focusout', handleFocusOut);
},
};
}