performance table rewrite and shared sort direction

This commit is contained in:
tresabhi committed 2026-11-17 22:01:46 -06:00
1 parent 966651284d
commit 0578f47914
38 files changed
+965 -874

No files matched your search

@@ -20,7 +20,7 @@
/* Row dividers sit inside the cells, so they take no space. Borders on a row itself don't paint with separate borders, so rows apply them to their cells here */
tbody tr > * {
box-shadow: inset 0 var(--space-0) var(--gray-a4);
box-shadow: inset 0 var(--space-0) var(--gray-5);
}
.column-header {
@@ -32,11 +32,43 @@
padding-bottom: var(--space-4);
background-color: var(--gray-3);
&[data-sortable] {
cursor: pointer;
}
&[data-sorted="true"] {
color: var(--accent-11);
}
}
/* Inline so the cell's alignment still centers it */
.sortable {
display: inline-flex;
align-items: center;
gap: var(--space-2);
}
.corner {
left: 0;
z-index: 2;
z-index: 1;
}
/* Fills the cell inside its padding, so the row's other headers decide the size */
.logo {
--padding: var(--space-3);
--max-height: 4rem;
position: absolute;
top: 50%;
left: var(--padding);
transform: translateY(-50%);
width: calc(100% - var(--padding) * 2);
height: min(var(--max-height), calc(100% - var(--padding) * 2));
color: var(--gray-6);
}
.row-header {
@@ -81,5 +113,21 @@
&[data-align="end"] {
text-align: right;
}
&[data-delta="better"] {
background-color: color-mix(
in srgb,
var(--jade-5) var(--intensity),
var(--jade-1)
);
}
&[data-delta="worse"] {
background-color: color-mix(
in srgb,
var(--red-5) var(--intensity),
var(--red-1)
);
}
}
}
+106 -2
View File
@@ -1,4 +1,17 @@
import { useEffect, useRef, type ComponentProps } from "react";
import {
CaretDownIcon,
CaretSortIcon,
CaretUpIcon,
} from "@radix-ui/react-icons";
import {
useEffect,
useRef,
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 { Scroller, ScrollerOrientation } from "../Scroller";
import styles from "./index.module.css";
@@ -21,7 +34,7 @@ function Root({ children, className, scrollable, ...props }: RootProps) {
const observer = new ResizeObserver(() => {
table.current!.style.setProperty(
"--head-height",
`${head.offsetHeight}px`
`${head.offsetHeight}px`,
);
});
@@ -78,12 +91,62 @@ function Corner({ className, ...props }: ComponentProps<"th">) {
);
}
function IconCorner({ children, ...props }: ComponentProps<"th">) {
return (
<Corner {...props}>
<Logo className={styles.logo} />
{children}
</Corner>
);
}
function ColumnHeader({ className, ...props }: ComponentProps<"th">) {
return (
<th className={classNames(styles["column-header"], className)} {...props} />
);
}
interface ColumnHeaderSortableProps extends ComponentProps<"th"> {
// Unsorted when undefined
direction: SortDirection | undefined;
onSort: () => void;
}
// The whole header toggles sorting, inheriting the header's font rather than a button's
function ColumnHeaderSortable({
className,
children,
direction,
onSort,
...props
}: ColumnHeaderSortableProps) {
return (
<th
className={classNames(styles["column-header"], className)}
onClick={onSort}
data-sortable
data-sorted={direction !== undefined}
aria-sort={
direction === undefined
? undefined
: direction === SortDirection.Ascending
? "ascending"
: "descending"
}
{...props}
>
{/* The cell stays a table cell, so its content lays out in here instead */}
<span className={styles.sortable}>
{children}
{direction === undefined && <CaretSortIcon />}
{direction === SortDirection.Ascending && <CaretUpIcon />}
{direction === SortDirection.Descending && <CaretDownIcon />}
</span>
</th>
);
}
function RowHeader({ className, ...props }: ComponentProps<"th">) {
return (
<th className={classNames(styles["row-header"], className)} {...props} />
@@ -124,15 +187,56 @@ function Cell({ className, align, ...props }: CellProps) {
);
}
interface CellRankedProps extends CellProps {
ranking: CharacteristicRanking | undefined;
value: number | undefined;
truth: number | undefined;
}
// Tints by how far the value strays from the truth, jade when better and red when worse
function CellRanked({
ranking,
value,
truth,
style,
...props
}: CellRankedProps) {
const isRanked =
ranking !== CharacteristicRanking.None &&
value !== undefined &&
truth !== undefined &&
value !== truth &&
Number.isFinite(value) &&
Number.isFinite(truth);
if (!isRanked) return <Cell style={style} {...props} />;
const higherIsBetter = ranking !== CharacteristicRanking.LowerIsBetter;
const better = higherIsBetter === value > truth;
const change = truth === 0 ? 1 : Math.abs(value / truth - 1);
const intensity = Math.round(Math.min(100, change * 2 * 100 + 25));
return (
<Cell
data-delta={better ? "better" : "worse"}
style={{ "--intensity": `${intensity}%`, ...style } as CSSProperties}
{...props}
/>
);
}
export const Table = {
Root,
Head,
Body,
Row,
Corner,
IconCorner,
ColumnHeader,
ColumnHeaderSortable,
RowHeader,
GroupTitle,
GroupCell,
Cell,
CellRanked,
};