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

[system] Warns if the hex color contains trailing space #44538

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/mui-system/src/colorManipulator/colorManipulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ export function hexToRgb(color) {
return colors
? `rgb${colors.length === 4 ? 'a' : ''}(${colors
.map((n, index) => {
return index < 3 ? parseInt(n, 16) : Math.round((parseInt(n, 16) / 255) * 1000) / 1000;
if (index < 3) {
return parseInt(n, 16);
}
const channel = Math.round((parseInt(n, 16) / 255) * 1000) / 1000;
if (Number.isNaN(channel)) {
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
console.error(
`MUI: The color:"${color}" has invalid alpha channel: "${channel}". Make sure the color input doesn't contain leading/trailing space.`,
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
);
}
return channel || 1;
})
.join(', ')})`
: '';
Expand Down
10 changes: 10 additions & 0 deletions packages/mui-system/src/colorManipulator/colorManipulator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ describe('utils/colorManipulator', () => {
it('converts a long alpha hex color to an argb color` ', () => {
expect(hexToRgb('#111111f8')).to.equal('rgba(17, 17, 17, 0.973)');
});

it('warns if the color contains space at the end', () => {
let result;
expect(() => {
result = hexToRgb('#aa0099 ');
Copy link
Member

@oliviertassinari oliviertassinari Nov 26, 2024

Choose a reason for hiding this comment

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

I think it would be great to test a high-level API, like alpha() instead. I don't see why hexToRgb() is public in the first place.

}).toErrorDev([
'MUI: The color:"aa0099 " has invalid alpha channel: "NaN". Make sure the color input doesn\'t contain leading/trailing space.',
]);
expect(result).to.equal('rgba(170, 0, 153, 1)');
});
});

describe('rgbToHex', () => {
Expand Down
Loading