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

Already on GitHub? Sign in to your account

refactor: use shorthand unsubscribe #1762

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 3 additions & 14 deletions src/Accordion/AccordionItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@
/** Specify the ARIA label for the accordion item chevron icon */
export let iconDescription = "Expand/Collapse";

import { onMount, getContext } from "svelte";
import { getContext } from "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;

onMount(() => {
return () => {
unsubscribe();
};
});
const { disableItems } = getContext("Accordion");
$: disabled = $disableItems;
</script>

<!-- svelte-ignore a11y-mouse-events-have-key-events -->
Expand Down
20 changes: 7 additions & 13 deletions src/ContentSwitcher/Switch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@
/** Obtain a reference to the button HTML element */
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 = $ === id;
});
$: selected = $currentId === id;

afterUpdate(() => {
if (selected) {
ref.focus();
}
});

onMount(() => {
return () => unsubscribe();
});
</script>

<!-- svelte-ignore a11y-mouse-events-have-key-events -->
Expand All @@ -52,17 +46,17 @@
{...$$restProps}
on:click
on:click|preventDefault="{() => {
ctx.update(id);
update(id);
}}"
on:mouseover
on:mouseenter
on:mouseleave
on:keydown
on:keydown="{({ key }) => {
if (key === 'ArrowRight') {
ctx.change(1);
change(1);
} else if (key === 'ArrowLeft') {
ctx.change(-1);
change(-1);
}
}}"
>
Expand Down
21 changes: 6 additions & 15 deletions src/ProgressIndicator/ProgressStep.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/** Set an id for the top-level element */
export let id = "ccs-" + Math.random().toString(36);

import { onMount, getContext } from "svelte";
import { getContext } from "svelte";
import CheckmarkOutline from "../icons/CheckmarkOutline.svelte";
import Warning from "../icons/Warning.svelte";
import CircleDash from "../icons/CircleDash.svelte";
Expand All @@ -35,20 +35,11 @@
getContext("ProgressIndicator");

$: add({ id, complete, disabled });

const unsubscribe = stepsById.subscribe((value) => {
if (value[id]) {
step = value[id];
current = step.current;
complete = step.complete;
}
});

onMount(() => {
return () => {
unsubscribe();
};
});
$: if ($stepsById[id]) {
step = $stepsById[id];
current = step.current;
complete = step.complete;
}
</script>

<!-- svelte-ignore a11y-mouse-events-have-key-events -->
Expand Down
15 changes: 3 additions & 12 deletions src/Tooltip/TooltipFooter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@
/** Specify a selector to be focused inside the footer when opening the tooltip */
export let selectorPrimaryFocus = "a[href], button:not([disabled])";

import { getContext, onMount } from "svelte";
import { getContext } from "svelte";

let ref = null;
let open = false;

const ctx = getContext("Tooltip");
const unsubscribe = ctx.tooltipOpen.subscribe((tooltipOpen) => {
open = tooltipOpen;
});
const { tooltipOpen } = getContext("Tooltip");

onMount(() => {
return () => {
unsubscribe();
};
});

$: if (open && ref) {
$: if ((open || $tooltipOpen) && ref) {
const node = ref.querySelector(selectorPrimaryFocus);
if (node) node.focus();
}
Expand Down