react-resizable-panels 0.0.38 → 0.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { RefObject, useEffect } from "react";
1
+ import { RefObject, useEffect } from "../vendor/react";
2
2
  import { PRECISION } from "../constants";
3
3
 
4
4
  import { CommittedValues, PanelDataMap } from "../PanelGroup";
@@ -15,6 +15,7 @@ import {
15
15
  getFlexGrow,
16
16
  panelsMapToSortedArray,
17
17
  } from "../utils/group";
18
+ import { assert } from "../utils/assert";
18
19
 
19
20
  // https://www.w3.org/WAI/ARIA/apg/patterns/windowsplitter/
20
21
 
@@ -34,14 +35,14 @@ export function useWindowSplitterPanelGroupBehavior({
34
35
  panelSizeBeforeCollapse: RefObject<Map<string, number>>;
35
36
  }): void {
36
37
  useEffect(() => {
37
- const { direction, panels } = committedValuesRef.current;
38
+ const { direction, panels } = committedValuesRef.current!;
38
39
 
39
- const groupElement = getPanelGroup(groupId);
40
+ const groupElement = getPanelGroup(groupId)!;
40
41
  const { height, width } = groupElement.getBoundingClientRect();
41
42
 
42
43
  const handles = getResizeHandlesForGroup(groupId);
43
44
  const cleanupFunctions = handles.map((handle) => {
44
- const handleId = handle.getAttribute("data-panel-resize-handle-id");
45
+ const handleId = handle.getAttribute("data-panel-resize-handle-id")!;
45
46
  const panelsArray = panelsMapToSortedArray(panels);
46
47
 
47
48
  const [idBefore, idAfter] = getResizeHandlePanelIds(
@@ -114,7 +115,7 @@ export function useWindowSplitterPanelGroupBehavior({
114
115
  idAfter,
115
116
  delta,
116
117
  sizes,
117
- panelSizeBeforeCollapse.current,
118
+ panelSizeBeforeCollapse.current!,
118
119
  null
119
120
  );
120
121
  if (sizes !== nextSizes) {
@@ -150,7 +151,14 @@ export function useWindowSplitterPanelGroupBehavior({
150
151
  return () => {
151
152
  cleanupFunctions.forEach((cleanupFunction) => cleanupFunction());
152
153
  };
153
- }, [groupId, panels, sizes]);
154
+ }, [
155
+ committedValuesRef,
156
+ groupId,
157
+ panels,
158
+ panelSizeBeforeCollapse,
159
+ setSizes,
160
+ sizes,
161
+ ]);
154
162
  }
155
163
 
156
164
  export function useWindowSplitterResizeHandlerBehavior({
@@ -195,6 +203,8 @@ export function useWindowSplitterResizeHandlerBehavior({
195
203
  const handles = getResizeHandles();
196
204
  const index = getResizeHandleIndex(handleId);
197
205
 
206
+ assert(index !== null);
207
+
198
208
  const nextIndex = event.shiftKey
199
209
  ? index > 0
200
210
  ? index - 1
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RefObject } from "react";
1
+ import { RefObject } from "./vendor/react";
2
2
 
3
3
  export type Direction = "horizontal" | "vertical";
4
4
 
@@ -18,7 +18,7 @@ export type PanelData = {
18
18
  onResize: PanelOnResize | null;
19
19
  }>;
20
20
  collapsible: boolean;
21
- defaultSize: number;
21
+ defaultSize: number | null;
22
22
  id: string;
23
23
  maxSize: number;
24
24
  minSize: number;
@@ -0,0 +1,10 @@
1
+ export function assert(
2
+ expectedCondition: boolean,
3
+ message: string = "Assertion failed!"
4
+ ): asserts expectedCondition {
5
+ if (!expectedCondition) {
6
+ console.error(message);
7
+
8
+ throw Error(message);
9
+ }
10
+ }
@@ -36,7 +36,7 @@ export function getDragOffset(
36
36
  return 0;
37
37
  }
38
38
 
39
- const handleElement = getResizeHandle(handleId);
39
+ const handleElement = getResizeHandle(handleId)!;
40
40
  const rect =
41
41
  initialHandleElementRect || handleElement.getBoundingClientRect();
42
42
  const elementOffset = isHorizontal ? rect.left : rect.top;
@@ -67,7 +67,7 @@ export function getMovement(
67
67
  if (isKeyDown(event)) {
68
68
  const isHorizontal = direction === "horizontal";
69
69
 
70
- const groupElement = getPanelGroup(groupId);
70
+ const groupElement = getPanelGroup(groupId)!;
71
71
  const rect = groupElement.getBoundingClientRect();
72
72
  const groupSizeInPixels = isHorizontal ? rect.width : rect.height;
73
73
 
@@ -5,7 +5,9 @@ export default function debounce<T extends Function>(
5
5
  let timeoutId: NodeJS.Timeout | null = null;
6
6
 
7
7
  let callable = (...args: any) => {
8
- clearTimeout(timeoutId);
8
+ if (timeoutId !== null) {
9
+ clearTimeout(timeoutId);
10
+ }
9
11
 
10
12
  timeoutId = setTimeout(() => {
11
13
  callback(...args);
@@ -124,7 +124,7 @@ export function callPanelCallbacks(
124
124
  const prevSize = prevSizes[index];
125
125
  if (prevSize !== nextSize) {
126
126
  const { callbacksRef, collapsible } = panelsArray[index];
127
- const { onCollapse, onResize } = callbacksRef.current;
127
+ const { onCollapse, onResize } = callbacksRef.current!;
128
128
 
129
129
  if (onResize) {
130
130
  onResize(nextSize);
@@ -238,7 +238,7 @@ export function getResizeHandlePanelIds(
238
238
  ): [idBefore: string | null, idAfter: string | null] {
239
239
  const handle = getResizeHandle(handleId);
240
240
  const handles = getResizeHandlesForGroup(groupId);
241
- const index = handles.indexOf(handle);
241
+ const index = handle ? handles.indexOf(handle) : -1;
242
242
 
243
243
  const idBefore: string | null = panelsArray[index]?.id ?? null;
244
244
  const idAfter: string | null = panelsArray[index + 1]?.id ?? null;
@@ -249,7 +249,19 @@ export function getResizeHandlePanelIds(
249
249
  export function panelsMapToSortedArray(
250
250
  panels: Map<string, PanelData>
251
251
  ): PanelData[] {
252
- return Array.from(panels.values()).sort((a, b) => a.order - b.order);
252
+ return Array.from(panels.values()).sort((panelA, panelB) => {
253
+ const orderA = panelA.order;
254
+ const orderB = panelB.order;
255
+ if (orderA == null && orderB == null) {
256
+ return 0;
257
+ } else if (orderA == null) {
258
+ return -1;
259
+ } else if (orderB == null) {
260
+ return 1;
261
+ } else {
262
+ return orderA - orderB;
263
+ }
264
+ });
253
265
  }
254
266
 
255
267
  function safeResizePanel(
@@ -0,0 +1,61 @@
1
+ // This module exists to work around Webpack issue https://github.com/webpack/webpack/issues/14814
2
+ // and limitations with ParcelJS parsing of the useId workaround (used below).
3
+ // For the time being, all react-resizable-panels must import "react" with the "* as React" syntax.
4
+ // To avoid mistakes, we use the ESLint "no-restricted-imports" to prevent "react" imports except in this file.
5
+ // See https://github.com/bvaughn/react-resizable-panels/issues/118
6
+
7
+ // eslint-disable-next-line no-restricted-imports
8
+ import * as React from "react";
9
+
10
+ // eslint-disable-next-line no-restricted-imports
11
+ import type {
12
+ CSSProperties,
13
+ ElementType,
14
+ ForwardedRef,
15
+ MouseEvent,
16
+ ReactNode,
17
+ RefObject,
18
+ TouchEvent,
19
+ } from "react";
20
+
21
+ const {
22
+ createElement,
23
+ createContext,
24
+ forwardRef,
25
+ useCallback,
26
+ useContext,
27
+ useEffect,
28
+ useImperativeHandle,
29
+ useLayoutEffect,
30
+ useMemo,
31
+ useRef,
32
+ useState,
33
+ } = React;
34
+
35
+ // `toString()` prevents bundlers from trying to `import { useId } from 'react'`
36
+ const useId = (React as any)["useId".toString()] as () => string;
37
+
38
+ export {
39
+ createElement,
40
+ createContext,
41
+ forwardRef,
42
+ useCallback,
43
+ useContext,
44
+ useEffect,
45
+ useId,
46
+ useImperativeHandle,
47
+ useLayoutEffect,
48
+ useMemo,
49
+ useRef,
50
+ useState,
51
+ };
52
+
53
+ export type {
54
+ CSSProperties,
55
+ ElementType,
56
+ ForwardedRef,
57
+ MouseEvent,
58
+ ReactNode,
59
+ RefObject,
60
+ TouchEvent,
61
+ };