Skip to content
Merged
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
8 changes: 6 additions & 2 deletions packages/ui/src/lib/actions/shortcut.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ActionReturn } from 'svelte/action';
import { on } from 'svelte/events';

export type Shortcut = {
key: string;
Expand Down Expand Up @@ -111,6 +112,9 @@ export const shortcuts = <T extends HTMLElement>(
options: ShortcutOptions<T>[],
): ActionReturn<ShortcutOptions<T>[]> => {
function onKeydown(event: KeyboardEvent) {
if (event.defaultPrevented) {
return;
}
const ignoreShortcut = shouldIgnoreEvent(event);
for (const { shortcut, onShortcut, ignoreInputFields = true, preventDefault = true } of options) {
if (ignoreInputFields && ignoreShortcut) {
Expand All @@ -127,14 +131,14 @@ export const shortcuts = <T extends HTMLElement>(
}
}

node.addEventListener('keydown', onKeydown);
const off = on(node, 'keydown', onKeydown);

return {
update(newOptions) {
options = newOptions;
},
destroy() {
node.removeEventListener('keydown', onKeydown);
off();
},
};
};
7 changes: 6 additions & 1 deletion packages/ui/src/lib/components/Modal/Modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
}
};

const handleEscapeKeydown = (event: KeyboardEvent) => {
event.stopImmediatePropagation();
onEscapeKeydown?.(event);
};

onMount(() => {
layer = modalState.incrementLayer();

Expand All @@ -100,7 +105,7 @@
<Dialog.Portal>
<Dialog.Overlay class="{zIndex.ModalBackdrop} fixed start-0 top-0 flex h-dvh max-h-dvh w-screen bg-black/30" />
<Dialog.Content
{onEscapeKeydown}
onEscapeKeydown={handleEscapeKeydown}
{escapeKeydownBehavior}
{interactOutsideBehavior}
class={cleanClass(modalContentStyles({ size }))}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export * from '$lib/services/translation.svelte.js';
export * from '$lib/types.js';
export * from '$lib/utilities/byte-units.js';
export * from '$lib/utilities/common.js';
export { isModalOpen } from '$lib/state/modal-state.svelte.js';

// site
export * from '$lib/site/constants.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { matchesShortcut, shortcuts, shouldIgnoreEvent } from '$lib/actions/shortcut.js';
import CommandPaletteModal from '$lib/internal/CommandPaletteModal.svelte';
import { modalManager } from '$lib/services/modal-manager.svelte.js';
import { isModalOpen } from '$lib/state/modal-state.svelte.js';
import type { ActionItem, MaybePromise, TranslationProps } from '$lib/types.js';
import { isEnabled } from '$lib/utilities/common.js';
import { asArray, generateId, getSearchString } from '$lib/utilities/internal.js';
import { on } from 'svelte/events';

export type CommandPaletteTranslations = TranslationProps<
| 'search_placeholder'
Expand Down Expand Up @@ -55,7 +57,7 @@ class CommandPaletteManager {
{ shortcut: { key: 'k', ctrl: true }, onShortcut: () => this.open() },
{ shortcut: { key: '/' }, preventDefault: true, onShortcut: () => this.open() },
]);
document.body.addEventListener('keydown', (event) => this.#handleKeydown(event));
on(document.body, 'keydown', (event) => this.#handleKeydown(event));
}
}

Expand All @@ -80,6 +82,10 @@ class CommandPaletteManager {
}

async #handleKeydown(event: KeyboardEvent) {
if (event.defaultPrevented || isModalOpen()) {
return;
}

const actions = await Promise.all(this.#providers.map((provider) => Promise.resolve(provider.onSearch())));

for (const action of actions.flat()) {
Expand All @@ -101,6 +107,7 @@ class CommandPaletteManager {
}

action?.onAction(action);
return;
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/lib/state/modal-state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ class ModalState {
}

export const modalState = new ModalState();
export const isModalOpen = () => modalState.layer > 0;
Loading