@linzjs/windows 7.3.0 → 8.1.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.
@@ -9,11 +9,11 @@ interface OpenPanelButtonProps extends OpenPanelOptions {
9
9
  }
10
10
 
11
11
  export const OpenPanelButton = ({ buttonText, uniqueId = buttonText, ...openPanelOptions }: OpenPanelButtonProps) => {
12
- const { openPanel, openPanels } = useContext(PanelsContext);
12
+ const { openPanel, hasOpenPanel } = useContext(PanelsContext);
13
13
 
14
14
  return (
15
15
  <button onClick={() => openPanel({ uniqueId, ...openPanelOptions })}>
16
- Show {buttonText} {openPanels.has(buttonText) ? '(Open)' : ''}
16
+ Show {buttonText} {hasOpenPanel(buttonText) ? '(Open)' : ''}
17
17
  </button>
18
18
  );
19
19
  };
@@ -37,25 +37,27 @@ export const PanelInstanceContextProvider = ({
37
37
 
38
38
  const undock = useCallback(() => setDockId(undefined), []);
39
39
 
40
+ const uniqueId = panelInstance.uniqueId;
41
+
40
42
  return (
41
43
  <PanelInstanceContext.Provider
42
44
  value={{
43
45
  title,
44
46
  setTitle,
45
- uniqueId: panelInstance.uniqueId,
47
+ uniqueId,
46
48
  bounds,
47
- panelName: panelInstance.uniqueId,
49
+ panelName: uniqueId,
48
50
  panelClose: () => {
49
51
  panelInstance.window?.close();
50
52
  panelInstance.window = null;
51
- closePanel(panelInstance.uniqueId);
53
+ closePanel(uniqueId);
52
54
  },
53
55
  panelTogglePopout: togglePopOut,
54
56
  panelPoppedOut: poppedOut,
55
57
  setPanelWindow: (w: Window) => {
56
58
  panelInstance.window = w;
57
59
  },
58
- bringPanelToFront: () => bringPanelToFront(panelInstance),
60
+ bringPanelToFront: () => bringPanelToFront(uniqueId),
59
61
  zIndex: panelInstance.zIndex,
60
62
  docked: !poppedOut && !!dockId && !!dockElements[dockId],
61
63
  dockId,
@@ -21,10 +21,11 @@ export interface OpenPanelOptions {
21
21
 
22
22
  export interface PanelsContextType {
23
23
  openPanels: Set<string>;
24
+ hasOpenPanel: (panelId: string) => boolean;
24
25
  // returns unique id of panel that was created/or arleady found, otherwise null on error.
25
26
  openPanel: (args: OpenPanelOptions) => string | null;
26
27
  closePanel: (uniqueId: string) => void;
27
- bringPanelToFront: (panelInstance: PanelInstance) => void;
28
+ bringPanelToFront: (panelUniqueId: string) => void;
28
29
  nextStackPosition: () => PanelPosition;
29
30
  dockElements: Record<string, HTMLDivElement>;
30
31
  setDockElement: (id: string, element: HTMLDivElement) => void;
@@ -38,6 +39,7 @@ const NoContext = <T,>(): T => {
38
39
 
39
40
  export const PanelsContext = createContext<PanelsContextType>({
40
41
  openPanels: new Set(),
42
+ hasOpenPanel: () => false,
41
43
  openPanel: NoContext,
42
44
  closePanel: NoContext,
43
45
  bringPanelToFront: NoContext,
@@ -50,7 +50,12 @@ export const PanelsContextProvider = ({
50
50
  );
51
51
 
52
52
  const bringPanelToFront = useCallback(
53
- (panelInstance: PanelInstance) => {
53
+ (panelUniqueId: string) => {
54
+ const panelInstance = panelInstances.find((panelInstance) => panelInstance.uniqueId === panelUniqueId);
55
+ if (!panelInstance) {
56
+ console.warn(`bringPanelToFront cannot find panel with uniqueId: ${panelUniqueId}`);
57
+ return;
58
+ }
54
59
  if (panelInstance.window) {
55
60
  panelInstance.window.focus();
56
61
  } else {
@@ -74,7 +79,7 @@ export const PanelsContextProvider = ({
74
79
  if (existingPanelInstance.window) {
75
80
  existingPanelInstance.window?.focus();
76
81
  } else {
77
- bringPanelToFront(existingPanelInstance);
82
+ bringPanelToFront(uniqueId);
78
83
  }
79
84
  return existingPanelInstance.uniqueId;
80
85
  }
@@ -100,9 +105,9 @@ export const PanelsContextProvider = ({
100
105
  [baseZIndex, bringPanelToFront, panelInstances],
101
106
  );
102
107
 
103
- const closePanel = useCallback(
104
- (closePanelUniqueIds: string | string[]) => {
105
- const panelNames = castArray(closePanelUniqueIds);
108
+ const closePanel = useCallback((closePanelUniqueIds: string | string[]) => {
109
+ const panelNames = castArray(closePanelUniqueIds);
110
+ setPanelInstances((panelInstances) => {
106
111
  const [closedPanelInstances, stillOpenPanelInstances] = partition(panelInstances, (pi) =>
107
112
  panelNames.includes(pi.uniqueId),
108
113
  );
@@ -110,11 +115,11 @@ export const PanelsContextProvider = ({
110
115
  closedPanelInstances.forEach(({ onClose }) => {
111
116
  onClose?.();
112
117
  });
113
- setPanelInstances(stillOpenPanelInstances);
118
+ return stillOpenPanelInstances;
114
119
  }
115
- },
116
- [panelInstances],
117
- );
120
+ return panelInstances;
121
+ });
122
+ }, []);
118
123
 
119
124
  /**
120
125
  * It's not easy to tell the difference between a window closing and a window popping in via events,
@@ -139,10 +144,13 @@ export const PanelsContextProvider = ({
139
144
 
140
145
  const openPanels = useMemo(() => new Set(panelInstances.map((pi) => pi.uniqueId)), [panelInstances]);
141
146
 
147
+ const hasOpenPanel = useCallback((panelId: string): boolean => openPanels.has(panelId), [openPanels]);
148
+
142
149
  return (
143
150
  <PanelsContext.Provider
144
151
  value={{
145
152
  openPanels,
153
+ hasOpenPanel,
146
154
  openPanel,
147
155
  closePanel,
148
156
  bringPanelToFront,
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "popout"
14
14
  ],
15
15
  "main": "./dist/index.ts",
16
- "version": "7.3.0",
16
+ "version": "8.1.0",
17
17
  "peerDependencies": {
18
18
  "@linzjs/lui": ">=21",
19
19
  "lodash-es": ">=4",