piral-core 1.11.0 → 1.11.1-beta.220bc90

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,7 @@
1
1
  import { withoutKey, withKey, includeItem, excludeItem, replaceOrAddItem } from '../utils';
2
+ function getPortalContainer(portal) {
3
+ return portal.containerInfo;
4
+ }
2
5
  export function destroyPortal(ctx, id) {
3
6
  ctx.dispatch((state) => ({
4
7
  ...state,
@@ -11,10 +14,15 @@ export function hidePortal(ctx, id, entry) {
11
14
  portals: withKey(state.portals, id, excludeItem(state.portals[id], entry)),
12
15
  }));
13
16
  }
17
+ function isSamePortal(current, candidate) {
18
+ const currentContainer = getPortalContainer(current);
19
+ const candidateContainer = getPortalContainer(candidate);
20
+ return candidate === current || (!!currentContainer && currentContainer === candidateContainer);
21
+ }
14
22
  export function updatePortal(ctx, id, current, next) {
15
23
  ctx.dispatch((state) => ({
16
24
  ...state,
17
- portals: withKey(state.portals, id, replaceOrAddItem(state.portals[id], next, (m) => m === current)),
25
+ portals: withKey(state.portals, id, replaceOrAddItem(state.portals[id], next, (m) => isSamePortal(current, m))),
18
26
  }));
19
27
  }
20
28
  export function showPortal(ctx, id, entry) {
@@ -1 +1 @@
1
- {"version":3,"file":"portal.js","sourceRoot":"","sources":["../../src/actions/portal.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG3F,MAAM,UAAU,aAAa,CAAC,GAAuB,EAAE,EAAU;IAC/D,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACvB,GAAG,KAAK;QACR,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;KACvC,CAAC,CAAC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,GAAuB,EAAE,EAAU,EAA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AED,MAAM,UAAU,UAAU,CAAC,GAAuB,EAAE,EAAU,EAAE,KAAkB;IAChF,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACvB,GAAG,KAAK;QACR,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,EAAE,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;KAC3E,CAAC,CAAC,CAAC;AACN,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-core",
3
- "version": "1.11.0",
3
+ "version": "1.11.1-beta.220bc90",
4
4
  "description": "The core library for creating a Piral instance.",
5
5
  "keywords": [
6
6
  "portal",
@@ -63,8 +63,8 @@
63
63
  "test": "echo \"Error: run tests from root\" && exit 1"
64
64
  },
65
65
  "dependencies": {
66
- "piral-base": "^1.11.0",
67
- "piral-debug-utils": "^1.11.0",
66
+ "piral-base": "1.11.1-beta.220bc90",
67
+ "piral-debug-utils": "1.11.1-beta.220bc90",
68
68
  "zustand": "^3.0.0"
69
69
  },
70
70
  "devDependencies": {
@@ -84,5 +84,5 @@
84
84
  "react-router-dom",
85
85
  "tslib"
86
86
  ],
87
- "gitHead": "7bb5f7938f1a3211aa3e81ac5b7101b7605140d6"
87
+ "gitHead": "220bc902cdf09a8debd69e4d0beda078315d6a76"
88
88
  }
@@ -70,6 +70,48 @@ describe('Piral-Core portal actions', () => {
70
70
  expect(portals.test).not.toBeNull();
71
71
  });
72
72
 
73
+ it('updatePortal replaces a portal with the same container', () => {
74
+ const children = React.createElement('div');
75
+ const container = {};
76
+ const stored = {
77
+ key: 'stored',
78
+ children: { children },
79
+ type: 'div',
80
+ props: null,
81
+ containerInfo: container,
82
+ } as unknown as React.ReactPortal & { containerInfo: unknown };
83
+ const current = {
84
+ key: 'current',
85
+ children: { children },
86
+ type: 'div',
87
+ props: null,
88
+ containerInfo: container,
89
+ } as unknown as React.ReactPortal & { containerInfo: unknown };
90
+ const next = {
91
+ key: 'next',
92
+ children: { children },
93
+ type: 'div',
94
+ props: null,
95
+ containerInfo: container,
96
+ } as unknown as React.ReactPortal & { containerInfo: unknown };
97
+
98
+ const state = create(() => ({
99
+ portals: { test: [stored] },
100
+ }));
101
+
102
+ const ctx: any = {
103
+ state,
104
+ dispatch(update) {
105
+ state.setState(update(state.getState()));
106
+ },
107
+ };
108
+
109
+ updatePortal(ctx, 'test', current, next);
110
+ const { portals } = ctx.state.getState();
111
+ expect(portals.test).toHaveLength(1);
112
+ expect(portals.test[0]).toBe(next);
113
+ });
114
+
73
115
  it('showPortal adds a portal', () => {
74
116
  const children = React.createElement('div');
75
117
  const newPortal: React.ReactPortal = { key: 'test', children: { children }, type: 'div', props: null };
@@ -2,6 +2,14 @@ import { ReactPortal } from 'react';
2
2
  import { withoutKey, withKey, includeItem, excludeItem, replaceOrAddItem } from '../utils';
3
3
  import { GlobalStateContext } from '../types';
4
4
 
5
+ type PortalWithContainer = ReactPortal & {
6
+ containerInfo?: unknown;
7
+ };
8
+
9
+ function getPortalContainer(portal: ReactPortal) {
10
+ return (portal as PortalWithContainer).containerInfo;
11
+ }
12
+
5
13
  export function destroyPortal(ctx: GlobalStateContext, id: string) {
6
14
  ctx.dispatch((state) => ({
7
15
  ...state,
@@ -16,13 +24,20 @@ export function hidePortal(ctx: GlobalStateContext, id: string, entry: ReactPort
16
24
  }));
17
25
  }
18
26
 
27
+ function isSamePortal(current: ReactPortal, candidate: ReactPortal) {
28
+ const currentContainer = getPortalContainer(current);
29
+ const candidateContainer = getPortalContainer(candidate);
30
+
31
+ return candidate === current || (!!currentContainer && currentContainer === candidateContainer);
32
+ }
33
+
19
34
  export function updatePortal(ctx: GlobalStateContext, id: string, current: ReactPortal, next: ReactPortal) {
20
35
  ctx.dispatch((state) => ({
21
36
  ...state,
22
37
  portals: withKey(
23
38
  state.portals,
24
39
  id,
25
- replaceOrAddItem(state.portals[id], next, (m) => m === current),
40
+ replaceOrAddItem(state.portals[id], next, (m) => isSamePortal(current, m)),
26
41
  ),
27
42
  }));
28
43
  }