Skip to content

Commit

Permalink
fix(project): fix header content shift due to image load
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Jun 22, 2021
1 parent a8f6733 commit 0dbde3f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const Header: React.FC<Props> = ({
}) => {
const { t } = useTranslation('menu');
const [mobileSearchActive, setMobileSearchActive] = useState(false);
const [logoLoaded, setLogoLoaded] = useState(false);
const breakpoint = useBreakpoint();
const headerClassName = classNames(styles.header, styles[headerType], {
[styles.mobileSearchActive]: mobileSearchActive && breakpoint <= Breakpoint.sm,
Expand Down Expand Up @@ -78,11 +79,11 @@ const Header: React.FC<Props> = ({
</div>
{logoSrc && (
<div className={styles.brand}>
<Logo src={logoSrc} />
<Logo src={logoSrc} onLoad={() => setLogoLoaded(true)} />
</div>
)}
<nav className={styles.nav} aria-label="menu">
{children}
{logoLoaded ? children : null}
</nav>
<div className={styles.search}>{searchEnabled ? search : null}</div>
</div>
Expand Down
12 changes: 1 addition & 11 deletions src/components/Header/__snapshots__/Header.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,7 @@ exports[`<Header /> renders header 1`] = `
<nav
aria-label="menu"
class="nav"
>
<a
aria-current="page"
class="button default outlined active"
href="/"
>
<span>
Home
</span>
</a>
</nav>
/>
<div
class="search"
>
Expand Down
12 changes: 1 addition & 11 deletions src/components/Layout/__snapshots__/Layout.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ exports[`<Layout /> renders layout 1`] = `
<nav
aria-label="menu"
class="nav"
>
<a
aria-current="page"
class="button default text active"
href="/"
>
<span>
home
</span>
</a>
</nav>
/>
<div
class="search"
/>
Expand Down
21 changes: 20 additions & 1 deletion src/components/Logo/Logo.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import React from 'react';
import { fireEvent } from '@testing-library/react';

import { render } from '../../testUtils';

import Logo from './Logo';

describe('<Logo/>', () => {
it('renders and matches snapshot', () => {
const { container } = render(<Logo src="123" />);
const { container } = render(<Logo src="123" onLoad={jest.fn()} />);

expect(container).toMatchSnapshot();
});

it('calls the onLoad function after the image has been loaded', () => {
const onLoad = jest.fn();
const { getByAltText } = render(<Logo src="testimage" onLoad={onLoad} />);

fireEvent.load(getByAltText('logo'));

expect(onLoad).toBeCalledTimes(1);
});

it('calls the onLoad function when the image fails to load', () => {
const onLoad = jest.fn();
const { getByAltText } = render(<Logo src="testimage" onLoad={onLoad} />);

fireEvent.error(getByAltText('logo'));

expect(onLoad).toBeCalledTimes(1);
});
});
5 changes: 3 additions & 2 deletions src/components/Logo/Logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import styles from './Logo.module.scss';

type Props = {
src: string;
onLoad: () => void;
};

const Logo: React.FC<Props> = ({ src }: Props) => {
const Logo: React.FC<Props> = ({ src, onLoad }: Props) => {
return (
<Link to="/">
<img className={styles.logo} alt="logo" src={src} />
<img className={styles.logo} alt="logo" src={src} onLoad={onLoad} onError={onLoad} />
</Link>
);
};
Expand Down
11 changes: 1 addition & 10 deletions src/components/Root/__snapshots__/Root.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ exports[`<Root /> renders and matches snapshot 1`] = `
<nav
aria-label="menu"
class="nav"
>
<a
class="button default text"
href="/"
>
<span>
home
</span>
</a>
</nav>
/>
<div
class="search"
/>
Expand Down

0 comments on commit 0dbde3f

Please sign in to comment.