-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): rework client modules lifecycles, officially make API pub…
…lic (#6732)
- Loading branch information
Showing
16 changed files
with
259 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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 nprogress from 'nprogress'; | ||
import './nprogress.css'; | ||
import type {ClientModule} from '@docusaurus/types'; | ||
|
||
nprogress.configure({showSpinner: false}); | ||
|
||
const delay = 200; | ||
|
||
const clientModule: ClientModule = { | ||
onRouteUpdate({location, previousLocation}) { | ||
if (previousLocation && location.pathname !== previousLocation.pathname) { | ||
const progressBarTimeout = window.setTimeout(() => { | ||
nprogress.start(); | ||
}, delay); | ||
return () => window.clearTimeout(progressBarTimeout); | ||
} | ||
return undefined; | ||
}, | ||
onRouteDidUpdate() { | ||
nprogress.done(); | ||
}, | ||
}; | ||
|
||
export default clientModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/docusaurus/src/client/ClientLifecyclesDispatcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* 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 {useLayoutEffect, type ReactElement} from 'react'; | ||
import clientModules from '@generated/client-modules'; | ||
import type {ClientModule} from '@docusaurus/types'; | ||
import type {Location} from 'history'; | ||
|
||
export function dispatchLifecycleAction<K extends keyof ClientModule>( | ||
lifecycleAction: K, | ||
...args: Parameters<NonNullable<ClientModule[K]>> | ||
): () => void { | ||
const callbacks = clientModules.map((clientModule) => { | ||
const lifecycleFunction = (clientModule?.default?.[lifecycleAction] ?? | ||
clientModule[lifecycleAction]) as | ||
| (( | ||
...a: Parameters<NonNullable<ClientModule[K]>> | ||
) => (() => void) | void) | ||
| undefined; | ||
|
||
return lifecycleFunction?.(...args); | ||
}); | ||
return () => callbacks.forEach((cb) => cb?.()); | ||
} | ||
|
||
function ClientLifecyclesDispatcher({ | ||
children, | ||
location, | ||
previousLocation, | ||
}: { | ||
children: ReactElement; | ||
location: Location; | ||
previousLocation: Location | null; | ||
}): JSX.Element { | ||
useLayoutEffect(() => { | ||
if (previousLocation !== location) { | ||
const {hash} = location; | ||
if (!hash) { | ||
window.scrollTo(0, 0); | ||
} else { | ||
const id = decodeURIComponent(hash.substring(1)); | ||
const element = document.getElementById(id); | ||
element?.scrollIntoView(); | ||
} | ||
dispatchLifecycleAction('onRouteDidUpdate', {previousLocation, location}); | ||
} | ||
}, [previousLocation, location]); | ||
return children; | ||
} | ||
|
||
export default ClientLifecyclesDispatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.