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

feat(v2): Add <Root> theme element #3932

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/docusaurus/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import renderRoutes from './exports/renderRoutes';
import DocusaurusContext from './exports/context';
import PendingNavigation from './PendingNavigation';
import BaseUrlIssueBanner from './baseUrlIssueBanner/BaseUrlIssueBanner';
import Root from '@theme/Root';

import './client-lifecycles-dispatcher';

Expand All @@ -37,10 +38,12 @@ function App(): JSX.Element {
codeTranslations,
isClient,
}}>
<BaseUrlIssueBanner />
<PendingNavigation routes={routes}>
{renderRoutes(routes)}
</PendingNavigation>
<Root>
<BaseUrlIssueBanner />
<PendingNavigation routes={routes}>
{renderRoutes(routes)}
</PendingNavigation>
</Root>
</DocusaurusContext.Provider>
);
}
Expand Down
21 changes: 21 additions & 0 deletions packages/docusaurus/src/client/theme-fallback/Root/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';

// Wrapper at the very top of the app, that is applied constantly
// and does not depend on current route (unlike the layout)
//
// Gives the opportunity to add stateful providers on top of the app
// and these providers won't reset state when we navigate
//
// See https://github.com/facebook/docusaurus/issues/3919
function Root({children}) {
return <>{children}</>;
}

export default Root;
25 changes: 24 additions & 1 deletion website/docs/using-themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,30 @@ And if you want to use Bootstrap styling, you can swap out the theme with `theme
}
```

The content plugin remains the same and the only thing you need to change is the theme.
## Wrapper your site with `<Root>`

A `<Root>` theme component is rendered at the very top of your Docusaurus site.

It allows you to wrap your site with additional logic, by creating a file at `website/src/theme/Root.js`:

```js title="website/src/theme/Root.js"
import React from 'react';

// Default implementation, that you can customize
function Root({children}) {
return <>{children}</>;
}

export default Root;
```

This component is applied above the router and the theme `<Layout>`, and will **never unmount**.

:::tip

Use this component render React Context providers and global stateful logic.

:::

## Swizzling theme components

Expand Down