Skip to content

Fix height prop to work with rem #347

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

Merged
merged 4 commits into from
Dec 12, 2024
Merged

Fix height prop to work with rem #347

merged 4 commits into from
Dec 12, 2024

Conversation

taysea
Copy link
Collaborator

@taysea taysea commented Dec 11, 2024

Closes #346. If unit is rem, when we parseMetricToNum we need to multiply by 16 to appropriately calculate padding.

Tested locally in storybook and added unit test.

Screenshot 2024-12-11 at 12 11 55 PM Screenshot 2024-12-11 at 12 11 47 PM
import React from 'react';
import { boolean, text } from '@storybook/addon-knobs';

import { ThemeProvider } from 'styled-components';
import { Box, Text, Grommet } from 'grommet';
import * as Icons from './icons';

const customTheme = {
  global: {
    colors: {
      icon: '#2196f3',
      attention: '#ff3333',
    },
  },
  icon: {
    size: {
      small: '12px',
      medium: '18px',
      large: '48px',
      xlarge: '300px',
    },
  },
  text: {
    small: {
      size: '0.75rem',
      height: '0.875rem',
    },
    medium: {
      size: '1.125rem',
      height: '1.5rem',
    },
  },
};

export default {
  title: 'Icon',
};

export const Default = () => {
  const Icon = Icons[text('Icon', 'Accessibility')];
  if (!Icon) {
    return null;
  }
  return <Icon />;
};

export const Color = () => {
  const Icon = Icons[text('Icon', 'Accessibility')];
  if (!Icon) {
    return null;
  }
  const theme = JSON.parse(JSON.stringify(customTheme));
  theme.icon.disableScaleDown = boolean('disableScaleDown', false);
  return (
    <ThemeProvider theme={theme}>
      <Icon size={text('Size', 'xlarge')} color={text('Color', 'attention')} />
    </ThemeProvider>
  );
};

export const Plain = () => <Icons.Pocket color="plain" />;

export const CustomTheme = () => {
  const Icon = Icons[text('Icon', 'Accessibility')];
  if (!Icon) {
    return null;
  }
  const theme = JSON.parse(JSON.stringify(customTheme));
  theme.icon.disableScaleDown = boolean('disableScaleDown', false);
  return (
    <Grommet theme={theme}>
      <Box direction="row">
        <Icon size={text('Size', 'medium')} height="medium" />
        <Text>Here is some text I want my icon to align with.</Text>
      </Box>
    </Grommet>
  );
};

@taysea taysea requested a review from halocline December 11, 2024 20:17
src/js/utils.js Outdated
@@ -57,7 +57,9 @@ export function iconPad(props) {

let style = '';
if (height && theme?.text?.[height]?.height) {
const lineHeight = parseMetricToNum(theme.text[height].height);
const unit = theme.text[height].height.match(/[a-z]+$/)?.[0] || 'px';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need both the regex and the || for px? I would think the regex would match both rem and px -- also I would think this would match any a-z chars such as em,ft, even zebra.

Should we make the regex a bit more targeted to the specific units which are valid/we support?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made adjustments in latest commit.

src/js/utils.js Outdated
const lineHeight = parseMetricToNum(theme.text[height].height);
const unit = theme.text[height].height.match(/[a-z]+$/)?.[0] || 'px';
const lineHeight =
parseMetricToNum(theme.text[height].height) * (unit === 'rem' ? 16 : 1);
Copy link
Collaborator

@halocline halocline Dec 11, 2024

Choose a reason for hiding this comment

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

Where does the 16 come from? Assuming this assumes and 16px root? Can we document this with a comment and/or a static variable (e.g. const ROOT_UNIT = 16 or ROOT_ELEMENT)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made adjustments in latest commit. It is worth noting, this PR does enhance height to work with rem but additional enhancements would be required to make it work with em because we'd need to pass in the parent element node to get its font-size. I think we should leave that for separate work.

@taysea taysea requested a review from halocline December 12, 2024 18:47
@taysea taysea requested a review from halocline December 12, 2024 19:17
@taysea taysea merged commit d1caa51 into master Dec 12, 2024
9 checks passed
@taysea taysea deleted the 346-height-rem branch December 12, 2024 19:27
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.

height prop doesn't work if theme text sizes use rem
2 participants