Skip to content

ResponsiveContainer is built-in to all charts#6388

Merged
ckifer merged 3 commits intomainfrom
responsivecontainer-built-in
Sep 28, 2025
Merged

ResponsiveContainer is built-in to all charts#6388
ckifer merged 3 commits intomainfrom
responsivecontainer-built-in

Conversation

@PavelVanecek
Copy link
Collaborator

Description

I never really understood why does this need to be its own element so let's have these props directly on the main chart shall we.

This also means that now charts without any dimensions at all will default to 'width=100% height=100%'. Before this PR, chart without dimensions is just quietly invisible which I think is a disaster. In a way this is a change of default behaviour - but I prefer to think of it as a bugfix.

The ResponsiveContainer itself is not as important as before but it still has some use I suppose - for example in the Bubble story it allows setting the dimensions from one place. Although that could be a variable too.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR integrates responsive container functionality directly into all chart components, eliminating the need to wrap charts with ResponsiveContainer in most cases. Charts without explicit dimensions now default to width=100% and height=100%, preventing invisible charts.

Key changes:

  • Built-in responsive behavior in all chart types (CartesianChart, PolarChart, Sankey, SunburstChart, Treemap)
  • New aspectRatio prop added to chart components
  • Type improvements with Percent types and proper ReactNode usage
  • Enhanced test coverage for dimension calculations

Reviewed Changes

Copilot reviewed 19 out of 22 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/chart/*.tsx Wraps chart components with ResponsiveContainer and adds aspectRatio prop
src/util/types.ts Updates chart prop types to support Percent and adds aspectRatio
src/component/ResponsiveContainer.tsx Refactors to avoid unnecessary container divs for fixed dimensions
test/**/*.spec.tsx Updates tests to provide explicit dimensions where needed
storybook/stories/Examples/ScatterChart.stories.tsx Removes redundant ResponsiveContainer wrappers

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@codecov
Copy link

codecov bot commented Sep 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.08%. Comparing base (0ccf349) to head (02476a0).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6388      +/-   ##
==========================================
+ Coverage   62.04%   62.08%   +0.03%     
==========================================
  Files         412      412              
  Lines       37883    37917      +34     
  Branches     4338     4337       -1     
==========================================
+ Hits        23505    23539      +34     
  Misses      14269    14269              
  Partials      109      109              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codecov
Copy link

codecov bot commented Sep 27, 2025

Bundle Report

Changes will increase total bundle size by 7.46kB (0.3%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.06MB 3.44kB (0.32%) ⬆️
recharts/bundle-es6 916.37kB 3.2kB (0.35%) ⬆️
recharts/bundle-umd 489.8kB 815 bytes (0.17%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
chart/Treemap.js 234 bytes 27.59kB 0.86%
chart/Sankey.js 234 bytes 26.49kB 0.89%
chart/SunburstChart.js 235 bytes 12.27kB 1.95%
component/ResponsiveContainer.js 2.06kB 10.28kB 25.13% ⚠️
context/chartLayoutContext.js 174 bytes 7.33kB 2.43%
chart/PolarChart.js 251 bytes 5.58kB 4.71%
chart/CartesianChart.js 251 bytes 4.79kB 5.53% ⚠️
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 815 bytes 489.8kB 0.17%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
chart/Treemap.js 213 bytes 25.98kB 0.83%
chart/Sankey.js 213 bytes 24.84kB 0.86%
chart/SunburstChart.js 214 bytes 10.98kB 1.99%
component/ResponsiveContainer.js 1.98kB 9.01kB 28.17% ⚠️
context/chartLayoutContext.js 119 bytes 6.4kB 1.89%
chart/PolarChart.js 230 bytes 4.64kB 5.22% ⚠️
chart/CartesianChart.js 230 bytes 3.85kB 6.34% ⚠️

Copy link
Member

@ckifer ckifer left a comment

Choose a reason for hiding this comment

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

My only qualm with this PR is that I think this makes SSR even more broken than it already is?

Pre-3.0 people would SSR charts without responsive container because otherwise resize observer wouldn't be available.

I suppose we can add a conditional to just return children at some point easily enough

* as whatever size we detect won't be helping anyway.
*/
return (
<ResponsiveContainerContextProvider width={calculatedWidth} height={calculatedHeight}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did add this shortcut @ckifer - in case we are able to calculate specific size from the given arguments then the helper div doesn't render at all.

I haven't tested SSR though, and I am not sure what is the problem in SSR in the first place, so can't tell if this makes it better or worse.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah just need to make sure the things before this don't crash things out and actually make it here I guess

* If you define both width and height on the treemap, this prop will be ignored for the main chart,
* but will still be used for the rectangles.
*/
aspectRatio?: number;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also callout on Treemap.aspectRatio - this prop was there before, but only applied on the individual nodes. I think it makes sense to have it apply on the chart as a whole as well.

@ckifer
Copy link
Member

ckifer commented Sep 28, 2025

We probably should update the docs when this one goes out

@ckifer ckifer merged commit 5b8d0b8 into main Sep 28, 2025
27 of 28 checks passed
@ckifer ckifer deleted the responsivecontainer-built-in branch September 28, 2025 03:38
@ckifer
Copy link
Member

ckifer commented Sep 28, 2025

storybook test timeout hmmm, will re-run once

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.

3 participants