nicer sticky tables

This commit is contained in:
tresabhi committed 2026-12-03 22:41:44 -06:00
1 parent 38f751a8b0
commit 895f5b645f
17 files changed
+272 -155

No files matched your search

@@ -34,6 +34,7 @@ import { EmptyState } from "../EmptyState";
import { IconButton } from "../IconButton";
import { InsertionMarker } from "../InsertionMarker";
import { LazyImage } from "../LazyImage";
import { StickyTable } from "../StickyTable";
import { Table } from "../Table";
import { Text } from "../Text";
import { Tooltip } from "../Tooltip";
@@ -84,6 +85,11 @@ function Content() {
});
}, [members, environment]);
const columns = useMemo(
() => ["row-headers", ...members.map(({ key }) => key)],
[members],
);
if (members.length === 0) {
return (
<EmptyState
@@ -95,8 +101,9 @@ function Content() {
return (
<>
<Table.Root>
<Table.Head>
<StickyTable
columns={columns}
head={
<Table.Row>
<Table.IconCorner />
@@ -108,8 +115,8 @@ function Content() {
/>
))}
</Table.Row>
</Table.Head>
}
>
<Table.Body>
<Table.Row>
<Table.RowHeader>
@@ -129,7 +136,7 @@ function Content() {
characteristics={characteristics}
/>
))}
</Table.Root>
</StickyTable>
<InsertionMarker ref={compareInsertionMarker} />
</>
@@ -9,6 +9,7 @@
.tank {
display: flex;
align-items: center;
justify-content: flex-start;
gap: var(--space-2);
white-space: nowrap;
@@ -4,13 +4,7 @@ import {
type AverageDefinitionsAllStats,
type TankDefinition,
} from "@blitzkit/protos";
import {
memo,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { memo, useEffect, useMemo, useRef, useState } from "react";
import { api } from "../../api/dynamic";
import {
performanceStatNames,
@@ -37,6 +31,7 @@ import { InlineSkeleton } from "../InlineSkeleton";
import { LazyImage } from "../LazyImage";
import { LinkI18n } from "../LinkI18n";
import { Skeleton } from "../Skeleton";
import { StickyTable } from "../StickyTable";
import { Table } from "../Table";
import { Text } from "../Text";
import styles from "./index.module.css";
@@ -57,6 +52,8 @@ const tanksWithAverages = Object.values(tankDefinitions.tanks).filter(
const exclusionRatio =
discoveredIdsDefinitions.count / averageDefinitions.samples!.total;
const COLUMNS = ["row-headers", ...performanceStatNames];
interface PerformanceRow {
tank: TankDefinition;
entry: PerformanceEntry;
@@ -102,24 +99,21 @@ export function PerformanceTable() {
}
return (
<Table.Root>
<Head />
<StickyTable columns={COLUMNS} head={<Head />}>
<Body filtered={filtered} />
</Table.Root>
</StickyTable>
);
}
function Head() {
return (
<Table.Head>
<Table.Row>
<Table.IconCorner />
<Table.Row>
<Table.IconCorner />
{performanceStatNames.map((name) => (
<ColumnHeader key={name} name={name} />
))}
</Table.Row>
</Table.Head>
{performanceStatNames.map((name) => (
<ColumnHeader key={name} name={name} />
))}
</Table.Row>
);
}
@@ -332,8 +326,8 @@ function TankLink({ tank }: TankLinkProps) {
tank.type === TankType.TANK_TYPE_COLLECTOR
? "blue"
: tank.type === TankType.TANK_TYPE_PREMIUM
? "amber"
: "gray"
? "amber"
: "gray"
}
highContrast={tank.type === TankType.TANK_TYPE_RESEARCHABLE}
>
@@ -19,6 +19,10 @@ interface ScrollerCurtainsProps {
// Whether the fade at each end of the scroll axis is rendered
startCurtain?: boolean;
endCurtain?: boolean;
// Whether a horizontal scroller turns vertical mouse wheels sideways. Tall
// scrollers, like tables, need the wheel to keep scrolling the page
translateWheel?: boolean;
}
const MAX_FADE_DISTANCE = 60;
@@ -50,6 +54,7 @@ export function ScrollerCurtains({
orientation = ScrollerOrientation.Horizontal,
startCurtain = true,
endCurtain = true,
translateWheel = true,
}: ScrollerCurtainsProps) {
const anchor = useRef<HTMLSpanElement>(null!);
@@ -118,7 +123,8 @@ export function ScrollerCurtains({
event.preventDefault();
element.scrollLeft += delta;
};
const horizontal = orientation === ScrollerOrientation.Horizontal;
const translatesWheel =
translateWheel && orientation === ScrollerOrientation.Horizontal;
const resizeObserver = new ResizeObserver(handleSync);
// Tracks children as they mount and unmount instead of re-subscribing every render
@@ -140,7 +146,7 @@ export function ScrollerCurtains({
);
mutationObserver.observe(element, { childList: true });
element.addEventListener("scroll", handleSync, { passive: true });
if (horizontal) element.addEventListener("wheel", handleWheel);
if (translatesWheel) element.addEventListener("wheel", handleWheel);
handleSync();
@@ -157,7 +163,7 @@ export function ScrollerCurtains({
}
}
};
}, [sync, orientation]);
}, [sync, orientation, translateWheel]);
return <span ref={anchor} className={styles.anchor} />;
}
@@ -0,0 +1,33 @@
/* Reaches the screen edges through the page padding, keeping it as an inset */
.scroller {
overflow-x: auto;
overscroll-behavior-x: contain;
margin-inline: calc(-1 * var(--page-padding));
padding-inline: var(--page-padding);
/* Keeps the body's z-index: 1 cells under the sticky head */
isolation: isolate;
.body-table {
border-top-left-radius: 0;
border-top-right-radius: 0;
}
}
/* Sticks to the page. Kept apart from its scroller, and as wide, so the curtains fade the head into this opaque background rather than the rows scrolling under it */
.head {
position: sticky;
top: var(--navbar-big-height, 0);
z-index: 1;
margin-inline: calc(-1 * var(--page-padding));
padding-inline: var(--page-padding);
background-color: var(--gray-1);
.head-table {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
}
@@ -0,0 +1,119 @@
import { useEffect, useRef, type ReactNode } from "react";
import { ScrollerCurtains } from "../ScrollerCurtains";
import { Table } from "../Table";
import styles from "./index.module.css";
interface StickyTableProps {
// A key per column, the row headers' first
columns: string[];
// The head's rows
head: ReactNode;
// The body's tbodies
children: ReactNode;
}
// A table whose head sticks to the page while it scrolls sideways. Sticky
// content can't stick past a scroller, so the head is a table of its own,
// sharing the body's column widths and horizontal scroll
export function StickyTable({ columns, head, children }: StickyTableProps) {
const headScroller = useRef<HTMLDivElement>(null);
const bodyScroller = useRef<HTMLDivElement>(null);
const headTable = useRef<HTMLTableElement>(null);
const bodyTable = useRef<HTMLTableElement>(null);
useEffect(() => {
const headElement = headScroller.current!;
const bodyElement = bodyScroller.current!;
const headCells = headTable.current!.rows[0].cells;
const bodyCells = bodyTable.current!.rows[0].cells;
const headColumns = headTable.current!.getElementsByTagName("col");
const bodyColumns = bodyTable.current!.getElementsByTagName("col");
// Each column takes the wider of its head and body, measured with the
// widths from last time cleared
const observer = new ResizeObserver(() => {
for (let index = 0; index < headColumns.length; index++) {
headColumns[index].style.width = "";
bodyColumns[index].style.width = "";
}
const widths = Array.from(
headCells,
(cell, index) =>
`${Math.max(
cell.getBoundingClientRect().width,
bodyCells[index].getBoundingClientRect().width,
)}px`,
);
for (let index = 0; index < widths.length; index++) {
headColumns[index].style.width = widths[index];
bodyColumns[index].style.width = widths[index];
}
});
// Following fires a scroll on the follower; echoing it back would drag the
// scroller in use to a stale position
let echo: HTMLElement | null = null;
function follow(source: HTMLElement, target: HTMLElement) {
return () => {
if (echo === source) {
echo = null;
return;
}
if (target.scrollLeft === source.scrollLeft) return;
echo = target;
target.scrollLeft = source.scrollLeft;
};
}
const followHead = follow(headElement, bodyElement);
const followBody = follow(bodyElement, headElement);
for (const cell of headCells) observer.observe(cell);
for (const cell of bodyCells) observer.observe(cell);
headElement.addEventListener("scroll", followHead, { passive: true });
bodyElement.addEventListener("scroll", followBody, { passive: true });
return () => {
observer.disconnect();
headElement.removeEventListener("scroll", followHead);
bodyElement.removeEventListener("scroll", followBody);
};
}, [columns]);
const colgroup = (
<colgroup>
{columns.map((key) => (
<col key={key} />
))}
</colgroup>
);
return (
<>
<div className={styles.head}>
<div ref={headScroller} className={styles.scroller}>
<ScrollerCurtains translateWheel={false} />
<Table.Root ref={headTable} className={styles["head-table"]}>
{colgroup}
<Table.Head>{head}</Table.Head>
</Table.Root>
</div>
</div>
<div ref={bodyScroller} className={styles.scroller}>
<ScrollerCurtains translateWheel={false} />
<Table.Root ref={bodyTable} className={styles["body-table"]}>
{colgroup}
{children}
</Table.Root>
</div>
</>
);
}
@@ -10,7 +10,7 @@
th,
td {
padding: var(--space-3);
padding: var(--space-3) var(--space-4);
text-align: center;
font-weight: inherit;
}
@@ -21,10 +21,6 @@
}
.column-header {
position: sticky;
top: 0;
z-index: 1;
padding-top: var(--space-4);
padding-bottom: var(--space-4);
@@ -47,23 +43,25 @@
}
.corner {
position: sticky;
left: 0;
z-index: 1;
}
/* Fills the cell inside its padding, so the row's other headers decide the size */
.logo {
--padding: var(--space-3);
--padding-x: var(--space-4);
--padding-y: var(--space-3);
--max-height: 4rem;
position: absolute;
top: 50%;
left: var(--padding);
left: var(--padding-x);
transform: translateY(-50%);
width: calc(100% - var(--padding) * 2);
height: min(var(--max-height), calc(100% - var(--padding) * 2));
width: calc(100% - var(--padding-x) * 2);
height: min(var(--max-height), calc(100% - var(--padding-y) * 2));
color: var(--gray-6);
}
@@ -78,10 +76,6 @@
.group-title,
.group-cell {
position: sticky;
top: var(--head-height, 0);
z-index: 1;
padding-top: var(--space-4);
padding-bottom: var(--space-4);
@@ -89,7 +83,10 @@
}
.group-title {
position: sticky;
left: 0;
z-index: 1;
color: var(--gray-11);
text-align: left;
}
@@ -3,51 +3,15 @@ import {
CaretSortIcon,
CaretUpIcon,
} from "@radix-ui/react-icons";
import {
useEffect,
useRef,
type ComponentProps,
type CSSProperties,
} from "react";
import { type ComponentProps, type CSSProperties } from "react";
import { Logo } from "../../icons/Logo";
import { SortDirection } from "../../stores/tankopediaPersistent";
import { CharacteristicRanking } from "../../types/characteristics";
import { classNames } from "../../ui/classNames";
import styles from "./index.module.css";
interface RootProps extends ComponentProps<"table"> {}
// When scrollable, scrolls both ways, keeping the head, the row headers and the group titles in view
function Root({ children, className, ...props }: RootProps) {
const table = useRef<HTMLTableElement>(null);
// Group titles stick right below the head, whatever its height
useEffect(() => {
const head = table.current!.tHead;
if (!head) return;
const observer = new ResizeObserver(() => {
table.current!.style.setProperty(
"--head-height",
`${head.offsetHeight}px`,
);
});
observer.observe(head);
return () => observer.disconnect();
}, []);
return (
<table
ref={table}
className={classNames(styles.table, className)}
{...props}
>
{children}
</table>
);
function Root({ className, ...props }: ComponentProps<"table">) {
return <table className={classNames(styles.table, className)} {...props} />;
}
function Head(props: ComponentProps<"thead">) {
@@ -111,8 +75,8 @@ function ColumnHeaderSortable({
direction === undefined
? undefined
: direction === SortDirection.Ascending
? "ascending"
: "descending"
? "ascending"
: "descending"
}
{...props}
>
@@ -25,8 +25,4 @@
background-color: var(--gray-2);
}
.filters {
overflow-x: auto;
}
}
@@ -1,6 +1,5 @@
import type { TankDefinition } from "@blitzkit/protos";
import type { LocaleAcceptorProps } from "../../hooks/useLocale";
import { ScrollerCurtains } from "../ScrollerCurtains";
import { TankSearchBar } from "../TankSearchBar";
import { TankSearchFilters } from "../TankSearchFilters";
import styles from "./index.module.css";
@@ -26,10 +25,7 @@ export function TankSearchControls({
<div className={styles.controls} data-surface={surface}>
<TankSearchBar locale={locale} onSelect={onSelect} />
<div className={styles.filters}>
<ScrollerCurtains />
<TankSearchFilters locale={locale} />
</div>
<TankSearchFilters locale={locale} />
</div>
);
}
@@ -1,39 +1,46 @@
.filters {
display: flex;
/* Scrolls sideways when the filters don't fit */
.scroller {
/* A centered parent sizes it to its content, which is never narrower than the full row of filters */
max-width: 100%;
overflow-x: auto;
gap: var(--space-3);
.nation {
.flag {
position: absolute;
top: 0;
right: 0;
width: var(--space-7);
height: 100%;
background-size: 200%;
background-position: 0% 50%;
}
}
.plus {
color: var(--gray-11);
}
.icon {
filter: drop-shadow(var(--shadow-icon));
& + &:not([data-no-overlap]) {
margin-left: calc(-0.5em - var(--space-3));
}
&[data-no-overlap] {
margin: 0 calc(-1 * var(--space-1));
}
}
.grouped {
.filters {
display: flex;
gap: var(--space-3);
.nation {
.flag {
position: absolute;
top: 0;
right: 0;
width: var(--space-7);
height: 100%;
background-size: 200%;
background-position: 0% 50%;
}
}
.plus {
color: var(--gray-11);
}
.icon {
filter: drop-shadow(var(--shadow-icon));
& + &:not([data-no-overlap]) {
margin-left: calc(-0.5em - var(--space-3));
}
&[data-no-overlap] {
margin: 0 calc(-1 * var(--space-1));
}
}
.grouped {
display: flex;
}
}
}
@@ -27,6 +27,7 @@ import { MissingShellIcon } from "../MissingShellIcon";
import { ResearchedIcon } from "../ResearchedIcon";
import { ScienceIcon } from "../ScienceIcon";
import { ScienceOffIcon } from "../ScienceOffIcon";
import { ScrollerCurtains } from "../ScrollerCurtains";
import { Tooltip } from "../Tooltip";
import styles from "./index.module.css";
@@ -154,23 +155,27 @@ export const TankSearchFilters = withLocale(Content);
function Content() {
return (
<div className={styles.filters}>
<TiersFilter />
<ClassFilter />
<TypeFilter />
<NationsFilter />
<div className={styles.scroller}>
<ScrollerCurtains />
<OwnershipFilter />
<TestFilter />
<div className={styles.filters}>
<TiersFilter />
<ClassFilter />
<TypeFilter />
<NationsFilter />
<GunTypeFilter />
<ShellFilter />
<OwnershipFilter />
<TestFilter />
<ConsumablesFilter />
<ProvisionsFilter />
<GameModeAbilitiesFilter />
<GunTypeFilter />
<ShellFilter />
<ResetButton />
<ConsumablesFilter />
<ProvisionsFilter />
<GameModeAbilitiesFilter />
<ResetButton />
</div>
</div>
);
}
@@ -38,7 +38,7 @@ const { locale } = Astro.props;
</div>
</Section>
<Section overflow>
<Section>
<CompareTable {locale} client:load />
</Section>
</Sections>
@@ -1,6 +0,0 @@
.top {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--space-4);
}
@@ -3,18 +3,15 @@ import { PerformanceTable } from "../../../../components/PerformanceTable";
import { Section } from "../../../../components/Section";
import { withErrorWrapper } from "../../../../hocs/withErrorWrapper";
import { withLocale } from "../../../../hocs/withLocale";
import styles from "./_index.module.css";
export const Page = withLocale(
withErrorWrapper(() => (
<>
<Section>
<div className={styles.top}>
<PerformanceControls />
</div>
<PerformanceControls />
</Section>
<Section overflow>
<Section>
<PerformanceTable />
</Section>
</>
@@ -1,5 +1,6 @@
---
import ColorScale from "../../../../components/ColorScale.astro";
import Sections from "../../../../components/Sections.astro";
import ToolBanner from "../../../../components/ToolBanner.astro";
import { tools } from "../../../../config/tools";
import BaseLayout from "../../../../layouts/BaseLayout.astro";
@@ -18,5 +19,7 @@ const { locale } = Astro.props;
<BaseLayout tool={tools.performance} accent="jade" {locale}>
<!-- TODO: add svg art for this tool -->
<ToolBanner tool={tools.performance} {locale} />
<Page {locale} client:load />
<Sections>
<Page {locale} client:load />
</Sections>
</BaseLayout>
@@ -1,6 +1,5 @@
---
import ColorScale from "../../../../components/ColorScale.astro";
import { ScrollerCurtains } from "../../../../components/ScrollerCurtains";
import { Section } from "../../../../components/Section";
import { TankSearchFilters } from "../../../../components/TankSearchFilters";
import { TierListControls } from "../../../../components/TierListControls";
@@ -36,8 +35,7 @@ const { locale } = Astro.props;
<TierListTable {locale} client:load />
</Section>
<Section overflow>
<ScrollerCurtains client:load />
<Section>
<TankSearchFilters {locale} client:load />
</Section>