Skip to content

Commit 53bb030

Browse files
authored
refactor(theme-classic): split AnnouncementBar, increase z-index, use shadow (facebook#7940)
1 parent ad15605 commit 53bb030

File tree

7 files changed

+115
-37
lines changed

7 files changed

+115
-37
lines changed

packages/docusaurus-theme-classic/src/theme-classic.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ declare module '@theme/AnnouncementBar' {
5555
export default function AnnouncementBar(): JSX.Element | null;
5656
}
5757

58+
declare module '@theme/AnnouncementBar/Content' {
59+
import type {ComponentProps} from 'react';
60+
61+
export interface Props extends ComponentProps<'div'> {}
62+
63+
export default function AnnouncementBarContent(props: Props): JSX.Element;
64+
}
65+
66+
declare module '@theme/AnnouncementBar/CloseButton' {
67+
import type {ComponentProps} from 'react';
68+
69+
export interface Props extends ComponentProps<'button'> {}
70+
71+
export default function AnnouncementBarCloseButton(props: Props): JSX.Element;
72+
}
73+
5874
declare module '@theme/BackToTopButton' {
5975
export default function BackToTopButton(): JSX.Element;
6076
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import clsx from 'clsx';
10+
import {translate} from '@docusaurus/Translate';
11+
import IconClose from '@theme/Icon/Close';
12+
import type {Props} from '@theme/AnnouncementBar/CloseButton';
13+
import styles from './styles.module.css';
14+
15+
export default function AnnouncementBarCloseButton(
16+
props: Props,
17+
): JSX.Element | null {
18+
return (
19+
<button
20+
type="button"
21+
aria-label={translate({
22+
id: 'theme.AnnouncementBar.closeButtonAriaLabel',
23+
message: 'Close',
24+
description: 'The ARIA label for close button of announcement bar',
25+
})}
26+
{...props}
27+
className={clsx('clean-btn close', styles.closeButton, props.className)}>
28+
<IconClose width={14} height={14} strokeWidth={3.1} />
29+
</button>
30+
);
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
.closeButton {
9+
padding: 0;
10+
line-height: 0;
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import clsx from 'clsx';
10+
import {useThemeConfig} from '@docusaurus/theme-common';
11+
import type {Props} from '@theme/AnnouncementBar/Content';
12+
import styles from './styles.module.css';
13+
14+
export default function AnnouncementBarContent(
15+
props: Props,
16+
): JSX.Element | null {
17+
const {announcementBar} = useThemeConfig();
18+
const {content} = announcementBar!;
19+
return (
20+
<div
21+
{...props}
22+
className={clsx(styles.content, props.className)}
23+
// Developer provided the HTML, so assume it's safe.
24+
// eslint-disable-next-line react/no-danger
25+
dangerouslySetInnerHTML={{__html: content}}
26+
/>
27+
);
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
.content {
9+
font-size: 85%;
10+
text-align: center;
11+
padding: 5px 0;
12+
}
13+
14+
.content a {
15+
color: inherit;
16+
text-decoration: underline;
17+
}

packages/docusaurus-theme-classic/src/theme/AnnouncementBar/index.tsx

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,33 @@
66
*/
77

88
import React from 'react';
9-
import clsx from 'clsx';
109
import {useThemeConfig} from '@docusaurus/theme-common';
1110
import {useAnnouncementBar} from '@docusaurus/theme-common/internal';
12-
import {translate} from '@docusaurus/Translate';
13-
import IconClose from '@theme/Icon/Close';
11+
import AnnouncementBarCloseButton from '@theme/AnnouncementBar/CloseButton';
12+
import AnnouncementBarContent from '@theme/AnnouncementBar/Content';
1413

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

1716
export default function AnnouncementBar(): JSX.Element | null {
18-
const {isActive, close} = useAnnouncementBar();
1917
const {announcementBar} = useThemeConfig();
20-
18+
const {isActive, close} = useAnnouncementBar();
2119
if (!isActive) {
2220
return null;
2321
}
24-
25-
const {content, backgroundColor, textColor, isCloseable} = announcementBar!;
26-
22+
const {backgroundColor, textColor, isCloseable} = announcementBar!;
2723
return (
2824
<div
2925
className={styles.announcementBar}
3026
style={{backgroundColor, color: textColor}}
3127
role="banner">
3228
{isCloseable && <div className={styles.announcementBarPlaceholder} />}
33-
<div
34-
className={styles.announcementBarContent}
35-
// Developer provided the HTML, so assume it's safe.
36-
// eslint-disable-next-line react/no-danger
37-
dangerouslySetInnerHTML={{__html: content}}
38-
/>
39-
{isCloseable ? (
40-
<button
41-
type="button"
42-
className={clsx('clean-btn close', styles.announcementBarClose)}
29+
<AnnouncementBarContent className={styles.announcementBarContent} />
30+
{isCloseable && (
31+
<AnnouncementBarCloseButton
4332
onClick={close}
44-
aria-label={translate({
45-
id: 'theme.AnnouncementBar.closeButtonAriaLabel',
46-
message: 'Close',
47-
description: 'The ARIA label for close button of announcement bar',
48-
})}>
49-
<IconClose width={14} height={14} strokeWidth={3.1} />
50-
</button>
51-
) : null}
33+
className={styles.announcementBarClose}
34+
/>
35+
)}
5236
</div>
5337
);
5438
}

packages/docusaurus-theme-classic/src/theme/AnnouncementBar/styles.module.css

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
height: var(--docusaurus-announcement-bar-height);
1616
background-color: var(--ifm-color-white);
1717
color: var(--ifm-color-black);
18-
border-bottom: 1px solid var(--ifm-color-emphasis-100);
18+
box-shadow: var(--ifm-global-shadow-lw);
19+
z-index: calc(var(--ifm-z-index-fixed) + 1); /* just above the navbar */
1920
}
2021

2122
html[data-announcement-bar-initially-dismissed='true'] .announcementBar {
@@ -29,15 +30,10 @@ html[data-announcement-bar-initially-dismissed='true'] .announcementBar {
2930
.announcementBarClose {
3031
flex: 0 0 30px;
3132
align-self: stretch;
32-
padding: 0;
33-
line-height: 0;
3433
}
3534

3635
.announcementBarContent {
3736
flex: 1 1 auto;
38-
font-size: 85%;
39-
text-align: center;
40-
padding: 5px 0;
4137
}
4238

4339
@media print {
@@ -46,11 +42,6 @@ html[data-announcement-bar-initially-dismissed='true'] .announcementBar {
4642
}
4743
}
4844

49-
.announcementBarContent a {
50-
color: inherit;
51-
text-decoration: underline;
52-
}
53-
5445
@media (min-width: 997px) {
5546
:root {
5647
--docusaurus-announcement-bar-height: 30px;

0 commit comments

Comments
 (0)