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 { IconButton } from "../IconButton";
import { InsertionMarker } from "../InsertionMarker"; import { InsertionMarker } from "../InsertionMarker";
import { LazyImage } from "../LazyImage"; import { LazyImage } from "../LazyImage";
import { StickyTable } from "../StickyTable";
import { Table } from "../Table"; import { Table } from "../Table";
import { Text } from "../Text"; import { Text } from "../Text";
import { Tooltip } from "../Tooltip"; import { Tooltip } from "../Tooltip";
@@ -84,6 +85,11 @@ function Content() {
}); });
}, [members, environment]); }, [members, environment]);
const columns = useMemo(
() => ["row-headers", ...members.map(({ key }) => key)],
[members],
);
if (members.length === 0) { if (members.length === 0) {
return ( return (
<EmptyState <EmptyState
@@ -95,8 +101,9 @@ function Content() {
return ( return (
<> <>
<Table.Root> <StickyTable
<Table.Head> columns={columns}
head={
<Table.Row> <Table.Row>
<Table.IconCorner /> <Table.IconCorner />
@@ -108,8 +115,8 @@ function Content() {
/> />
))} ))}
</Table.Row> </Table.Row>
</Table.Head> }
>
<Table.Body> <Table.Body>
<Table.Row> <Table.Row>
<Table.RowHeader> <Table.RowHeader>
@@ -129,7 +136,7 @@ function Content() {
characteristics={characteristics} characteristics={characteristics}
/> />
))} ))}
</Table.Root> </StickyTable>
<InsertionMarker ref={compareInsertionMarker} /> <InsertionMarker ref={compareInsertionMarker} />
</> </>
@@ -9,6 +9,7 @@
.tank { .tank {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: flex-start;
gap: var(--space-2); gap: var(--space-2);
white-space: nowrap; white-space: nowrap;
@@ -4,13 +4,7 @@ import {
type AverageDefinitionsAllStats, type AverageDefinitionsAllStats,
type TankDefinition, type TankDefinition,
} from "@blitzkit/protos"; } from "@blitzkit/protos";
import { import { memo, useEffect, useMemo, useRef, useState } from "react";
memo,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { api } from "../../api/dynamic"; import { api } from "../../api/dynamic";
import { import {
performanceStatNames, performanceStatNames,
@@ -37,6 +31,7 @@ import { InlineSkeleton } from "../InlineSkeleton";
import { LazyImage } from "../LazyImage"; import { LazyImage } from "../LazyImage";
import { LinkI18n } from "../LinkI18n"; import { LinkI18n } from "../LinkI18n";
import { Skeleton } from "../Skeleton"; import { Skeleton } from "../Skeleton";
import { StickyTable } from "../StickyTable";
import { Table } from "../Table"; import { Table } from "../Table";
import { Text } from "../Text"; import { Text } from "../Text";
import styles from "./index.module.css"; import styles from "./index.module.css";
@@ -57,6 +52,8 @@ const tanksWithAverages = Object.values(tankDefinitions.tanks).filter(
const exclusionRatio = const exclusionRatio =
discoveredIdsDefinitions.count / averageDefinitions.samples!.total; discoveredIdsDefinitions.count / averageDefinitions.samples!.total;
const COLUMNS = ["row-headers", ...performanceStatNames];
interface PerformanceRow { interface PerformanceRow {
tank: TankDefinition; tank: TankDefinition;
entry: PerformanceEntry; entry: PerformanceEntry;
@@ -102,24 +99,21 @@ export function PerformanceTable() {
} }
return ( return (
<Table.Root> <StickyTable columns={COLUMNS} head={<Head />}>
<Head />
<Body filtered={filtered} /> <Body filtered={filtered} />
</Table.Root> </StickyTable>
); );
} }
function Head() { function Head() {
return ( return (
<Table.Head> <Table.Row>
<Table.Row> <Table.IconCorner />
<Table.IconCorner />
{performanceStatNames.map((name) => ( {performanceStatNames.map((name) => (
<ColumnHeader key={name} name={name} /> <ColumnHeader key={name} name={name} />
))} ))}
</Table.Row> </Table.Row>
</Table.Head>
); );
} }
@@ -332,8 +326,8 @@ function TankLink({ tank }: TankLinkProps) {
tank.type === TankType.TANK_TYPE_COLLECTOR tank.type === TankType.TANK_TYPE_COLLECTOR
? "blue" ? "blue"
: tank.type === TankType.TANK_TYPE_PREMIUM : tank.type === TankType.TANK_TYPE_PREMIUM
? "amber" ? "amber"
: "gray" : "gray"
} }
highContrast={tank.type === TankType.TANK_TYPE_RESEARCHABLE} 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 // Whether the fade at each end of the scroll axis is rendered
startCurtain?: boolean; startCurtain?: boolean;
endCurtain?: 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; const MAX_FADE_DISTANCE = 60;
@@ -50,6 +54,7 @@ export function ScrollerCurtains({
orientation = ScrollerOrientation.Horizontal, orientation = ScrollerOrientation.Horizontal,
startCurtain = true, startCurtain = true,
endCurtain = true, endCurtain = true,
translateWheel = true,
}: ScrollerCurtainsProps) { }: ScrollerCurtainsProps) {
const anchor = useRef<HTMLSpanElement>(null!); const anchor = useRef<HTMLSpanElement>(null!);
@@ -118,7 +123,8 @@ export function ScrollerCurtains({
event.preventDefault(); event.preventDefault();
element.scrollLeft += delta; element.scrollLeft += delta;
}; };
const horizontal = orientation === ScrollerOrientation.Horizontal; const translatesWheel =
translateWheel && orientation === ScrollerOrientation.Horizontal;
const resizeObserver = new ResizeObserver(handleSync); const resizeObserver = new ResizeObserver(handleSync);
// Tracks children as they mount and unmount instead of re-subscribing every render // 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 }); mutationObserver.observe(element, { childList: true });
element.addEventListener("scroll", handleSync, { passive: true }); element.addEventListener("scroll", handleSync, { passive: true });
if (horizontal) element.addEventListener("wheel", handleWheel); if (translatesWheel) element.addEventListener("wheel", handleWheel);
handleSync(); handleSync();
@@ -157,7 +163,7 @@ export function ScrollerCurtains({
} }
} }
}; };
}, [sync, orientation]); }, [sync, orientation, translateWheel]);
return <span ref={anchor} className={styles.anchor} />; 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, th,
td { td {
padding: var(--space-3); padding: var(--space-3) var(--space-4);
text-align: center; text-align: center;
font-weight: inherit; font-weight: inherit;
} }
@@ -21,10 +21,6 @@
} }
.column-header { .column-header {
position: sticky;
top: 0;
z-index: 1;
padding-top: var(--space-4); padding-top: var(--space-4);
padding-bottom: var(--space-4); padding-bottom: var(--space-4);
@@ -47,23 +43,25 @@
} }
.corner { .corner {
position: sticky;
left: 0; left: 0;
z-index: 1; z-index: 1;
} }
/* Fills the cell inside its padding, so the row's other headers decide the size */ /* Fills the cell inside its padding, so the row's other headers decide the size */
.logo { .logo {
--padding: var(--space-3); --padding-x: var(--space-4);
--padding-y: var(--space-3);
--max-height: 4rem; --max-height: 4rem;
position: absolute; position: absolute;
top: 50%; top: 50%;
left: var(--padding); left: var(--padding-x);
transform: translateY(-50%); transform: translateY(-50%);
width: calc(100% - var(--padding) * 2); width: calc(100% - var(--padding-x) * 2);
height: min(var(--max-height), calc(100% - var(--padding) * 2)); height: min(var(--max-height), calc(100% - var(--padding-y) * 2));
color: var(--gray-6); color: var(--gray-6);
} }
@@ -78,10 +76,6 @@
.group-title, .group-title,
.group-cell { .group-cell {
position: sticky;
top: var(--head-height, 0);
z-index: 1;
padding-top: var(--space-4); padding-top: var(--space-4);
padding-bottom: var(--space-4); padding-bottom: var(--space-4);
@@ -89,7 +83,10 @@
} }
.group-title { .group-title {
position: sticky;
left: 0; left: 0;
z-index: 1;
color: var(--gray-11); color: var(--gray-11);
text-align: left; text-align: left;
} }
@@ -3,51 +3,15 @@ import {
CaretSortIcon, CaretSortIcon,
CaretUpIcon, CaretUpIcon,
} from "@radix-ui/react-icons"; } from "@radix-ui/react-icons";
import { import { type ComponentProps, type CSSProperties } from "react";
useEffect,
useRef,
type ComponentProps,
type CSSProperties,
} from "react";
import { Logo } from "../../icons/Logo"; import { Logo } from "../../icons/Logo";
import { SortDirection } from "../../stores/tankopediaPersistent"; import { SortDirection } from "../../stores/tankopediaPersistent";
import { CharacteristicRanking } from "../../types/characteristics"; import { CharacteristicRanking } from "../../types/characteristics";
import { classNames } from "../../ui/classNames"; import { classNames } from "../../ui/classNames";
import styles from "./index.module.css"; import styles from "./index.module.css";
interface RootProps extends ComponentProps<"table"> {} function Root({ className, ...props }: ComponentProps<"table">) {
return <table className={classNames(styles.table, className)} {...props} />;
// 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 Head(props: ComponentProps<"thead">) { function Head(props: ComponentProps<"thead">) {
@@ -111,8 +75,8 @@ function ColumnHeaderSortable({
direction === undefined direction === undefined
? undefined ? undefined
: direction === SortDirection.Ascending : direction === SortDirection.Ascending
? "ascending" ? "ascending"
: "descending" : "descending"
} }
{...props} {...props}
> >
@@ -25,8 +25,4 @@
background-color: var(--gray-2); background-color: var(--gray-2);
} }
.filters {
overflow-x: auto;
}
} }
@@ -1,6 +1,5 @@
import type { TankDefinition } from "@blitzkit/protos"; import type { TankDefinition } from "@blitzkit/protos";
import type { LocaleAcceptorProps } from "../../hooks/useLocale"; import type { LocaleAcceptorProps } from "../../hooks/useLocale";
import { ScrollerCurtains } from "../ScrollerCurtains";
import { TankSearchBar } from "../TankSearchBar"; import { TankSearchBar } from "../TankSearchBar";
import { TankSearchFilters } from "../TankSearchFilters"; import { TankSearchFilters } from "../TankSearchFilters";
import styles from "./index.module.css"; import styles from "./index.module.css";
@@ -26,10 +25,7 @@ export function TankSearchControls({
<div className={styles.controls} data-surface={surface}> <div className={styles.controls} data-surface={surface}>
<TankSearchBar locale={locale} onSelect={onSelect} /> <TankSearchBar locale={locale} onSelect={onSelect} />
<div className={styles.filters}> <TankSearchFilters locale={locale} />
<ScrollerCurtains />
<TankSearchFilters locale={locale} />
</div>
</div> </div>
); );
} }
@@ -1,39 +1,46 @@
.filters { /* Scrolls sideways when the filters don't fit */
display: flex; .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); .filters {
.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; 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 { ResearchedIcon } from "../ResearchedIcon";
import { ScienceIcon } from "../ScienceIcon"; import { ScienceIcon } from "../ScienceIcon";
import { ScienceOffIcon } from "../ScienceOffIcon"; import { ScienceOffIcon } from "../ScienceOffIcon";
import { ScrollerCurtains } from "../ScrollerCurtains";
import { Tooltip } from "../Tooltip"; import { Tooltip } from "../Tooltip";
import styles from "./index.module.css"; import styles from "./index.module.css";
@@ -154,23 +155,27 @@ export const TankSearchFilters = withLocale(Content);
function Content() { function Content() {
return ( return (
<div className={styles.filters}> <div className={styles.scroller}>
<TiersFilter /> <ScrollerCurtains />
<ClassFilter />
<TypeFilter />
<NationsFilter />
<OwnershipFilter /> <div className={styles.filters}>
<TestFilter /> <TiersFilter />
<ClassFilter />
<TypeFilter />
<NationsFilter />
<GunTypeFilter /> <OwnershipFilter />
<ShellFilter /> <TestFilter />
<ConsumablesFilter /> <GunTypeFilter />
<ProvisionsFilter /> <ShellFilter />
<GameModeAbilitiesFilter />
<ResetButton /> <ConsumablesFilter />
<ProvisionsFilter />
<GameModeAbilitiesFilter />
<ResetButton />
</div>
</div> </div>
); );
} }
@@ -38,7 +38,7 @@ const { locale } = Astro.props;
</div> </div>
</Section> </Section>
<Section overflow> <Section>
<CompareTable {locale} client:load /> <CompareTable {locale} client:load />
</Section> </Section>
</Sections> </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 { Section } from "../../../../components/Section";
import { withErrorWrapper } from "../../../../hocs/withErrorWrapper"; import { withErrorWrapper } from "../../../../hocs/withErrorWrapper";
import { withLocale } from "../../../../hocs/withLocale"; import { withLocale } from "../../../../hocs/withLocale";
import styles from "./_index.module.css";
export const Page = withLocale( export const Page = withLocale(
withErrorWrapper(() => ( withErrorWrapper(() => (
<> <>
<Section> <Section>
<div className={styles.top}> <PerformanceControls />
<PerformanceControls />
</div>
</Section> </Section>
<Section overflow> <Section>
<PerformanceTable /> <PerformanceTable />
</Section> </Section>
</> </>
@@ -1,5 +1,6 @@
--- ---
import ColorScale from "../../../../components/ColorScale.astro"; import ColorScale from "../../../../components/ColorScale.astro";
import Sections from "../../../../components/Sections.astro";
import ToolBanner from "../../../../components/ToolBanner.astro"; import ToolBanner from "../../../../components/ToolBanner.astro";
import { tools } from "../../../../config/tools"; import { tools } from "../../../../config/tools";
import BaseLayout from "../../../../layouts/BaseLayout.astro"; import BaseLayout from "../../../../layouts/BaseLayout.astro";
@@ -18,5 +19,7 @@ const { locale } = Astro.props;
<BaseLayout tool={tools.performance} accent="jade" {locale}> <BaseLayout tool={tools.performance} accent="jade" {locale}>
<!-- TODO: add svg art for this tool --> <!-- TODO: add svg art for this tool -->
<ToolBanner tool={tools.performance} {locale} /> <ToolBanner tool={tools.performance} {locale} />
<Page {locale} client:load /> <Sections>
<Page {locale} client:load />
</Sections>
</BaseLayout> </BaseLayout>
@@ -1,6 +1,5 @@
--- ---
import ColorScale from "../../../../components/ColorScale.astro"; import ColorScale from "../../../../components/ColorScale.astro";
import { ScrollerCurtains } from "../../../../components/ScrollerCurtains";
import { Section } from "../../../../components/Section"; import { Section } from "../../../../components/Section";
import { TankSearchFilters } from "../../../../components/TankSearchFilters"; import { TankSearchFilters } from "../../../../components/TankSearchFilters";
import { TierListControls } from "../../../../components/TierListControls"; import { TierListControls } from "../../../../components/TierListControls";
@@ -36,8 +35,7 @@ const { locale } = Astro.props;
<TierListTable {locale} client:load /> <TierListTable {locale} client:load />
</Section> </Section>
<Section overflow> <Section>
<ScrollerCurtains client:load />
<TankSearchFilters {locale} client:load /> <TankSearchFilters {locale} client:load />
</Section> </Section>