feat(v2): add isCloseable property for theme-classic announcement bar (#3388)

* feat(v2): add `isCloseable` property for announcement bar

* correct styling for non-closeable announcement

* fix tests
This commit is contained in:
Bartosz Kaszubowski 2020-09-02 11:41:38 +02:00 committed by GitHub
parent 0d018a88c5
commit e8e16a45d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 12 deletions

View file

@ -41,6 +41,7 @@ describe('themeConfig', () => {
content: 'pls support',
backgroundColor: '#fff',
textColor: '#000',
isCloseable: true,
},
image: 'img/docusaurus-soc.png',
navbar: {

View file

@ -6,6 +6,7 @@
*/
import React from 'react';
import clsx from 'clsx';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
@ -15,13 +16,13 @@ function AnnouncementBar(): JSX.Element | null {
const {
siteConfig: {themeConfig: {announcementBar = {}} = {}} = {},
} = useDocusaurusContext();
const {content, backgroundColor, textColor} = announcementBar;
const {content, backgroundColor, textColor, isCloseable} = announcementBar;
const {
isAnnouncementBarClosed,
closeAnnouncementBar,
} = useUserPreferencesContext();
if (!content || isAnnouncementBarClosed) {
if (!content || (isCloseable && isAnnouncementBarClosed)) {
return null;
}
@ -31,19 +32,22 @@ function AnnouncementBar(): JSX.Element | null {
style={{backgroundColor, color: textColor}}
role="banner">
<div
className={styles.announcementBarContent}
className={clsx(styles.announcementBarContent, {
[styles.announcementBarCloseable]: isCloseable,
})}
// Developer provided the HTML, so assume it's safe.
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{__html: content}}
/>
{isCloseable ? (
<button
type="button"
className={styles.announcementBarClose}
onClick={closeAnnouncementBar}
aria-label="Close">
<span aria-hidden="true">&times;</span>
<span aria-hidden="true">×</span>
</button>
) : null}
</div>
);
}

View file

@ -42,6 +42,9 @@
width: 100%;
text-align: center;
padding: 5px 0;
}
.announcementBarCloseable {
margin-right: 55px;
}

View file

@ -169,6 +169,7 @@ const ThemeConfigSchema = Joi.object({
content: Joi.string(),
backgroundColor: Joi.string().default('#fff'),
textColor: Joi.string().default('#000'),
isCloseable: Joi.bool().default(true),
}).optional(),
navbar: Joi.object({
hideOnScroll: Joi.bool().default(false),

View file

@ -87,7 +87,7 @@ 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.
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 optionally dismissable panel above the navbar.
```js {4-10} title="docusaurus.config.js"
module.exports = {
@ -99,6 +99,7 @@ module.exports = {
'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`.
isCloseable: false, // Defaults to `true`.
},
// ...
},