Skip to content

Comments

Add px to unitless numbers when interpolate objects#2173

Merged
quantizor merged 15 commits intomasterfrom
add-px-to-unitless
Nov 12, 2018
Merged

Add px to unitless numbers when interpolate objects#2173
quantizor merged 15 commits intomasterfrom
add-px-to-unitless

Conversation

@Fer0x
Copy link
Contributor

@Fer0x Fer0x commented Oct 30, 2018

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.

@quantizor
Copy link
Contributor

Ugh that adds almost half a kB

@Fer0x
Copy link
Contributor Author

Fer0x commented Oct 30, 2018

@probablyup yep, would be better if react-dom just exported this function.

@quantizor
Copy link
Contributor

We could leverage this emotion package to save having to maintain it locally: https://github.com/emotion-js/emotion/tree/master/packages/unitless

@Fer0x
Copy link
Contributor Author

Fer0x commented Nov 1, 2018

@probablyup done


if (
typeof value === 'number' &&
value !== 0 &&
Copy link
Contributor

Choose a reason for hiding this comment

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

This line can probably go. There's no harm to 0 vs 0px and some older browsers want the unit anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes but it's not required so let's save the few bytes

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 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 😅

return value;
}

const isEmpty = value == null || typeof value === 'boolean' || value === '';
Copy link
Contributor

Choose a reason for hiding this comment

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

let's remove the variable assignment since it isn't being used again

export default function dangerousStyleValue(name: string, value: any): any {
// https://github.com/amilajack/eslint-plugin-flowtype-errors/issues/133
// $FlowFixMe
if (typeof value === 'symbol') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't think of when this would ever happen, let's remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@probablyup it necessary for this:

    const Bar = styled.div`
      ${Foo}: {
        background-color: red;
      };
    `;

Copy link
Contributor

Choose a reason for hiding this comment

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

How so? Nothing in that block is a symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, let's do an isValidElement check to just skip processing that stuff then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Or even throw an actual error...

Copy link
Contributor Author

@Fer0x Fer0x Nov 12, 2018

Choose a reason for hiding this comment

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

@probablyup

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) {}
      }

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's do it.

if (
typeof value === 'number' &&
value !== 0 &&
!(unitless.hasOwnProperty(name) && unitless[name] === 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can just be simplified to !(name in unitless)

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

This naming is a little ominous, let's change it to addUnitIfNeeded

@quantizor
Copy link
Contributor

Changelog entry too afterward 😄

@Fer0x
Copy link
Contributor Author

Fer0x commented Nov 12, 2018

@probablyup ready for final review

@Fer0x Fer0x requested a review from quantizor November 12, 2018 15:46
@quantizor quantizor merged commit 3af9bef into master Nov 12, 2018
@quantizor quantizor deleted the add-px-to-unitless branch November 12, 2018 17:20
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.

4 participants