Skip to content

Commit

Permalink
chore: simplifying styling and width computing
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneburdet committed Dec 13, 2024
1 parent 8df9038 commit 742a511
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 46 deletions.
2 changes: 0 additions & 2 deletions packages/visualizations/src/components/Table/Body.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
export let emptyStateLabel: string | undefined;
let hoveredRow: number | null;
export let isHorizontallyScrolled: boolean;
</script>

<tbody
Expand All @@ -36,7 +35,6 @@
{columns}
{rowProps}
{record}
{isHorizontallyScrolled}
setHovered={() => {
hoveredRow = rowIndex;
}}
Expand Down
13 changes: 5 additions & 8 deletions packages/visualizations/src/components/Table/Cell/Cell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@
import NumberFormat from 'components/Format/NumberFormat.svelte';
import URLFormat from 'components/Format/URLFormat.svelte';
import { DATA_FORMAT } from '../constants';
import { locale, stickyColumnsOffset, lastStickyColumn } from '../store';
import { isHorizontallyScrolled, locale, stickyColumnsOffset } from '../store';
import type { Column } from '../types';
import { isColumnOfType, getStickyClasses } from '../utils';
export let rawValue: unknown;
export let column: Column;
export let isHorizontallyScrolled: boolean;
$: ({ dataFormat } = column);
$: offset = $stickyColumnsOffset.get(column.key);
$: stickyClasses = getStickyClasses(
$stickyColumnsOffset.has(column.key),
column.key === $lastStickyColumn,
isHorizontallyScrolled
);
</script>

