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

UI: Fix TreeNode alignment when using a different font #22221

Merged
merged 6 commits into from
Sep 14, 2023

Conversation

bdriguesdev
Copy link
Contributor

@bdriguesdev bdriguesdev commented Apr 23, 2023

Closes #18607

What I did

The spacing to align the icons of TreeNode with the first line of text is hard coded, so when we change the theme's font it will not line up correctly with the text.
In order to be able to adjust this to work with every custom font that the user can specify, we did a trick: we added a blank character to an element that receives the icons to set the min-height to exactly one line of text, so the icons are aligned with TreeNode text.

Before:

Screenshot_8

After:

Screenshot_9

How to test

You can reproduce in two ways:

  1. https://stackblitz.com/edit/github-zf44nu?file=README.md
  2. Run a sandbox for any template, you just need to change the Storybook theme font (can be Arial)

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Maintainers

  • If this PR should be tested against many or all sandboxes,
    make sure to add the ci:merged or ci:daily GH label to it.
  • Make sure this PR contains one of the labels below.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]

@valentinpalkovic
Copy link
Contributor

@cdedreuille I would like to assign this to you if you don't mind.

export const IconsWrapper: FunctionComponent<{ children?: React.ReactNode }> = ({ children }) => {
return (
<Wrapper>
<InvisibleText>&nbsp;</InvisibleText>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need this InvisibleText? It should automatically center it in the middle without this line, no?

Copy link
Contributor Author

@bdriguesdev bdriguesdev Jun 26, 2023

Choose a reason for hiding this comment

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

The reason is that when the text for the TreeNode breaks in multiple lines the icon needs to be centered with the first line of text on the right side not with the whole container, like the first example here:
image

To achieve this the only way I found is to wrap the Icon within an element that has a height of just one line of text and align the icon vertically inside it, so it will be aligned with the first line of text of the TreeNode. We can't use a fixed value that represents the height of one line of text because each font can have a different height, to deal with it I used this trick of placing an invisible character to set a height for the container of the icon to be exactly the same as one line of a text.
Makes sense?
Also, we could align the icon in the center of the TreeNode not with the first line of text, that will be way easier and we can remove the InvisibleText


Just for visualization
Aligning with the first line of text:
Screenshot_10

Aligning with the TreeNode:
Screenshot_11

});

// Make the content have a min-height equal to one line of text
export const IconsWrapper: FunctionComponent<{ children?: React.ReactNode }> = ({ children }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use FC instead of FunctionComponent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know, this is better 😄
I changed it here: f562120

@ndelangen ndelangen assigned ndelangen and unassigned cdedreuille Aug 25, 2023
@ndelangen ndelangen changed the title Fix: Adjust TreeNode alignment when using a different font UI: Fix TreeNode alignment when using a different font Aug 25, 2023
@cdedreuille
Copy link
Contributor

What's the status on this one @ndelangen?

@ndelangen
Copy link
Member

I haven't worked on it @cdedreuille

@cdedreuille
Copy link
Contributor

The change seems good to me @ndelangen. I'll approve it but perhaps someone else in the team can review it as well.

@ndelangen
Copy link
Member

@cdedreuille does this look good to you:
https://www.chromatic.com/build?appId=635781f3500dd2c49e189caf&number=7513
?

@cdedreuille
Copy link
Contributor

Yes I checked everything on Chromatic. All good for me @ndelangen

@ndelangen ndelangen merged commit 9e7c93b into storybookjs:next Sep 14, 2023
15 of 16 checks passed
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.

Alignment of text and icon on sidebar is changed when font of storybook is changed
4 participants