feat: allow HTML attributes passthrough on ResponsiveContainer#7168
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThe ResponsiveContainer Props interface is enhanced to accept additional standard HTML div attributes through extending React.HTMLAttributes, excluding id, className, style, and onResize. The style prop type is simplified, and the component forwards remaining props to the rendered div element. A new test is added. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/component/ResponsiveContainer.tsx (1)
257-304:⚠️ Potential issue | 🟠 MajorHTML attributes are not forwarded in all code paths.
The
ResponsiveContainercomponent has three return paths, but only one forwards HTML attributes:
- Line 264 (nested container): Returns only
props.children— attributes lost- Lines 294-298 (fixed dimensions): Returns only context provider — attributes lost
- Line 304 (size observer): Uses
SizeDetectorContainer— attributes forwarded ✓When users provide fixed numeric
widthandheight(or when nested), props likedata-testid,aria-label, androlewill be silently dropped.🐛 Proposed fix to forward attributes in all paths
if (isPositiveNumber(responsiveContainerContext.width) && isPositiveNumber(responsiveContainerContext.height)) { - return props.children; + return <SizeDetectorContainer {...props} ref={ref} />; }And for the fixed dimensions path, consider wrapping children in a div with the HTML attributes, or always use
SizeDetectorContainer:if (isNumber(calculatedWidth) && isNumber(calculatedHeight)) { - return ( - <ResponsiveContainerContextProvider width={calculatedWidth} height={calculatedHeight}> - {props.children} - </ResponsiveContainerContextProvider> - ); + return <SizeDetectorContainer {...props} width={width} height={height} ref={ref} />; }Alternatively, if avoiding the extra div is important for these cases, document this limitation clearly.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/component/ResponsiveContainer.tsx` around lines 257 - 304, ResponsiveContainer currently returns three different roots but only the SizeDetectorContainer path forwards HTML attributes, so attributes like data-testid/aria-label are dropped when returning props.children (nested case) or ResponsiveContainerContextProvider (fixed-dimensions case). Fix by ensuring HTML attributes from props are passed through on all return paths: spread the leftover DOM props (e.g., using restProps or extracting known non-DOM props) onto a wrapper element or onto ResponsiveContainerContextProvider when returning fixed dimensions, and when returning props.children in the nested-container case wrap children in a fragment that preserves attributes (e.g., a <div> or the existing SizeDetectorContainer) so attributes are applied; update ResponsiveContainer to forward ref and DOM props consistently and reuse SizeDetectorContainer or ResponsiveContainerContextProvider wrappers to centralize attribute forwarding (references: ResponsiveContainer, SizeDetectorContainer, ResponsiveContainerContextProvider, useResponsiveContainerContext, getDefaultWidthAndHeight, calculateChartDimensions).
🧹 Nitpick comments (1)
test/component/ResponsiveContainerDataTest.spec.tsx (1)
7-18: Consider adding tests for other HTML attributes and edge cases.The test only verifies
data-testidwithinitialDimension. Consider adding coverage for:
- Other HTML attributes mentioned in the PR (e.g.,
aria-label,role)- The case where fixed numeric
widthandheightare provided (which currently may not forward attributes — see the issue flagged in the source file)💡 Additional test cases to consider
test('should pass down aria-label and role attributes', () => { const { getByTestId } = render( <ResponsiveContainer data-testid="accessible-chart" aria-label="Sales chart" role="img" initialDimension={{ width: 100, height: 100 }} > <BarChart width={100} height={100} data={[]}> <Bar dataKey="value" /> </BarChart> </ResponsiveContainer>, ); vi.runOnlyPendingTimers(); const container = getByTestId('accessible-chart'); expect(container).toHaveAttribute('aria-label', 'Sales chart'); expect(container).toHaveAttribute('role', 'img'); }); test('should pass down data-testid with fixed dimensions', () => { const { getByTestId } = render( <ResponsiveContainer data-testid="fixed-chart" width={200} height={200}> <BarChart width={200} height={200} data={[]}> <Bar dataKey="value" /> </BarChart> </ResponsiveContainer>, ); vi.runOnlyPendingTimers(); // This test may fail until the source issue is fixed expect(getByTestId('fixed-chart')).toBeInTheDocument(); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/component/ResponsiveContainerDataTest.spec.tsx` around lines 7 - 18, Add tests to cover forwarding other HTML attributes and the fixed-dimension path: render ResponsiveContainer (use the ResponsiveContainer component symbol) with initialDimension and with aria-label and role props and assert via getByTestId that the root element has aria-label and role; also add a separate test rendering ResponsiveContainer with numeric width and height props (width, height) and data-testid and assert that getByTestId('fixed-chart') is present (run timers with vi.runOnlyPendingTimers where needed). These tests will target attribute forwarding for both the initialDimension path and the fixed width/height path so you can detect and fix the reported attribute-forwarding bug.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@test/component/ResponsiveContainerDataTest.spec.tsx`:
- Around line 6-18: Add fake timer setup and advancement around the render in
this test: call vi.useFakeTimers() before calling render(…) and after the render
call invoke vi.runOnlyPendingTimers() to flush timers; then replace the
container.querySelector assertion with the render result's
getByTestId('my-container') (from the render return) to assert existence. Update
the test for ResponsiveContainer and its render usage accordingly and ensure
timers are restored/left as per suite conventions if needed.
---
Outside diff comments:
In `@src/component/ResponsiveContainer.tsx`:
- Around line 257-304: ResponsiveContainer currently returns three different
roots but only the SizeDetectorContainer path forwards HTML attributes, so
attributes like data-testid/aria-label are dropped when returning props.children
(nested case) or ResponsiveContainerContextProvider (fixed-dimensions case). Fix
by ensuring HTML attributes from props are passed through on all return paths:
spread the leftover DOM props (e.g., using restProps or extracting known non-DOM
props) onto a wrapper element or onto ResponsiveContainerContextProvider when
returning fixed dimensions, and when returning props.children in the
nested-container case wrap children in a fragment that preserves attributes
(e.g., a <div> or the existing SizeDetectorContainer) so attributes are applied;
update ResponsiveContainer to forward ref and DOM props consistently and reuse
SizeDetectorContainer or ResponsiveContainerContextProvider wrappers to
centralize attribute forwarding (references: ResponsiveContainer,
SizeDetectorContainer, ResponsiveContainerContextProvider,
useResponsiveContainerContext, getDefaultWidthAndHeight,
calculateChartDimensions).
---
Nitpick comments:
In `@test/component/ResponsiveContainerDataTest.spec.tsx`:
- Around line 7-18: Add tests to cover forwarding other HTML attributes and the
fixed-dimension path: render ResponsiveContainer (use the ResponsiveContainer
component symbol) with initialDimension and with aria-label and role props and
assert via getByTestId that the root element has aria-label and role; also add a
separate test rendering ResponsiveContainer with numeric width and height props
(width, height) and data-testid and assert that getByTestId('fixed-chart') is
present (run timers with vi.runOnlyPendingTimers where needed). These tests will
target attribute forwarding for both the initialDimension path and the fixed
width/height path so you can detect and fix the reported attribute-forwarding
bug.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a8c096c2-7bab-48eb-98b9-b75c21e3508b
📒 Files selected for processing (2)
src/component/ResponsiveContainer.tsxtest/component/ResponsiveContainerDataTest.spec.tsx
ResponsiveContainer now extends React.HTMLAttributes<HTMLDivElement>, allowing users to pass standard HTML attributes like data-testid, aria-label, role, etc. to the underlying container div. This enables better testing (data-testid) and accessibility (aria-*) without requiring wrapper elements. Conflicting internal props (id, className, style, onResize) are omitted from the HTML attributes to avoid type collisions.
fdd2a3a to
f41b1ad
Compare
Bundle ReportChanges will increase total bundle size by 2.07kB (0.04%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: recharts/bundle-cjsAssets Changed:
view changes for bundle: recharts/bundle-es6Assets Changed:
view changes for bundle: recharts/bundle-umdAssets Changed:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7168 +/- ##
=======================================
Coverage 89.05% 89.05%
=======================================
Files 548 548
Lines 40575 40577 +2
Branches 5581 5580 -1
=======================================
+ Hits 36134 36136 +2
Misses 4432 4432
Partials 9 9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
ResponsiveContainerpreviously did not accept standard HTML attributes likedata-testid,aria-label,role, etc. Users who needed these for testing or accessibility had to wrap the container in an extradiv.Changes
Propsinterface to includeReact.HTMLAttributes<HTMLDivElement>(withOmitfor internally-managed props:id,className,style,onResize)...others) onto the root containerdivstyleprop type fromOmit<CSSProperties, keyof Props>toCSSProperties— the previous type was overly restrictive and stripped valid CSS properties likecolorandwidththat happened to share names with component propsUsage
Tests
Added
test/component/ResponsiveContainerDataTest.spec.tsxverifyingdata-testidpassthrough.All 26 existing ResponsiveContainer tests continue to pass.
Summary by CodeRabbit
New Features
Tests