@linzjs/windows 5.5.1 → 5.6.0

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.
@@ -3,7 +3,8 @@ import './PanelBlue.scss';
3
3
  import '@linzjs/lui/dist/scss/base.scss';
4
4
 
5
5
  import clsx from 'clsx';
6
- import { PropsWithChildren, ReactElement, useContext, useEffect, useRef, useState } from 'react';
6
+ import { NumberSize } from 're-resizable';
7
+ import { PropsWithChildren, ReactElement, useCallback, useContext, useEffect, useRef, useState } from 'react';
7
8
  import { createPortal } from 'react-dom';
8
9
 
9
10
  import { useConstFunction } from '../common';
@@ -14,14 +15,20 @@ import { PopoutWindow } from './PopoutWindow';
14
15
  import { PanelProps } from './types/PanelProps';
15
16
  import { useRestoreStateFrom } from './usePanelStateHandler';
16
17
 
18
+ const defaultInitialSize = { width: 320, height: 200 };
19
+ const nullOpFunction = () => {};
20
+
17
21
  export const Panel = (props: PanelProps): ReactElement => {
18
22
  const panelRef = useRef<HTMLDivElement>(null);
19
- const { title, size = { width: 320, height: 200 }, className, children, onClose } = props;
23
+ const { title, size = defaultInitialSize, className, children, onClose } = props;
20
24
 
21
- const { dockElements } = useContext(PanelsContext);
25
+ const { dockElements, nextStackPosition } = useContext(PanelsContext);
22
26
  const { panelPoppedOut, uniqueId, setTitle, dockId, docked } = useContext(PanelInstanceContext);
23
27
 
24
- const onCloseConstFn = useConstFunction(onClose ?? (() => {}));
28
+ const savedState = useRestoreStateFrom({ uniqueId, panelPoppedOut: false });
29
+
30
+ // The function has to be const as the panel must no re-render as that would cause popout windows to close
31
+ const onCloseConstFn = useConstFunction(onClose ?? nullOpFunction);
25
32
 
26
33
  useEffect(() => {
27
34
  return () => {
@@ -43,10 +50,6 @@ export const Panel = (props: PanelProps): ReactElement => {
43
50
  }
44
51
  }, [title]);
45
52
 
46
- const { nextStackPosition } = useContext(PanelsContext);
47
-
48
- const savedState = useRestoreStateFrom({ uniqueId, panelPoppedOut: false });
49
-
50
53
  const [panelPosition, setPanelPosition] = useState(() => {
51
54
  if (savedState?.panelPosition) {
52
55
  return savedState.panelPosition;
@@ -66,7 +69,7 @@ export const Panel = (props: PanelProps): ReactElement => {
66
69
  return nextStackPosition();
67
70
  });
68
71
 
69
- const [panelSize, setPanelSize] = useState(() => {
72
+ const [panelSize, setPanelSize] = useState<NumberSize>(() => {
70
73
  if (savedState?.panelSize) {
71
74
  return savedState.panelSize;
72
75
  }
@@ -74,6 +77,18 @@ export const Panel = (props: PanelProps): ReactElement => {
74
77
  return size ?? { width: 320, height: 200 };
75
78
  });
76
79
 
80
+ const setInitialPanelSize = useCallback(
81
+ (size: NumberSize) => {
82
+ // If panel was already sized from state, then don't resize
83
+ if (savedState?.panelSize) {
84
+ return;
85
+ }
86
+
87
+ setPanelSize(size);
88
+ },
89
+ [savedState?.panelSize],
90
+ );
91
+
77
92
  const dockElement = docked && dockId && dockElements[dockId];
78
93
 
79
94
  return (
@@ -108,6 +123,7 @@ export const Panel = (props: PanelProps): ReactElement => {
108
123
  setPanelPosition={setPanelPosition}
109
124
  panelSize={panelSize}
110
125
  setPanelSize={setPanelSize}
126
+ setInitialPanelSize={setInitialPanelSize}
111
127
  >
112
128
  {children}
113
129
  </PopinWindow>
@@ -4,6 +4,7 @@ import { PanelSize } from './types';
4
4
 
5
5
  export interface PanelContextType {
6
6
  resizePanel: (size: Partial<PanelSize>) => void;
7
+ initialResizePanel: (size: Partial<PanelSize>) => void;
7
8
  resizeable: boolean;
8
9
  }
9
10
 
@@ -16,5 +17,6 @@ const NoContextError = () => {
16
17
  */
17
18
  export const PanelContext = createContext<PanelContextType>({
18
19
  resizePanel: NoContextError,
20
+ initialResizePanel: NoContextError,
19
21
  resizeable: true,
20
22
  });
@@ -23,11 +23,13 @@ export function PopinWindow({
23
23
  setPanelPosition,
24
24
  panelSize,
25
25
  setPanelSize,
26
+ setInitialPanelSize,
26
27
  }: PanelProps & {
27
28
  panelPosition: PanelPosition;
28
29
  setPanelPosition: Dispatch<PanelPosition>;
29
30
  panelSize: PanelSize;
30
31
  setPanelSize: Dispatch<PanelSize>;
32
+ setInitialPanelSize: Dispatch<PanelSize>;
31
33
  }): ReactNode {
32
34
  const panelRef = useRef<Rnd>(null);
33
35
 
@@ -51,12 +53,25 @@ export function PopinWindow({
51
53
  return;
52
54
  }, [centerWindow, position, resizeable]);
53
55
 
54
- const resizePanel = (newPanelSize: Partial<PanelSize>) => {
55
- const newSize = { ...panelSize, ...newPanelSize };
56
- if (newSize.width !== panelSize.width || newSize.height !== panelSize.height) {
57
- setPanelSize(newSize);
58
- }
59
- };
56
+ const resizePanel = useCallback(
57
+ (newPanelSize: Partial<PanelSize>) => {
58
+ const newSize = { ...panelSize, ...newPanelSize };
59
+ if (newSize.width !== panelSize.width || newSize.height !== panelSize.height) {
60
+ setPanelSize(newSize);
61
+ }
62
+ },
63
+ [panelSize, setPanelSize],
64
+ );
65
+
66
+ const initialResizePanel = useCallback(
67
+ (newPanelSize: Partial<PanelSize>) => {
68
+ const newSize = { ...panelSize, ...newPanelSize };
69
+ if (newSize.width !== panelSize.width || newSize.height !== panelSize.height) {
70
+ setInitialPanelSize(newSize);
71
+ }
72
+ },
73
+ [panelSize, setInitialPanelSize],
74
+ );
60
75
 
61
76
  const saveStateIn = useSaveStateIn({ uniqueId });
62
77
 
@@ -68,7 +83,7 @@ export function PopinWindow({
68
83
  }, [panelPosition, panelSize, saveStateIn]);
69
84
 
70
85
  return (
71
- <PanelContext.Provider value={{ resizePanel, resizeable }}>
86
+ <PanelContext.Provider value={{ resizePanel, initialResizePanel, resizeable }}>
72
87
  {modal && (
73
88
  <div
74
89
  style={{
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "popout"
14
14
  ],
15
15
  "main": "./dist/index.ts",
16
- "version": "5.5.1",
16
+ "version": "5.6.0",
17
17
  "peerDependencies": {
18
18
  "@linzjs/lui": ">=21",
19
19
  "lodash-es": ">=4",