fix(console): address modal accessibility and dismissal review

This commit is contained in:
harsh mahajan committed 2026-09-09 18:55:34 +05:30
1 parent 306715d62c
commit 39e1b140bd
3 files changed
+369 -44

No files matched your search

+77 -39
View File
@@ -1,14 +1,24 @@
<script lang="ts">
import { base } from '$app/paths';
import { page } from '$app/state';
import { trackEvent } from '$lib/actions/analytics';
import { Button } from '$lib/elements/forms';
import { disableCommands } from '$lib/commandCenter';
import { hideNotification } from '$lib/helpers/notifications';
import NewConsoleCover from '$lib/images/promos/new-console-modal.png';
import { Alert, Icon, Typography } from '@appwrite.io/pink-svelte';
import { Alert, Button, Icon, Typography } from '@appwrite.io/pink-svelte';
import { IconArrowSmRight, IconX } from '@appwrite.io/pink-icons-svelte';
export let show = false;
let { show = $bindable(false) }: { show?: boolean } = $props();
const isOnOnboarding = $derived(
page.url.pathname === `${base}/onboarding` ||
page.url.pathname.startsWith(`${base}/onboarding/`)
);
// TODO: replace with the confirmed retirement date once it is set.
$effect(() => {
$disableCommands(show && !isOnOnboarding);
});
// @todo: replace with the confirmed retirement date once it is set.
const SUNSET_NOTE = 'The old Console will be retired in the coming weeks.';
// utm_medium separates this from the banner and the promo card.
@@ -22,44 +32,53 @@
let recorded = false;
function close(action: 'try' | 'continue') {
if (!recorded) {
recorded = true;
trackEvent('close_new_console_modal', { source: 'new_console_modal', action });
hideNotification('newConsoleModal', {
coolOffPeriod: COOL_OFF_HOURS,
exponentialBackoff: true
});
}
if (!show || recorded) return;
recorded = true;
trackEvent('close_new_console_modal', { source: 'new_console_modal', action });
hideNotification('newConsoleModal', {
coolOffPeriod: COOL_OFF_HOURS,
exponentialBackoff: true
});
show = false;
}
function onKeydown(event: KeyboardEvent) {
if (show && event.key === 'Escape') close('continue');
}
function openDialog(node: HTMLDialogElement) {
const previousFocus = document.activeElement;
recorded = false;
// Native modality contains keyboard focus and makes the rest of the page inert.
node.showModal();
// Moves the reading cursor into the dialog so Escape and the actions are reachable at once.
function focusOnMount(node: HTMLElement) {
node.focus({ preventScroll: true });
return {
destroy() {
node.close();
// Also restore focus when Svelte removes the dialog during teardown.
if (previousFocus instanceof HTMLElement && previousFocus.isConnected) {
previousFocus.focus({ preventScroll: true });
}
}
};
}
</script>
<svelte:window on:keydown={onKeydown} />
{#if show}
<div
{#if show && !isOnOnboarding}
<dialog
class="scrim"
role="presentation"
aria-labelledby="new-console-title"
use:openDialog
oncancel={(event) => {
event.preventDefault();
close('continue');
}}
onkeydown={(event) => {
// Let the native cancel event close only this dialog, not a background wizard.
if (event.key === 'Escape') event.stopPropagation();
}}
onclick={(event) => {
if (event.target === event.currentTarget) close('continue');
}}>
<div
class="dialog"
role="dialog"
tabindex="-1"
aria-modal="true"
aria-labelledby="new-console-title"
use:focusOnMount>
<div class="dialog">
<button
class="dismiss"
type="button"
@@ -87,32 +106,51 @@
</div>
<div class="actions">
<Button
<Button.Anchor
{href}
external
on:click={() => {
size="s"
target="_blank"
rel="noopener noreferrer"
onclick={() => {
trackEvent('click_new_console', { source: 'new_console_modal' });
close('try');
}}>
Take me to the new Console
<Icon icon={IconArrowSmRight} size="s" slot="end" />
</Button>
</Button.Anchor>
<Button text on:click={() => close('continue')}>Stay here for now</Button>
<Button.Button
type="button"
variant="text"
size="s"
onclick={() => close('continue')}>Stay here for now</Button.Button>
</div>
</div>
</div>
</dialog>
{/if}
<style lang="scss">
.scrim {
box-sizing: border-box;
position: fixed;
inset: 0;
z-index: 1000;
display: grid;
place-items: center;
inline-size: 100%;
max-inline-size: 100%;
block-size: 100%;
max-block-size: 100%;
margin: 0;
border: none;
background: transparent;
padding: var(--space-6, 12px);
overflow-y: auto;
}
.scrim[open] {
display: grid;
place-items: center;
}
.scrim::backdrop {
background: var(--overlay-scrim);
backdrop-filter: blur(4px);
}
@@ -0,0 +1,290 @@
import '@testing-library/jest-dom/vitest';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, cleanup, fireEvent, render, screen } from '@testing-library/svelte';
import { page } from '$app/state';
import { trackEvent } from '$lib/actions/analytics';
import { sdk } from '$lib/stores/sdk';
import { user, type Account } from '$lib/stores/user';
import type { Writable } from 'svelte/store';
import NewConsoleModal from './newConsoleModal.svelte';
vi.mock('$lib/actions/analytics', () => ({ trackEvent: vi.fn() }));
vi.mock('$lib/stores/sdk', () => ({
sdk: { forConsole: { account: { updatePrefs: vi.fn().mockResolvedValue({}) } } }
}));
vi.mock('$lib/stores/user', async () => {
const { writable } = await import('svelte/store');
return { user: writable(null) };
});
vi.mock('$lib/commandCenter', async () => {
const { readable } = await import('svelte/store');
return { disableCommands: readable(vi.fn()) };
});
vi.mock('$app/state', async () => {
const { fromStore, writable } = await import('svelte/store');
const url = fromStore(writable(new URL('https://cloud.appwrite.io/console')));
return {
page: {
get url() {
return url.current;
},
set url(value) {
url.current = value;
}
}
};
});
const now = Date.UTC(2026, 8, 9);
const coolOff = 30 * 24 * 60 * 60 * 1000;
const showModal = vi.fn(function (this: HTMLDialogElement) {
this.open = true;
});
const closeDialog = vi.fn(function (this: HTMLDialogElement) {
this.open = false;
});
function setAccount(notificationPrefs = {}) {
(user as Writable<Account>).set({
prefs: { theme: 'dark', notificationPrefs }
} as unknown as Account);
}
function navigate(path: string) {
return act(() => {
Object.assign(page, { url: new URL(path, 'https://cloud.appwrite.io') });
});
}
function expectSnoozed(action: 'continue' | 'try') {
expect(sdk.forConsole.account.updatePrefs).toHaveBeenCalledExactlyOnceWith({
prefs: {
theme: 'dark',
notificationPrefs: {
newConsoleModal: { expiry: now + coolOff, hideCount: 1, state: 'hidden' }
}
}
});
expect(trackEvent).toHaveBeenCalledWith('close_new_console_modal', {
source: 'new_console_modal',
action
});
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(closeDialog).toHaveBeenCalledOnce();
}
describe('new Console modal', () => {
beforeAll(() => {
// jsdom has no dialog API. These shims only expose open/closed state; native focus
// containment, restoration, and Escape delivery require a real browser.
Object.defineProperties(HTMLDialogElement.prototype, {
showModal: { configurable: true, value: showModal },
close: { configurable: true, value: closeDialog }
});
});
beforeEach(async () => {
vi.spyOn(Date, 'now').mockReturnValue(now);
setAccount();
await navigate('/console');
});
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});
afterAll(() => {
delete HTMLDialogElement.prototype.showModal;
delete HTMLDialogElement.prototype.close;
});
it('does not write preferences or analytics when mounted hidden', () => {
render(NewConsoleModal);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(showModal).not.toHaveBeenCalled();
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
expect(trackEvent).not.toHaveBeenCalled();
});
it('opens an accessible modal without snoozing it', () => {
render(NewConsoleModal, { show: true });
expect(screen.getByRole('dialog', { name: 'The new Appwrite Console' })).toHaveAttribute(
'open'
);
expect(showModal).toHaveBeenCalledOnce();
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
expect(trackEvent).not.toHaveBeenCalled();
});
it.each(['Close', 'Stay here for now'])('snoozes once when clicking %s', async (name) => {
render(NewConsoleModal, { show: true });
await fireEvent.click(screen.getByRole('button', { name }));
expectSnoozed('continue');
expect(trackEvent).toHaveBeenCalledOnce();
});
it('snoozes once when Escape requests cancellation', async () => {
render(NewConsoleModal, { show: true });
const cancel = new Event('cancel', { cancelable: true });
await fireEvent(screen.getByRole('dialog'), cancel);
expect(cancel.defaultPrevented).toBe(true);
expectSnoozed('continue');
expect(trackEvent).toHaveBeenCalledOnce();
});
it('keeps Escape from reaching background keyboard handlers without preventing cancellation', async () => {
const backgroundKeydown = vi.fn();
window.addEventListener('keydown', backgroundKeydown);
render(NewConsoleModal, { show: true });
const escape = new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
cancelable: true
});
try {
await fireEvent(screen.getByRole('button', { name: 'Close' }), escape);
expect(backgroundKeydown).not.toHaveBeenCalled();
expect(escape.defaultPrevented).toBe(false);
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
} finally {
window.removeEventListener('keydown', backgroundKeydown);
}
});
it('restores focus to the previously focused control when dismissed', async () => {
const previous = document.createElement('button');
document.body.append(previous);
previous.focus();
render(NewConsoleModal, { show: true });
const dismiss = screen.getByRole('button', { name: 'Close' });
dismiss.focus();
try {
await fireEvent.click(dismiss);
expect(previous).toHaveFocus();
} finally {
previous.remove();
}
});
it('snoozes on a backdrop click but ignores clicks within the dialog content', async () => {
render(NewConsoleModal, { show: true });
await fireEvent.click(screen.getByRole('heading', { name: 'The new Appwrite Console' }));
expect(screen.getByRole('dialog')).toBeInTheDocument();
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
await fireEvent.click(screen.getByRole('dialog'));
expectSnoozed('continue');
expect(trackEvent).toHaveBeenCalledOnce();
});
it('tracks the new Console link and snoozes once when following it', async () => {
render(NewConsoleModal, { show: true });
const link = screen.getByRole('link', { name: 'Take me to the new Console' });
expect(link).toHaveAttribute(
'href',
'https://appwrite.io/?utm_source=old-console&utm_medium=modal&utm_campaign=new-console'
);
expect(link).toHaveAttribute('target', '_blank');
await fireEvent.click(link);
expectSnoozed('try');
expect(trackEvent).toHaveBeenCalledWith('click_new_console', {
source: 'new_console_modal'
});
expect(trackEvent).toHaveBeenCalledTimes(2);
});
it('does not double the snooze when two dismissal events arrive together', async () => {
render(NewConsoleModal, { show: true });
const dialog = screen.getByRole('dialog');
const dismiss = screen.getByRole('button', { name: 'Close' });
await act(() => {
dismiss.click();
dialog.dispatchEvent(new Event('cancel', { cancelable: true }));
});
expectSnoozed('continue');
expect(trackEvent).toHaveBeenCalledOnce();
});
it('doubles the existing cool-off while preserving unrelated preferences', async () => {
const bannerPref = { expiry: now + 1000, hideCount: 1, state: 'hidden' };
setAccount({
newConsoleModal: { expiry: now - 1, hideCount: 2, state: 'shown' },
newConsoleBanner: bannerPref
});
render(NewConsoleModal, { show: true });
await fireEvent.click(screen.getByRole('button', { name: 'Close' }));
expect(sdk.forConsole.account.updatePrefs).toHaveBeenCalledExactlyOnceWith({
prefs: {
theme: 'dark',
notificationPrefs: {
newConsoleModal: { expiry: now + coolOff * 4, hideCount: 3, state: 'hidden' },
newConsoleBanner: bannerPref
}
}
});
});
it('closes silently when the parent hides it and can be shown again', async () => {
const { rerender } = render(NewConsoleModal, { show: true });
await rerender({ show: false });
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(closeDialog).toHaveBeenCalledOnce();
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
expect(trackEvent).not.toHaveBeenCalled();
await rerender({ show: true });
expect(screen.getByRole('dialog')).toHaveAttribute('open');
expect(showModal).toHaveBeenCalledTimes(2);
});
it.each(['/console/onboarding', '/console/onboarding/create-organization'])(
'stays hidden on initial onboarding route %s, then opens after leaving',
async (path) => {
await navigate(path);
render(NewConsoleModal, { show: true });
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(showModal).not.toHaveBeenCalled();
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
await navigate('/console/organization-example');
expect(screen.getByRole('dialog')).toHaveAttribute('open');
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
expect(trackEvent).not.toHaveBeenCalled();
}
);
it('closes without snoozing when navigating into onboarding', async () => {
render(NewConsoleModal, { show: true });
await navigate('/console/onboarding');
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
expect(closeDialog).toHaveBeenCalledOnce();
expect(sdk.forConsole.account.updatePrefs).not.toHaveBeenCalled();
expect(trackEvent).not.toHaveBeenCalled();
});
});
+2 -5
View File
@@ -342,11 +342,8 @@
let showNewConsoleModal = false;
onMount(() => {
// Never during onboarding: a first run is the worst moment to interrupt.
const isOnOnboarding = page.url.pathname.includes(base + '/onboarding');
showNewConsoleModal =
isCloud && !isOnOnboarding && shouldShowNotification('newConsoleModal');
// The modal reacts to onboarding navigation without recording a dismissal.
showNewConsoleModal = isCloud && shouldShowNotification('newConsoleModal');
});
$: (void $headerAlert, ($activeHeaderAlert = headerAlert.getExcluding('impersonation')));