Skip to content

Commit a31f80d

Browse files
author
Cezar Sampaio
committed
feat: add logo component supporting srcSet
1 parent 4866130 commit a31f80d

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

src/components/footer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import is from 'prop-types';
33
import styled from '@emotion/styled';
44
import { Link } from 'gatsby';
55
import Wrap from './wrap';
6+
import Logo from './logo';
67

78
const FooterContainer = styled.footer`
89
margin-top: 32px;
@@ -65,7 +66,7 @@ export default function Footer(props) {
6566
<FooterContainer>
6667
<LogoContainer>
6768
<Link to="/">
68-
<img src={props.logoUrl} alt={props.logoDescription} />
69+
<Logo />
6970
</Link>
7071
</LogoContainer>
7172

@@ -100,8 +101,6 @@ export default function Footer(props) {
100101
}
101102

102103
Footer.propTypes = {
103-
logoUrl: is.string.isRequired,
104-
logoDescription: is.string.isRequired,
105104
links: is.arrayOf(
106105
is.shape({
107106
url: is.string.isRequired,

src/components/header.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import styled from '@emotion/styled';
44
import { Link } from 'gatsby';
55
import Wrap from './wrap';
66
import { HamburgerIcon, CloseIcon } from './icons';
7+
import Logo from './logo';
78

89
const HeaderWrap = styled(Wrap)`
910
margin-bottom: 12px;
@@ -110,14 +111,12 @@ export default function Header(props) {
110111
setIsMenuOpened(false);
111112
}
112113

113-
function renderLogo() {
114-
return <img src={props.logoUrl} alt={props.logoDescription} />;
115-
}
116-
117114
return (
118115
<HeaderWrap>
119116
<HeaderContainerDesktop>
120-
<Link to="/">{renderLogo()}</Link>
117+
<Link to="/">
118+
<Logo />
119+
</Link>
121120
<LinksNav>
122121
{props.links.map((link, index) => (
123122
<Anchor
@@ -134,7 +133,9 @@ export default function Header(props) {
134133

135134
<HeaderContainerMobile>
136135
<div>
137-
<Link to="/">{renderLogo()}</Link>
136+
<Link to="/">
137+
<Logo />
138+
</Link>
138139
</div>
139140

140141
<HamburgerIcon onClick={openMenu} />
@@ -146,7 +147,7 @@ export default function Header(props) {
146147
<MobileContainer>
147148
<MobileMenuLogoContainer>
148149
<Link to="/" onClick={closeMenu}>
149-
{renderLogo()}
150+
<Logo />
150151
</Link>
151152
<CloseIcon onClick={closeMenu} />
152153
</MobileMenuLogoContainer>
@@ -171,8 +172,6 @@ export default function Header(props) {
171172
}
172173

173174
Header.propTypes = {
174-
logoUrl: is.string.isRequired,
175-
logoDescription: is.string.isRequired,
176175
links: is.arrayOf(
177176
is.shape({
178177
url: is.string.isRequired,

src/components/logo.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import useSiteSettings from '../hooks/useSiteSettings';
3+
4+
export default function Logo() {
5+
const { logo } = useSiteSettings();
6+
7+
if (!logo?.fixed?.src) return null;
8+
9+
return (
10+
<img
11+
src={logo?.fixed?.src}
12+
alt={logo?.title}
13+
srcSet={logo?.fixed?.srcSet}
14+
width={logo?.fixed?.width}
15+
height={logo?.fixed?.height}
16+
/>
17+
);
18+
}

src/hooks/useSiteSettings.js

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default function useSiteSettings() {
1717
title
1818
fixed(width: 160) {
1919
src
20+
srcSet
21+
width
22+
height
2023
}
2124
}
2225
googleAnalyticsId

src/templates/layout.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ export default function Layout(props) {
3434
return (
3535
<>
3636
<UpperContainer>
37-
<Header
38-
links={settings.headerLinks}
39-
logoUrl={settings.logo.fixed.src}
40-
logoDescription={settings.logo.title}
41-
/>
37+
<Header links={settings.headerLinks} />
4238

4339
{props.withSearch && (
4440
<SearchContainer>
@@ -51,11 +47,7 @@ export default function Layout(props) {
5147

5248
<Wrap>{props.children}</Wrap>
5349

54-
<Footer
55-
links={settings.headerLinks}
56-
logoUrl={settings.logo.fixed.src}
57-
logoDescription={settings.logo.title}
58-
/>
50+
<Footer links={settings.headerLinks} />
5951

6052
<CookieConsent />
6153

0 commit comments

Comments
 (0)