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

Fix polyfill for unitless lineHeight #106

Merged
merged 1 commit into from
Apr 26, 2024
Merged

Conversation

necolas
Copy link
Contributor

@necolas necolas commented Apr 25, 2024

When a unitless lineHeight is inherited, it multiples the value of the fontSize of the inheriting element. But when an em value is inherited, it multiples the value of the fontSize of the element it was originally applied to, not the one on the inheriting element.

This patch fixes the resolved values to match web's computed values, but React Native itself renders line-height differently from web, as you can see in these screenshots. Text elements have a semi-transparent red background. React Native changes the shape of the text element box when the line box size changes, and the line box of the parent text seems to grow to match the largest nested line box.

Web

image

Native

image

Copy link

github-actions bot commented Apr 25, 2024

compressed-size: runtime library

Size change: +0.06 kB
Total size: 18.95 kB

Filename: gzip (minify) kB size kB change % change
./packages/react-strict-dom/dist/native/index.js 15.01 (47.00) +0.06 (+0.22) +0.4% (+0.5%) 🔴
View unchanged
Filename: gzip (minify) kB size kB change % change
./packages/react-strict-dom/dist/dom/index.js 2.99 (9.00) 0.00 (0.00) 0.0% (0.0%)
./packages/react-strict-dom/dist/dom/runtime.js 0.95 (2.33) 0.00 (0.00) 0.0% (0.0%)

When a unitless lineHeight is inherited, it multiples the value of the
fontSize of the inheriting element. But when an em value is inherited,
it multiples the value of the fontSize of the element it was originally
applied to, not the one on the inheriting element.
@necolas necolas force-pushed the fix-polyfill-lineheight-unitless branch from 934a050 to 85f011c Compare April 25, 2024 20:08
const lineHeightValue = parseFloat(styleValue);
// Only convert unitless lineHeight if fontSize exists
if (style.fontSize != null) {
if (style.fontSize instanceof CSSLengthUnitValue) {
Copy link
Member

Choose a reason for hiding this comment

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

awesome! so now we process the multiplier into a px value based on the current font size?

Copy link
Contributor Author

@necolas necolas Apr 25, 2024

Choose a reason for hiding this comment

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

We were doing that before too, but earlier in the translation process. Before, it would be converted to px straight away based on whatever the fontSize was on the element that defined lineHeight. After, it will not be converted to px until it is being rendered to an element (e.g., deeper in the tree), which should allow the unitless number to be inherited properly.

@necolas necolas merged commit f195f24 into main Apr 26, 2024
6 checks passed
@nmn nmn deleted the fix-polyfill-lineheight-unitless branch April 26, 2024 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants