Skip to content

Commit e53dee6

Browse files
authored
Merge pull request #2532 from exceptionless/issue/improve-floating-table-controls
Improve floating table controls
2 parents ef8eaf2 + dcc88f1 commit e53dee6

8 files changed

Lines changed: 355 additions & 19 deletions

File tree

‎src/Exceptionless.Web/ClientApp/e2e/tests/table-toolbar-pagination.e2e.ts‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { resolve } from 'node:path';
33
import { expect, test } from '../fixtures/e2e-test';
44
import { seedRepresentativeEvent } from '../support/event-data';
55

6-
test('table toolbar remains accessible while moving between different-height result pages', async ({ e2eApi, e2eScenario, page }) => {
6+
test('floating table controls keep the first row visible between different-height result pages', async ({ e2eApi, e2eScenario, page }) => {
77
for (let index = 1; index <= 6; index++) {
88
await seedRepresentativeEvent(e2eApi, e2eScenario.userToken, {
99
message: `${e2eScenario.message} ${index}`,
@@ -36,31 +36,96 @@ test('table toolbar remains accessible while moving between different-height res
3636
expect(firstToolbarBox).not.toBeNull();
3737
expect(firstGridBox).not.toBeNull();
3838
expect(await toolbar.evaluate((element) => getComputedStyle(element).position)).toBe('sticky');
39+
await expect(toolbar).not.toHaveAttribute('data-floating');
3940
expect(await toolbar.evaluate((element) => element.nextElementSibling?.getAttribute('data-slot'))).toBe('data-table-body');
4041
expect(firstToolbarBox!.height).toBeLessThanOrEqual(34);
4142
expect(firstGridBox!.y - (firstToolbarBox!.y + firstToolbarBox!.height)).toBeLessThanOrEqual(8);
4243
expect(firstToolbarBox!.y + firstToolbarBox!.height).toBeLessThanOrEqual(firstGridBox!.y);
4344

45+
const scrollContainer = page.locator('main').locator('..');
46+
const scrollTopAtPageTop = await scrollContainer.evaluate((element) => element.scrollTop);
47+
await pager.getByRole('button', { name: 'Go to next page' }).click();
48+
await expect(pager.getByLabel('Page 2 of 2')).toBeVisible();
49+
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBe(scrollTopAtPageTop);
50+
await pager.getByRole('button', { name: 'Go to first page' }).click();
51+
await expect(pager.getByLabel('Page 1 of 2')).toBeVisible();
52+
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBe(scrollTopAtPageTop);
53+
4454
if (process.env.E2E_CAPTURE_PAGER_TOOLBAR_SCREENSHOTS === 'true') {
4555
await page.screenshot({ path: resolve(process.cwd(), '../../../dogfood-output/pager-toolbar/desktop-page-1.png') });
4656
}
4757

58+
const scrollTopBeforeVisiblePageSizeChange = await scrollContainer.evaluate((element) => element.scrollTop);
59+
await pager.getByLabel('Rows per page').click();
60+
await page.getByRole('option', { name: '10 rows' }).click();
61+
await expect(pager.getByLabel('Page 1 of 1')).toBeVisible();
62+
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBe(scrollTopBeforeVisiblePageSizeChange);
63+
64+
await page.setViewportSize({ height: 400, width: 1280 });
65+
await scrollContainer.evaluate((element) => element.scrollTo({ top: element.scrollHeight }));
66+
await expect(toolbar).toHaveAttribute('data-floating', '');
67+
68+
if (process.env.E2E_CAPTURE_PAGER_TOOLBAR_SCREENSHOTS === 'true') {
69+
await page.screenshot({ path: resolve(process.cwd(), '../../../dogfood-output/pager-toolbar/desktop-floating.png') });
70+
71+
const wasDark = await page.locator('html').evaluate((element) => element.classList.contains('dark'));
72+
await page.locator('html').evaluate((element) => {
73+
element.classList.add('dark');
74+
element.style.colorScheme = 'dark';
75+
});
76+
await page.screenshot({ path: resolve(process.cwd(), '../../../dogfood-output/pager-toolbar/desktop-floating-dark.png') });
77+
78+
if (!wasDark) {
79+
await page.locator('html').evaluate((element) => {
80+
element.classList.remove('dark');
81+
element.style.colorScheme = 'light';
82+
});
83+
}
84+
}
85+
86+
const scrollTopBeforeHiddenPageSizeChange = await scrollContainer.evaluate((element) => element.scrollTop);
87+
await pager.getByLabel('Rows per page').click();
88+
await page.getByRole('option', { name: '5 rows' }).click();
89+
await expect(pager.getByLabel('Page 1 of 2')).toBeVisible();
90+
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBeLessThan(scrollTopBeforeHiddenPageSizeChange);
91+
await scrollContainer.evaluate((element) => element.scrollTo({ top: element.scrollHeight }));
92+
await expect(toolbar).toHaveAttribute('data-floating', '');
93+
4894
const nextButton = pager.getByRole('button', { name: 'Go to next page' });
49-
const scrollContainer = page.locator('main').locator('..');
5095
const scrollTopBeforePaging = await scrollContainer.evaluate((element) => element.scrollTop);
5196
await nextButton.focus();
5297
await nextButton.click();
5398
await expect(pager.getByLabel('Page 2 of 2')).toBeVisible();
5499
await expect(pager.getByRole('button', { name: 'Go to next page' })).toBeFocused();
55100
await expect(bulkActionsButton).toHaveAttribute('aria-disabled', 'true');
56-
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBe(scrollTopBeforePaging);
101+
await expect(toolbar).toHaveAttribute('data-floating', '');
102+
103+
const scrollTopAfterPaging = await scrollContainer.evaluate((element) => element.scrollTop);
104+
expect(scrollTopAfterPaging).toBeGreaterThan(0);
105+
expect(scrollTopAfterPaging).toBeLessThan(scrollTopBeforePaging);
57106

58107
const secondToolbarBox = await toolbar.boundingBox();
59108
const secondGridBox = await grid.boundingBox();
109+
const firstRow = page.locator('tbody tr:visible').first();
110+
await expect(firstRow).toBeVisible({ timeout: 30_000 });
111+
const firstRowBox = await firstRow.boundingBox();
60112
expect(secondToolbarBox).not.toBeNull();
61113
expect(secondGridBox).not.toBeNull();
114+
expect(firstRowBox).not.toBeNull();
62115
expect(secondToolbarBox!.y).toBeGreaterThanOrEqual(8);
63116
expect(secondToolbarBox!.y + secondToolbarBox!.height).toBeLessThanOrEqual(secondGridBox!.y);
117+
expect(firstRowBox!.y).toBeGreaterThanOrEqual(secondToolbarBox!.y + secondToolbarBox!.height);
118+
expect(firstRowBox!.y).toBeLessThan(400);
119+
120+
const scrollTopWithFirstRowVisible = await scrollContainer.evaluate((element) => element.scrollTop);
121+
const previousButton = pager.getByRole('button', { name: 'Go to previous page' });
122+
await previousButton.click();
123+
await expect(pager.getByLabel('Page 1 of 2')).toBeVisible();
124+
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBe(scrollTopWithFirstRowVisible);
125+
126+
await pager.getByRole('button', { name: 'Go to next page' }).click();
127+
await expect(pager.getByLabel('Page 2 of 2')).toBeVisible();
128+
await expect.poll(() => scrollContainer.evaluate((element) => element.scrollTop)).toBe(scrollTopWithFirstRowVisible);
64129

65130
await page.locator('main').evaluate((element) => element.parentElement?.scrollTo({ top: element.parentElement.scrollHeight }));
66131
const appFooter = page.getByRole('link', { exact: true, name: 'Terms' }).locator('xpath=ancestor::div[contains(@class, "border-t")][1]');

‎src/Exceptionless.Web/ClientApp/src/lib/features/shared/components/data-table/data-table-footer.svelte‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
import DataTablePageCount from './data-table-page-count.svelte';
1212
import DataTablePagination from './data-table-pagination.svelte';
13+
import { findScrollableAncestor } from './data-table-scroll';
1314
import DataTableSelection from './data-table-selection.svelte';
1415
1516
type Props = HTMLAttributes<Element> & {
@@ -19,17 +20,61 @@
1920
};
2021
2122
let { children, class: className, table, variant = 'simple' }: Props = $props();
23+
24+
let isFloating = $state(false);
25+
let toolbarElement = $state<HTMLDivElement>();
26+
27+
$effect(() => {
28+
const element = toolbarElement;
29+
if (variant !== 'floating' || !element || typeof window === 'undefined') {
30+
isFloating = false;
31+
return;
32+
}
33+
34+
const scrollContainer = findScrollableAncestor(element);
35+
36+
function updateFloatingState(): void {
37+
const styles = window.getComputedStyle(element!);
38+
const stickyTop = Number.parseFloat(styles.top);
39+
40+
if (styles.position !== 'sticky' || !Number.isFinite(stickyTop)) {
41+
isFloating = false;
42+
return;
43+
}
44+
45+
const scrollContainerTop = scrollContainer?.getBoundingClientRect().top ?? 0;
46+
const stickyBoundary = scrollContainerTop + (scrollContainer?.clientTop ?? 0) + stickyTop;
47+
isFloating = Math.abs(element!.getBoundingClientRect().top - stickyBoundary) < 1;
48+
}
49+
50+
updateFloatingState();
51+
const scrollTarget = scrollContainer ?? window;
52+
scrollTarget.addEventListener('scroll', updateFloatingState, {
53+
passive: true
54+
});
55+
window.addEventListener('resize', updateFloatingState, {
56+
passive: true
57+
});
58+
59+
return () => {
60+
scrollTarget.removeEventListener('scroll', updateFloatingState);
61+
window.removeEventListener('resize', updateFloatingState);
62+
};
63+
});
2264
</script>
2365

2466
<div
2567
aria-label="Table controls"
68+
bind:this={toolbarElement}
2669
class={[
2770
'flex w-full items-center',
2871
variant === 'floating'
2972
? 'border-border bg-background/95 sticky top-2 z-30 flex-wrap justify-between gap-0 rounded-lg border backdrop-blur-sm'
3073
: 'justify-end gap-2',
74+
isFloating && 'floating-glow',
3175
className
3276
]}
77+
data-floating={isFloating ? '' : undefined}
3378
data-slot="data-table-footer"
3479
data-variant={variant}
3580
role="toolbar"
@@ -44,3 +89,11 @@
4489
</div>
4590
{/if}
4691
</div>
92+
93+
<style>
94+
.floating-glow {
95+
box-shadow:
96+
0 0 0 1px color-mix(in oklab, var(--foreground) 32%, transparent),
97+
0 0 14px 2px color-mix(in oklab, var(--foreground) 18%, transparent);
98+
}
99+
</style>

‎src/Exceptionless.Web/ClientApp/src/lib/features/shared/components/data-table/data-table-page-size.svelte‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
1111
interface Props {
1212
joined?: boolean;
13+
onBeforePageSizeChange?: () => void;
1314
onPageSizeChange?: () => void;
1415
size?: 'default' | 'sm';
1516
table: Table<StockFeatures, TData>;
1617
value: number;
1718
}
1819
19-
let { joined = false, onPageSizeChange, size = 'sm', table, value = $bindable() }: Props = $props();
20+
let { joined = false, onBeforePageSizeChange, onPageSizeChange, size = 'sm', table, value = $bindable() }: Props = $props();
2021
2122
type Item = { label: string; value: string };
2223
const items: Item[] = [
@@ -50,6 +51,7 @@
5051
let selected = $derived((items.find((item) => item.value === valueString) || items[0]) as Item);
5152
5253
function onValueChange(newValue: string) {
54+
onBeforePageSizeChange?.();
5355
value = Number(newValue);
5456
table.setPageSize(Number(newValue));
5557
onPageSizeChange?.();

‎src/Exceptionless.Web/ClientApp/src/lib/features/shared/components/data-table/data-table-pager.svelte‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import ChevronsLeftIcon from '@lucide/svelte/icons/chevrons-left';
1515
1616
import DataTablePageSize from './data-table-page-size.svelte';
17+
import { scrollTableToFirstRow } from './data-table-scroll';
1718
1819
interface Props {
1920
table: Table<StockFeatures, TData>;
@@ -22,34 +23,54 @@
2223
}
2324
2425
let { table, value = $bindable(), variant = 'simple' }: Props = $props();
26+
let pagerElement = $state<HTMLElement>();
2527
2628
const currentPage = $derived((table.options.state?.pagination?.pageIndex ?? table.store.state.pagination.pageIndex) + 1);
2729
const totalPages = $derived(Math.max(1, table.getPageCount() || 1));
2830
const canGoNext = $derived(currentPage < totalPages);
2931
const canGoPrevious = $derived(currentPage > 1);
3032
31-
function goToFirstPage(): void {
33+
function goToFirstPage(event: MouseEvent): void {
3234
if (canGoPrevious) {
33-
table.setPageIndex(0);
35+
goToPage(0, event);
3436
}
3537
}
3638
37-
function goToNextPage(): void {
39+
function goToNextPage(event: MouseEvent): void {
3840
if (canGoNext) {
39-
table.setPageIndex(currentPage);
41+
goToPage(currentPage, event);
4042
}
4143
}
4244
43-
function goToPreviousPage(): void {
45+
function goToPreviousPage(event: MouseEvent): void {
4446
if (canGoPrevious) {
45-
table.setPageIndex(currentPage - 2);
47+
goToPage(currentPage - 2, event);
4648
}
4749
}
50+
51+
function goToPage(pageIndex: number, event: MouseEvent): void {
52+
const trigger = event.currentTarget as HTMLElement;
53+
if (shouldAdjustScroll(trigger)) {
54+
scrollTableToFirstRow(trigger);
55+
}
56+
57+
table.setPageIndex(pageIndex);
58+
}
59+
60+
function onBeforePageSizeChange(): void {
61+
if (pagerElement && shouldAdjustScroll(pagerElement)) {
62+
scrollTableToFirstRow(pagerElement);
63+
}
64+
}
65+
66+
function shouldAdjustScroll(trigger: HTMLElement): boolean {
67+
return variant === 'floating' && trigger.closest('[data-slot="data-table-footer"]')?.hasAttribute('data-floating') === true;
68+
}
4869
</script>
4970

50-
<nav aria-label="Table pagination" class="ml-auto shrink-0" data-variant={variant}>
71+
<nav aria-label="Table pagination" bind:this={pagerElement} class="ml-auto shrink-0" data-variant={variant}>
5172
<ButtonGroup.Root aria-label="Pagination controls">
52-
<DataTablePageSize bind:value joined={variant === 'floating'} size="default" {table} />
73+
<DataTablePageSize bind:value joined={variant === 'floating'} {onBeforePageSizeChange} size="default" {table} />
5374
<ButtonGroup.Text
5475
aria-label={`Page ${currentPage} of ${totalPages}`}
5576
class={cn('min-w-14 justify-center select-none', variant === 'floating' && 'rounded-none border-y-0')}

0 commit comments

Comments
 (0)
⌂ 换网址