Skip to content

Add useMargin hook#6224

Merged
ckifer merged 4 commits intomainfrom
useMargin
Aug 16, 2025
Merged

Add useMargin hook#6224
ckifer merged 4 commits intomainfrom
useMargin

Conversation

@PavelVanecek
Copy link
Collaborator

@PavelVanecek PavelVanecek commented Aug 16, 2025

Description

Export and document hook, refresh documentation a little, add story

Related Issue

Closes #5783

Motivation and Context

Margins are not exactly internal state - they are defined externally - but the hook will still be useful for shared components and other libraries.

Screenshots (if appropriate):

image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • I have added a storybook story or VR test, or extended an existing story or VR test to show my changes

Copilot AI review requested due to automatic review settings August 16, 2025 02:21
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 adds and exports a new useMargin hook to the public API, along with comprehensive documentation and storybook examples. The hook provides access to chart margin values defined by users in chart props, distinct from the useOffset hook which includes calculated space for axes, legends, and other elements.

  • Export useMargin hook from the main index and add comprehensive documentation
  • Update margin type definitions to distinguish between user-defined partial margins and internal complete margins
  • Add storybook stories and visual components to demonstrate the hook functionality

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/index.ts Exports the new useMargin hook to make it publicly available
src/context/chartLayoutContext.tsx Updates useMargin hook implementation with proper documentation and typing
src/util/types.ts Refines Margin type definitions to distinguish partial vs complete margins
src/state/layoutSlice.ts Updates margin setter to handle partial margin objects with defaults
storybook/stories/API/hooks/useMargin.* Adds complete documentation and interactive examples for the new hook
test/component/Legend.spec.tsx Updates test to use proper chart context instead of isolated portal context

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

const [lastBoundingBox, updateBoundingBox] = useElementOffset([contextPayload]);
const chartWidth = useChartWidth();
const chartHeight = useChartHeight();
if (chartWidth == null || chartHeight == null) {
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using strict equality comparison (===) instead of loose equality (==) for null/undefined checks. Replace chartWidth == null with chartWidth === undefined and chartHeight == null with chartHeight === undefined for more explicit type checking.

Suggested change
if (chartWidth == null || chartHeight == null) {
if (chartWidth === undefined || chartHeight === undefined) {

Copilot uses AI. Check for mistakes.
if (chartWidth == null || chartHeight == null) {
return null;
}
const maxWidth = chartWidth - (margin.left || 0) - (margin.right || 0);
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

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

This line will throw a runtime error if chartWidth is undefined. The null check on line 140 should return early before this calculation, but the code continues to execute. Move this calculation inside the conditional block after the null check or ensure chartWidth is guaranteed to be defined at this point.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Aug 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.67%. Comparing base (38df1df) to head (55680a1).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6224      +/-   ##
==========================================
- Coverage   96.71%   96.67%   -0.04%     
==========================================
  Files         221      221              
  Lines       19886    19923      +37     
  Branches     4103     4112       +9     
==========================================
+ Hits        19233    19261      +28     
- Misses        647      656       +9     
  Partials        6        6              

☔ 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 Aug 16, 2025

Bundle Report

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

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.03MB 952 bytes (0.09%) ⬆️
recharts/bundle-es6 886.99kB 823 bytes (0.09%) ⬆️
recharts/bundle-umd 484.76kB 120 bytes (0.02%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
component/Legend.js 72 bytes 8.08kB 0.9%
hooks.js 71 bytes 4.81kB 1.5%
context/chartLayoutContext.js 183 bytes 4.77kB 3.99%
index.js 11 bytes 3.17kB 0.35%
state/layoutSlice.js 486 bytes 1.45kB 50.36% ⚠️
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
index.js 140 bytes 11.3kB 1.25%
component/Legend.js 72 bytes 9.12kB 0.8%
hooks.js 71 bytes 5.65kB 1.27%
context/chartLayoutContext.js 183 bytes 5.61kB 3.37%
state/layoutSlice.js 486 bytes 1.8kB 36.9% ⚠️
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 120 bytes 484.76kB 0.02%

@ckifer
Copy link
Member

ckifer commented Aug 16, 2025

I guess I don't really get this one. But doesn't hurt anything either I suppose

@ckifer ckifer merged commit 5541397 into main Aug 16, 2025
25 of 27 checks passed
@PavelVanecek PavelVanecek deleted the useMargin branch August 16, 2025 05:25
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.

Expose chart's dimensions, offset, and margin with a hook

3 participants