10000 DT-131 - improve keyboard navigation for modals by rossedfort · Pull Request #966 · temporalio/ui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

DT-131 - improve keyboard navigation for modals #966

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 8000 statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 2, 2022
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
18 changes: 15 additions & 3 deletions src/lib/holocene/modal.story.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<script lang="ts">
import type { Hst as HST } from '@histoire/plugin-svelte';
import { logEvent } from 'histoire/client';

import Button from './button.svelte';

import Modal from './modal.svelte';

export let Hst: HST;
let open = true;
let open = false;

const handleConfirm = () => {
logEvent('Confirm', {});
open = false;
};

const handleCancel = () => {
logEvent('Cancel', {});
open = false;
};
</script>

<Hst.Story>
Expand All @@ -14,8 +26,8 @@
bind:open
confirmType="destructive"
confirmText="Delete"
on:confirmModal={() => (open = false)}
on:cancelModal={() => (open = false)}
on:confirmModal={handleConfirm}
on:cancelModal={handleCancel}
>
<h3 slot="title">Delete User</h3>
<p slot="content">
Expand Down
54 changes: 47 additions & 7 deletions src/lib/holocene/modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
export let large: boolean = false;
export let loading: boolean = false;

let modalElement: HTMLDivElement;

const dispatch = createEventDispatcher<{
cancelModal: undefined;
confirmModal: undefined;
Expand All @@ -21,32 +23,70 @@
dispatch('cancelModal');
};

const handleKeyUp = (event: KeyboardEvent) => {
const handleKeyboardNavigation = (event: KeyboardEvent) => {
if (!open) {
return;
}

if (event.key === 'Escape') {
cancelModal();
return;
}

const focusable = modalElement.querySelectorAll('button');
const firstFocusable = focusable[0];
const lastFocusable = focusable[focusable.length - 1];
if (event.key === 'Tab') {
if (event.shiftKey) {
if (document.activeElement === firstFocusable) {
lastFocusable.focus();
event.preventDefault();
}
} else if (document.activeElement === lastFocusable) {
firstFocusable.focus();
event.preventDefault();
}
}
};

$: {
if (open && modalElement) {
modalElement.focus();
}
}
</script>

<svelte:window on:keyup={handleKeyUp} />
<svelte:window on:keydown={handleKeyboardNavigation} />
{#if open}
<div class="modal">
<div on:click={cancelModal} class="overlay" />
<div class="body" class:large>
<div
bind:this={modalElement}
class="body"
class:large
tabindex="-1"
role="alertdialog"
aria-labelledby="modal-title"
aria-describedby="modal-description"
>
{#if !loading}
<div class="float-right p-6" on:click={cancelModal}>
<button
aria-label={cancelText}
class="float-right m-4"
on:click={cancelModal}
>
<Icon
name="close"
class="cursor-pointer rounded-full hover:bg-gray-900 hover:text-white"
/>
</div>
</button>
{/if}
<div class="title">
<div id="modal-title" class="title">
<slot name="title">
<h3>Title</h3>
</slot>
</div>
<div class="content">
<div id="modal-content" class="content">
<slot name="content">
<span>Content</span>
</slot>
Expand Down
0