Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v2): add useThemeConfig hook + cleanup useless theme default values #3394

Merged
merged 8 commits into from
Oct 2, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
import styles from './styles.module.css';

function AnnouncementBar(): JSX.Element | null {
const {
siteConfig: {themeConfig: {announcementBar = {}} = {}} = {},
} = useDocusaurusContext();
const {announcementBar} = useDocusaurusContext().siteConfig.themeConfig;
const {content, backgroundColor, textColor, isCloseable} = announcementBar;
imskr marked this conversation as resolved.
Show resolved Hide resolved
const {
isAnnouncementBarClosed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ export default ({
className: languageClassName,
metastring,
}: Props): JSX.Element => {
const {
siteConfig: {
themeConfig: {prism = {}},
},
} = useDocusaurusContext();
const {prism} = useDocusaurusContext().siteConfig.themeConfig;

const [showCopied, setShowCopied] = useState(false);
const [mounted, setMounted] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,8 @@ function DocSidebar({
sidebarCollapsible = true,
}: Props): JSX.Element | null {
const [showResponsiveSidebar, setShowResponsiveSidebar] = useState(false);
const {
siteConfig: {
themeConfig: {navbar: {title = '', hideOnScroll = false} = {}} = {},
} = {},
isClient,
} = useDocusaurusContext();
const {title, hideOnScroll} = useDocusaurusContext().siteConfig.themeConfig;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me, as before the title/hideOnScroll should be read on navbar, not themeConfig.
(take care, navbar can be null)

const {isClient} = useDocusaurusContext();
const {logoLink, logoLinkProps, logoImageUrl, logoAlt} = useLogo();
const {isAnnouncementBarClosed} = useUserPreferencesContext();
const {scrollY} = useScrollPosition();
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus-theme-classic/src/theme/Heading/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import styles from './styles.module.css';
const Heading = (Tag: HeadingType): ((props: Props) => JSX.Element) =>
function TargetComponent({id, ...props}) {
const {
siteConfig: {
themeConfig: {navbar: {hideOnScroll = false} = {}} = {},
} = {},
} = useDocusaurusContext();
hideOnScroll = false,
} = useDocusaurusContext().siteConfig.themeConfig.navbar;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

navbar can be null (see Joi validation, there is no .require() call nor .default() so it is optional)


if (!id) {
return <Tag {...props} />;
Expand Down
16 changes: 8 additions & 8 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ function splitNavItemsByPosition(items) {

function Navbar(): JSX.Element {
const {
siteConfig: {
themeConfig: {
navbar: {title = '', items = [], hideOnScroll = false} = {},
colorMode: {disableSwitch: disableColorModeSwitch = false} = {},
},
},
isClient,
} = useDocusaurusContext();
title,
items = [],
hideOnScroll = false,
} = useDocusaurusContext().siteConfig.themeConfig.navbar;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

navbar can be null

const {
disableColorModeSwitch = false,
} = useDocusaurusContext().siteConfig.themeConfig.colorMode.disableSwitch;
const {isClient} = useDocusaurusContext();
const [sidebarShown, setSidebarShown] = useState(false);
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false);

Expand Down
20 changes: 6 additions & 14 deletions packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,12 @@ const Light = ({icon, style}) => (

export default function (props: ComponentProps<typeof Toggle>): JSX.Element {
const {
siteConfig: {
themeConfig: {
colorMode: {
switchConfig: {
darkIcon,
darkIconStyle,
lightIcon,
lightIconStyle,
},
},
},
},
isClient
} = useDocusaurusContext();
darkIcon,
darkIconStyle,
lightIcon,
lightIconStyle,
} = useDocusaurusContext().siteConfig.themeConfig.colorMode.switchConfig;
const {isClient} = useDocusaurusContext();

return (
<Toggle
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus-theme-classic/src/theme/hooks/useLogo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import isInternalUrl from '@docusaurus/isInternalUrl';
import type {LogoLinkProps, useLogoReturns} from '@theme/hooks/useLogo';

const useLogo = (): useLogoReturns => {
const {
siteConfig: {themeConfig: {navbar: {logo = {}} = {}} = {}} = {},
} = useDocusaurusContext();
const {logo} = useDocusaurusContext().siteConfig.themeConfig.navbar;
const {isDarkTheme} = useThemeContext();
const logoLink = useBaseUrl(logo.href || '/');
let logoLinkProps: LogoLinkProps = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useThemeContext from '@theme/hooks/useThemeContext';

const usePrismTheme = (): typeof defaultTheme => {
const {
siteConfig: {
themeConfig: {prism = {}},
},
} = useDocusaurusContext();
const {prism} = useDocusaurusContext().siteConfig.themeConfig;
const {isDarkTheme} = useThemeContext();
const lightModeTheme = prism.theme || defaultTheme;
const darkModeTheme = prism.darkTheme || lightModeTheme;
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus-theme-classic/src/theme/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ const storeTheme = (newTheme) => {

const useTheme = (): useThemeReturns => {
const {
siteConfig: {
themeConfig: {colorMode: {disableSwitch = false} = {}} = {},
} = {},
} = useDocusaurusContext();
disableSwitch = false,
} = useDocusaurusContext().siteConfig.themeConfig.colorMode;
const [theme, setTheme] = useState(getInitialTheme);

const setLightTheme = useCallback(() => {
Expand Down