Skip to content

Commit 49e0b3b

Browse files
authored
Merge pull request #960 from nteract/fix-legend-margin
Treat undefined user margin sides as omitted
2 parents 4b89492 + 3784476 commit 49e0b3b

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/components/charts/shared/hooks.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,20 @@ describe("useChartLegendAndMargin", () => {
457457
expect(result.current.margin.right).toBe(200)
458458
})
459459

460+
it("treats undefined margin sides as omitted for legend reservation", () => {
461+
const colorScale = (_v: string) => "#ccc"
462+
const { result } = renderHook(() =>
463+
useChartLegendAndMargin({
464+
data,
465+
colorBy: "cat",
466+
colorScale,
467+
showLegend: true,
468+
userMargin: { right: undefined },
469+
})
470+
)
471+
expect(result.current.margin.right).toBe(110)
472+
})
473+
460474
it("merges user margin with defaults", () => {
461475
const { result } = renderHook(() =>
462476
useChartLegendAndMargin({

src/components/charts/shared/hooks.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,12 @@ export function useChartLegendAndMargin({
490490
const userSides: Partial<MarginType> = typeof userMargin === "number"
491491
? { top: userMargin, bottom: userMargin, left: userMargin, right: userMargin }
492492
: (userMargin ?? {})
493-
const finalMargin: MarginType = { ...defaults, ...userSides }
493+
const finalMargin: MarginType = {
494+
top: userSides.top ?? defaults.top,
495+
right: userSides.right ?? defaults.right,
496+
bottom: userSides.bottom ?? defaults.bottom,
497+
left: userSides.left ?? defaults.left,
498+
}
494499
// Auto-reserve margin for the legend ONLY on sides the user
495500
// didn't set explicitly. A caller passing `margin={{ right: 30 }}`
496501
// (e.g. positioning their own external legend) shouldn't get

0 commit comments

Comments
 (0)