Skip to content

Commit

Permalink
feat(v2): add announcement bar (#2330)
Browse files Browse the repository at this point in the history
* feat(v2): add announcement bar

* Refactor: move to Layout

* Fixes

* Refactor to simplify

* Changes: id, styles, docs
  • Loading branch information
lex111 authored Mar 31, 2020
1 parent c3aa162 commit 0f73a1f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, {useState, useEffect} from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

import styles from './styles.module.css';

const STORAGE_DISMISS_KEY = 'docusaurus.announcement.dismiss';
const STORAGE_ID_KEY = 'docusaurus.announcement.id';

function AnnouncementBar() {
const {
siteConfig: {themeConfig: {announcementBar = {}}} = {},
} = useDocusaurusContext();
const {id, content, backgroundColor, textColor} = announcementBar;
const [isClosed, setClosed] = useState(true);
const handleClose = () => {
sessionStorage.setItem(STORAGE_DISMISS_KEY, true);
setClosed(true);
};

useEffect(() => {
const viewedId = sessionStorage.getItem(STORAGE_ID_KEY);
const isNewAnnouncement = id !== viewedId;

sessionStorage.setItem(STORAGE_ID_KEY, id);

if (isNewAnnouncement) {
sessionStorage.setItem(STORAGE_DISMISS_KEY, false);
}

if (
isNewAnnouncement ||
sessionStorage.getItem(STORAGE_DISMISS_KEY) === 'false'
) {
setClosed(false);
}
}, []);

if (!content || isClosed) {
return null;
}

return (
<div
className={styles.announcementBar}
style={{backgroundColor, color: textColor}}
role="banner">
<div
className={styles.announcementBarContent}
dangerouslySetInnerHTML={{__html: content}}
/>

<button
type="button"
className={styles.announcementBarClose}
onClick={handleClose}
aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
);
}

export default AnnouncementBar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.announcementBar {
position: relative;
width: 100%;
background-color: #fff;
color: #000;
}

.announcementBarClose {
position: absolute;
right: 0;
top: 0;
width: 55px;
font-size: 25px;
padding: 0;
border: none;
cursor: pointer;
background: none;
height: 100%;
}

.announcementBarContent {
font-size: 85%;
width: 100%;
text-align: center;
padding: 5px 0;
margin-right: 55px;
}

@media screen and (max-width: 576px) {
.announcementBarClose {
width: 35px;
}
.announcementBarContent {
margin-right: 35px;
}
}

.announcementBarContent a {
color: inherit;
text-decoration: underline;
}
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useBaseUrl from '@docusaurus/useBaseUrl';

import ThemeProvider from '@theme/ThemeProvider';
import TabGroupChoiceProvider from '@theme/TabGroupChoiceProvider';
import AnnouncementBar from '@theme/AnnouncementBar';
import Navbar from '@theme/Navbar';
import Footer from '@theme/Footer';

Expand Down Expand Up @@ -76,6 +77,7 @@ function Layout(props) {
)}
<meta name="twitter:card" content="summary_large_image" />
</Head>
<AnnouncementBar />
<Navbar />
<div className="main-wrapper">{children}</div>
{!noFooter && <Footer />}
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
*/

import React, {useCallback, useState} from 'react';
import classnames from 'classnames';
import Link from '@docusaurus/Link';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useBaseUrl from '@docusaurus/useBaseUrl';

import SearchBar from '@theme/SearchBar';
import Toggle from '@theme/Toggle';

import classnames from 'classnames';

import useThemeContext from '@theme/hooks/useThemeContext';
import useHideableNavbar from '@theme/hooks/useHideableNavbar';
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const useHideableNavbar = hideOnScroll => {
const handleScroll = () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;

if (scrollTop === 0) {
setIsNavbarVisible(true);
}

if (scrollTop < navbarHeight) {
return;
}
Expand All @@ -39,7 +43,7 @@ const useHideableNavbar = hideOnScroll => {
const documentHeight = document.documentElement.scrollHeight - navbarHeight;
const windowHeight = window.innerHeight;

if (lastScrollTop && scrollTop > lastScrollTop) {
if (lastScrollTop && scrollTop >= lastScrollTop) {
setIsNavbarVisible(false);
} else if (scrollTop + windowHeight < documentHeight) {
setIsNavbarVisible(true);
Expand Down
19 changes: 19 additions & 0 deletions website/docs/theme-classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ module.exports = {
}
```

### Announcement bar

Sometimes you want to announce something in your website. Just for such a case, you can add an announcement bar. This is a non-fixed and dismissable panel above the navbar.

```js {4-9} title="docusaurus.config.js"
module.exports = {
...
themeConfig: {
announcementBar: {
id: 'support_us', // Any value that will identify this message
content: 'We are looking to revamp our docs, please fill <a target="_blank" rel="noopener noreferrer" href="#">this survey</a>',
backgroundColor: '#fafbfc', // Defaults to `#fff`
textColor: '#091E42', // Defaults to `#000`
},
...
},
}
```

## Navbar

### Navbar Title & Logo
Expand Down
7 changes: 7 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ module.exports = {
],
],
themeConfig: {
announcementBar: {
id: 'supportus',
content:
'⭐️ If you like Docusaurus, give it a star on <a target="_blank" rel="noopener noreferrer" href="https://github.com/facebook/docusaurus">GitHub</a>! ⭐️',
backgroundColor: '#fafbfc',
textColor: '#091E42',
},
prism: {
theme: require('prism-react-renderer/themes/github'),
darkTheme: require('prism-react-renderer/themes/dracula'),
Expand Down

0 comments on commit 0f73a1f

Please sign in to comment.