MCPcopy
hub / github.com/graphql/graphiql / useDragResize

Function useDragResize

packages/graphiql-react/src/utility/resize.ts:45–278  ·  view source on GitHub ↗
({
  defaultSizeRelation = DEFAULT_FLEX,
  direction,
  initiallyHidden,
  onHiddenElementChange,
  sizeThresholdFirst = 100,
  sizeThresholdSecond = 100,
  storageKey,
}: UseDragResizeArgs)

Source from the content-addressed store, hash-verified

43};
44
45export function useDragResize({
46 defaultSizeRelation = DEFAULT_FLEX,
47 direction,
48 initiallyHidden,
49 onHiddenElementChange,
50 sizeThresholdFirst = 100,
51 sizeThresholdSecond = 100,
52 storageKey,
53}: UseDragResizeArgs) {
54 const storage = useStorageContext();
55
56 const store = debounce(500, (value: string) => {
57 if (storageKey) {
58 storage?.set(storageKey, value);
59 }
60 });
61
62 const [hiddenElement, setHiddenElement] = useState<ResizableElement | null>(
63 () => {
64 const storedValue = storageKey && storage?.get(storageKey);
65 if (storedValue === HIDE_FIRST || initiallyHidden === 'first') {
66 return 'first';
67 }
68 if (storedValue === HIDE_SECOND || initiallyHidden === 'second') {
69 return 'second';
70 }
71 return null;
72 },
73 );
74
75 const setHiddenElementWithCallback = // eslint-disable-line react-hooks/exhaustive-deps -- false positive, function is optimized by react-compiler, no need to wrap with useCallback
76 (element: ResizableElement | null) => {
77 if (element !== hiddenElement) {
78 setHiddenElement(element);
79 onHiddenElementChange?.(element);
80 }
81 };
82
83 const firstRef = useRef<HTMLDivElement>(null);
84 const dragBarRef = useRef<HTMLDivElement>(null);
85 const secondRef = useRef<HTMLDivElement>(null);
86
87 const defaultFlexRef = useRef(`${defaultSizeRelation}`);
88
89 /**
90 * Set initial flex values
91 */
92 useLayoutEffect(() => {
93 const storedValue =
94 (storageKey && storage?.get(storageKey)) || defaultFlexRef.current;
95
96 if (firstRef.current) {
97 firstRef.current.style.display = 'flex';
98 firstRef.current.style.flex =
99 storedValue === HIDE_FIRST || storedValue === HIDE_SECOND
100 ? defaultFlexRef.current
101 : storedValue;
102 }

Callers 1

GraphiQLInterfaceFunction · 0.90

Calls 5

debounceFunction · 0.90
hideFunction · 0.85
showFunction · 0.85
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected