Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Use named imports to allow tree-shaking to work properly
import { useEffect, useState, Suspense, lazy } from 'react';
import { createRoot } from 'react-dom/client';

Check warning on line 3 in src/App.tsx

View workflow job for this annotation

GitHub Actions / Typecheck, lint, test and build

'createRoot' is defined but never used
import Head from "@/components/layout/Head";
import Navigation from "@/components/layout/Navigation";
import Footer from "@/components/layout/Footer";
Expand Down Expand Up @@ -31,6 +31,7 @@

// Lazy load components
const Greetings = lazy(() => import("@organisms/Greetings"));
const ClusterTelemetry = lazy(() => import("@organisms/ClusterTelemetry"));
const Skills = lazy(() => import("@organisms/Skills"));
const Education = lazy(() => import("@organisms/Education"));
const Experience = lazy(() => import("@organisms/Experience"));
Expand All @@ -54,6 +55,7 @@

// Organism components
Greetings,
ClusterTelemetry,
Skills,
Experience,
Projects,
Expand Down Expand Up @@ -150,6 +152,7 @@
<main id="main-content">
<Suspense fallback={<Loading />}>
<Greetings />
<ClusterTelemetry />
<Skills />
<Experience />
<Projects />
Expand Down
113 changes: 113 additions & 0 deletions src/components/molecules/StatTile/StatTile.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* StatTile: one live number in a card. Figure is the hero. */

.stat-tile {
position: relative;
display: flex;
flex-direction: column;
gap: var(--spacing-2);
box-sizing: border-box;
min-width: 0;
margin: 0;
padding: var(--spacing-4) min(var(--spacing-4), 4vw);
background: var(--surface-card);
border: 1px solid var(--surface-border);
border-radius: var(--radius-card);
box-shadow: var(--shadow-card);
overflow: hidden;
}

/* Thin tone rule along the top edge */
.stat-tile::before {
content: '';
position: absolute;
inset: 0 0 auto 0;
height: 2px;
background: var(--stat-tile-accent, var(--surface-border));
}

.stat-tile--success {
--stat-tile-accent: var(--color-success);
}

.stat-tile--warning {
--stat-tile-accent: var(--color-warning);
}

.stat-tile__label {
font-family: var(--font-family-jetbrains);
font-size: var(--font-size-xs);
font-weight: var(--font-weight-medium);
letter-spacing: var(--letter-spacing-technical);
text-transform: uppercase;
color: var(--color-text-muted);
overflow-wrap: anywhere;
}

.stat-tile__reading {
display: flex;
flex-wrap: wrap;
align-items: baseline;
gap: 0 var(--spacing-1);
margin: 0;
min-height: 2.75rem;
font-family: var(--font-family-jetbrains);
}

.stat-tile__value {
font-size: min(2.5rem, 8.5vw);
font-weight: var(--font-weight-bold);
line-height: 1.1;
font-variant-numeric: tabular-nums;
color: var(--color-text);
overflow-wrap: anywhere;
}

.stat-tile--success .stat-tile__value {
color: var(--color-success);
}

.stat-tile--warning .stat-tile__value {
color: var(--color-warning);
}

.stat-tile__unit {
font-size: var(--font-size-sm);
font-weight: var(--font-weight-medium);
letter-spacing: var(--letter-spacing-technical);
text-transform: uppercase;
color: var(--color-primary);
}

/* Loading shimmer */
.stat-tile__skeleton {
display: block;
width: min(6rem, 70%);
height: 2rem;
border-radius: var(--border-radius-sm, 4px);
background: var(--color-skeleton-bg);
}

.stat-tile--animated .stat-tile__skeleton {
background: linear-gradient(
90deg,
var(--color-skeleton-bg) 0%,
rgba(var(--color-primary-rgb), 0.16) 50%,
var(--color-skeleton-bg) 100%
);
background-size: 200% 100%;
animation: stat-tile-shimmer 1.4s ease-in-out infinite;
}

.stat-tile__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}

@keyframes stat-tile-shimmer {
from { background-position: 200% 0; }
to { background-position: -200% 0; }
}
59 changes: 59 additions & 0 deletions src/components/molecules/StatTile/StatTile.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, waitFor, within } from 'storybook/test';
import StatTile from './StatTile';

const meta = {
title: 'Molecules/StatTile',
component: StatTile,
tags: ['autodocs'],
parameters: {
docs: {
description: {
component:
'One live number: a big monospace figure, a small unit and a mono caption. The figure counts up from 0 the first time it is seen (final value at once under reduced motion). Tones: success for good news, warning (amber) for anything that needs a look. Never red. Must sit inside a `dl`.',
},
},
},
decorators: [
(Story) => (
<dl style={{ margin: 0, width: 200 }}>
<Story />
</dl>
),
],
args: { label: 'Pods', value: '266', animated: true },
argTypes: {
tone: { control: 'inline-radio', options: ['default', 'success', 'warning'] },
loading: { control: 'boolean' },
animated: { control: 'boolean' },
},
} satisfies Meta<typeof StatTile>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(canvas.getByText('Pods')).toBeInTheDocument();
await waitFor(() => expect(canvas.getByText('266')).toBeInTheDocument(), { timeout: 3000 });
},
};

