Skip to content

Commit

Permalink
add generic DragResizeContainer component for drag-resizing element…
Browse files Browse the repository at this point in the history
…s to `@graphiql/react` (#2461)

* add `DragResizeContainer` component

* use `DragResizeContainer` for horizontal editor sizing

* use `DragResizeContainer` for secondary editors

* use `DragResizeContainer` for resizing docs

* remove unused refs

* remove `headerEditorEnabled` from state

* simplify state

* fix resizing of editors

* rename reset to show and restore size when programmatically hiding and showing elements

* use hooks for greater programatic flexibility
  • Loading branch information
thomasheyenbrock authored Jun 4, 2022
1 parent d6558e4 commit 7dfe3ec
Show file tree
Hide file tree
Showing 20 changed files with 608 additions and 553 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-pears-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphiql': patch
---

Use the `DragResizeContainer` component from `@graphiql/react` for the sizing of the editors and the docs explorer
5 changes: 5 additions & 0 deletions .changeset/early-roses-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/react': minor
---

Add `DragResizeContainer` utility component
3 changes: 0 additions & 3 deletions packages/graphiql-react/src/editor/header-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
useKeyMap,
useMergeQuery,
usePrettifyEditors,
useResizeEditor,
} from './hooks';

export type UseHeaderEditorArgs = {
Expand Down Expand Up @@ -117,8 +116,6 @@ export function useHeaderEditor({
useKeyMap(headerEditor, ['Shift-Ctrl-P'], prettify);
useKeyMap(headerEditor, ['Shift-Ctrl-M'], merge);

useResizeEditor(headerEditor, ref);

return ref;
}

Expand Down
17 changes: 0 additions & 17 deletions packages/graphiql-react/src/editor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,6 @@ export function useKeyMap(
}, [editor, keys, callback]);
}

export function useResizeEditor(
editor: CodeMirrorEditor | null,
ref: RefObject<HTMLDivElement>,
) {
const sizeRef = useRef<number>();
useEffect(() => {
if (!ref.current || !editor) {
return;
}
const size = ref.current.clientHeight;
if (size !== sizeRef.current) {
editor.setSize(null, null); // TODO: added the args here. double check no effects. might be version issue
}
sizeRef.current = size;
});
}

export type CopyQueryCallback = (query: string) => void;

export function useCopyQuery({
Expand Down
12 changes: 6 additions & 6 deletions packages/graphiql-react/src/editor/query-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
useKeyMap,
useMergeQuery,
usePrettifyEditors,
useResizeEditor,
} from './hooks';
import { CodeMirrorEditor, CodeMirrorType } from './types';
import { normalizeWhitespace } from './whitespace';
Expand All @@ -38,19 +37,21 @@ type OnClickReference = (reference: SchemaReference) => void;
export type UseQueryEditorArgs = {
editorTheme?: string;
externalFragments?: string | FragmentDefinitionNode[];
onClickReference?: OnClickReference;
onCopyQuery?: CopyQueryCallback;
onEdit?(value: string, documentAST?: DocumentNode): void;
onEditOperationName?: EditCallback;
onCopyQuery?: CopyQueryCallback;
readOnly?: boolean;
validationRules?: ValidationRule[];
};

export function useQueryEditor({
editorTheme = 'graphiql',
externalFragments,
onClickReference,
onCopyQuery,
onEdit,
onEditOperationName,
onCopyQuery,
readOnly = false,
validationRules,
}: UseQueryEditorArgs = {}) {
Expand Down Expand Up @@ -93,8 +94,9 @@ export function useQueryEditor({
} else if (reference.kind === 'EnumValue' && reference.type) {
explorer.push({ name: reference.type.name, def: reference.type });
}
onClickReference?.(reference);
};
}, [explorer]);
}, [explorer, onClickReference]);

useEffect(() => {
let isActive = true;
Expand Down Expand Up @@ -335,8 +337,6 @@ export function useQueryEditor({
);
useKeyMap(queryEditor, ['Shift-Ctrl-M'], merge);

useResizeEditor(queryEditor, ref);

return ref;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/graphiql-react/src/editor/response-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useSchemaContext } from '../schema';
import { commonKeys, importCodeMirror } from './common';
import { ImagePreview } from './components';
import { useEditorContext } from './context';
import { useResizeEditor, useSynchronizeValue } from './hooks';
import { useSynchronizeValue } from './hooks';
import { CodeMirrorEditor } from './types';

export type ResponseTooltipType = ComponentType<{ pos: Position }>;
Expand Down Expand Up @@ -121,8 +121,6 @@ export function useResponseEditor({

useSynchronizeValue(responseEditor, value);

useResizeEditor(responseEditor, ref);

useEffect(() => {
if (fetchError) {
responseEditor?.setValue(fetchError);
Expand Down
3 changes: 0 additions & 3 deletions packages/graphiql-react/src/editor/variable-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
useKeyMap,
useMergeQuery,
usePrettifyEditors,
useResizeEditor,
} from './hooks';
import { CodeMirrorType } from './types';

Expand Down Expand Up @@ -135,8 +134,6 @@ export function useVariableEditor({
useKeyMap(variableEditor, ['Shift-Ctrl-P'], prettify);
useKeyMap(variableEditor, ['Shift-Ctrl-M'], merge);

useResizeEditor(variableEditor, ref);

return ref;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/graphiql-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
StorageContextProvider,
useStorageContext,
} from './storage';
import { useDragResize } from './utility/resize';

import type {
EditorContextType,
Expand Down Expand Up @@ -96,6 +97,8 @@ export {
StorageContext,
StorageContextProvider,
useStorageContext,
// utility/resize
useDragResize,
};

export type {
Expand Down
Loading

0 comments on commit 7dfe3ec

Please sign in to comment.