Unverified Commit bc95c4f8 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Unify bundle analyzer delta colors (#99607)

parent ba80ee48
...@@ -34,6 +34,8 @@ ...@@ -34,6 +34,8 @@
--sidebar-ring: oklch(0.708 0 0); --sidebar-ring: oklch(0.708 0 0);
--polyfill: #5f707f; --polyfill: #5f707f;
--polyfill-foreground: #ffffff; --polyfill-foreground: #ffffff;
--delta-increase: #2563eb;
--delta-decrease: #b45309;
} }
.dark { .dark {
...@@ -66,6 +68,8 @@ ...@@ -66,6 +68,8 @@
--sidebar-ring: oklch(0.439 0 0); --sidebar-ring: oklch(0.439 0 0);
--polyfill: #5f707f; --polyfill: #5f707f;
--polyfill-foreground: #ffffff; --polyfill-foreground: #ffffff;
--delta-increase: #60a5fa;
--delta-decrease: #fbbf24;
} }
@theme inline { @theme inline {
...@@ -104,6 +108,8 @@ ...@@ -104,6 +108,8 @@
--color-sidebar-ring: var(--sidebar-ring); --color-sidebar-ring: var(--sidebar-ring);
--color-polyfill: var(--polyfill); --color-polyfill: var(--polyfill);
--color-polyfill-foreground: var(--polyfill-foreground); --color-polyfill-foreground: var(--polyfill-foreground);
--color-delta-increase: var(--delta-increase);
--color-delta-decrease: var(--delta-decrease);
} }
@layer base { @layer base {
......
...@@ -305,8 +305,8 @@ export function DiffTable<Row extends DiffRow>({ ...@@ -305,8 +305,8 @@ export function DiffTable<Row extends DiffRow>({
<td <td
className={cn( className={cn(
'whitespace-nowrap px-4 py-2 text-right font-mono', 'whitespace-nowrap px-4 py-2 text-right font-mono',
totalDelta > 0 && 'text-red-600 dark:text-red-400', totalDelta > 0 && 'text-delta-increase',
totalDelta < 0 && 'text-green-600 dark:text-green-400' totalDelta < 0 && 'text-delta-decrease'
)} )}
> >
{formatDelta(totalDelta)} {formatDelta(totalDelta)}
...@@ -601,8 +601,8 @@ function DiffPackageHeaderRow<Row extends DiffRow>({ ...@@ -601,8 +601,8 @@ function DiffPackageHeaderRow<Row extends DiffRow>({
<td <td
className={cn( className={cn(
'cursor-pointer whitespace-nowrap border-b border-border bg-muted/30 px-4 py-2 text-right font-mono text-xs transition-colors hover:bg-muted/50', 'cursor-pointer whitespace-nowrap border-b border-border bg-muted/30 px-4 py-2 text-right font-mono text-xs transition-colors hover:bg-muted/50',
d > 0 && 'text-red-600 dark:text-red-400', d > 0 && 'text-delta-increase',
d < 0 && 'text-green-600 dark:text-green-400', d < 0 && 'text-delta-decrease',
d === 0 && 'text-muted-foreground' d === 0 && 'text-muted-foreground'
)} )}
onClick={onToggle} onClick={onToggle}
...@@ -628,17 +628,17 @@ function PackageCountBreakdown({ ...@@ -628,17 +628,17 @@ function PackageCountBreakdown({
{ {
label: 'added', label: 'added',
value: counts.added, value: counts.added,
className: 'text-red-600 dark:text-red-400', className: 'text-delta-increase',
}, },
{ {
label: 'removed', label: 'removed',
value: counts.removed, value: counts.removed,
className: 'text-green-600 dark:text-green-400', className: 'text-delta-decrease',
}, },
{ {
label: 'changed', label: 'changed',
value: counts.changed, value: counts.changed,
className: 'text-amber-500', className: 'text-muted-foreground',
}, },
] ]
const visible = entries.filter((e) => e.value > 0) const visible = entries.filter((e) => e.value > 0)
...@@ -851,8 +851,8 @@ function DiffTableRow<Row extends DiffRow>({ ...@@ -851,8 +851,8 @@ function DiffTableRow<Row extends DiffRow>({
className={cn( className={cn(
'whitespace-nowrap px-4 py-2 text-right font-mono text-xs', 'whitespace-nowrap px-4 py-2 text-right font-mono text-xs',
cellClassName, cellClassName,
d > 0 && 'text-red-600 dark:text-red-400', d > 0 && 'text-delta-increase',
d < 0 && 'text-green-600 dark:text-green-400', d < 0 && 'text-delta-decrease',
d === 0 && 'text-muted-foreground' d === 0 && 'text-muted-foreground'
)} )}
onClick={onClick} onClick={onClick}
...@@ -1021,7 +1021,7 @@ function StatusIcon({ status }: { status: DiffStatus }) { ...@@ -1021,7 +1021,7 @@ function StatusIcon({ status }: { status: DiffStatus }) {
if (status === 'added') { if (status === 'added') {
return ( return (
<PlusCircle <PlusCircle
className="h-3.5 w-3.5 shrink-0 text-red-600 dark:text-red-400" className="h-3.5 w-3.5 shrink-0 text-delta-increase"
aria-label="Added" aria-label="Added"
/> />
) )
...@@ -1029,15 +1029,15 @@ function StatusIcon({ status }: { status: DiffStatus }) { ...@@ -1029,15 +1029,15 @@ function StatusIcon({ status }: { status: DiffStatus }) {
if (status === 'removed') { if (status === 'removed') {
return ( return (
<MinusCircle <MinusCircle
className="h-3.5 w-3.5 shrink-0 text-green-600 dark:text-green-400" className="h-3.5 w-3.5 shrink-0 text-delta-decrease"
aria-label="Removed" aria-label="Removed"
/> />
) )
} }
if (status === 'changed') { if (status === 'changed') {
return ( return (
<ArrowUp <ArrowUpDown
className="h-3.5 w-3.5 shrink-0 text-amber-500" className="h-3.5 w-3.5 shrink-0 text-muted-foreground"
aria-label="Changed" aria-label="Changed"
/> />
) )
......
'use client' 'use client'
import { useMemo, useState } from 'react' import { useMemo, useState } from 'react'
import { rgba } from 'polished'
import type { AnalyzeData } from '@/lib/analyze-data' import type { AnalyzeData } from '@/lib/analyze-data'
import type { DiffSummary, SourceDiffRow } from '@/lib/diff' import type { DiffSummary, SourceDiffRow } from '@/lib/diff'
import { delta, formatDelta } from '@/lib/diff' import { delta, formatDelta } from '@/lib/diff'
import { createDiffTreemapLayout } from '@/lib/diff-treemap-layout' import { createDiffTreemapLayout } from '@/lib/diff-treemap-layout'
import { SizeMode, type LayoutNode } from '@/lib/treemap-layout' import { SizeMode, type LayoutNode } from '@/lib/treemap-layout'
import { TreemapVisualizer } from '@/components/treemap-visualizer' import {
TreemapVisualizer,
/** Color-blind-safe blue/amber palette for bundle-size increases/decreases. */ type CanvasColors,
const COLOR_INCREASE = '#2563eb' // blue-600 } from '@/components/treemap-visualizer'
const COLOR_INCREASE_MUTED = 'rgba(37, 99, 235, 0.6)'
const COLOR_DECREASE = '#d97706' // amber-600
const COLOR_DECREASE_MUTED = 'rgba(217, 119, 6, 0.6)'
interface DiffTreemapProps { interface DiffTreemapProps {
summary: DiffSummary<SourceDiffRow> summary: DiffSummary<SourceDiffRow>
...@@ -68,10 +66,13 @@ export function DiffTreemap({ ...@@ -68,10 +66,13 @@ export function DiffTreemap({
) )
const { rowBySourceIndex, sourceIndexByKey } = diffLayout const { rowBySourceIndex, sourceIndexByKey } = diffLayout
function getFileColorOverride(node: LayoutNode): string | undefined { function getFileColorOverride(
node: LayoutNode,
colors: CanvasColors
): string | undefined {
if (node.sourceIndex === undefined) return undefined if (node.sourceIndex === undefined) return undefined
const row = rowBySourceIndex.get(node.sourceIndex) const row = rowBySourceIndex.get(node.sourceIndex)
return row ? colorForRow(row, useCompressed) : undefined return row ? colorForRow(row, useCompressed, colors) : undefined
} }
// Show size deltas on changed tiles instead of absolute sizes. // Show size deltas on changed tiles instead of absolute sizes.
...@@ -129,9 +130,13 @@ export function DiffTreemap({ ...@@ -129,9 +130,13 @@ export function DiffTreemap({
* scheme: bright blue/amber for added/removed and lighter tints scaled by the * scheme: bright blue/amber for added/removed and lighter tints scaled by the
* relative magnitude of the change for grew/shrank. * relative magnitude of the change for grew/shrank.
*/ */
function colorForRow(row: SourceDiffRow, useCompressed: boolean): string { function colorForRow(
if (row.status === 'added') return COLOR_INCREASE row: SourceDiffRow,
if (row.status === 'removed') return COLOR_DECREASE useCompressed: boolean,
colors: CanvasColors
): string {
if (row.status === 'added') return colors.increase
if (row.status === 'removed') return colors.decrease
const deltaValue = useCompressed const deltaValue = useCompressed
? row.compressedB - row.compressedA ? row.compressedB - row.compressedA
: row.sizeB - row.sizeA : row.sizeB - row.sizeA
...@@ -141,18 +146,16 @@ function colorForRow(row: SourceDiffRow, useCompressed: boolean): string { ...@@ -141,18 +146,16 @@ function colorForRow(row: SourceDiffRow, useCompressed: boolean): string {
const ratio = const ratio =
baseline === 0 ? 1 : Math.min(1, Math.abs(deltaValue) / baseline) baseline === 0 ? 1 : Math.min(1, Math.abs(deltaValue) / baseline)
const alpha = 0.35 + ratio * 0.5 const alpha = 0.35 + ratio * 0.5
return deltaValue > 0 return rgba(deltaValue > 0 ? colors.increase : colors.decrease, alpha)
? `rgba(37, 99, 235, ${alpha.toFixed(2)})`
: `rgba(217, 119, 6, ${alpha.toFixed(2)})`
} }
/** Static legend overlay so users can decode the color scheme at a glance. */ /** Static legend overlay so users can decode the color scheme at a glance. */
function DiffLegend() { function DiffLegend() {
const items: Array<{ label: string; color: string }> = [ const items: Array<{ label: string; className: string }> = [
{ label: 'Added', color: COLOR_INCREASE }, { label: 'Added', className: 'bg-delta-increase' },
{ label: 'Grew', color: COLOR_INCREASE_MUTED }, { label: 'Grew', className: 'bg-delta-increase/60' },
{ label: 'Shrank', color: COLOR_DECREASE_MUTED }, { label: 'Shrank', className: 'bg-delta-decrease/60' },
{ label: 'Removed', color: COLOR_DECREASE }, { label: 'Removed', className: 'bg-delta-decrease' },
] ]
return ( return (
<div className="absolute bottom-2 left-2 flex items-center gap-3 rounded border border-border bg-background/90 px-2 py-1 text-xs text-muted-foreground shadow-sm"> <div className="absolute bottom-2 left-2 flex items-center gap-3 rounded border border-border bg-background/90 px-2 py-1 text-xs text-muted-foreground shadow-sm">
...@@ -160,8 +163,7 @@ function DiffLegend() { ...@@ -160,8 +163,7 @@ function DiffLegend() {
<span key={item.label} className="flex items-center gap-1.5"> <span key={item.label} className="flex items-center gap-1.5">
<span <span
aria-hidden aria-hidden
className="inline-block h-3 w-3 rounded-sm" className={`inline-block h-3 w-3 rounded-sm ${item.className}`}
style={{ backgroundColor: item.color }}
/> />
{item.label} {item.label}
</span> </span>
......
...@@ -309,9 +309,9 @@ function DeltaBadge({ ...@@ -309,9 +309,9 @@ function DeltaBadge({
const tone = const tone =
row.status === 'added' || d > 0 row.status === 'added' || d > 0
? 'text-red-600 dark:text-red-400' ? 'text-delta-increase'
: row.status === 'removed' || d < 0 : row.status === 'removed' || d < 0
? 'text-green-600 dark:text-green-400' ? 'text-delta-decrease'
: 'text-muted-foreground' : 'text-muted-foreground'
return ( return (
......
...@@ -300,8 +300,8 @@ function CompareSidebarContent({ ...@@ -300,8 +300,8 @@ function CompareSidebarContent({
<span <span
className={cn( className={cn(
'font-mono', 'font-mono',
compressedDelta > 0 && 'text-red-600 dark:text-red-400', compressedDelta > 0 && 'text-delta-increase',
compressedDelta < 0 && 'text-green-600 dark:text-green-400', compressedDelta < 0 && 'text-delta-decrease',
compressedDelta === 0 && 'text-muted-foreground' compressedDelta === 0 && 'text-muted-foreground'
)} )}
> >
......
...@@ -2,8 +2,7 @@ import { cn } from '@/lib/utils' ...@@ -2,8 +2,7 @@ import { cn } from '@/lib/utils'
/** /**
* Inline colored delta rendered next to the `A → B` value. For size deltas, * Inline colored delta rendered next to the `A → B` value. For size deltas,
* positive deltas (regressions) are red and negative (improvements) are * positive deltas are blue and negative deltas are amber. Neutral deltas
* emerald — matching how regressions are usually framed. Neutral deltas
* (counts) render muted. * (counts) render muted.
*/ */
export function DeltaChip({ export function DeltaChip({
...@@ -18,9 +17,9 @@ export function DeltaChip({ ...@@ -18,9 +17,9 @@ export function DeltaChip({
const toneClass = const toneClass =
tone === 'size' tone === 'size'
? delta > 0 ? delta > 0
? 'text-red-400' ? 'text-delta-increase'
: delta < 0 : delta < 0
? 'text-emerald-400' ? 'text-delta-decrease'
: 'text-muted-foreground' : 'text-muted-foreground'
: 'text-muted-foreground' : 'text-muted-foreground'
return ( return (
...@@ -105,12 +104,10 @@ export function CountCard({ ...@@ -105,12 +104,10 @@ export function CountCard({
}) { }) {
const toneClass = const toneClass =
tone === 'added' tone === 'added'
? 'text-red-500' ? 'text-delta-increase'
: tone === 'removed' : tone === 'removed'
? 'text-green-500' ? 'text-delta-decrease'
: tone === 'changed' : 'text-muted-foreground'
? 'text-amber-500'
: 'text-muted-foreground'
return ( return (
<div className="flex min-w-[80px] flex-col gap-1 rounded-md border border-border bg-background/60 px-3 py-2"> <div className="flex min-w-[80px] flex-col gap-1 rounded-md border border-border bg-background/60 px-3 py-2">
<div className="text-[10px] font-medium uppercase tracking-wide text-muted-foreground"> <div className="text-[10px] font-medium uppercase tracking-wide text-muted-foreground">
......
'use client' 'use client'
import { darken, lighten, readableColor } from 'polished' import { darken, lighten, readableColor } from 'polished'
import { useTheme } from 'next-themes'
import type React from 'react' import type React from 'react'
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { import {
...@@ -35,7 +36,10 @@ interface TreemapVisualizerProps { ...@@ -35,7 +36,10 @@ interface TreemapVisualizerProps {
* to the default file-type-based color. Used by the compare view to color * to the default file-type-based color. Used by the compare view to color
* tiles by diff status (added/removed/grew/shrank) instead of by file type. * tiles by diff status (added/removed/grew/shrank) instead of by file type.
*/ */
getFileColorOverride?: (node: LayoutNode) => string | undefined getFileColorOverride?: (
node: LayoutNode,
colors: CanvasColors
) => string | undefined
/** /**
* Optional override for the size label shown on a tile. Returning `undefined` * Optional override for the size label shown on a tile. Returning `undefined`
* falls back to `formatBytes(node.size)`. Used by the compare view to show * falls back to `formatBytes(node.size)`. Used by the compare view to show
...@@ -50,20 +54,23 @@ interface TreemapVisualizerProps { ...@@ -50,20 +54,23 @@ interface TreemapVisualizerProps {
overlay?: React.ReactNode overlay?: React.ReactNode
} }
function getFileColor(node: { function getFileColor(
js?: boolean node: {
css?: boolean js?: boolean
json?: boolean css?: boolean
asset?: boolean json?: boolean
server?: boolean asset?: boolean
client?: boolean server?: boolean
traced?: boolean client?: boolean
specialModuleType: SpecialModule | null traced?: boolean
}): string { specialModuleType: SpecialModule | null
},
colors: CanvasColors
): string {
const { js, css, json, asset, client, traced, specialModuleType } = node const { js, css, json, asset, client, traced, specialModuleType } = node
if (isPolyfill(specialModuleType)) { if (isPolyfill(specialModuleType)) {
return '#5f707f' return colors.polyfill
} }
let color = '#9ca3af' // gray-400 default let color = '#9ca3af' // gray-400 default
...@@ -284,6 +291,7 @@ function nodeOrDescendantsMatchSearch( ...@@ -284,6 +291,7 @@ function nodeOrDescendantsMatchSearch(
function drawTreemap( function drawTreemap(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
colors: CanvasColors,
node: LayoutNode, node: LayoutNode,
hoveredAncestorChain: number[] | null, hoveredAncestorChain: number[] | null,
selectedAncestorChain: number[], selectedAncestorChain: number[],
...@@ -292,7 +300,9 @@ function drawTreemap( ...@@ -292,7 +300,9 @@ function drawTreemap(
searchQuery: string, searchQuery: string,
originalData: LayoutNode, originalData: LayoutNode,
immediateHoveredSourceIndex: number | undefined, immediateHoveredSourceIndex: number | undefined,
getFileColorOverride: ((node: LayoutNode) => string | undefined) | undefined, getFileColorOverride:
| ((node: LayoutNode, colors: CanvasColors) => string | undefined)
| undefined,
getFileSizeLabel: ((node: LayoutNode) => string | undefined) | undefined, getFileSizeLabel: ((node: LayoutNode) => string | undefined) | undefined,
getFileLoadScope: ((sourceIndex: number) => SourceLoadScope) | undefined, getFileLoadScope: ((sourceIndex: number) => SourceLoadScope) | undefined,
currentPath: string[] = [], currentPath: string[] = [],
...@@ -314,8 +324,6 @@ function drawTreemap( ...@@ -314,8 +324,6 @@ function drawTreemap(
// Draw ancestor title bars (nodes on path but before the focused node) // Draw ancestor title bars (nodes on path but before the focused node)
if (isOnFocusPath && !isFocusedNode && type === 'directory') { if (isOnFocusPath && !isFocusedNode && type === 'directory') {
const colors = getThemeColors()
if (titleBarHeight && rect.height > 20) { if (titleBarHeight && rect.height > 20) {
ctx.fillStyle = colors.dirTitleBg ctx.fillStyle = colors.dirTitleBg
ctx.globalAlpha = 1.0 ctx.globalAlpha = 1.0
...@@ -344,6 +352,7 @@ function drawTreemap( ...@@ -344,6 +352,7 @@ function drawTreemap(
for (const child of children) { for (const child of children) {
drawTreemap( drawTreemap(
ctx, ctx,
colors,
child, child,
hoveredAncestorChain, hoveredAncestorChain,
selectedAncestorChain, selectedAncestorChain,
...@@ -411,14 +420,13 @@ function drawTreemap( ...@@ -411,14 +420,13 @@ function drawTreemap(
} }
const opacity = fadeOut ? 0.3 : 1.0 const opacity = fadeOut ? 0.3 : 1.0
const colors = getThemeColors()
// Check if this is the immediately hovered node for brightness boost // Check if this is the immediately hovered node for brightness boost
const isImmediateHovered = const isImmediateHovered =
sourceIndex !== undefined && sourceIndex === immediateHoveredSourceIndex sourceIndex !== undefined && sourceIndex === immediateHoveredSourceIndex
if (type === 'file') { if (type === 'file') {
let color = getFileColorOverride?.(node) ?? getFileColor(node) let color =
getFileColorOverride?.(node, colors) ?? getFileColor(node, colors)
// Apply brightness boost to immediately hovered node // Apply brightness boost to immediately hovered node
if (isImmediateHovered) { if (isImmediateHovered) {
...@@ -663,6 +671,7 @@ function drawTreemap( ...@@ -663,6 +671,7 @@ function drawTreemap(
drawTreemap( drawTreemap(
ctx, ctx,
colors,
child, child,
hoveredAncestorChain, hoveredAncestorChain,
selectedAncestorChain, selectedAncestorChain,
...@@ -798,7 +807,7 @@ export function TreemapVisualizer({ ...@@ -798,7 +807,7 @@ export function TreemapVisualizer({
canvasHeight: 800, canvasHeight: 800,
devicePixelRatio: 1, devicePixelRatio: 1,
}) })
const [, _setTheme] = useState<'light' | 'dark'>('light') const { resolvedTheme } = useTheme()
// The focused chain needs a manual memo: without it the compiler misses // The focused chain needs a manual memo: without it the compiler misses
// the expensive layout and redraws the canvas on unrelated renders. // the expensive layout and redraws the canvas on unrelated renders.
...@@ -904,6 +913,7 @@ export function TreemapVisualizer({ ...@@ -904,6 +913,7 @@ export function TreemapVisualizer({
drawTreemap( drawTreemap(
ctx, ctx,
getThemeColors(),
layout, layout,
hoveredAncestorChain, hoveredAncestorChain,
selectedAncestorChain, selectedAncestorChain,
...@@ -923,6 +933,7 @@ export function TreemapVisualizer({ ...@@ -923,6 +933,7 @@ export function TreemapVisualizer({
dimensions.cssWidth, dimensions.cssWidth,
dimensions.cssHeight, dimensions.cssHeight,
dimensions.devicePixelRatio, dimensions.devicePixelRatio,
resolvedTheme,
isMouseInTreemap, isMouseInTreemap,
focusedAncestorChain, focusedAncestorChain,
searchQuery, searchQuery,
...@@ -1079,7 +1090,11 @@ function isDarkMode(): boolean { ...@@ -1079,7 +1090,11 @@ function isDarkMode(): boolean {
function getThemeColors() { function getThemeColors() {
const dark = isDarkMode() const dark = isDarkMode()
const styles = getComputedStyle(document.documentElement)
return { return {
increase: styles.getPropertyValue('--delta-increase').trim(),
decrease: styles.getPropertyValue('--delta-decrease').trim(),
polyfill: styles.getPropertyValue('--polyfill').trim(),
text: dark ? '#ffffff' : '#000000', text: dark ? '#ffffff' : '#000000',
textMuted: dark ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.6)', textMuted: dark ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.6)',
border: dark ? 'rgba(255, 255, 255, 0.4)' : 'rgba(180, 180, 180, 0.5)', border: dark ? 'rgba(255, 255, 255, 0.4)' : 'rgba(180, 180, 180, 0.5)',
...@@ -1097,3 +1112,5 @@ function getThemeColors() { ...@@ -1097,3 +1112,5 @@ function getThemeColors() {
: 'rgba(128, 128, 128, 0.6)', : 'rgba(128, 128, 128, 0.6)',
} }
} }
export type CanvasColors = ReturnType<typeof getThemeColors>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment