Skip to content

Commit

Permalink
feat: add option to remove site switcher from header (#1132)
Browse files Browse the repository at this point in the history
* feat: add option to disable switcher

* docs: add documentation for switcher disabling
  • Loading branch information
vpicone committed Jun 8, 2021
1 parent c6e39f5 commit 06ab644
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
5 changes: 2 additions & 3 deletions packages/example/src/pages/guides/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,8 @@ plugins: [

- `mdxExtensions` – change the file extensions processed by `gatsby-mdx`
(default ['.mdx', '.md']).
- `pngCompressionSpeed` - a speed/quality trade-off from 1 (brute-force) to 10
(fastest). Speed 10 has 5% lower quality, but is 8 times faster than the
default (4).
- `isSwitcherEnabled` - pass `false` to remove the Site switcher from the header
navigation.
- `titleType` – pick between four formats for the `<title>` element for your
site. Here are the four options using this page as an example:

Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-theme-carbon/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = (themeOptions) => {
remarkPlugins = [],
gatsbyPluginSharpOptions = {},
isServiceWorkerEnabled = false,
isSwitcherEnabled = true,
} = themeOptions;

const theme = { ...defaultTheme, ...themeOption };
Expand Down Expand Up @@ -78,6 +79,7 @@ module.exports = (themeOptions) => {
homepageTheme: theme.homepage,
interiorTheme: theme.interior,
isServiceWorkerEnabled,
isSwitcherEnabled,
title: 'Gatsby Theme Carbon',
description:
'Add a description by supplying it to siteMetadata in your gatsby-config.js file.',
Expand Down
30 changes: 16 additions & 14 deletions packages/gatsby-theme-carbon/src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Header = ({ children }) => {
searchIsOpen,
switcherTooltipText = 'Switch sites',
} = useContext(NavContext);
const { isSearchEnabled, navigationStyle } = useMetadata();
const { isSearchEnabled, navigationStyle, isSwitcherEnabled } = useMetadata();

return (
<ShellHeader
Expand Down Expand Up @@ -55,19 +55,21 @@ const Header = ({ children }) => {
<HeaderGlobalBar
className={cx({ [styles.searchIsOpenOnBar]: searchIsOpen })}>
{isSearchEnabled && <GlobalSearch />}
<HeaderGlobalAction
className={cx(styles.headerButton, styles.switcherButton, {
[styles.switcherButtonOpen]: switcherIsOpen,
})}
aria-label={switcherTooltipText}
tooltipAlignment="end"
onClick={() => {
toggleNavState('switcherIsOpen');
toggleNavState('searchIsOpen', 'close');
toggleNavState('leftNavIsOpen', 'close');
}}>
{switcherIsOpen ? <Close20 /> : <Switcher20 />}
</HeaderGlobalAction>
{isSwitcherEnabled && (
<HeaderGlobalAction
className={cx(styles.headerButton, styles.switcherButton, {
[styles.switcherButtonOpen]: switcherIsOpen,
})}
aria-label={switcherTooltipText}
tooltipAlignment="end"
onClick={() => {
toggleNavState('switcherIsOpen');
toggleNavState('searchIsOpen', 'close');
toggleNavState('leftNavIsOpen', 'close');
}}>
{switcherIsOpen ? <Close20 /> : <Switcher20 />}
</HeaderGlobalAction>
)}
</HeaderGlobalBar>
</ShellHeader>
);
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby-theme-carbon/src/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Header from './Header';
import Switcher from './Switcher';
import Footer from './Footer';
import Container from './Container';
import useMetadata from '../util/hooks/useMetadata';

import '../styles/index.scss';

Expand All @@ -21,6 +22,7 @@ const Layout = ({
tabs,
}) => {
const is404 = children.key === null;
const { isSwitcherEnabled } = useMetadata();

useLayoutEffect(() => {
// eslint-disable-next-line global-require
Expand All @@ -43,8 +45,8 @@ const Layout = ({
pageDescription={pageDescription}
pageKeywords={pageKeywords}
/>
<Header />
<Switcher />
<Header isSwitcherEnabled={isSwitcherEnabled} />
{isSwitcherEnabled && <Switcher />}
<LeftNav homepage={homepage} is404Page={is404} theme={theme} />
<Container homepage={homepage} theme={theme}>
{children}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-theme-carbon/src/util/hooks/useMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const useMetadata = () => {
description
keywords
isSearchEnabled
isSwitcherEnabled
homepageTheme
interiorTheme
navigationStyle
Expand Down

1 comment on commit 06ab644

@vercel
Copy link

@vercel vercel bot commented on 06ab644 Jun 8, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.