anki/ts/components/Select.svelte

298 lines
8.2 KiB
Svelte
Raw Normal View History

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
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 05:23:18 +01:00
import { altPressed, isArrowDown, isArrowUp } from "@tslib/keys";
import { createEventDispatcher, setContext } from "svelte";
import { writable } from "svelte/store";
import { selectKey } from "./context-keys";
import IconConstrain from "./IconConstrain.svelte";
import { chevronDown } from "./icons";
import Popover from "./Popover.svelte";
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 05:23:18 +01:00
import SelectOption from "./SelectOption.svelte";
import WithFloating from "./WithFloating.svelte";
Update to Svelte 4, and update most other JS deps (#2565) * eslint-plugin-svelte3 -> eslint-plugin-svelte The former is deprecated, and blocks an update to Svelte 4. Also drop unused svelte2tsx and types package. * Drop unused symbols code for now It may be added back in the future, but for now dropping it will save 200k from our editor bundle. * Remove sass and caniuse-lite pins The latter no longer seems to be required. The former was added to suppress deprecation warnings when compiling the old bootstrap version we have pinned. Those are hidden by the build tool now (though we really need to address them at one point: https://github.com/ankitects/anki/issues/1385) Also removed unused files section. * Prevent proto compile from looking in node_modules/@types/sass When deps are updated, tsc aborts because @types/sass is a dummy package without an index.d.ts file. * Filter Svelte warnings out of ./run * Update to latest Bootstrap This fixes the deprecation warnings we were getting during build: bootstrap doesn't accept runtime CSS variables being set in Sass, as it wants to apply transforms to the colors. Closes #1385 * Start port to Svelte 4 - svelte-check tests have a bunch of failures; ./run works - Svelte no longer exposes internals, so we can't use create_in_transition - Also update esbuild and related components like esbuild-svelte * Fix test failures Had to add some more a11y warning ignores - have added https://github.com/ankitects/anki/issues/2564 to address that in the future. * Remove some dependency pins + Remove sass, we don't need it directly * Bump remaining JS deps that have a current semver * Upgrade dprint/license-checker/marked The new helper method avoids marked printing deprecation warnings to the console. Also remove unused lodash/long types, and move lodahs-es to devdeps * Upgrade eslint and fluent packages * Update @floating-ui/dom The only dependencies remaining are currently blocked: - Jest 29 gives some error about require vs import; may not be worth investigating if we switch to Deno for the tests - CodeMirror 6 is a big API change and will need work. * Roll dprint back to an earlier version GitHub dropped support for Ubuntu 18 runners, causing dprint's artifacts to require a glibc version greater than what Anki CI currently has.
2023-07-01 08:21:53 +02:00
// eslint-disable
Migrate to protobuf-es (#2547) * Fix .no-reduce-motion missing from graphs spinner, and not being honored * Begin migration from protobuf.js -> protobuf-es Motivation: - Protobuf-es has a nicer API: messages are represented as classes, and fields which should exist are not marked as nullable. - As it uses modules, only the proto messages we actually use get included in our bundle output. Protobuf.js put everything in a namespace, which prevented tree-shaking, and made it awkward to access inner messages. - ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff is slower decoding/encoding (#2043), but that was mainly a concern for the graphs page, and was unblocked by https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78 Approach/notes: - We generate the new protobuf-es interface in addition to existing protobuf.js interface, so we can migrate a module at a time, starting with the graphs module. - rslib:proto now generates RPC methods for TS in addition to the Python interface. The input-arg-unrolling behaviour of the Python generation is not required here, as we declare the input arg as a PlainMessage<T>, which marks it as requiring all fields to be provided. - i64 is represented as bigint in protobuf-es. We were using a patch to protobuf.js to get it to output Javascript numbers instead of long.js types, but now that our supported browser versions support bigint, it's probably worth biting the bullet and migrating to bigint use. Our IDs fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future fields we add. - Oneofs are handled differently in protobuf-es, and are going to need some refactoring. Other notable changes: - Added a --mkdir arg to our build runner, so we can create a dir easily during the build on Windows. - Simplified the preference handling code, by wrapping the preferences in an outer store, instead of a separate store for each individual preference. This means a change to one preference will trigger a redraw of all components that depend on the preference store, but the redrawing is cheap after moving the data processing to Rust, and it makes the code easier to follow. - Drop async(Reactive).ts in favour of more explicit handling with await blocks/updating. - Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding dependencies to parent groups. Renamed add() -> add_action() for clarity. * Remove a couple of unused proto imports * Migrate card info * Migrate congrats, image occlusion, and tag editor + Fix imports for multi-word proto files. * Migrate change-notetype * Migrate deck options * Bump target to es2020; simplify ts lib list Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome on Android support the full es2017-es2020 features. * Migrate import-csv * Migrate i18n and fix missing output types in .js * Migrate custom scheduling, and remove protobuf.js To mostly maintain our old API contract, we make use of protobuf-es's ability to convert to JSON, which follows the same format as protobuf.js did. It doesn't cover all case: users who were previously changing the variant of a type will need to update their code, as assigning to a new variant no longer automatically removes the old one, which will cause an error when we try to convert back from JSON. But I suspect the large majority of users are adjusting the current variant rather than creating a new one, and this saves us having to write proxy wrappers, so it seems like a reasonable compromise. One other change I made at the same time was to rename value->kind for the oneofs in our custom study protos, as 'value' was easily confused with the 'case/value' output that protobuf-es has. With protobuf.js codegen removed, touching a proto file and invoking ./run drops from about 8s to 6s. This closes #2043. * Allow tree-shaking on protobuf types * Display backend error messages in our ts alert() * Make sourcemap generation opt-in for ts-run Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
type T = $$Generic;
let className = "";
export { className as class };
export let disabled = false;
export let label = "<br>";
Migrate to protobuf-es (#2547) * Fix .no-reduce-motion missing from graphs spinner, and not being honored * Begin migration from protobuf.js -> protobuf-es Motivation: - Protobuf-es has a nicer API: messages are represented as classes, and fields which should exist are not marked as nullable. - As it uses modules, only the proto messages we actually use get included in our bundle output. Protobuf.js put everything in a namespace, which prevented tree-shaking, and made it awkward to access inner messages. - ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff is slower decoding/encoding (#2043), but that was mainly a concern for the graphs page, and was unblocked by https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78 Approach/notes: - We generate the new protobuf-es interface in addition to existing protobuf.js interface, so we can migrate a module at a time, starting with the graphs module. - rslib:proto now generates RPC methods for TS in addition to the Python interface. The input-arg-unrolling behaviour of the Python generation is not required here, as we declare the input arg as a PlainMessage<T>, which marks it as requiring all fields to be provided. - i64 is represented as bigint in protobuf-es. We were using a patch to protobuf.js to get it to output Javascript numbers instead of long.js types, but now that our supported browser versions support bigint, it's probably worth biting the bullet and migrating to bigint use. Our IDs fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future fields we add. - Oneofs are handled differently in protobuf-es, and are going to need some refactoring. Other notable changes: - Added a --mkdir arg to our build runner, so we can create a dir easily during the build on Windows. - Simplified the preference handling code, by wrapping the preferences in an outer store, instead of a separate store for each individual preference. This means a change to one preference will trigger a redraw of all components that depend on the preference store, but the redrawing is cheap after moving the data processing to Rust, and it makes the code easier to follow. - Drop async(Reactive).ts in favour of more explicit handling with await blocks/updating. - Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding dependencies to parent groups. Renamed add() -> add_action() for clarity. * Remove a couple of unused proto imports * Migrate card info * Migrate congrats, image occlusion, and tag editor + Fix imports for multi-word proto files. * Migrate change-notetype * Migrate deck options * Bump target to es2020; simplify ts lib list Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome on Android support the full es2017-es2020 features. * Migrate import-csv * Migrate i18n and fix missing output types in .js * Migrate custom scheduling, and remove protobuf.js To mostly maintain our old API contract, we make use of protobuf-es's ability to convert to JSON, which follows the same format as protobuf.js did. It doesn't cover all case: users who were previously changing the variant of a type will need to update their code, as assigning to a new variant no longer automatically removes the old one, which will cause an error when we try to convert back from JSON. But I suspect the large majority of users are adjusting the current variant rather than creating a new one, and this saves us having to write proxy wrappers, so it seems like a reasonable compromise. One other change I made at the same time was to rename value->kind for the oneofs in our custom study protos, as 'value' was easily confused with the 'case/value' output that protobuf-es has. With protobuf.js codegen removed, touching a proto file and invoking ./run drops from about 8s to 6s. This closes #2043. * Allow tree-shaking on protobuf types * Display backend error messages in our ts alert() * Make sourcemap generation opt-in for ts-run Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
export let value: T;
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 05:23:18 +01:00
// E may need to derive content, but we default to them being the same for convenience of usage
type E = $$Generic;
type C = $$Generic;
let selected: number | undefined = undefined;
let initialSelected: number;
export let list: E[];
export let parser: (item: E) => { content: C; value?: T; disabled?: boolean } = (
item,
) => {
return {
content: item as unknown as C,
};
};
const parsed = list
.map(parser)
.map(({ content, value: initialValue, disabled = false }, i) => {
if ((initialValue === undefined && i === value) || initialValue === value) {
initialSelected = i;
}
return {
content,
parsedValue: initialValue === undefined ? (i as T) : initialValue,
disabled,
};
});
const buttons: HTMLButtonElement[] = Array(list.length);
const last = list.length - 1;
const ids = {
popover: "popover",
focused: "focused",
};
export let id: string | undefined = undefined;
const dispatch = createEventDispatcher();
Migrate to protobuf-es (#2547) * Fix .no-reduce-motion missing from graphs spinner, and not being honored * Begin migration from protobuf.js -> protobuf-es Motivation: - Protobuf-es has a nicer API: messages are represented as classes, and fields which should exist are not marked as nullable. - As it uses modules, only the proto messages we actually use get included in our bundle output. Protobuf.js put everything in a namespace, which prevented tree-shaking, and made it awkward to access inner messages. - ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff is slower decoding/encoding (#2043), but that was mainly a concern for the graphs page, and was unblocked by https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78 Approach/notes: - We generate the new protobuf-es interface in addition to existing protobuf.js interface, so we can migrate a module at a time, starting with the graphs module. - rslib:proto now generates RPC methods for TS in addition to the Python interface. The input-arg-unrolling behaviour of the Python generation is not required here, as we declare the input arg as a PlainMessage<T>, which marks it as requiring all fields to be provided. - i64 is represented as bigint in protobuf-es. We were using a patch to protobuf.js to get it to output Javascript numbers instead of long.js types, but now that our supported browser versions support bigint, it's probably worth biting the bullet and migrating to bigint use. Our IDs fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future fields we add. - Oneofs are handled differently in protobuf-es, and are going to need some refactoring. Other notable changes: - Added a --mkdir arg to our build runner, so we can create a dir easily during the build on Windows. - Simplified the preference handling code, by wrapping the preferences in an outer store, instead of a separate store for each individual preference. This means a change to one preference will trigger a redraw of all components that depend on the preference store, but the redrawing is cheap after moving the data processing to Rust, and it makes the code easier to follow. - Drop async(Reactive).ts in favour of more explicit handling with await blocks/updating. - Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding dependencies to parent groups. Renamed add() -> add_action() for clarity. * Remove a couple of unused proto imports * Migrate card info * Migrate congrats, image occlusion, and tag editor + Fix imports for multi-word proto files. * Migrate change-notetype * Migrate deck options * Bump target to es2020; simplify ts lib list Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome on Android support the full es2017-es2020 features. * Migrate import-csv * Migrate i18n and fix missing output types in .js * Migrate custom scheduling, and remove protobuf.js To mostly maintain our old API contract, we make use of protobuf-es's ability to convert to JSON, which follows the same format as protobuf.js did. It doesn't cover all case: users who were previously changing the variant of a type will need to update their code, as assigning to a new variant no longer automatically removes the old one, which will cause an error when we try to convert back from JSON. But I suspect the large majority of users are adjusting the current variant rather than creating a new one, and this saves us having to write proxy wrappers, so it seems like a reasonable compromise. One other change I made at the same time was to rename value->kind for the oneofs in our custom study protos, as 'value' was easily confused with the 'case/value' output that protobuf-es has. With protobuf.js codegen removed, touching a proto file and invoking ./run drops from about 8s to 6s. This closes #2043. * Allow tree-shaking on protobuf types * Display backend error messages in our ts alert() * Make sourcemap generation opt-in for ts-run Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
function setValue(v: T) {
value = v;
dispatch("change", { value });
}
export let element: HTMLElement | undefined = undefined;
export let tooltip: string | undefined = undefined;
const rtl: boolean = window.getComputedStyle(document.body).direction == "rtl";
let hover = false;
let showFloating = false;
let clientWidth: number;
const selectStore = writable({ value, setValue });
$: $selectStore.value = value;
setContext(selectKey, selectStore);
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 05:23:18 +01:00
function onKeyDown(event: KeyboardEvent) {
// In accordance with ARIA APG combobox (https://www.w3.org/WAI/ARIA/apg/patterns/combobox/)
const arrowDown = isArrowDown(event);
const arrowUp = isArrowUp(event);
const alt = altPressed(event);
if (arrowDown || arrowUp || event.code === "Space") {
event.preventDefault();
}
if (
!showFloating &&
((arrowDown && alt) ||
event.code === "Enter" ||
event.code === "Space" ||
arrowDown ||
event.code === "Home" ||
arrowUp ||
event.code === "End")
) {
showFloating = true;
if (selected === undefined) {
selected = initialSelected;
}
return;
}
if (selected === undefined) {
return;
}
if (
event.code === "Enter" ||
event.code === "Space" ||
event.code === "Tab" ||
(arrowUp && alt)
) {
showFloating = false;
setValue(parsed[selected].parsedValue);
} else if (arrowUp) {
if (selected < 0) {
selected = last + 1;
}
selectFocus(selected - 1);
} else if (arrowDown) {
selectFocus(selected + 1);
} else if (event.code === "Escape") {
// TODO This doesn't work as the window typically catches the Escape as well
// and closes the window
// - qt/aqt/browser/browser.py:377
showFloating = false;
} else if (event.code === "Home") {
selectFocus(0);
} else if (event.code === "End") {
selectFocus(last);
}
}
function revealed() {
clientWidth = element?.clientWidth ?? 150;
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 05:23:18 +01:00
if (selected === undefined) {
return;
}
setTimeout(selectFocus, 0, selected);
}
/**
* Focus on an option.
* Values outside the range clip to either end
* @param num index number to focus on
*/
function selectFocus(num: number) {
if (selected === -2) {
selected = -1;
return;
}
if (num < 0) {
num = 0;
} else if (num > last) {
num = last;
}
if (selected !== undefined && 0 <= selected && selected <= last) {
buttons[selected].classList.remove("focus");
}
if (num >= 0) {
const el = buttons[num];
el.classList.add("focus");
if (!isScrolledIntoView(el)) {
el.scrollIntoView();
}
}
selected = num;
}
function isScrolledIntoView(el: HTMLElement) {
// This could probably be a helper function of some sort, I don't know where to put it
const rect = el.getBoundingClientRect();
return rect.top >= 0 && rect.bottom <= window.innerHeight;
}
</script>
<WithFloating
show={showFloating}
offset={0}
shift={0}
hideArrow
inline
closeOnInsideClick
keepOnKeyup
on:close={() => (showFloating = false)}
let:asReference
>
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 05:23:18 +01:00
<!-- TODO implement aria-label with semantic label -->
<div
{id}
class="{className} select-container"
class:rtl
class:hover
Update to Svelte 4, and update most other JS deps (#2565) * eslint-plugin-svelte3 -> eslint-plugin-svelte The former is deprecated, and blocks an update to Svelte 4. Also drop unused svelte2tsx and types package. * Drop unused symbols code for now It may be added back in the future, but for now dropping it will save 200k from our editor bundle. * Remove sass and caniuse-lite pins The latter no longer seems to be required. The former was added to suppress deprecation warnings when compiling the old bootstrap version we have pinned. Those are hidden by the build tool now (though we really need to address them at one point: https://github.com/ankitects/anki/issues/1385) Also removed unused files section. * Prevent proto compile from looking in node_modules/@types/sass When deps are updated, tsc aborts because @types/sass is a dummy package without an index.d.ts file. * Filter Svelte warnings out of ./run * Update to latest Bootstrap This fixes the deprecation warnings we were getting during build: bootstrap doesn't accept runtime CSS variables being set in Sass, as it wants to apply transforms to the colors. Closes #1385 * Start port to Svelte 4 - svelte-check tests have a bunch of failures; ./run works - Svelte no longer exposes internals, so we can't use create_in_transition - Also update esbuild and related components like esbuild-svelte * Fix test failures Had to add some more a11y warning ignores - have added https://github.com/ankitects/anki/issues/2564 to address that in the future. * Remove some dependency pins + Remove sass, we don't need it directly * Bump remaining JS deps that have a current semver * Upgrade dprint/license-checker/marked The new helper method avoids marked printing deprecation warnings to the console. Also remove unused lodash/long types, and move lodahs-es to devdeps * Upgrade eslint and fluent packages * Update @floating-ui/dom The only dependencies remaining are currently blocked: - Jest 29 gives some error about require vs import; may not be worth investigating if we switch to Deno for the tests - CodeMirror 6 is a big API change and will need work. * Roll dprint back to an earlier version GitHub dropped support for Ubuntu 18 runners, causing dprint's artifacts to require a glibc version greater than what Anki CI currently has.
2023-07-01 08:21:53 +02:00
class:disabled
title={tooltip}
tabindex="0"
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 05:23:18 +01:00
role="combobox"
aria-controls={ids.popover}
aria-expanded={showFloating}
aria-activedescendant={ids.focused}
on:keydown={onKeyDown}
on:mouseenter={() => (hover = true)}
on:mouseleave={() => (hover = false)}
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 05:23:18 +01:00
on:click={() => {
if (selected === undefined) {
selected = initialSelected;
}
showFloating = !showFloating;
}}
bind:this={element}
use:asReference
>
<div class="inner">
<div class="label">{@html label}</div>
</div>
<div class="chevron">
<IconConstrain iconSize={80}>
{@html chevronDown}
</IconConstrain>
</div>
</div>
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 05:23:18 +01:00
<Popover
slot="floating"
scrollable
--popover-width="{clientWidth}px"
id={ids.popover}
on:revealed={revealed}
>
{#each parsed as { content, parsedValue, disabled }, idx (idx)}
<SelectOption
value={parsedValue}
bind:element={buttons[idx]}
{disabled}
selected={idx === selected}
id={ids.focused}
>
{content}
</SelectOption>
{/each}
</Popover>
</WithFloating>
<style lang="scss">
@use "sass/button-mixins" as button;
$padding-inline: 0.5rem;
.select-container {
@include button.select($with-disabled: false);
line-height: 1.5;
height: 100%;
position: relative;
display: flex;
flex-flow: row;
justify-content: space-between;
.inner {
flex-grow: 1;
position: relative;
.label {
position: absolute;
top: 0;
right: $padding-inline;
bottom: 0;
left: $padding-inline;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
}
Update to Svelte 4, and update most other JS deps (#2565) * eslint-plugin-svelte3 -> eslint-plugin-svelte The former is deprecated, and blocks an update to Svelte 4. Also drop unused svelte2tsx and types package. * Drop unused symbols code for now It may be added back in the future, but for now dropping it will save 200k from our editor bundle. * Remove sass and caniuse-lite pins The latter no longer seems to be required. The former was added to suppress deprecation warnings when compiling the old bootstrap version we have pinned. Those are hidden by the build tool now (though we really need to address them at one point: https://github.com/ankitects/anki/issues/1385) Also removed unused files section. * Prevent proto compile from looking in node_modules/@types/sass When deps are updated, tsc aborts because @types/sass is a dummy package without an index.d.ts file. * Filter Svelte warnings out of ./run * Update to latest Bootstrap This fixes the deprecation warnings we were getting during build: bootstrap doesn't accept runtime CSS variables being set in Sass, as it wants to apply transforms to the colors. Closes #1385 * Start port to Svelte 4 - svelte-check tests have a bunch of failures; ./run works - Svelte no longer exposes internals, so we can't use create_in_transition - Also update esbuild and related components like esbuild-svelte * Fix test failures Had to add some more a11y warning ignores - have added https://github.com/ankitects/anki/issues/2564 to address that in the future. * Remove some dependency pins + Remove sass, we don't need it directly * Bump remaining JS deps that have a current semver * Upgrade dprint/license-checker/marked The new helper method avoids marked printing deprecation warnings to the console. Also remove unused lodash/long types, and move lodahs-es to devdeps * Upgrade eslint and fluent packages * Update @floating-ui/dom The only dependencies remaining are currently blocked: - Jest 29 gives some error about require vs import; may not be worth investigating if we switch to Deno for the tests - CodeMirror 6 is a big API change and will need work. * Roll dprint back to an earlier version GitHub dropped support for Ubuntu 18 runners, causing dprint's artifacts to require a glibc version greater than what Anki CI currently has.
2023-07-01 08:21:53 +02:00
.disabled {
pointer-events: none;
opacity: 0.5;
}
.chevron {
height: 100%;
align-self: flex-end;
Add comments to Sass variables and tweak main window (#2137) * Prevent multiple inclusion of variables in CSS files * Use dict instead of tuple for variables * Add comments to variables * Improve appearance of main window * Tweak main window styles * Use json.dumps over pprint.format * Make study button primary * Improve header margin * Make bottom toolbar slimmer * Make congrats page more balanced * Fix type issue * Replace day/night with light/dark * Exclude top-level-drag-row from hover effect * Create dataclass for variables * Run formatter * Apply CSS variables from Python side Why go full-circle with the Sass variables? This way we only need one interface for add-on authors to interact with. It also makes it easier for us to apply additional themes in the future. * Fix typing * Fix rgba values in Qt * Darken button background * Fix palette not being applied in light theme For some odd reason this problem arose much later than #2016. * Tweak default button look * Reformat * Apply CSS vars to ts pages * Include elevation in button_mixins_lib * Cast opacity to int * Add some margin to studiedToday info * Tweak light theme button gradient * Tweak highlight-bg for light theme * Add back default button color as it made the browser sidebar tool icons dark in light theme. * Reformat * Tweak light theme buttons once more Sorry for the back-and-forth. Sass only compiles when there are changes in user files, not when I only change the vars. * Fix bottom toolbar button indicators * Make buttons more clicky * Fix button padding * Handle macOS separately again * Decrease elevation effect for main window buttons to 1 * Imitate box-shadow for Qt elements * Adjust shadow vars * Adjust primary border color because the save button in the deck options had a lighter color than its background gradient. * Boost box-shadow color of primary buttons * Format * Adjust Qt box-shadow imitation and shadow colors * Use more subtle default shadow color * Add some more padding to top toolbar * Revert "Apply CSS vars to ts pages" This reverts commit 5d8e7f6b7ffc8894b6517ecbb8cfba35407fc69a. * Revert "Apply CSS variables from Python side" This reverts commit 87db774412fd2bfd75e2630d2c5e782daef96b5f. * Better match the standard macOS buttons In the dark theme the standard color is a lighter grey, but at least the size/shape is similar again. This doesn't work for the editor buttons. * Reduce the top margin of the congrats screen * Fix illegible buttons when changing theme on macOS; match dark button style
2022-10-29 02:48:53 +02:00
border-left: 1px solid var(--border-subtle);
}
:global([dir="rtl"]) {
.chevron {
border-left: none;
Add comments to Sass variables and tweak main window (#2137) * Prevent multiple inclusion of variables in CSS files * Use dict instead of tuple for variables * Add comments to variables * Improve appearance of main window * Tweak main window styles * Use json.dumps over pprint.format * Make study button primary * Improve header margin * Make bottom toolbar slimmer * Make congrats page more balanced * Fix type issue * Replace day/night with light/dark * Exclude top-level-drag-row from hover effect * Create dataclass for variables * Run formatter * Apply CSS variables from Python side Why go full-circle with the Sass variables? This way we only need one interface for add-on authors to interact with. It also makes it easier for us to apply additional themes in the future. * Fix typing * Fix rgba values in Qt * Darken button background * Fix palette not being applied in light theme For some odd reason this problem arose much later than #2016. * Tweak default button look * Reformat * Apply CSS vars to ts pages * Include elevation in button_mixins_lib * Cast opacity to int * Add some margin to studiedToday info * Tweak light theme button gradient * Tweak highlight-bg for light theme * Add back default button color as it made the browser sidebar tool icons dark in light theme. * Reformat * Tweak light theme buttons once more Sorry for the back-and-forth. Sass only compiles when there are changes in user files, not when I only change the vars. * Fix bottom toolbar button indicators * Make buttons more clicky * Fix button padding * Handle macOS separately again * Decrease elevation effect for main window buttons to 1 * Imitate box-shadow for Qt elements * Adjust shadow vars * Adjust primary border color because the save button in the deck options had a lighter color than its background gradient. * Boost box-shadow color of primary buttons * Format * Adjust Qt box-shadow imitation and shadow colors * Use more subtle default shadow color * Add some more padding to top toolbar * Revert "Apply CSS vars to ts pages" This reverts commit 5d8e7f6b7ffc8894b6517ecbb8cfba35407fc69a. * Revert "Apply CSS variables from Python side" This reverts commit 87db774412fd2bfd75e2630d2c5e782daef96b5f. * Better match the standard macOS buttons In the dark theme the standard color is a lighter grey, but at least the size/shape is similar again. This doesn't work for the editor buttons. * Reduce the top margin of the congrats screen * Fix illegible buttons when changing theme on macOS; match dark button style
2022-10-29 02:48:53 +02:00
border-right: 1px solid var(--border-subtle);
}
}
</style>