Skip to content

fix(legend): render Rect shape as SVG for print compatibility#1985

Open
pranay0703 wants to merge 1 commit intoairbnb:masterfrom
pranay0703:fix/legend-rect-print-svg
Open

fix(legend): render Rect shape as SVG for print compatibility#1985
pranay0703 wants to merge 1 commit intoairbnb:masterfrom
pranay0703:fix/legend-rect-print-svg

Conversation

@pranay0703
Copy link
Copy Markdown

Summary

Fixes #1984

The Rect legend shape (shape="rect") was implemented as a <div> with background CSS color. Browsers suppress CSS backgrounds during printing unless the user explicitly enables "Print background colors". SVG fill attributes are treated as document content and always print regardless of this setting.

Changes

  • packages/visx-legend/src/shapes/Rect.tsx — Changed from <div style={{ background: fill }}> to <svg><rect fill={fill}></svg>, consistent with how Circle and Line shapes are already implemented.
  • packages/visx-legend/test/LegendThreshold.test.tsx — Updated assertions to query the <rect> element and check its fill attribute instead of querying <div> and checking background style.

Before / After

Before After
Element <div style={{ background: fill }}> <svg><rect fill={fill}></svg>
Screen ✅ Visible ✅ Visible
Print/PDF ❌ Invisible ✅ Visible

Testing

All 18 existing tests pass with the updated implementation.

Closes airbnb#1984

The Rect legend shape was rendered as a <div> with a CSS background
color. Browsers suppress CSS backgrounds during printing unless the
user enables 'Print background colors'. SVG fill attributes are treated
as document content and always print.

Changed ShapeRect to use <svg><rect fill={fill}> matching the existing
Circle and Line shape implementations. Updated LegendThreshold tests to
assert on the SVG rect fill attribute instead of the div background style.
};

export default function ShapeRect({ fill, width, height, style }: ShapeRectProps) {
const cleanWidth = typeof width === 'string' || typeof width === 'undefined' ? 15 : width;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the magic number 15 and why it's needed here? Previously we passed width and height directly to the element without a fallback value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: LegendOrdinal shape="rect" does not appear in print / print preview

2 participants