-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): add announcement bar (#2330)
* feat(v2): add announcement bar * Refactor: move to Layout * Fixes * Refactor to simplify * Changes: id, styles, docs
- Loading branch information
Showing
7 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/docusaurus-theme-classic/src/theme/AnnouncementBar/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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">×</span> | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default AnnouncementBar; |
48 changes: 48 additions & 0 deletions
48
packages/docusaurus-theme-classic/src/theme/AnnouncementBar/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters