react-resizable-panels 0.0.44 → 0.0.46

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.
package/src/PanelGroup.ts CHANGED
@@ -45,6 +45,7 @@ import {
45
45
  panelsMapToSortedArray,
46
46
  } from "./utils/group";
47
47
  import { loadPanelLayout, savePanelGroupLayout } from "./utils/serialization";
48
+ import { isServerRendering } from "./utils/ssr";
48
49
 
49
50
  const debounceMap: {
50
51
  [key: string]: (
@@ -55,19 +56,40 @@ const debounceMap: {
55
56
  ) => void;
56
57
  } = {};
57
58
 
58
- function throwServerError() {
59
- throw new Error('PanelGroup "storage" prop required for server rendering.');
59
+ // PanelGroup might be rendering in a server-side environment where localStorage is not available
60
+ // or on a browser with cookies/storage disabled.
61
+ // In either case, this function avoids accessing localStorage until needed,
62
+ // and avoids throwing user-visible errors.
63
+ function initializeDefaultStorage(storageObject: PanelGroupStorage) {
64
+ try {
65
+ if (typeof localStorage !== "undefined") {
66
+ // Bypass this check for future calls
67
+ storageObject.getItem = (name: string) => {
68
+ return localStorage.getItem(name);
69
+ };
70
+ storageObject.setItem = (name: string, value: string) => {
71
+ localStorage.setItem(name, value);
72
+ };
73
+ } else {
74
+ throw new Error("localStorage not supported in this environment");
75
+ }
76
+ } catch (error) {
77
+ console.error(error);
78
+
79
+ storageObject.getItem = () => null;
80
+ storageObject.setItem = () => {};
81
+ }
60
82
  }
61
83
 
62
84
  const defaultStorage: PanelGroupStorage = {
63
- getItem:
64
- typeof localStorage !== "undefined"
65
- ? (name: string) => localStorage.getItem(name)
66
- : (throwServerError as any),
67
- setItem:
68
- typeof localStorage !== "undefined"
69
- ? (name: string, value: string) => localStorage.setItem(name, value)
70
- : (throwServerError as any),
85
+ getItem: (name: string) => {
86
+ initializeDefaultStorage(defaultStorage);
87
+ return defaultStorage.getItem(name);
88
+ },
89
+ setItem: (name: string, value: string) => {
90
+ initializeDefaultStorage(defaultStorage);
91
+ defaultStorage.setItem(name, value);
92
+ },
71
93
  };
72
94
 
73
95
  export type CommittedValues = {
@@ -309,16 +331,22 @@ function PanelGroupWithForwardedRef({
309
331
  }, [autoSaveId, panels, sizes, storage]);
310
332
 
311
333
  const getPanelStyle = useCallback(
312
- (id: string): CSSProperties => {
334
+ (id: string, defaultSize: number | null): CSSProperties => {
313
335
  const { panels } = committedValuesRef.current;
314
336
 
315
337
  // Before mounting, Panels will not yet have registered themselves.
316
338
  // This includes server rendering.
317
339
  // At this point the best we can do is render everything with the same size.
318
340
  if (panels.size === 0) {
341
+ if (isServerRendering() && defaultSize == null) {
342
+ console.warn(
343
+ `WARNING: Panel defaultSize prop recommended to avoid layout shift after server rendering`
344
+ );
345
+ }
346
+
319
347
  return {
320
- flexBasis: "auto",
321
- flexGrow: 1,
348
+ flexBasis: 0,
349
+ flexGrow: defaultSize != null ? defaultSize : undefined,
322
350
  flexShrink: 1,
323
351
 
324
352
  // Without this, Panel sizes may be unintentionally overridden by their content.
@@ -382,7 +410,7 @@ function PanelGroupWithForwardedRef({
382
410
  return;
383
411
  }
384
412
 
385
- const movement = getMovement(
413
+ let movement = getMovement(
386
414
  event,
387
415
  groupId,
388
416
  handleId,
@@ -398,6 +426,12 @@ function PanelGroupWithForwardedRef({
398
426
  const groupElement = getPanelGroup(groupId)!;
399
427
  const rect = groupElement.getBoundingClientRect();
400
428
  const isHorizontal = direction === "horizontal";
429
+
430
+ // Support RTL layouts
431
+ if (document.dir === "rtl" && isHorizontal) {
432
+ movement = -movement;
433
+ }
434
+
401
435
  const size = isHorizontal ? rect.width : rect.height;
402
436
  const delta = (movement / size) * 100;
403
437
 
@@ -0,0 +1,7 @@
1
+ export function isServerRendering(): boolean {
2
+ try {
3
+ return typeof window === undefined;
4
+ } catch (error) {}
5
+
6
+ return true;
7
+ }