Skip to content

Commit

Permalink
refresh editor after hiding (#2537)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheyenbrock committed Aug 9, 2022
1 parent d9d385c commit 5f62d76
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
14 changes: 14 additions & 0 deletions packages/graphiql-react/src/editor/components/header-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { useEffect } from 'react';

import { useEditorContext } from '../context';
import { useHeaderEditor, UseHeaderEditorArgs } from '../header-editor';

import '../style/codemirror.css';
Expand All @@ -7,7 +10,18 @@ import '../style/editor.css';
type HeaderEditorProps = UseHeaderEditorArgs & { isHidden?: boolean };

export function HeaderEditor({ isHidden, ...hookArgs }: HeaderEditorProps) {
const { headerEditor } = useEditorContext({
nonNull: true,
caller: HeaderEditor,
});
const ref = useHeaderEditor(hookArgs);

useEffect(() => {
if (headerEditor && !isHidden) {
headerEditor.refresh();
}
}, [headerEditor, isHidden]);

return (
<div className={`graphiql-editor${isHidden ? ' hidden' : ''}`} ref={ref} />
);
Expand Down
14 changes: 14 additions & 0 deletions packages/graphiql-react/src/editor/components/variable-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { useEffect } from 'react';

import { useEditorContext } from '../context';
import { useVariableEditor, UseVariableEditorArgs } from '../variable-editor';

import '../style/codemirror.css';
Expand All @@ -11,7 +14,18 @@ type VariableEditorProps = UseVariableEditorArgs & {
};

export function VariableEditor({ isHidden, ...hookArgs }: VariableEditorProps) {
const { variableEditor } = useEditorContext({
nonNull: true,
caller: VariableEditor,
});
const ref = useVariableEditor(hookArgs);

useEffect(() => {
if (variableEditor && !isHidden) {
variableEditor.refresh();
}
}, [variableEditor, isHidden]);

return (
<div className={`graphiql-editor${isHidden ? ' hidden' : ''}`} ref={ref} />
);
Expand Down
22 changes: 6 additions & 16 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,9 @@ class GraphiQLWithContext extends React.Component<
null,
);
}
this.setState(
{
activeSecondaryEditor: 'variable',
},
() => {
this.props.editorContext.variableEditor?.refresh();
},
);
this.setState({
activeSecondaryEditor: 'variable',
});
}}>
Variables
</UnStyledButton>
Expand All @@ -844,14 +839,9 @@ class GraphiQLWithContext extends React.Component<
null,
);
}
this.setState(
{
activeSecondaryEditor: 'header',
},
() => {
this.props.editorContext.headerEditor?.refresh();
},
);
this.setState({
activeSecondaryEditor: 'header',
});
}}>
Headers
</UnStyledButton>
Expand Down

0 comments on commit 5f62d76

Please sign in to comment.