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 display of custom brand image #13355

Merged
merged 4 commits into from Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 24 additions & 37 deletions lib/ui/src/components/sidebar/Brand.tsx
Expand Up @@ -31,41 +31,28 @@ export const LogoLink = styled.a(({ theme }) => ({
},
}));

export const Brand = withTheme(
({
theme: {
brand: { title = 'Storybook', url = './', image },
},
}) => {
const targetValue = url === './' ? '' : '_blank';
if (image === undefined && url === null) {
return <StorybookLogoStyled alt={title} />;
}
if (image === undefined && url) {
return (
<LogoLink title={title} href={url} target={targetValue}>
<StorybookLogoStyled alt={title} />
</LogoLink>
);
}
if (image === null && url === null) {
return title;
}
if (image === null && url) {
return (
<LogoLink href={url} target={targetValue} dangerouslySetInnerHTML={{ __html: title }} />
);
}
if (image && url === null) {
return <Img src={image} alt={title} />;
}
if (image && url) {
return (
<LogoLink title={title} href={url} target={targetValue}>
<Img src={image} alt={title} />
</LogoLink>
);
}
return null;
export const Brand = withTheme(({ theme }) => {
const { title = 'Storybook', url = './', image } = theme.brand;
const targetValue = url === './' ? '' : '_blank';

// When image is explicitly set to null, enable custom HTML support
if (image === null) {
if (title === null) return null;
// eslint-disable-next-line react/no-danger
if (!url) return <div dangerouslySetInnerHTML={{ __html: title }} />;
return <LogoLink href={url} target={targetValue} dangerouslySetInnerHTML={{ __html: title }} />;
}

const logo = image ? <Img src={image} alt={title} /> : <StorybookLogoStyled alt={title} />;

if (url) {
return (
<LogoLink title={title} href={url} target={targetValue}>
{logo}
</LogoLink>
);
}
);

// The wrapper div serves to prevent image misalignment
return <div>{logo}</div>;
});
54 changes: 54 additions & 0 deletions lib/ui/src/components/sidebar/Heading.stories.tsx
Expand Up @@ -131,3 +131,57 @@ export const customBrandImage = () => {
</ThemeProvider>
);
};

export const customBrandImageTall = () => {
const theme = useTheme() as Theme;
return (
<ThemeProvider
theme={{
...theme,
brand: {
title: 'My Title',
url: 'https://example.com',
image: 'https://via.placeholder.com/100x150',
},
}}
>
<Heading menu={menuItems} />
</ThemeProvider>
);
};

export const customBrandImageUnsizedSVG = () => {
const theme = useTheme() as Theme;
return (
<ThemeProvider
theme={{
...theme,
brand: {
title: 'My Title',
url: 'https://example.com',
image: 'https://s.cdpn.io/91525/potofgold.svg',
},
}}
>
<Heading menu={menuItems} />
</ThemeProvider>
);
};

export const noBrand = () => {
const theme = useTheme() as Theme;
return (
<ThemeProvider
theme={{
...theme,
brand: {
title: null,
url: null,
image: null,
},
}}
>
<Heading menu={menuItems} />
</ThemeProvider>
);
};
4 changes: 2 additions & 2 deletions lib/ui/src/components/sidebar/Heading.tsx
Expand Up @@ -13,7 +13,7 @@ const BrandArea = styled.div(({ theme }) => ({
fontSize: theme.typography.size.s2,
fontWeight: theme.typography.weight.bold,
color: theme.color.defaultText,
marginRight: 40,
marginRight: 20,
display: 'flex',
width: '100%',
alignItems: 'center',
Expand All @@ -22,8 +22,8 @@ const BrandArea = styled.div(({ theme }) => ({
'& > *': {
maxWidth: '100%',
height: 'auto',
width: 'auto',
display: 'block',
flex: '1 1 auto',
},
}));

Expand Down