Skip to content

Commit

Permalink
polish announcement bar
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 27, 2020
1 parent 3ccca39 commit 9e65431
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

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

const STORAGE_DISMISS_KEY = 'docusaurus.announcement.dismiss';
Expand All @@ -15,23 +15,29 @@ const useAnnouncementBar = (): {
isAnnouncementBarClosed: boolean;
closeAnnouncementBar: () => void;
} => {
const {
siteConfig: {
themeConfig: {announcementBar: {id = 'annoucement-bar'} = {}} = {},
} = {},
} = useDocusaurusContext();
const {announcementBar} = useDocusaurusContext().siteConfig.themeConfig;

const [isClosed, setClosed] = useState(true);
const handleClose = () => {

const handleClose = useCallback(() => {
localStorage.setItem(STORAGE_DISMISS_KEY, 'true');
setClosed(true);
};
}, []);

useEffect(() => {
if (!id) {
if (!announcementBar) {
return;
}
const {id} = announcementBar;

let viewedId = localStorage.getItem(STORAGE_ID_KEY);

// retrocompatibility due to spelling mistake of default id
// see https://github.com/facebook/docusaurus/issues/3338
if (viewedId === 'annoucement-bar') {
viewedId = 'announcement-bar';
}

const viewedId = localStorage.getItem(STORAGE_ID_KEY);
const isNewAnnouncement = id !== viewedId;

localStorage.setItem(STORAGE_ID_KEY, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const ThemeConfigSchema = Joi.object({
colorMode: ColorModeSchema,
image: Joi.string(),
announcementBar: Joi.object({
id: Joi.string(),
id: Joi.string().default('announcement-bar'),
content: Joi.string(),
backgroundColor: Joi.string().default('#fff'),
textColor: Joi.string().default('#000'),
Expand Down

0 comments on commit 9e65431

Please sign in to comment.