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

feat: observe resize segment for better indicator display #565

Merged
merged 4 commits into from
Jan 21, 2025
Merged
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
27 changes: 25 additions & 2 deletions src/lib/components/Segment.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { writable } from "svelte/store";
import type { SegmentContext, SelectedSegment } from "$lib/types/segment";
import { SEGMENT_CONTEXT_KEY } from "$lib/types/segment";
import { setContext, tick } from "svelte";
import { onDestroy, setContext, tick } from "svelte";
import { isNullish, nonNullish } from "@dfinity/utils";

export let selectedSegmentId: symbol | undefined = undefined;
Expand Down Expand Up @@ -59,14 +59,37 @@
};
};

$: selectedElement, (() => initIndicator())();
$: selectedElement, initIndicator();

// TODO: support adding segmebt buttons dynamically
let segmentsCount = 0;
$: segment,
(() =>
(segmentsCount =
segment?.querySelectorAll(".segment-button").length ?? 0))();

// The SegmentButton has a width set to 100%—i.e., not fixed. Therefore, its size might change.
// Likewise, on mount, when the segment is bound and the indicator is set for the first time, the buttons might not be fully rendered in terms of size yet.
// Furthermore, if the content of the button dynamically changes—such as in applications that support runtime translations—their size might change.
// That is why, if the overall size of the component changes, we re-evaluate the position of the indicator.
let resizeObserver: ResizeObserver | undefined;

const disconnectResizeObserver = () => resizeObserver?.disconnect();

onDestroy(disconnectResizeObserver);

const observeSegmentResize = () => {
disconnectResizeObserver();

if (isNullish(segment)) {
return;
}

resizeObserver = new ResizeObserver(initIndicator);
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
resizeObserver.observe(segment);
};

$: segment, observeSegmentResize();
</script>

<svelte:window on:resize={initIndicator} />
Expand Down
12 changes: 12 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ const purify = DOMPurify(window as unknown as Window);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: used for testing only
global.DOMPurify = purify;

global.ResizeObserver = class ResizeObserver {
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
// do nothing
}
};
Loading