Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions apps/www/src/app/examples/timeline-stress/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ const STATUS_COLOR: Record<Task['status'], string> = {
};

/**
* Card height is fixed at 64 to sit inside the 66px lane pitch. Under
* `virtualized` the lane height is exactly `estimatedRowHeight`, so a taller
* card would overflow into the lane below instead of growing its own.
* Card height is fixed at 64 × scaling to sit inside the default lane height
* of 66 × scaling. Under `virtualized` the lane height is exactly
* `estimatedRowHeight`, so a taller card would overflow toward the lane below
* instead of growing its own.
*/
function TaskCard({
task,
Expand All @@ -114,7 +115,7 @@ function TaskCard({
return (
<div
style={{
height: 64,
height: 'calc(64px * var(--rs-scaling, 1))',
boxSizing: 'border-box',
overflow: 'hidden',
padding: 'var(--rs-space-3)',
Expand Down
3 changes: 2 additions & 1 deletion apps/www/src/components/dataview-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,9 @@ function TaskCard({
}) {
// Card height is content-driven (the wrapper auto-measures, like
// DataView.List rows), so fix it here and collapsed stubs match full cards.
// The content scales with the theme, so the height scales too.
const chrome: React.CSSProperties = {
height: 64,
height: 'calc(64px * var(--rs-scaling, 1))',
boxSizing: 'border-box',
borderRadius: 'var(--rs-radius-3)',
border: '1px solid var(--rs-color-border-base-primary)',
Expand Down
2 changes: 2 additions & 0 deletions apps/www/src/content/docs/dataview/timeline.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Under `virtualized` this inverts. A culled card never reports a height, so measu

Give a card an explicit height if you want uniform cards, and keep that height within the pitch if you virtualize.

The default `estimatedRowHeight` scales with the theme's `scaling`. If the scaling can change, scale an explicit card height too, for example `calc(64px * var(--rs-scaling, 1))`.

Keep `renderCard` referentially stable by defining it outside the component or wrapping it in `useCallback`. Cards are memoized against it, and an inline closure forces every visible card to re-render on each scroll frame.

### Point markers
Expand Down
50 changes: 48 additions & 2 deletions packages/react/components/data-view/__tests__/timeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
import { Theme } from '../../theme';
import { SCALINGS } from '../../theme/settings';

// biome-ignore lint/suspicious/noShadowRestrictedNames: legitimate export name
import { DataView } from '../data-view';
Expand All @@ -13,7 +14,12 @@ import type {
TimelineActions
} from '../data-view.types';
import { useDataView } from '../hooks/useDataView';
import { buildAxis, createTimeScale, toTimestamp } from '../utils/time-scale';
import {
buildAxis,
createTimeScale,
TIMELINE_DEFAULT_UNIT_WIDTH,
toTimestamp
} from '../utils/time-scale';

beforeAll(() => {
// jsdom doesn't implement ResizeObserver, and the timeline observes its scroll
Expand Down Expand Up @@ -198,6 +204,41 @@ describe('buildAxis', () => {
expect(roomy.every(t => t.showLabel)).toBe(true);
});

it('labels at exact multiples of the label spacing despite float error', () => {
// The call site divides by scaling: 29.4 / 1.05 = 27.999999999999996.
expect(
buildAxis(januaryScale, 'day', 29.4 / 1.05).ticks.every(t => t.showLabel)
).toBe(true);
const half = buildAxis(januaryScale, 'day', 14.7 / 1.05).ticks;
expect(half.map(t => t.showLabel).slice(0, 4)).toEqual([
true,
false,
true,
false
]);
});

it('keeps the default label spacing at every scaling', () => {
for (const s of SCALINGS) {
const scaling = Number(s);
const every = (['day', 'week', 'month', 'quarter'] as const).map(
scale => {
const unitWidth = TIMELINE_DEFAULT_UNIT_WIDTH[scale] * scaling;
const scaleForUnit = createTimeScale({
minTime: new Date(2025, 0, 1).getTime(),
maxTime: new Date(2027, 0, 1).getTime(),
scale,
unitWidth,
padUnits: 0
});
const { ticks } = buildAxis(scaleForUnit, scale, unitWidth / scaling);
return ticks.findIndex((t, i) => i > 0 && t.showLabel);
}
);
expect(every).toEqual([2, 1, 1, 1]);
}
});

it('labels every Nth unit when labelEvery is passed', () => {
const roomyScale = createTimeScale({
minTime: new Date(2025, 0, 1).getTime(),
Expand Down Expand Up @@ -2225,7 +2266,7 @@ describe('DataView.Timeline theme scaling', () => {
];

function renderScaled(
scaling: '1' | '1.1',
scaling: '1' | '1.05' | '1.1',
props: Partial<DataViewTimelineProps<Order>> = {},
data: Order[] = scaledOrders
) {
Expand Down Expand Up @@ -2319,6 +2360,11 @@ describe('DataView.Timeline theme scaling', () => {
expect(lefts(scaled.container, 'data-view-timeline-axis-tick')[1]).toBe(42);
});

it('labels every tick when unitWidth equals the scaled label floor', () => {
const { container } = renderScaled('1.05', { unitWidth: 29.4 });
expect(labels(container)).toHaveLength(32);
});

it('scales the gap between cards sharing a lane', () => {
// At scaling 1, b starts 7.5px after a ends, under the 8px card gap.
const nearlyTouching: Order[] = [
Expand Down
16 changes: 13 additions & 3 deletions packages/react/components/data-view/utils/time-scale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export const TIMELINE_DEFAULT_UNIT_WIDTH: Record<TimelineScale, number> = {
/** Minimum px between rendered tick labels. Denser ticks skip labels. */
const TICK_LABEL_MIN_SPACE = 28;

/**
* Absorbs float error in the spacing ratio. The call site divides the unit
* width by scaling, so a width on an exact multiple of the floor can come out
* a hair under it (29.4 / 1.05 = 27.999999999999996).
*/
const LABEL_SPACING_EPSILON = 1e-9;

/** Coerce a consumer-provided date (Date | epoch ms | parseable string | object with a numeric `valueOf`) to ms. */
export function toTimestamp(value: unknown): number | null {
return toInstant(value)?.getTime() ?? null;
Expand Down Expand Up @@ -167,8 +174,9 @@ function tickLabel(date: Date, scale: TimelineScale): string {
* year in its label ("Jan 2025", then "Feb").
*
* `labelEvery` labels every Nth unit, counted from the domain start. The
* collision floor (labels never closer than `TICK_LABEL_MIN_SPACE`) still
* applies, so a too-dense request degrades instead of overlapping.
* collision floor (labels never closer than `TICK_LABEL_MIN_SPACE`, within
* float error) still applies, so a too-dense request degrades instead of
* overlapping.
*
* Cost note: this materializes one tick per `scale` unit across the whole
* domain on every rebuild, since `virtualized` culls what renders, not what gets
Expand All @@ -184,7 +192,9 @@ export function buildAxis(
): { ticks: TimelineTick[]; bands: TimelineBand[] } {
const autoLabelEvery = Math.max(
1,
Math.ceil(TICK_LABEL_MIN_SPACE / Math.max(1, unitWidth))
Math.ceil(
TICK_LABEL_MIN_SPACE / Math.max(1, unitWidth) - LABEL_SPACING_EPSILON
)
);
const effectiveLabelEvery = Math.max(
autoLabelEvery,
Expand Down
Loading