mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-18 11:36:53 +02:00
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:
parent
0d018a88c5
commit
e8e16a45d3
5 changed files with 22 additions and 12 deletions
|
@ -41,6 +41,7 @@ describe('themeConfig', () => {
|
||||||
content: 'pls support',
|
content: 'pls support',
|
||||||
backgroundColor: '#fff',
|
backgroundColor: '#fff',
|
||||||
textColor: '#000',
|
textColor: '#000',
|
||||||
|
isCloseable: true,
|
||||||
},
|
},
|
||||||
image: 'img/docusaurus-soc.png',
|
image: 'img/docusaurus-soc.png',
|
||||||
navbar: {
|
navbar: {
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import clsx from 'clsx';
|
||||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||||
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
|
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
|
||||||
|
|
||||||
|
@ -15,13 +16,13 @@ function AnnouncementBar(): JSX.Element | null {
|
||||||
const {
|
const {
|
||||||
siteConfig: {themeConfig: {announcementBar = {}} = {}} = {},
|
siteConfig: {themeConfig: {announcementBar = {}} = {}} = {},
|
||||||
} = useDocusaurusContext();
|
} = useDocusaurusContext();
|
||||||
const {content, backgroundColor, textColor} = announcementBar;
|
const {content, backgroundColor, textColor, isCloseable} = announcementBar;
|
||||||
const {
|
const {
|
||||||
isAnnouncementBarClosed,
|
isAnnouncementBarClosed,
|
||||||
closeAnnouncementBar,
|
closeAnnouncementBar,
|
||||||
} = useUserPreferencesContext();
|
} = useUserPreferencesContext();
|
||||||
|
|
||||||
if (!content || isAnnouncementBarClosed) {
|
if (!content || (isCloseable && isAnnouncementBarClosed)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,19 +32,22 @@ function AnnouncementBar(): JSX.Element | null {
|
||||||
style={{backgroundColor, color: textColor}}
|
style={{backgroundColor, color: textColor}}
|
||||||
role="banner">
|
role="banner">
|
||||||
<div
|
<div
|
||||||
className={styles.announcementBarContent}
|
className={clsx(styles.announcementBarContent, {
|
||||||
|
[styles.announcementBarCloseable]: isCloseable,
|
||||||
|
})}
|
||||||
// Developer provided the HTML, so assume it's safe.
|
// Developer provided the HTML, so assume it's safe.
|
||||||
// eslint-disable-next-line react/no-danger
|
// eslint-disable-next-line react/no-danger
|
||||||
dangerouslySetInnerHTML={{__html: content}}
|
dangerouslySetInnerHTML={{__html: content}}
|
||||||
/>
|
/>
|
||||||
|
{isCloseable ? (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className={styles.announcementBarClose}
|
className={styles.announcementBarClose}
|
||||||
onClick={closeAnnouncementBar}
|
onClick={closeAnnouncementBar}
|
||||||
aria-label="Close">
|
aria-label="Close">
|
||||||
<span aria-hidden="true">×</span>
|
<span aria-hidden="true">×</span>
|
||||||
</button>
|
</button>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,9 @@
|
||||||
width: 100%;
|
width: 100%;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
padding: 5px 0;
|
padding: 5px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.announcementBarCloseable {
|
||||||
margin-right: 55px;
|
margin-right: 55px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,6 +169,7 @@ const ThemeConfigSchema = Joi.object({
|
||||||
content: Joi.string(),
|
content: Joi.string(),
|
||||||
backgroundColor: Joi.string().default('#fff'),
|
backgroundColor: Joi.string().default('#fff'),
|
||||||
textColor: Joi.string().default('#000'),
|
textColor: Joi.string().default('#000'),
|
||||||
|
isCloseable: Joi.bool().default(true),
|
||||||
}).optional(),
|
}).optional(),
|
||||||
navbar: Joi.object({
|
navbar: Joi.object({
|
||||||
hideOnScroll: Joi.bool().default(false),
|
hideOnScroll: Joi.bool().default(false),
|
||||||
|
|
|
@ -87,7 +87,7 @@ module.exports = {
|
||||||
|
|
||||||
### Announcement bar
|
### 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"
|
```js {4-10} title="docusaurus.config.js"
|
||||||
module.exports = {
|
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>',
|
'We are looking to revamp our docs, please fill <a target="_blank" rel="noopener noreferrer" href="#">this survey</a>',
|
||||||
backgroundColor: '#fafbfc', // Defaults to `#fff`.
|
backgroundColor: '#fafbfc', // Defaults to `#fff`.
|
||||||
textColor: '#091E42', // Defaults to `#000`.
|
textColor: '#091E42', // Defaults to `#000`.
|
||||||
|
isCloseable: false, // Defaults to `true`.
|
||||||
},
|
},
|
||||||
// ...
|
// ...
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue