@linzjs/windows 7.0.1 → 7.1.1

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.
@@ -15,11 +15,13 @@ import {
15
15
  useState,
16
16
  } from 'react';
17
17
  import { createPortal } from 'react-dom';
18
+ import { useInterval } from 'usehooks-ts';
18
19
 
19
20
  import { PanelInstanceContext } from './PanelInstanceContext';
20
21
  import { PanelsContext } from './PanelsContext';
21
22
  import { PopinWindow } from './PopinWIndow';
22
23
  import { PopoutWindow } from './PopoutWindow';
24
+ import { PanelPosition } from './types';
23
25
  import { PanelProps } from './types/PanelProps';
24
26
  import { useRestoreStateFrom } from './usePanelStateHandler';
25
27
 
@@ -27,7 +29,7 @@ const defaultInitialSize = { width: 320, height: 200 };
27
29
 
28
30
  export const Panel = (props: PanelProps): ReactElement => {
29
31
  const panelRef = useRef<HTMLDivElement>(null);
30
- const { title, size = defaultInitialSize, className, children } = props;
32
+ const { title, size = defaultInitialSize, className, ignoreSavedState, children } = props;
31
33
 
32
34
  const { dockElements, nextStackPosition } = useContext(PanelsContext);
33
35
  const { panelPoppedOut, uniqueId, setTitle, dockId, docked } = useContext(PanelInstanceContext);
@@ -48,25 +50,53 @@ export const Panel = (props: PanelProps): ReactElement => {
48
50
  }
49
51
  }, [title]);
50
52
 
51
- const [panelPosition, setPanelPosition] = useState(() => {
52
- if (savedState?.panelPosition) {
53
+ const [panelPosition, setPanelPosition] = useState((): PanelPosition => {
54
+ const pos = props.position;
55
+ if (!ignoreSavedState && savedState?.panelPosition) {
53
56
  return savedState.panelPosition;
54
57
  }
55
58
 
56
- if (props.position === 'center') {
59
+ // PanelPosition
60
+ if (pos && typeof pos === 'object' && 'selector' in pos) {
61
+ const b = document.querySelector(pos.selector)?.getBoundingClientRect();
62
+ if (b) {
63
+ return { x: b[pos.edgeX ?? 'left'] + (pos.offsetX ?? 0), y: b[pos.edgeY ?? 'bottom'] + (pos.offsetY ?? 0) };
64
+ }
65
+ }
66
+
67
+ if (pos === 'center') {
57
68
  return {
58
69
  x: Math.max((window.innerWidth - size.width) / 2, 0),
59
70
  y: Math.max((window.innerHeight - size.height) / 2, 0),
60
71
  };
61
72
  }
62
73
 
63
- if (props.position != null && props.position !== 'tile') {
64
- return props.position;
74
+ if (pos != null && pos !== 'tile') {
75
+ return pos as PanelPosition;
65
76
  }
66
77
 
67
78
  return nextStackPosition();
68
79
  });
69
80
 
81
+ useInterval(
82
+ () => {
83
+ const pos = props.position;
84
+ if (pos && typeof pos === 'object' && 'selector' in pos) {
85
+ const b = document.querySelector(pos.selector)?.getBoundingClientRect();
86
+ if (b) {
87
+ const newPos = {
88
+ x: b[pos.edgeX ?? 'left'] + (pos.offsetX ?? 0),
89
+ y: b[pos.edgeY ?? 'bottom'] + (pos.offsetY ?? 0),
90
+ };
91
+ if (newPos.x !== panelPosition.x || newPos.y !== panelPosition.y) {
92
+ setPanelPosition(newPos);
93
+ }
94
+ }
95
+ }
96
+ },
97
+ props.position && typeof props.position === 'object' && 'selector' in props.position ? 250 : null,
98
+ );
99
+
70
100
  const cropSizeToWindow = useCallback((size: NumberSize) => {
71
101
  return typeof window !== 'undefined' && window.innerWidth && window.innerHeight
72
102
  ? {
@@ -77,7 +107,7 @@ export const Panel = (props: PanelProps): ReactElement => {
77
107
  }, []);
78
108
 
79
109
  const [panelSize, _setPanelSize] = useState<NumberSize>(() => {
80
- if (savedState?.panelSize) {
110
+ if (!ignoreSavedState && savedState?.panelSize) {
81
111
  return cropSizeToWindow(savedState.panelSize);
82
112
  }
83
113
 
@@ -3,6 +3,14 @@ import { PropsWithChildren } from 'react';
3
3
  import { PanelPosition } from './PanelPosition';
4
4
  import { PanelSize } from './PanelSize';
5
5
 
6
+ export interface PanelRelativePositon {
7
+ selector: string;
8
+ edgeX?: 'left' | 'right';
9
+ edgeY?: 'top' | 'bottom';
10
+ offsetX?: number;
11
+ offsetY?: number;
12
+ }
13
+
6
14
  interface _PanelProps {
7
15
  className?: string;
8
16
  maxHeight?: number | string;
@@ -10,10 +18,11 @@ interface _PanelProps {
10
18
  minHeight?: number | string;
11
19
  minWidth?: number | string;
12
20
  modal?: boolean;
13
- position?: PanelPosition | 'tile' | 'center';
21
+ position?: PanelPosition | PanelRelativePositon | 'tile' | 'center';
14
22
  resizeable?: boolean;
15
23
  size?: PanelSize;
16
24
  title: string;
25
+ ignoreSavedState?: boolean;
17
26
  }
18
27
 
19
28
  export type PanelProps = PropsWithChildren<_PanelProps>;
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "popout"
14
14
  ],
15
15
  "main": "./dist/index.ts",
16
- "version": "7.0.1",
16
+ "version": "7.1.1",
17
17
  "peerDependencies": {
18
18
  "@linzjs/lui": ">=21",
19
19
  "lodash-es": ">=4",