<!-- To display a format value, rawValue must be different from undefined or null -->
<td style={isNil(offset) ? '' : `--sticky-offset: ${offset}px;`} class={stickyClasses}>
<td
style={isNil(offset) ? '' : `--sticky-offset: ${offset}px;`}
class={getStickyClasses(column, $isHorizontallyScrolled)}
>
<div class={`table-data--${dataFormat}`}>
{#if isValidRawValue(rawValue)}
{#if isColumnOfType(column, DATA_FORMAT.boolean)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import type { Column } from '../types';
import Th from './Th.svelte';
export let isHorizontallyScrolled: boolean;
export let columns: Column[];
export let extraButtonColumn = false;
</script>
Expand All @@ -13,7 +12,7 @@
<th />
{/if}
{#each columns as column (column.key)}
<Th {column} dataFormat={column.dataFormat} {isHorizontallyScrolled} />
<Th {column} dataFormat={column.dataFormat} />
{/each}
</tr>
</thead>
Expand Down
10 changes: 2 additions & 8 deletions packages/visualizations/src/components/Table/Headers/Th.svelte
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
<script lang="ts">
import type { Column, DataFormat } from '../types';
import { stickyColumnsWidth, stickyColumnsOffset, lastStickyColumn } from '../store';
import { stickyColumnsWidth, stickyColumnsOffset, isHorizontallyScrolled } from '../store';
import { getStickyClasses } from '../utils';
import SortButton from './SortButton.svelte';
export let column: Column;
export let dataFormat: DataFormat;
export let isHorizontallyScrolled: boolean;
let clientWidth: number;
// Only updates columns that have been initialized after a reset in Table.svelte
$: if ($stickyColumnsWidth.has(column.key)) {
stickyColumnsWidth.updateColumn(column.key, clientWidth);
}
$: stickyClasses = getStickyClasses(
$stickyColumnsWidth.has(column.key),
column.key === $lastStickyColumn,
isHorizontallyScrolled
);
</script>

<th
style={`--sticky-offset: ${$stickyColumnsOffset.get(column.key)}px`}
class={`table-header--${dataFormat} ${stickyClasses}`}
class={`table-header--${dataFormat} ${getStickyClasses(column, $isHorizontallyScrolled)}`}
bind:clientWidth
>
{#if column.onClick}
Expand Down
3 changes: 1 addition & 2 deletions packages/visualizations/src/components/Table/Row.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
export let record: Record<string, unknown>;
export let isHovered = false;
export let setHovered: () => void;
export let isHorizontallyScrolled: boolean;
$: ({ onClick, onMouseEnter, onMouseLeave, actionAriaLabel } = rowProps || {});
$: handleMouseEnter = (e: HoverEvent<HTMLTableRowElement>) => {
Expand Down Expand Up @@ -48,7 +47,7 @@
</td>
{/if}
{#each columns as column}
<Cell rawValue={record[column.key]} {column} {isHorizontallyScrolled} />
<Cell rawValue={record[column.key]} {column} />
{/each}
</tr>

Expand Down
27 changes: 7 additions & 20 deletions packages/visualizations/src/components/Table/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { Column, RowProps } from './types';
import Headers from './Headers';
import Body from './Body.svelte';
import { stickyColumnsWidth } from './store';
import { stickyColumnsWidth, isHorizontallyScrolled } from './store';
export let loadingRowsNumber: number | null;
export let columns: Column[];
Expand All @@ -15,22 +15,14 @@
const tableId = `table-${generateId()}`;
let isHorizontallyScrolled = false;
let scrollBox: HTMLDivElement;
let sortedStickyColumns: Column[] = [];
function handleScroll() {
isHorizontallyScrolled =
$isHorizontallyScrolled =
document.dir === 'rtl' ? scrollBox?.scrollLeft < 0 : scrollBox?.scrollLeft > 0;
}
$: sortedStickyColumns = [...columns].sort((colA, colB) => {
if (Boolean(colA?.sticky) === Boolean(colB?.sticky)) {
return 0;
}
return colA?.sticky ? -1 : 1;
});
// resets scroll when changing columns parameters
$: if (columns && scrollBox) {
scrollBox.scrollLeft = 0;
Expand All @@ -41,28 +33,23 @@
return colA?.sticky ? -1 : 1;
});
stickyColumnsWidth.reset();
sortedStickyColumns
.filter((col) => col?.sticky)
.forEach((col) => {
sortedStickyColumns.forEach((col) => {
if (col?.sticky) {
stickyColumnsWidth.updateColumn(col.key, 0);
});
}
});
}
</script>

<div class="scrollbox" bind:this={scrollBox} on:scroll={handleScroll}>
<table aria-describedby={description ? tableId : undefined}>
<Headers
columns={sortedStickyColumns}
{isHorizontallyScrolled}
extraButtonColumn={Boolean(rowProps?.onClick)}
/>
<Headers columns={sortedStickyColumns} extraButtonColumn={Boolean(rowProps?.onClick)} />
<Body
{records}
columns={sortedStickyColumns}
{rowProps}
{emptyStateLabel}
{loadingRowsNumber}
{isHorizontallyScrolled}
/>
</table>
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/visualizations/src/components/Table/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export const stickyColumnsOffset = derived(stickyColumnsWidth, ($widths) => {
export const lastStickyColumn = derived(stickyColumnsWidth, ($widths) =>
[...$widths.keys()].at(-1)
);

export const isHorizontallyScrolled = writable(false);
12 changes: 8 additions & 4 deletions packages/visualizations/src/components/Table/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { get } from 'svelte/store';
import type { DataFormat, Column, ColumnOfType } from './types';
import { lastStickyColumn } from './store';

export const getStickyClasses = (sticky: boolean, lastSticky: boolean, scrolled: boolean) =>
`${sticky && 'sticky'}
${lastSticky && 'isLastSticky'}
${scrolled && 'isHorizontallyScrolled'}`;
export const getStickyClasses = (column: Column, scrolled: boolean) => `
${column.sticky && 'sticky'}
${column.key === get(lastStickyColumn) && 'isLastSticky'}
${scrolled && 'isHorizontallyScrolled'}
`;

export function isColumnOfType<K extends DataFormat>(
column: Column,
Expand Down

0 comments on commit 742a511

Please sign in to comment.