react-magic icon indicating copy to clipboard operation
react-magic copied to clipboard

HTMLtoJSX issue with line-height style

Open ghost opened this issue 9 years ago • 10 comments

React has shorthand for specifying pixel values in style. So when you put into HTMLtoJSX something like this <div style="line-height: 50px;"></div>' it produces <div style={{lineHeight: 50}}> The problem is that line-height acceptes plain values (like line-height: 50), so the above JSX is actually sets line-height style for 50, not 50px.

ghost avatar Feb 28 '16 15:02 ghost

Good catch, we probably need to special-case line-height. May need some special case here: https://github.com/reactjs/react-magic/blob/master/src/htmltojsx.js#L127-L129

Daniel15 avatar Apr 18 '16 02:04 Daniel15

Should determine where React pulls this blacklist from: http://facebook.github.io/react/tips/style-props-value-px.html

Daniel15 avatar Apr 25 '16 02:04 Daniel15

@Daniel15 Why modify those values at all? React 16 will stop appending 'px' for CSS values, so that modification will potentially break. Can HTMLtoJSX simply use the values verbatim?

ssorallen avatar Apr 25 '16 18:04 ssorallen

https://facebook.github.io/react/blog/2016/04/07/react-v15.html#new-helpful-warnings

React DOM: When specifying a unit-less CSS value as a string, a future version will not add px automatically.

ssorallen avatar Apr 25 '16 18:04 ssorallen

@ssorallen - That changelog entry only applies to strings (eg. {{width: '123'}}, not numbers (eg. {{width: 123}}). The behaviour for numbers remains unchanged.

Having said that, there's no harm in keeping the px suffix, so maybe HTMLtoJSX should just use it verbatim.

Daniel15 avatar Apr 25 '16 20:04 Daniel15

any news about this issue?

sizer avatar Feb 12 '18 10:02 sizer

@mrdShinse I don't think anyone has submitted a PR to fix it yet

Daniel15 avatar Feb 14 '18 06:02 Daniel15

@ssorallen - That changelog entry only applies to strings (eg. {{width: '123'}}, not numbers (eg. {{width: 123}}). The behaviour for numbers remains unchanged.

Having said that, there's no harm in keeping the px suffix, so maybe HTMLtoJSX should just use it verbatim.

If this is the case, this can be solved simply by removing these lines https://github.com/reactjs/react-magic/blob/master/src/htmltojsx.js#L727-L729 along with the isConvertiblePixelValue function. Please advise if I'm missing some reason some styles need the px suffix omitted.

drax10 avatar Nov 19 '18 06:11 drax10

ESLint rule so this behavior can be prevented.

{
  ...
  "rules": {
    "no-restricted-syntax": [
      "error",
      {
        "selector": "Property[key.name=lineHeight] > Literal[value=/([0-9]+)[^px]$/]",
        "message": "Make sure to specify an unit when using lineHeight (see React issue: https://github.com/reactjs/react-magic/issues/53)"
      }
    ]
  }
}

SkyzohKey avatar Dec 04 '18 14:12 SkyzohKey

I'm willing to make a PR if someone can tell me where I can add an exception for line-height.

EDIT: I see that you have this method to strip px values but I can't where is being used:

https://github.com/reactjs/react-magic/blob/b915f5a3c7ef4294f8b8ac0381012e93fd4f1bd1/src/htmltojsx.js#L259

zomars avatar May 30 '22 22:05 zomars