Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(quill-charts): extract bar config resolution into utils/bar-config (#61971)

Sam Penningtonda388cd5e2d0
diff --git a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx
index 98e2507f79db..eead28d20dd9 100644
--- a/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx
+++ b/packages/quill/packages/charts/src/charts/BarChart/BarChart.tsx
@@ -12,7 +12,6 @@ import {
BAR_TRACK_HOVER_ALPHA,
type BarRect,
type BarRoundedCorners,
- type BarShadow,
clipToRoundedRects,
drawBarHighlight,
drawBars,
@@ -23,7 +22,6 @@ import {
import { Chart } from '../../core/Chart'
import { ChartErrorBoundary } from '../../core/ChartErrorBoundary'
import { barColorAt } from '../../core/color-utils'
-import { DEFAULT_MARGINS, X_AXIS_TITLE_MARGIN } from '../../core/hooks/useChartMargins'
import { useLatest } from '../../core/hooks/useLatest'
import {
buildSegmentResolveValue,
@@ -38,7 +36,6 @@ import {
} from '../../core/scales'
import type {
BarChartConfig,
- BarsConfig,
ChartDimensions,
ChartDrawArgs,
ChartScales,
@@ -53,6 +50,7 @@ import type {
import { DEFAULT_Y_AXIS_ID } from '../../core/types'
import { computeVisibleXLabels } from '../../overlays/AxisLabels'
import { BarTooltip } from './BarTooltip'
+import { computeWrapperMinHeight, HORIZONTAL_MIN_BAND_SIZE_DEFAULT, resolveBarShadow } from './utils/bar-config'
import {
type BarLayout,
barContainsPointOnBandAxis,
@@ -77,14 +75,6 @@ export interface BarChartProps<Meta = unknown> {
onError?: (error: Error, info: React.ErrorInfo) => void
}
-// Negative offsetY casts the shadow upward onto the visible track above the bar.
-const DEFAULT_BAR_SHADOW: BarShadow = { color: 'rgba(0,0,0,0.30)', blur: 12, offsetY: -4 }
-
-// Horizontal floor: each row gets at least this much px so tick labels don't crush; wrapper scrolls.
-const HORIZONTAL_MIN_BAND_SIZE_DEFAULT = 24
-// Reserve room for chart-edge margins + worst-case x-axis title margin (matches useChartMargins).
-const HORIZONTAL_CHART_MARGIN_PX = DEFAULT_MARGINS.top + DEFAULT_MARGINS.bottom + X_AXIS_TITLE_MARGIN
-
const ALL_CORNERS: BarRoundedCorners = { topLeft: true, topRight: true, bottomLeft: true, bottomRight: true }
/** One fully-rounded rect per band, spanning the union of that band's stacked segments — the
@@ -116,16 +106,6 @@ function stackPillRects(bars: BarRect[], isHorizontal: boolean): BarRect[] {
return [...byBand.values()]
}
-function resolveBarShadow(barShadow: BarsConfig['shadow']): BarShadow | undefined {
- if (barShadow === true) {
- return DEFAULT_BAR_SHADOW
- }
- if (barShadow === false || barShadow == null) {
- return undefined
- }
- return barShadow
-}
-
export function BarChart<Meta = unknown>({ onError, ...rest }: BarChartProps<Meta>): React.ReactElement {
return (
<ChartErrorBoundary onError={onError}>
@@ -167,18 +147,10 @@ function BarChartInner<Meta = unknown>({
const isHorizontal = axisOrientation === 'horizontal'
const resolvedMinBandSize = minBandSize ?? (isHorizontal ? HORIZONTAL_MIN_BAND_SIZE_DEFAULT : 0)
- // Fit-to-height drops overflow rows instead of growing the container, so it never sets a
- // wrapper floor — the chart fills whatever height the tile gives it.
- const wrapperMinHeight = useMemo(() => {
- if (!isHorizontal || fitToHeight || resolvedMinBandSize <= 0) {
- return undefined
- }
- const uniqueBands = new Set(labels).size
- if (uniqueBands === 0) {
- return undefined
- }
- return uniqueBands * resolvedMinBandSize + HORIZONTAL_CHART_MARGIN_PX
- }, [isHorizontal, fitToHeight, resolvedMinBandSize, labels])
+ const wrapperMinHeight = useMemo(
+ () => computeWrapperMinHeight({ isHorizontal, fitToHeight, resolvedMinBandSize, labels }),
+ [isHorizontal, fitToHeight, resolvedMinBandSize, labels]
+ )
const stackedData = useMemo((): Map<string, StackedBand> | undefined => {
if (barLayout === 'percent') {
diff --git a/packages/quill/packages/charts/src/charts/BarChart/utils/bar-config.test.ts b/packages/quill/packages/charts/src/charts/BarChart/utils/bar-config.test.ts
new file mode 100644
index 000000000000..7425d6926b6f
--- /dev/null
+++ b/packages/quill/packages/charts/src/charts/BarChart/utils/bar-config.test.ts
@@ -0,0 +1,57 @@
+import { DEFAULT_MARGINS, X_AXIS_TITLE_MARGIN } from '../../../core/hooks/useChartMargins'
+import { computeWrapperMinHeight, HORIZONTAL_MIN_BAND_SIZE_DEFAULT, resolveBarShadow } from './bar-config'
+
+const CHART_MARGIN_PX = DEFAULT_MARGINS.top + DEFAULT_MARGINS.bottom + X_AXIS_TITLE_MARGIN
+
+describe('bar-config', () => {
+ describe('resolveBarShadow', () => {
+ it('returns the default upward shadow when true', () => {
+ expect(resolveBarShadow(true)).toEqual({ color: 'rgba(0,0,0,0.30)', blur: 12, offsetY: -4 })
+ })
+
+ it.each([
+ { desc: 'false', value: false as const },
+ { desc: 'undefined', value: undefined },
+ ])('returns undefined when $desc', ({ value }) => {
+ expect(resolveBarShadow(value)).toBeUndefined()
+ })
+
+ it('passes an explicit shadow object through unchanged', () => {
+ const custom = { color: 'red', blur: 2, offsetX: 1, offsetY: 3 }
+ expect(resolveBarShadow(custom)).toBe(custom)
+ })
+ })
+
+ describe('computeWrapperMinHeight', () => {
+ const base = {
+ isHorizontal: true,
+ fitToHeight: false,
+ resolvedMinBandSize: HORIZONTAL_MIN_BAND_SIZE_DEFAULT,
+ labels: ['a', 'b', 'c'],
+ }
+
+ it.each([
+ {
+ desc: '3 distinct labels',
+ labels: ['a', 'b', 'c'],
+ expected: 3 * HORIZONTAL_MIN_BAND_SIZE_DEFAULT + CHART_MARGIN_PX,
+ },
+ {
+ desc: 'duplicate labels counted once',
+ labels: ['a', 'a', 'b'],
+ expected: 2 * HORIZONTAL_MIN_BAND_SIZE_DEFAULT + CHART_MARGIN_PX,
+ },
+ ])('reserves the min band size per unique band plus chart margins ($desc)', ({ labels, expected }) => {
+ expect(computeWrapperMinHeight({ ...base, labels })).toBe(expected)
+ })
+
+ it.each([
+ { desc: 'vertical', override: { isHorizontal: false } },
+ { desc: 'fit-to-height', override: { fitToHeight: true } },
+ { desc: 'min band size is zero', override: { resolvedMinBandSize: 0 } },
+ { desc: 'there are no bands', override: { labels: [] } },
+ ])('returns undefined when $desc', ({ override }) => {
+ expect(computeWrapperMinHeight({ ...base, ...override })).toBeUndefined()
+ })
+ })
+})
diff --git a/packages/quill/packages/charts/src/charts/BarChart/utils/bar-config.ts b/packages/quill/packages/charts/src/charts/BarChart/utils/bar-config.ts
new file mode 100644
index 000000000000..7f7bd7a32ec4
--- /dev/null
+++ b/packages/quill/packages/charts/src/charts/BarChart/utils/bar-config.ts
@@ -0,0 +1,45 @@
+import type { BarShadow } from '../../../core/canvas-renderer'
+import { DEFAULT_MARGINS, X_AXIS_TITLE_MARGIN } from '../../../core/hooks/useChartMargins'
+import type { BarsConfig } from '../../../core/types'
+
+// Negative offsetY casts the shadow upward onto the visible track above the bar.
+const DEFAULT_BAR_SHADOW: BarShadow = { color: 'rgba(0,0,0,0.30)', blur: 12, offsetY: -4 }
+
+// Horizontal floor: each row gets at least this much px so tick labels don't crush; wrapper scrolls.
+export const HORIZONTAL_MIN_BAND_SIZE_DEFAULT = 24
+// Reserve room for chart-edge margins + worst-case x-axis title margin (matches useChartMargins).
+const HORIZONTAL_CHART_MARGIN_PX = DEFAULT_MARGINS.top + DEFAULT_MARGINS.bottom + X_AXIS_TITLE_MARGIN
+
+export function resolveBarShadow(barShadow: BarsConfig['shadow']): BarShadow | undefined {
+ if (barShadow === true) {
+ return DEFAULT_BAR_SHADOW
+ }
+ if (barShadow === false || barShadow == null) {
+ return undefined
+ }
+ return barShadow
+}
+
+export interface WrapperMinHeightOptions {
+ isHorizontal: boolean
+ fitToHeight: boolean
+ resolvedMinBandSize: number
+ labels: string[]
+}
+
+// fit-to-height returns no floor: it drops overflow rows instead of growing the wrapper.
+export function computeWrapperMinHeight({
+ isHorizontal,
+ fitToHeight,
+ resolvedMinBandSize,
+ labels,
+}: WrapperMinHeightOptions): number | undefined {
+ if (!isHorizontal || fitToHeight || resolvedMinBandSize <= 0) {
+ return undefined
+ }
+ const uniqueBands = new Set(labels).size
+ if (uniqueBands === 0) {
+ return undefined
+ }
+ return uniqueBands * resolvedMinBandSize + HORIZONTAL_CHART_MARGIN_PX
+}