Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Remove colors prop from SparkLineChart. #16494

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

bernardobelchior
Copy link
Member

Remove colors prop from SparkLineChart.

In #16477, I deprecated the colors prop, but we can still do breaking changes for v8. This will reduce the maintenance burden slightly.

Also added codemod to migrate from the old colors prop to the new color prop.

<SparkLineChart data={data} colors={['red']} />
<SparkLineChart data={data} colors={fn} />
<SparkLineChart data={data} colors={(mode) => (mode === 'light' ? ['black'] : ['white'])} />
<SparkLineChart data={data} colors="red" />
Copy link
Member Author

Choose a reason for hiding this comment

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

This is valid since colors can be an array of strings or a function, but added it to ensure that we handle invalid well (i.e., by not applying any transformation).

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed this test case because TypeScript wasn't happy with invalid properties.

@bernardobelchior bernardobelchior added breaking change component: charts This is the name of the generic UI component, not the React module! labels Feb 6, 2025
@mui-bot
Copy link

mui-bot commented Feb 6, 2025

Deploy preview: https://deploy-preview-16494--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against 237df3b

Copy link

codspeed-hq bot commented Feb 6, 2025

CodSpeed Performance Report

Merging #16494 will not alter performance

Comparing bernardobelchior:remove-sparkline-colors (237df3b) with master (3ba4176)

Summary

✅ 6 untouched benchmarks

(<React.Fragment>
<SparkLineChart data={data} color={['red'][0]} />
<SparkLineChart data={data} color={typeof fn === "function" ? mode => fn(mode)?.[0] : fn} />
<SparkLineChart data={data} color={mode => (mode => (mode === 'light' ? ['black'] : ['white']))(mode)?.[0]} />
Copy link
Member Author

Choose a reason for hiding this comment

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

This might seem a bit confusing, but what I'm doing is:

Given an arrow function declaration fnDecl, transform it into: mode => (fnDecl)(mode)?.[0].

We need the optional chaining operator because undefined is a valid value for colors, so we need be careful not to access [0] of an undefined value.

return (
(<React.Fragment>
<SparkLineChart data={data} color={['red'][0]} />
<SparkLineChart data={data} color={typeof fn === "function" ? mode => fn(mode)?.[0] : fn} />
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if this is a valid approach.

This introduces a slight runtime performance hit because, when codemodding, we can't guarantee whether fn is a function or an array.

If you think this or another example are too complex, I can just remove this and leave this as an unhandled case.

// Don't know how to handle this case
}

// Only apply transformation if we know how to handle it
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there any best practice for letting the user know where we caught an unhandled case? I know we aren't covering 100% of the cases, and some are easy to detect, but hard to fix. It would be nice if we could let the user know which ones have been detected to guide them towards where they need to do a manual fix.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we currently notify unhandled cases

Copy link
Member

Choose a reason for hiding this comment

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

You can propose that though 👍

// prettier-ignore
return (
(<React.Fragment>
<SparkLineChart data={data} color={['red']?.[0]} />
Copy link
Member Author

Choose a reason for hiding this comment

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

I followed a "better safe than sorry" approach here. Adding the optional chain operator might save us from some crashes in edge cases. Happy to remove it if you think that's too much.

Copy link
Member

Choose a reason for hiding this comment

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

what will happen to var when var=['blue']?

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 7, 2025
Copy link

github-actions bot commented Feb 7, 2025

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@bernardobelchior bernardobelchior marked this pull request as ready for review February 7, 2025 16:07
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change component: charts This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants