refactor: use shorthand unsubscribe

This commit is contained in:
Eric Liu 2023-07-04 10:48:57 -07:00
commit b756f84b65
4 changed files with 19 additions and 54 deletions

View file

@ -14,24 +14,13 @@
/** Specify the ARIA label for the accordion item chevron icon */ /** Specify the ARIA label for the accordion item chevron icon */
export let iconDescription = "Expand/Collapse"; export let iconDescription = "Expand/Collapse";
import { onMount, getContext } from "svelte"; import { getContext } from "svelte";
import ChevronRight from "../icons/ChevronRight.svelte"; import ChevronRight from "../icons/ChevronRight.svelte";
let initialDisabled = disabled;
const ctx = getContext("Accordion");
const unsubscribe = ctx.disableItems.subscribe((value) => {
if (!value && initialDisabled) return;
disabled = value;
});
let animation = undefined; let animation = undefined;
onMount(() => { const { disableItems } = getContext("Accordion");
return () => { $: disabled = $disableItems;
unsubscribe();
};
});
</script> </script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->

View file

@ -17,25 +17,19 @@
/** Obtain a reference to the button HTML element */ /** Obtain a reference to the button HTML element */
export let ref = null; export let ref = null;
import { afterUpdate, getContext, onMount } from "svelte"; import { afterUpdate, getContext } from "svelte";
const ctx = getContext("ContentSwitcher"); const { add, update, change, currentId } = getContext("ContentSwitcher");
ctx.add({ id, text, selected }); add({ id, text, selected });
const unsubscribe = ctx.currentId.subscribe(($) => { $: selected = $currentId === id;
selected = $ === id;
});
afterUpdate(() => { afterUpdate(() => {
if (selected) { if (selected) {
ref.focus(); ref.focus();
} }
}); });
onMount(() => {
return () => unsubscribe();
});
</script> </script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->
@ -52,7 +46,7 @@
{...$$restProps} {...$$restProps}
on:click on:click
on:click|preventDefault="{() => { on:click|preventDefault="{() => {
ctx.update(id); update(id);
}}" }}"
on:mouseover on:mouseover
on:mouseenter on:mouseenter
@ -60,9 +54,9 @@
on:keydown on:keydown
on:keydown="{({ key }) => { on:keydown="{({ key }) => {
if (key === 'ArrowRight') { if (key === 'ArrowRight') {
ctx.change(1); change(1);
} else if (key === 'ArrowLeft') { } else if (key === 'ArrowLeft') {
ctx.change(-1); change(-1);
} }
}}" }}"
> >

View file

@ -23,7 +23,7 @@
/** Set an id for the top-level element */ /** Set an id for the top-level element */
export let id = "ccs-" + Math.random().toString(36); export let id = "ccs-" + Math.random().toString(36);
import { onMount, getContext } from "svelte"; import { getContext } from "svelte";
import CheckmarkOutline from "../icons/CheckmarkOutline.svelte"; import CheckmarkOutline from "../icons/CheckmarkOutline.svelte";
import Warning from "../icons/Warning.svelte"; import Warning from "../icons/Warning.svelte";
import CircleDash from "../icons/CircleDash.svelte"; import CircleDash from "../icons/CircleDash.svelte";
@ -35,20 +35,11 @@
getContext("ProgressIndicator"); getContext("ProgressIndicator");
$: add({ id, complete, disabled }); $: add({ id, complete, disabled });
$: if ($stepsById[id]) {
const unsubscribe = stepsById.subscribe((value) => { step = $stepsById[id];
if (value[id]) { current = step.current;
step = value[id]; complete = step.complete;
current = step.current; }
complete = step.complete;
}
});
onMount(() => {
return () => {
unsubscribe();
};
});
</script> </script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->

View file

@ -2,23 +2,14 @@
/** Specify a selector to be focused inside the footer when opening the tooltip */ /** Specify a selector to be focused inside the footer when opening the tooltip */
export let selectorPrimaryFocus = "a[href], button:not([disabled])"; export let selectorPrimaryFocus = "a[href], button:not([disabled])";
import { getContext, onMount } from "svelte"; import { getContext } from "svelte";
let ref = null; let ref = null;
let open = false; let open = false;
const ctx = getContext("Tooltip"); const { tooltipOpen } = getContext("Tooltip");
const unsubscribe = ctx.tooltipOpen.subscribe((tooltipOpen) => {
open = tooltipOpen;
});
onMount(() => { $: if ((open || $tooltipOpen) && ref) {
return () => {
unsubscribe();
};
});
$: if (open && ref) {
const node = ref.querySelector(selectorPrimaryFocus); const node = ref.querySelector(selectorPrimaryFocus);
if (node) node.focus(); if (node) node.focus();
} }