Skip to content

Commit

Permalink
feat(v2): Add <Root> theme element (#3932)
Browse files Browse the repository at this point in the history
* Add <Root> component

* add some <Root> doc
  • Loading branch information
slorber authored Dec 17, 2020
1 parent 5757cdc commit df47c17
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
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

0 comments on commit df47c17

Please sign in to comment.