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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions code/ui/manager/src/components/sidebar/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const CollapseIcon = styled.span<{ isExpanded: boolean }>(({ theme, isExp
display: 'inline-block',
width: 0,
height: 0,
marginTop: 6,
marginLeft: 8,
marginRight: 5,
color: transparentize(0.4, theme.textMutedColor),
Expand Down Expand Up @@ -41,8 +40,6 @@ const TypeIcon = styled(Icons)<{ docsMode?: boolean }>(
{
width: 12,
height: 12,
padding: 1,
marginTop: 3,
marginRight: 5,
flex: '0 0 auto',
},
Expand Down Expand Up @@ -145,6 +142,26 @@ export const RootNode = styled.div(({ theme }) => ({
color: theme.textMutedColor,
}));

const Wrapper = styled.div({
display: 'flex',
alignItems: 'center',
});

const InvisibleText = styled.p({
margin: 0,
width: 0,
});

// 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

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

{children}
</Wrapper>
);
};

export const GroupNode: FunctionComponent<
ComponentProps<typeof BranchNode> & { isExpanded?: boolean; isExpandable?: boolean }
> = React.memo(function GroupNode({
Expand All @@ -155,8 +172,10 @@ export const GroupNode: FunctionComponent<
}) {
return (
<BranchNode isExpandable={isExpandable} tabIndex={-1} {...props}>
{isExpandable ? <CollapseIcon isExpanded={isExpanded} /> : null}
<TypeIcon icon="folder" useSymbol color="primary" />
<IconsWrapper>
{isExpandable ? <CollapseIcon isExpanded={isExpanded} /> : null}
<TypeIcon icon="folder" useSymbol color="primary" />
</IconsWrapper>
{children}
</BranchNode>
);
Expand All @@ -166,8 +185,10 @@ export const ComponentNode: FunctionComponent<ComponentProps<typeof BranchNode>>
function ComponentNode({ theme, children, isExpanded, isExpandable, isSelected, ...props }) {
return (
<BranchNode isExpandable={isExpandable} tabIndex={-1} {...props}>
{isExpandable && <CollapseIcon isExpanded={isExpanded} />}
<TypeIcon icon="component" useSymbol color="secondary" />
<IconsWrapper>
{isExpandable && <CollapseIcon isExpanded={isExpanded} />}
<TypeIcon icon="component" useSymbol color="secondary" />
</IconsWrapper>
{children}
</BranchNode>
);
Expand All @@ -179,7 +200,9 @@ export const DocumentNode: FunctionComponent<
> = React.memo(function DocumentNode({ theme, children, docsMode, ...props }) {
return (
<LeafNode tabIndex={-1} {...props}>
<TypeIcon icon="document" useSymbol docsMode={docsMode} />
<IconsWrapper>
<TypeIcon icon="document" useSymbol docsMode={docsMode} />
</IconsWrapper>
{children}
</LeafNode>
);
Expand All @@ -189,7 +212,9 @@ export const StoryNode: FunctionComponent<ComponentProps<typeof LeafNode>> = Rea
function StoryNode({ theme, children, ...props }) {
return (
<LeafNode tabIndex={-1} {...props}>
<TypeIcon icon="bookmarkhollow" useSymbol />
<IconsWrapper>
<TypeIcon icon="bookmarkhollow" useSymbol />
</IconsWrapper>
{children}
</LeafNode>
);
Expand Down