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

Supporting rem as fontSize and lineheight #121

Open
LukeFinch opened this issue Jan 11, 2023 · 4 comments
Open

Supporting rem as fontSize and lineheight #121

LukeFinch opened this issue Jan 11, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@LukeFinch
Copy link

I'm looking into supporting 'rem' as a unit for our design system, and migrating away from pixel values.

Currently, if I want to do that, I have to override the outputs provided from capsize like so:

export const textCrop = ({
  lineHeight,
  fontSize,
  fontMetrics,
}: TextCropProps) => {
  const fontSizeAsNumber = parseFloat(fontSize);
  const leading = lineHeight * fontSizeAsNumber;

  const capsizeStyles = createStyleObject({
    fontSize: fontSizeAsNumber,
    leading,
    fontMetrics,
  });

  capsizeStyles.fontSize = fontSize;
  capsizeStyles.lineHeight = lineHeight.toString();
  return capsizeStyles;
};

with arguments as an example:fontSize: '2rem' lineHeight: 1.5

Looking into the source code - it seems like pixels are only needed for rounding – I assume if I wanted to use capHeight as a pixel value, and lineGap?

Is there any potential issue with me:

a) passing values in as rem - my assumption is not due to the relative nature of all the calculations
b) overriding the output back to a rem unit?

@andriytyurnikov
Copy link

I am thinking about same thing, and I lean towards CSS calc() for font-size property, instead of compile-time computations over values normalised to px.

@andriytyurnikov
Copy link

andriytyurnikov commented Oct 3, 2023

@LukeFinch I've successfully reimplemended computations in CSS calc, discovered workaround is to pass values as numbers (implied rem), storing as CSS variables and to convert into units only after computation - exactly at the assignment. With this approach CSS-only rem solution is possible: https://jsbin.com/danobedoye/11/edit?html,css,output
Works only in Safari (possibly due to calc() nesting limits)

@michaeltaranto michaeltaranto added the enhancement New feature or request label Feb 19, 2024
@smashercosmo
Copy link

@andriytyurnikov I think it only works in Safari (and now also in Firefox) due to the usage of css abs() function. You can emulate abs() function by doing this:

max(var(--x), -1 * var(--x))

And it will work everywhere.

@andriytyurnikov
Copy link

@smashercosmo that is an interesting hypothesis, but no.
CSS calc evaluation has various limits in different browsers.
w3c/csswg-drafts#3462

Replacing abs() would make nesting deeper, and would brake things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants