Add px to unitless numbers when interpolate objects#2173
Conversation
|
Ugh that adds almost half a kB |
|
@probablyup yep, would be better if react-dom just exported this function. |
|
We could leverage this emotion package to save having to maintain it locally: https://github.com/emotion-js/emotion/tree/master/packages/unitless |
|
@probablyup done |
src/utils/dangerousStyleValue.js
Outdated
|
|
||
| if ( | ||
| typeof value === 'number' && | ||
| value !== 0 && |
There was a problem hiding this comment.
This line can probably go. There's no harm to 0 vs 0px and some older browsers want the unit anyway.
There was a problem hiding this comment.
@probablyup unitless zero is part of CSS 2.1 which is supported even in IE 8.
As i know, React supports IE 9+, which makes adding px to 0 senseless.
This file is mostly copied "as it" from react source code. I think it will be strange when react itself doesn't apply px to zero, but styled-components will do it.
There was a problem hiding this comment.
Yes but it's not required so let's save the few bytes
There was a problem hiding this comment.
I don't want to be nitpicky, but a few bytes that we save here is putting more strain on gzip; either way were talking 6-7 chars after uglification. The bulk is still the map of properties 😅
src/utils/dangerousStyleValue.js
Outdated
| return value; | ||
| } | ||
|
|
||
| const isEmpty = value == null || typeof value === 'boolean' || value === ''; |
There was a problem hiding this comment.
let's remove the variable assignment since it isn't being used again
src/utils/dangerousStyleValue.js
Outdated
| export default function dangerousStyleValue(name: string, value: any): any { | ||
| // https://github.com/amilajack/eslint-plugin-flowtype-errors/issues/133 | ||
| // $FlowFixMe | ||
| if (typeof value === 'symbol') { |
There was a problem hiding this comment.
I can't think of when this would ever happen, let's remove.
There was a problem hiding this comment.
@probablyup it necessary for this:
const Bar = styled.div`
${Foo}: {
background-color: red;
};
`;There was a problem hiding this comment.
How so? Nothing in that block is a symbol.
There was a problem hiding this comment.
@probablyup because flatten only warns about passing non styled component but doesn't break function execution. So, here https://github.com/styled-components/styled-components/blob/master/src/utils/flatten.js#L88 React component passed to objToCss where it loops over all React component properties.
There was a problem hiding this comment.
Ah, let's do an isValidElement check to just skip processing that stuff then.
There was a problem hiding this comment.
@probablyup maybe it will be better to have return in https://github.com/styled-components/styled-components/blob/master/src/utils/flatten.js#L73 ?
There was a problem hiding this comment.
Or even throw an actual error...
There was a problem hiding this comment.
How about to throw StyledError here in both dev/prod envs?
if (process.env.NODE_ENV !== 'production') {
/* Warn if not referring styled component */
try {
// eslint-disable-next-line new-cap
if (isElement(new chunk(executionContext))) {
console.warn(
`${getComponentName(
chunk
)} is not a styled component and cannot be referred to via component selector. See https://www.styled-components.com/docs/advanced#referring-to-other-components for more details.`
);
}
// eslint-disable-next-line no-empty
} catch (e) {}
}
src/utils/dangerousStyleValue.js
Outdated
| if ( | ||
| typeof value === 'number' && | ||
| value !== 0 && | ||
| !(unitless.hasOwnProperty(name) && unitless[name] === 1) |
There was a problem hiding this comment.
I think this can just be simplified to !(name in unitless)
src/utils/dangerousStyleValue.js
Outdated
| import unitless from '@emotion/unitless'; | ||
|
|
||
| // Taken from https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/shared/dangerousStyleValue.js | ||
| export default function dangerousStyleValue(name: string, value: any): any { |
There was a problem hiding this comment.
This naming is a little ominous, let's change it to addUnitIfNeeded
|
Changelog entry too afterward 😄 |
|
@probablyup ready for final review |
we don't use danger anymore, so it's generated needlessly
Fix #2165, #825
This PR highly based on slightly modified react-dom internal code.
It adds px to all number values excerpt whitelisted properties.
I know that will a slightly bloat library, but it will be very helpful for those, who is moving from object-like css-in-js solutions to styled-components.