anki/ts/deck-options/ConfigSelector.svelte
Ben Olson 9a301665a4
Improve keyboard handling and accessibility for Select.svelte and refactor (#2811)
* resolve TagAddButton a11y
better comments to document tagindex reasoning

* resolved a11y for TagsSelectedButton
allow focus to TagsSelectedButton with Shift+Tab and Enter or Space to show popover

* safely ignore a11y warning as container for interactables is not itself interactable

* Update CONTRIBUTORS

* quick fix syntax

* quick fix syntax

* quick fix syntax

* quick fix syntax

* resolved a11y in accordance with ARIA APG Disclure pattern

* resolved a11y
ideally should replace with  with
a11y-click-events-have-key-events is explicitly ignored as the alternative (adding ) seems more clunky

* resolved SpinBox a11y
cannot focus on these buttons, so no key event handling needed (keyboard editting already possible by just typing in the field)
widget already properly follows ARIA APG Spinbutton pattern

* cleanup

* onEnterOrSpace() function implemented as discussed in #2787 and #2564

* I think this is the main keyboard handling of Select
Still need to fix focus and handle roles and attributes

* fixed the keyboard interaction

focus is janky because you need to wait until after the listed options load and for some reason that needs a tiny delay on onMount
I think this technically violates a11y, but it really doesn't since the delay is literally zero. But the code still needs it to happen.

* Select and SelectOption reference the same focus function

* SelectOption moved inside Select
+ started roles and a11y

* quick syntax and such changes

* finish handling roles and attributes

* fixed keyboard handling and only visual focus

* cleanup and slight refactoring

* fixed syntax

* what even is this?

* bug fixes + revert key selection

* fixed scrolling

* better control scrolling and focus

* Adjusted selection
Up/Down Arrows: start selection on active option
Enter/Space/Click: no initial selection, down arrow to first option, up arrow to last option

* Only set selected the first time Select is opened, all subsequent times use the previous selected
2023-11-21 14:23:18 +10:00

117 lines
3.5 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import * as tr from "@tslib/ftl";
import { noop } from "@tslib/functional";
import type Modal from "bootstrap/js/dist/modal";
import { createEventDispatcher, getContext } from "svelte";
import ButtonToolbar from "../components/ButtonToolbar.svelte";
import { modalsKey } from "../components/context-keys";
import Select from "../components/Select.svelte";
import StickyContainer from "../components/StickyContainer.svelte";
import type { ConfigListEntry, DeckOptionsState } from "./lib";
import SaveButton from "./SaveButton.svelte";
import TextInputModal from "./TextInputModal.svelte";
export let state: DeckOptionsState;
const configList = state.configList;
const dispatch = createEventDispatcher();
const dispatchPresetChange = () => dispatch("presetchange");
$: value = $configList.findIndex((entry) => entry.current);
$: label = configLabel($configList[value]);
function configLabel(entry: ConfigListEntry): string {
const count = tr.deckConfigUsedByDecks({ decks: entry.useCount });
return `${entry.name} (${count})`;
}
function blur(e: CustomEvent): void {
state.setCurrentIndex(e.detail.value);
dispatchPresetChange();
}
function onAddConfig(text: string): void {
const trimmed = text.trim();
if (trimmed.length) {
state.addConfig(trimmed);
dispatchPresetChange();
}
}
function onCloneConfig(text: string): void {
const trimmed = text.trim();
if (trimmed.length) {
state.cloneConfig(trimmed);
dispatchPresetChange();
}
}
function onRenameConfig(text: string): void {
state.setCurrentName(text);
}
const modals = getContext<Map<string, Modal>>(modalsKey);
let modalKey: string;
let modalStartingValue = "";
let modalTitle = "";
let modalSuccess: (text: string) => void = noop;
function promptToAdd() {
modalTitle = tr.deckConfigAddGroup();
modalSuccess = onAddConfig;
modalStartingValue = "";
modals.get(modalKey)!.show();
}
function promptToClone() {
modalTitle = tr.deckConfigCloneGroup();
modalSuccess = onCloneConfig;
modalStartingValue = state.getCurrentName();
modals.get(modalKey)!.show();
}
function promptToRename() {
modalTitle = tr.deckConfigRenameGroup();
modalSuccess = onRenameConfig;
modalStartingValue = state.getCurrentName();
modals.get(modalKey)!.show();
}
</script>
<TextInputModal
title={modalTitle}
prompt={tr.deckConfigNamePrompt()}
value={modalStartingValue}
onOk={modalSuccess}
bind:modalKey
/>
<StickyContainer --gutter-block="0.5rem" --sticky-borders="0 0 1px" breakpoint="sm">
<ButtonToolbar class="justify-content-between flex-grow-1" wrap={false}>
<Select
class="flex-grow-1"
bind:value
{label}
list={$configList}
parser={(entry) => ({
content: configLabel(entry),
value: entry.idx,
})}
on:change={blur}
/>
<SaveButton
{state}
on:add={promptToAdd}
on:clone={promptToClone}
on:rename={promptToRename}
on:remove={dispatchPresetChange}
/>
</ButtonToolbar>
</StickyContainer>