export const WithUnit: Story = { args: { label: 'Power draw', value: '1,074', unit: 'W' } };

export const Decimal: Story = { args: { label: 'Memory', value: '13.8', unit: '%' } };

export const Success: Story = { args: { label: 'Alerts', value: '0', tone: 'success' } };

/** Alerts above zero are amber with the real count. */
export const Warning: Story = { args: { label: 'Alerts', value: '1', tone: 'warning' } };

export const Loading: Story = { args: { loading: true } };

export const NoAnimation: Story = {
args: { animated: false, label: 'Nodes', value: '11' },
play: async ({ canvasElement }) => {
await expect(within(canvasElement).getByText('11')).toBeInTheDocument();
},
};
125 changes: 125 additions & 0 deletions src/components/molecules/StatTile/StatTile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { useEffect, useRef, useState } from 'react';
import { useMotionEnabled } from '@utils/motion';
import './StatTile.css';

export type StatTileTone = 'default' | 'success' | 'warning';

export interface StatTileProps {
/** Small caption, e.g. "Pods" */
label: string;
/** The figure as written, e.g. "1,074" or "13.8". Omit while loading. */
value?: string;
/** Small unit after the figure, e.g. "W", "%" */
unit?: string;
/** default: neutral; success: good news; warning: needs a look (amber, never red) */
tone?: StatTileTone;
/** Shows a shimmer instead of a figure */
loading?: boolean;
/** Count up from 0 the first time the tile is seen */
animated?: boolean;
}

const COUNT_MS = 800;

const decimalsOf = (figure: string): number => figure.split('.')[1]?.length ?? 0;

const toNumber = (figure: string): number | null => {
const n = Number(figure.replace(/,/g, ''));
return Number.isFinite(n) ? n : null;
};

const format = (n: number, decimals: number): string =>
n.toLocaleString('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals });

/** Counts from 0 to the figure once the element is on screen. Returns the text to show. */
const useCountUp = (figure: string | undefined, enabled: boolean, target: React.RefObject<HTMLElement | null>) => {
const end = figure === undefined ? null : toNumber(figure);
const counts = enabled && end !== null;
const [text, setText] = useState<string | undefined>(
figure !== undefined && counts ? format(0, decimalsOf(figure)) : figure,
);
const played = useRef(false);

useEffect(() => {
if (figure === undefined) {
setText(undefined);
return;
}
if (!counts || end === null || played.current) {
setText(figure);
return;
}

const decimals = decimalsOf(figure);
let frame = 0;
let observer: IntersectionObserver | null = null;

const run = () => {
played.current = true;
const start = performance.now();
const tick = (now: number) => {
const t = Math.min(1, (now - start) / COUNT_MS);
const eased = 1 - Math.pow(1 - t, 3);
setText(t >= 1 ? figure : format(end * eased, decimals));
if (t < 1) frame = requestAnimationFrame(tick);
};
frame = requestAnimationFrame(tick);
};

const node = target.current;
if (node && typeof IntersectionObserver !== 'undefined') {
observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
observer?.disconnect();
run();
}
}, { threshold: 0.4 });
observer.observe(node);
} else {
run();
}

return () => {
observer?.disconnect();
cancelAnimationFrame(frame);
};
}, [figure, counts, end, target]);

return { text, done: text === figure };
};

/**
* One live number: a big monospace figure with a small unit and a caption.
* Shows a shimmer while loading. Props only; the parent supplies the data.
*/
const StatTile = ({ label, value, unit, tone = 'default', loading = false, animated = true }: StatTileProps) => {
const motionOn = useMotionEnabled(animated);
const ref = useRef<HTMLDivElement>(null);
const { text, done } = useCountUp(loading ? undefined : value, motionOn, ref);

return (
<div
ref={ref}
className={`stat-tile stat-tile--${tone}${loading ? ' stat-tile--loading' : ''}${motionOn ? ' stat-tile--animated' : ''}`}
data-testid="stat-tile"
>
<dt className="stat-tile__label">{label}</dt>
<dd className="stat-tile__reading">
{loading ? (
<span className="stat-tile__skeleton" role="presentation">
<span className="stat-tile__sr">Loading</span>
</span>
) : (
<>
<span className="stat-tile__value" aria-hidden={done ? undefined : true}>
{text}
</span>
{unit ? <span className="stat-tile__unit">{unit}</span> : null}
</>
)}
</dd>
</div>
);
};

export default StatTile;
2 changes: 2 additions & 0 deletions src/components/molecules/StatTile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './StatTile';
export type { StatTileProps, StatTileTone } from './StatTile';
15 changes: 15 additions & 0 deletions src/components/molecules/StatTileGrid/StatTileGrid.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.stat-tile-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 9rem), 1fr));
gap: min(var(--spacing-3), 3vw);
margin: 0;
padding: 0;
}

/* Wide screens: two even rows of four */
@media (min-width: 900px) {
.stat-tile-grid {
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: var(--spacing-4);
}
}
Loading
Loading