From 895f5b645fe3015d8373da2af119475e63d526ea Mon Sep 17 00:00:00 2001 From: tresabhi <43380238+tresabhi@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:41:44 -0600 Subject: [PATCH] nicer sticky tables --- .../src/components/CompareTable/index.tsx | 17 ++- .../PerformanceTable/index.module.css | 1 + .../src/components/PerformanceTable/index.tsx | 34 +++-- .../src/components/ScrollerCurtains/index.tsx | 12 +- .../components/StickyTable/index.module.css | 33 +++++ .../src/components/StickyTable/index.tsx | 119 ++++++++++++++++++ .../src/components/Table/index.module.css | 23 ++-- .../website/src/components/Table/index.tsx | 46 +------ .../TankSearchControls/index.module.css | 4 - .../components/TankSearchControls/index.tsx | 6 +- .../TankSearchFilters/index.module.css | 77 ++++++------ .../components/TankSearchFilters/index.tsx | 31 +++-- .../[...locale]/compare/index.astro | 2 +- .../[...locale]/performance/_index.module.css | 6 - .../[...locale]/performance/_index.tsx | 7 +- .../[...locale]/performance/index.astro | 5 +- .../[...locale]/tier-list/index.astro | 4 +- 17 files changed, 272 insertions(+), 155 deletions(-) create mode 100644 packages/website/src/components/StickyTable/index.module.css create mode 100644 packages/website/src/components/StickyTable/index.tsx delete mode 100644 packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.module.css diff --git a/packages/website/src/components/CompareTable/index.tsx b/packages/website/src/components/CompareTable/index.tsx index 809ec8d5f..453808558 100644 --- a/packages/website/src/components/CompareTable/index.tsx +++ b/packages/website/src/components/CompareTable/index.tsx @@ -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 ( - - + @@ -108,8 +115,8 @@ function Content() { /> ))} - - + } + > @@ -129,7 +136,7 @@ function Content() { characteristics={characteristics} /> ))} - + diff --git a/packages/website/src/components/PerformanceTable/index.module.css b/packages/website/src/components/PerformanceTable/index.module.css index 62f6d8d6f..86d76a319 100644 --- a/packages/website/src/components/PerformanceTable/index.module.css +++ b/packages/website/src/components/PerformanceTable/index.module.css @@ -9,6 +9,7 @@ .tank { display: flex; align-items: center; + justify-content: flex-start; gap: var(--space-2); white-space: nowrap; diff --git a/packages/website/src/components/PerformanceTable/index.tsx b/packages/website/src/components/PerformanceTable/index.tsx index f2cb04380..a9dc2460e 100644 --- a/packages/website/src/components/PerformanceTable/index.tsx +++ b/packages/website/src/components/PerformanceTable/index.tsx @@ -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 ( - - + }> - + ); } function Head() { return ( - - - + + - {performanceStatNames.map((name) => ( - - ))} - - + {performanceStatNames.map((name) => ( + + ))} + ); } @@ -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} > diff --git a/packages/website/src/components/ScrollerCurtains/index.tsx b/packages/website/src/components/ScrollerCurtains/index.tsx index 475e00084..51273872b 100644 --- a/packages/website/src/components/ScrollerCurtains/index.tsx +++ b/packages/website/src/components/ScrollerCurtains/index.tsx @@ -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(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 ; } diff --git a/packages/website/src/components/StickyTable/index.module.css b/packages/website/src/components/StickyTable/index.module.css new file mode 100644 index 000000000..b8b6241f3 --- /dev/null +++ b/packages/website/src/components/StickyTable/index.module.css @@ -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; + } +} diff --git a/packages/website/src/components/StickyTable/index.tsx b/packages/website/src/components/StickyTable/index.tsx new file mode 100644 index 000000000..84e865faf --- /dev/null +++ b/packages/website/src/components/StickyTable/index.tsx @@ -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(null); + const bodyScroller = useRef(null); + const headTable = useRef(null); + const bodyTable = useRef(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 = ( + + {columns.map((key) => ( + + ))} + + ); + + return ( + <> +
+
+ + + + {colgroup} + {head} + +
+
+ +
+ + + + {colgroup} + {children} + +
+ + ); +} diff --git a/packages/website/src/components/Table/index.module.css b/packages/website/src/components/Table/index.module.css index 2f879d3e2..a00834d93 100644 --- a/packages/website/src/components/Table/index.module.css +++ b/packages/website/src/components/Table/index.module.css @@ -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; } diff --git a/packages/website/src/components/Table/index.tsx b/packages/website/src/components/Table/index.tsx index 91271c64a..c33b05c4b 100644 --- a/packages/website/src/components/Table/index.tsx +++ b/packages/website/src/components/Table/index.tsx @@ -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(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 ( - - {children} -
- ); +function Root({ className, ...props }: ComponentProps<"table">) { + return ; } function Head(props: ComponentProps<"thead">) { @@ -111,8 +75,8 @@ function ColumnHeaderSortable({ direction === undefined ? undefined : direction === SortDirection.Ascending - ? "ascending" - : "descending" + ? "ascending" + : "descending" } {...props} > diff --git a/packages/website/src/components/TankSearchControls/index.module.css b/packages/website/src/components/TankSearchControls/index.module.css index bc2777292..63b90e89c 100644 --- a/packages/website/src/components/TankSearchControls/index.module.css +++ b/packages/website/src/components/TankSearchControls/index.module.css @@ -25,8 +25,4 @@ background-color: var(--gray-2); } - - .filters { - overflow-x: auto; - } } diff --git a/packages/website/src/components/TankSearchControls/index.tsx b/packages/website/src/components/TankSearchControls/index.tsx index 8f4b33c1b..3575d5bca 100644 --- a/packages/website/src/components/TankSearchControls/index.tsx +++ b/packages/website/src/components/TankSearchControls/index.tsx @@ -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({
-
- - -
+
); } diff --git a/packages/website/src/components/TankSearchFilters/index.module.css b/packages/website/src/components/TankSearchFilters/index.module.css index 4f60fc8e4..3e01fc4dd 100644 --- a/packages/website/src/components/TankSearchFilters/index.module.css +++ b/packages/website/src/components/TankSearchFilters/index.module.css @@ -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; + } } } diff --git a/packages/website/src/components/TankSearchFilters/index.tsx b/packages/website/src/components/TankSearchFilters/index.tsx index 89849deb7..1981923b2 100644 --- a/packages/website/src/components/TankSearchFilters/index.tsx +++ b/packages/website/src/components/TankSearchFilters/index.tsx @@ -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 ( -
- - - - +
+ - - +
+ + + + - - + + - - - + + - + + + + + +
); } diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/compare/index.astro b/packages/website/src/pages/[...chunk_default]/[...locale]/compare/index.astro index c7bef4511..bd8204b6c 100644 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/compare/index.astro +++ b/packages/website/src/pages/[...chunk_default]/[...locale]/compare/index.astro @@ -38,7 +38,7 @@ const { locale } = Astro.props;
-
+
diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.module.css b/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.module.css deleted file mode 100644 index d221ecf5f..000000000 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.module.css +++ /dev/null @@ -1,6 +0,0 @@ -.top { - display: flex; - flex-direction: column; - align-items: center; - gap: var(--space-4); -} diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.tsx b/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.tsx index 12082284e..835a6e14d 100644 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.tsx +++ b/packages/website/src/pages/[...chunk_default]/[...locale]/performance/_index.tsx @@ -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(() => ( <>
-
- -
+
-
+
diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/performance/index.astro b/packages/website/src/pages/[...chunk_default]/[...locale]/performance/index.astro index 3912ee3f4..f1937fad6 100644 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/performance/index.astro +++ b/packages/website/src/pages/[...chunk_default]/[...locale]/performance/index.astro @@ -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; - + + + diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/tier-list/index.astro b/packages/website/src/pages/[...chunk_default]/[...locale]/tier-list/index.astro index d07f0ca77..6df54bcde 100644 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/tier-list/index.astro +++ b/packages/website/src/pages/[...chunk_default]/[...locale]/tier-list/index.astro @@ -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;
-
- +