Skip to content

Commit d0bea90

Browse files
authored
Merge pull request #341 from processing/banner
Adding sitewide banner
2 parents c65281c + 3083172 commit d0bea90

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

content/sitewide/banner.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"visible": true,
3+
"text": "This is some text",
4+
"url": "https://designsystems.international"
5+
}

gatsby-config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ module.exports = {
5353
]
5454
}
5555
},
56+
{
57+
resolve: `gatsby-source-filesystem`,
58+
options: {
59+
name: `sitewide`,
60+
path: path.resolve(__dirname, 'content/sitewide')
61+
}
62+
},
5663
{
5764
resolve: `gatsby-source-filesystem`,
5865
options: {

src/components/Banner.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { memo, useEffect, useState } from 'react';
2+
import { slugify } from '../utils';
3+
import * as css from './Banner.module.css';
4+
5+
export const Banner = ({ text, url }) => {
6+
const [visible, setVisible] = useState(false);
7+
const key = slugify(text);
8+
9+
const hide = () => {
10+
if (window.localStorage) {
11+
window.localStorage.setItem(
12+
`banner-${key}`,
13+
JSON.stringify({
14+
visible: false
15+
})
16+
);
17+
}
18+
setVisible(false);
19+
};
20+
21+
// Only show the banner if this text has not been dismissed yet
22+
useEffect(() => {
23+
if (window.localStorage) {
24+
const savedState = window.localStorage.getItem(`banner-${key}`);
25+
if (savedState) {
26+
setVisible(JSON.parse(savedState).visible);
27+
} else {
28+
setVisible(true);
29+
}
30+
}
31+
}, [key]);
32+
33+
return visible ? (
34+
<div className={css.root}>
35+
<a className={css.link} href={url} target="_blank" rel="noreferrer">
36+
{text}
37+
</a>
38+
<button className={css.close} onClick={hide}>
39+
x
40+
</button>
41+
</div>
42+
) : null;
43+
};
44+
45+
export default memo(Banner);

src/components/Banner.module.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.root {
2+
text-align: center;
3+
background-color: var(--processing-blue-deep);
4+
color: white;
5+
font-size: var(--text-normal);
6+
margin-top: -2px;
7+
display: flex;
8+
}
9+
10+
.link {
11+
display: block;
12+
padding: var(--margin-quarter);
13+
flex: 1;
14+
}
15+
16+
.close {
17+
flex: 0 0 60px;
18+
cursor: pointer;
19+
background: none;
20+
color: white;
21+
border: none;
22+
font-size: var(--text-large);
23+
}

src/components/Header.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3+
import { graphql, useStaticQuery } from 'gatsby';
34

45
import Topbar from './Topbar';
56
import Navbar from './Navbar';
7+
import Banner from './Banner';
68

79
import * as css from './Header.module.css';
810

911
const Header = ({ siteTitle, scrolled }) => {
12+
const data = useStaticQuery(graphql`
13+
query {
14+
banner: file(
15+
name: { eq: "banner" }
16+
sourceInstanceName: { eq: "sitewide" }
17+
) {
18+
name
19+
childJson {
20+
visible
21+
url
22+
text
23+
}
24+
}
25+
}
26+
`);
27+
const bannerData = data.banner.childJson;
28+
1029
return (
1130
<header className={css.root}>
1231
<Topbar show={!scrolled} />
32+
{bannerData && bannerData.visible && <Banner {...bannerData} />}
1333
<Navbar siteTitle={siteTitle} scrolled={scrolled} />
1434
</header>
1535
);

0 commit comments

Comments
 (0)