Skip to content

Commit

Permalink
fix: SSR considerations
Browse files Browse the repository at this point in the history
  • Loading branch information
kausko committed Aug 22, 2024
1 parent f0b813f commit d7e8d3e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const config: Config = {
} satisfies Preset.ThemeConfig,

clientModules: [
'node_modules/provenance-widgets/web-components/index.js'
require.resolve('./src/pw.js')
],

themes: ['@docusaurus/theme-live-codeblock'],
Expand Down
5 changes: 4 additions & 1 deletion src/components/lib/AbstractSelectionComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import type { MultiselectComponent } from 'provenance-widgets';
import type { NgWebComponent } from './types';
import StateView from './StateView';
import useIsBrowser from '@docusaurus/useIsBrowser';

type AbstractProps = Pick<MultiselectComponent, 'provenance' | 'provenanceChange' | 'selectedChange' | 'visualize' | 'freeze' | 'selected'>;

Expand All @@ -15,9 +16,11 @@ interface SelectionProps extends AbstractProps {
}

export default function AbstractSelectionComponent (props: SelectionProps) {

const isBrowser = useIsBrowser();
if (!isBrowser) return <div>loading...</div>

const ref = React.useRef<HTMLDivElement>(null);

const [selected, setSelected] = React.useState<any>(null);
const [provenance, setProvenance] = React.useState<any>({});

Expand Down
5 changes: 5 additions & 0 deletions src/components/lib/ProvenanceInputText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import React from 'react';
import type { InputtextComponent } from 'provenance-widgets';
import type { NgWebComponent } from './types';
import StateView from './StateView';
import useIsBrowser from '@docusaurus/useIsBrowser';

type InputtextComponentProps = Pick<InputtextComponent, 'id' | 'value' | 'visualize' | 'freeze' | 'provenance' | 'provenanceChange' | 'valueChange'>;

export default function ProvenanceInputText(props: InputtextComponentProps) {

const isBrowser = useIsBrowser();
if (!isBrowser) return <div>loading...</div>

const ref = React.useRef<HTMLDivElement>(null);

const [value, setValue] = React.useState<any>(null);
Expand Down
4 changes: 4 additions & 0 deletions src/components/lib/ProvenanceSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import React from 'react';
import type { SliderComponent } from 'provenance-widgets';
import type { NgWebComponent } from './types';
import StateView from './StateView';
import useIsBrowser from '@docusaurus/useIsBrowser';

type SliderComponentProps = Pick<SliderComponent, 'id' | 'mode' | 'value' | 'highValue' | 'options' | 'provenance' | 'provenanceChange' | 'selectedChange' | 'visualize' | 'freeze'>;

export default function ProvenanceSlider (props: SliderComponentProps) {

const isBrowser = useIsBrowser();
if (!isBrowser) return <div>loading...</div>

const ref = React.useRef<HTMLDivElement>(null);

Expand Down
37 changes: 23 additions & 14 deletions src/components/lib/StateView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ReactJson, { ReactJsonViewProps } from '@microlink/react-json-view'
import BrowserOnly from '@docusaurus/BrowserOnly';
// import ReactJson, { ReactJsonViewProps } from '@microlink/react-json-view'
import type { ReactJsonViewProps } from '@microlink/react-json-view'

interface StateViewProps {
selected: any;
Expand All @@ -14,19 +16,26 @@ const reactJsonProps: Omit<ReactJsonViewProps, 'src'> = {
export default function StateView(props: StateViewProps) {
return (
<div className="row margin-top--md">
<div className="col col--6">
<ReactJson src={props.provenance} name="provenance" {...reactJsonProps} />
</div>
{
props.selected !== null &&
<div className="col col--6">
{
typeof props.selected === 'string' ?
<code style={{ color: "black" }}>"{props.selectedAlias || "selected"}": "{props.selected}"</code> :
<ReactJson src={props.selected} name={props.selectedAlias || "selected"} {...reactJsonProps} />
}
</div>
}
<BrowserOnly>
{() => {
const ReactJson = require('@microlink/react-json-view').default;
return <>
<div className="col col--6">
<ReactJson src={props.provenance} name="provenance" {...reactJsonProps} />
</div>
{
props.selected !== null &&
<div className="col col--6">
{
typeof props.selected === 'string' ?
<code style={{ color: "black" }}>"{props.selectedAlias || "selected"}": "{props.selected}"</code> :
<ReactJson src={props.selected} name={props.selectedAlias || "selected"} {...reactJsonProps} />
}
</div>
}
</>
}}
</BrowserOnly>
</div>
)
}
3 changes: 3 additions & 0 deletions src/pw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (typeof window !== "undefined") {
require('../node_modules/provenance-widgets/web-components/index.js')
}

0 comments on commit d7e8d3e

Please sign in to comment.