-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for client only rendering (#263)
- Loading branch information
1 parent
a15e835
commit e222fec
Showing
10 changed files
with
173 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'docusaurus-theme-redoc': minor | ||
--- | ||
|
||
Add support for client only rendering |
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
53 changes: 11 additions & 42 deletions
53
packages/docusaurus-theme-redoc/src/theme/Redoc/Redoc.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 |
---|---|---|
@@ -1,68 +1,37 @@ | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import '../../global'; | ||
import { | ||
Redoc as RedocComponent, | ||
RedocStandalone, | ||
RedocRawOptions, | ||
} from 'redoc'; | ||
import { RedocStandalone, RedocRawOptions } from 'redoc'; | ||
import { SpecProps } from '../../types/common'; | ||
import { useSpec } from '../../utils/useSpec'; | ||
import { ServerStyles } from './Styles'; | ||
import { useSpecOptions } from '../../utils/useSpecOptions'; | ||
import './styles.css'; | ||
import ServerRedoc from './ServerRedoc'; | ||
|
||
/*! | ||
* Redocusaurus | ||
* https://redocusaurus.vercel.app/ | ||
* (c) 2022 Rohit Gohri | ||
* (c) 2023 Rohit Gohri | ||
* Released under the MIT License | ||
*/ | ||
function Redoc( | ||
props: SpecProps & { | ||
props: Partial<SpecProps> & { | ||
className?: string; | ||
optionsOverrides?: RedocRawOptions; | ||
}, | ||
): JSX.Element { | ||
const { className, optionsOverrides, ...specProps } = props; | ||
const { store, darkThemeOptions, lightThemeOptions, hasLogo } = useSpec( | ||
specProps, | ||
optionsOverrides, | ||
); | ||
|
||
const { className, optionsOverrides, spec, url, themeId, isSpecFile } = props; | ||
const { options } = useSpecOptions(themeId, optionsOverrides); | ||
const isDevMode = process.env.NODE_ENV === 'development'; | ||
|
||
if (isDevMode && specProps.isSpecFile === false) { | ||
if ((isDevMode && isSpecFile === false) || !spec) { | ||
return ( | ||
<div | ||
className={clsx([ | ||
'redocusaurus', | ||
hasLogo && 'redocusaurus-has-logo', | ||
className, | ||
])} | ||
> | ||
<RedocStandalone specUrl={specProps.url} options={store.rawOptions} /> | ||
<div className={clsx(['redocusaurus', className])}> | ||
<RedocStandalone specUrl={url} options={options} /> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<ServerStyles | ||
specProps={specProps} | ||
lightThemeOptions={lightThemeOptions} | ||
darkThemeOptions={darkThemeOptions} | ||
/> | ||
<div | ||
className={clsx([ | ||
'redocusaurus', | ||
hasLogo && 'redocusaurus-has-logo', | ||
className, | ||
])} | ||
> | ||
<RedocComponent store={store} /> | ||
</div> | ||
</> | ||
); | ||
return <ServerRedoc {...props} spec={spec} />; | ||
} | ||
|
||
export default Redoc; |
48 changes: 48 additions & 0 deletions
48
packages/docusaurus-theme-redoc/src/theme/Redoc/ServerRedoc.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,48 @@ | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import '../../global'; | ||
import { Redoc as RedocComponent, RedocRawOptions } from 'redoc'; | ||
import { SpecProps } from '../../types/common'; | ||
import { useSpec } from '../../utils/useSpec'; | ||
import { ServerStyles } from './Styles'; | ||
import './styles.css'; | ||
|
||
/*! | ||
* Redocusaurus | ||
* https://redocusaurus.vercel.app/ | ||
* (c) 2023 Rohit Gohri | ||
* Released under the MIT License | ||
*/ | ||
function ServerRedoc( | ||
props: SpecProps & { | ||
className?: string; | ||
optionsOverrides?: RedocRawOptions; | ||
}, | ||
): JSX.Element { | ||
const { className, optionsOverrides, ...specProps } = props; | ||
const { store, darkThemeOptions, lightThemeOptions, hasLogo } = useSpec( | ||
specProps, | ||
optionsOverrides, | ||
); | ||
|
||
return ( | ||
<> | ||
<ServerStyles | ||
specProps={specProps} | ||
lightThemeOptions={lightThemeOptions} | ||
darkThemeOptions={darkThemeOptions} | ||
/> | ||
<div | ||
className={clsx([ | ||
'redocusaurus', | ||
hasLogo && 'redocusaurus-has-logo', | ||
className, | ||
])} | ||
> | ||
<RedocComponent store={store} /> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default ServerRedoc; |
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
77 changes: 77 additions & 0 deletions
77
packages/docusaurus-theme-redoc/src/utils/useSpecOptions.ts
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,77 @@ | ||
import { useMemo } from 'react'; | ||
import useIsBrowser from '@docusaurus/useIsBrowser'; | ||
import { | ||
usePluginData, | ||
useAllPluginInstancesData, | ||
} from '@docusaurus/useGlobalData'; | ||
import { useColorMode } from '@docusaurus/theme-common'; | ||
import merge from 'lodash/merge'; | ||
import '../global'; | ||
import { RedocRawOptions } from 'redoc'; | ||
import { SpecProps } from '../types/common'; | ||
import { GlobalData } from '../types/options'; | ||
|
||
/** | ||
* Redocusaurus | ||
* https://redocusaurus.vercel.app/ | ||
* (c) 2023 Rohit Gohri | ||
* Released under the MIT License | ||
*/ | ||
export function useSpecOptions( | ||
themeId: SpecProps['themeId'] = 'theme-redoc', | ||
optionsOverrides?: RedocRawOptions, | ||
) { | ||
const isBrowser = useIsBrowser(); | ||
const isDarkTheme = useColorMode().colorMode === 'dark'; | ||
|
||
const defaultThemeOptions = useAllPluginInstancesData( | ||
'docusaurus-theme-redoc', | ||
{ | ||
failfast: true, | ||
}, | ||
); | ||
const themeOptions = | ||
(usePluginData('docusaurus-theme-redoc', themeId) as GlobalData) || | ||
Object.values(defaultThemeOptions)[0]; | ||
|
||
const result = useMemo(() => { | ||
const { lightTheme, darkTheme, options: redocOptions } = themeOptions; | ||
|
||
const commonOptions: Partial<RedocRawOptions> = { | ||
// Disable offset when server rendering and set to selector | ||
scrollYOffset: | ||
!isBrowser && typeof redocOptions.scrollYOffset === 'string' | ||
? 0 | ||
: redocOptions.scrollYOffset, | ||
}; | ||
|
||
const lightThemeOptions: RedocRawOptions = merge( | ||
{ | ||
...redocOptions, | ||
...commonOptions, | ||
theme: lightTheme, | ||
}, | ||
optionsOverrides, | ||
); | ||
|
||
const darkThemeOptions: RedocRawOptions = merge( | ||
{ | ||
...redocOptions, | ||
...commonOptions, | ||
theme: darkTheme, | ||
}, | ||
optionsOverrides, | ||
); | ||
|
||
const options = | ||
isBrowser && isDarkTheme ? darkThemeOptions : lightThemeOptions; | ||
|
||
return { | ||
options, | ||
darkThemeOptions, | ||
lightThemeOptions, | ||
}; | ||
}, [isBrowser, isDarkTheme, themeOptions, optionsOverrides]); | ||
|
||
return result; | ||
} |
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,18 @@ | ||
import React from 'react'; | ||
import ApiDoc from '@theme/ApiDoc'; | ||
|
||
function ClientOnly() { | ||
return ( | ||
<ApiDoc | ||
layoutProps={{ | ||
title: `Client only page using url`, | ||
description: 'Example showcasing client only loading of yaml', | ||
}} | ||
specProps={{ | ||
url: 'https://redocly.github.io/redoc/openapi.yaml', | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export default ClientOnly; |
e222fec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
redocusaurus – ./
redocusaurus-v1.vercel.app
redocusaurus-rohit-gohri.vercel.app
redocusaurus-git-main-rohit-gohri.vercel.app
redocusaurus.vercel.app