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

Feature/sc-48047/table-sdk-sticky-columns #266

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

etienneburdet
Copy link
Contributor

@etienneburdet etienneburdet commented Nov 26, 2024

Summary

The goal for this PR is to add sticky columns to the table. I tried to support a "pin" column scenario, where users can add/remove multiple columns to sticky side.

(Internal for Opendatasoft only) Associated Shortcut ticket: sc-48047.

Changes

Sticky style
I chose position: sticky with computed offsets.
Other possibilities: back to full div, second non scrolling table.
Pros: a11y because it's a single table.
Cons: shadow is a bit harder to do, requires js to compute offsets.

Sticky state API
I chose to declare the sticky in the column declaration.
Other possibilities: declaring a stickyColumns: … in the config, exposing an headless state manager (column.pin()) à la/with Tanstack table.
Pros: can only pin defined colmun no id mismatch.
Cons: can be a it of a burden to update since it's an object, start mixing definition and state management.

Sticky state internal
I went with initilazing a Map and reseting both the map and scroll on each change of columns (even non-sticky) and sorting columns based on sticky and their initial order.
Other possibilities: using array/indexes, pushing in order of selection, not messing with order (sticky only when "met"), using column.sticky in palce.
Pros: no need for index matching in 3 different places, everything derived from base store, seperation between user config and internal state.
Cons: May reset on colmuns update that didn't require it.

I made a specific Th component first because it was getting really big, but mostly to avoid an array of clientWidth since array reactivity in Svelte can be annoying.

The shadow is made with a linear gradient, because box-shadow cannot be applied to only one side and thus required some keyed z-index manipulations for each column to hide the non-wanted parts.

Breaking changes

Since I needed border: separate, we end-up with a 1px diffs an all table stories, one way or another. Here I removed the extra 1px td padding, the other way around added it. I thought it wasn't worth the 0.5px padding just to minimize the diff.

Changelog

"It's now possible to add sticky columns to the table, that remains visible while scrolling".

Review checklist

  • Description is complete
  • Commits respect the Conventional Commits Specification
  • 2 reviewers (1 if trivial)
  • Tests coverage has improved
  • Code is ready for a release on NPM

@etienneburdet etienneburdet marked this pull request as ready for review November 26, 2024 16:04
@etienneburdet etienneburdet force-pushed the feature/sc-48047/table-sdk-sticky-columns branch 2 times, most recently from a71c0df to ffb8a3a Compare November 27, 2024 08:49
Copy link
Contributor

@KevinFabre-ods KevinFabre-ods left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice feature GG! 👏

The offset computation implementation is not so big and is reactive to column widths !(column definition doesn't need a size)

Sticky style
I chose position: sticky with computed offsets.
Other possibilities: back to full div, second non scrolling table.
Pros: a11y because it's a single table.
Cons: shadow is a bit harder to do, requires js to compute offsets.

I think it's looking nice! The offset computation is quite lite to be fair.

Sticky state API
I chose to declare the sticky in the column declaration.
Other possibilities: declaring a stickyColumns: … in the config, exposing an headless state manager (column.pin()) à la/with Tanstack table.
Pros: can only pin defined colmun no id mismatch.
Cons: can be a it of a burden to update since it's an object, start mixing definition and state management.

It's a good start to stay consistence with our other declarations (sort system for example).
You're right to point out that it can be heavy to declare if we have UIs where sticky columns could be change frequently by end users.

Sticky state internal
I went with initilazing a Map and reseting both the map and scroll on each change of columns (even non-sticky) and sorting columns based on sticky and their initial order.
Other possibilities: using array/indexes, pushing in order of selection, not messing with order (sticky only when "met"), using column.sticky in palce.
Pros: no need for index matching in 3 different places, everything derived from base store, seperation between user config and internal state.
Cons: May reset on colmuns update that didn't require it.

For a start, IMO it's acceptable that the scroll resets when columns option change. There is room for optimization

Other than that, i left some comment to fix minors issues

<svelte:component this={Format[dataFormat]} {rawValue} {...options} />
{/if}
<td
style={`--sticky-offset: ${$stickyColumnsOffset.get(column.key)}px;`}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to not declare this variable when stickyColumnsOffset[key] is undefined
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can set a generic type <key, number> for the Map objects

<div style={{marginBottom: '20px'}}>
<h3>Pinned columns</h3>
{stickyColumns.map(col => (
<>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<>
<React.Fragment key={col.key}>

Just because we have an error in the console

:global(.ods-dataviz--default td) {
padding: var(--spacing-75);
background-color: white;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

We lost the ability to apply a background color on tbody, tr

Maybe we could move this style to the tr tag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be on the td because we will have overlapping tds with no background elese. I changed the customization stories, it seems acceptable.

{#each columns as column}
<th class={`table-header--${column.dataFormat}`}>
{#each columns as column (column.key)}
<Th dataFormat={column.dataFormat} key={column.key} {isHorizontallyScrolled}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could put L14-L24 into the Th component ?

Comment on lines 19 to 21
class:sticky={$stickyColumnsWidth.has(key)}
class:isLastSticky={key === $lastStickyColumn}
class:isHorizontallyScrolled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three classes are shared between Th and Cell
What do you think to create a hook to build these sticky classes ?

Comment on lines 27 to 49
$: if (columns && scrollBox) {
scrollBox.scrollLeft = 0;
sortedStickyColumns = [...columns].sort((colA, colB) => {
if (Boolean(colA?.sticky) === Boolean(colB?.sticky)) {
return 0;
}
return colA?.sticky ? -1 : 1;
});
stickyColumnsWidth.reset();
sortedStickyColumns
.filter((col) => col?.sticky)
.forEach((col) => {
stickyColumnsWidth.updateColumn(col.key, 0);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$: if (columns && scrollBox) {
scrollBox.scrollLeft = 0;
sortedStickyColumns = [...columns].sort((colA, colB) => {
if (Boolean(colA?.sticky) === Boolean(colB?.sticky)) {
return 0;
}
return colA?.sticky ? -1 : 1;
});
stickyColumnsWidth.reset();
sortedStickyColumns
.filter((col) => col?.sticky)
.forEach((col) => {
stickyColumnsWidth.updateColumn(col.key, 0);
});
}
$: if (columns && scrollBox) {
scrollBox.scrollLeft = 0;
stickyColumnsWidth.reset();
columns.forEach((col) => {
col?.sticky && stickyColumnsWidth.updateColumn(col.key, 0);
});
}

Did I miss something?
Do we want to get update the store state with only columns keys that are sticky?
(While keeping the original order of the column)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to sort the sticky column first so that they are already in their sticky position. In the version you shared colmun will stick "as they come". I thought it was pretty common for "pinned" columns to be first. This could also be done by putting sticky columns in a different array (in which case we don't need to sort).

Copy link
Contributor

@KevinFabre-ods KevinFabre-ods Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...columns].sort((colA, colB) => {
            if (Boolean(colA?.sticky) === Boolean(colB?.sticky)) {
                return 0;
            }
            return colA?.sticky ? -1 : 1;
        });

or

columns.filter((col) => col?.sticky)

seems to produce the same result.
I don't understand why we need this sort function

@etienneburdet etienneburdet force-pushed the feature/sc-48047/table-sdk-sticky-columns branch from ffb8a3a to 8df9038 Compare December 12, 2024 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants