@lumx/react 3.16.1-alpha.0 → 3.16.1-alpha.2
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/index.js +8 -13
- package/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/dialog/Dialog.tsx +89 -88
- package/src/components/lightbox/Lightbox.tsx +2 -1
- package/src/components/notification/Notification.tsx +34 -33
- package/src/components/popover/Popover.tsx +33 -39
- package/src/components/tooltip/Tooltip.tsx +1 -1
- package/src/utils/Portal/Portal.test.tsx +32 -0
- package/src/utils/Portal/Portal.tsx +33 -0
- package/src/utils/Portal/PortalProvider.stories.jsx +34 -0
- package/src/utils/Portal/PortalProvider.test.tsx +73 -0
- package/src/utils/Portal/PortalProvider.tsx +24 -0
- package/src/utils/Portal/index.tsx +2 -0
- package/src/utils/index.ts +2 -2
- package/utils/index.d.ts +19 -14
- package/utils/index.js +16 -19
- package/utils/index.js.map +1 -1
- package/src/utils/PortalProvider/PortalProvider.stories.jsx +0 -40
- package/src/utils/PortalProvider/index.tsx +0 -43
package/package.json
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
"url": "https://github.com/lumapps/design-system/issues"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@lumx/core": "^3.16.1-alpha.
|
|
10
|
-
"@lumx/icons": "^3.16.1-alpha.
|
|
9
|
+
"@lumx/core": "^3.16.1-alpha.2",
|
|
10
|
+
"@lumx/icons": "^3.16.1-alpha.2",
|
|
11
11
|
"@popperjs/core": "^2.5.4",
|
|
12
12
|
"body-scroll-lock": "^3.1.5",
|
|
13
13
|
"classnames": "^2.3.2",
|
|
@@ -105,5 +105,5 @@
|
|
|
105
105
|
"build:storybook": "storybook build"
|
|
106
106
|
},
|
|
107
107
|
"sideEffects": false,
|
|
108
|
-
"version": "3.16.1-alpha.
|
|
108
|
+
"version": "3.16.1-alpha.2"
|
|
109
109
|
}
|
|
@@ -19,7 +19,8 @@ import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
|
19
19
|
import { useDisableBodyScroll } from '@lumx/react/hooks/useDisableBodyScroll';
|
|
20
20
|
import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibility';
|
|
21
21
|
import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
|
|
22
|
-
|
|
22
|
+
|
|
23
|
+
import { Portal } from '@lumx/react/utils';
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Defines the props of the component.
|
|
@@ -193,94 +194,94 @@ export const Dialog = forwardRef<DialogProps, HTMLDivElement>((props, ref) => {
|
|
|
193
194
|
|
|
194
195
|
const shouldPreventCloseOnClickAway = preventAutoClose || preventCloseOnClick;
|
|
195
196
|
|
|
196
|
-
return isOpen || isVisible
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
197
|
+
return isOpen || isVisible ? (
|
|
198
|
+
<Portal>
|
|
199
|
+
<div
|
|
200
|
+
ref={mergeRefs(rootRef, ref)}
|
|
201
|
+
{...forwardedProps}
|
|
202
|
+
className={classNames(
|
|
203
|
+
className,
|
|
204
|
+
handleBasicClasses({
|
|
205
|
+
isHidden: !isOpen,
|
|
206
|
+
isLoading,
|
|
207
|
+
isShown: isOpen || isVisible,
|
|
208
|
+
prefix: CLASSNAME,
|
|
209
|
+
size,
|
|
210
|
+
}),
|
|
211
|
+
)}
|
|
212
|
+
style={{ zIndex }}
|
|
213
|
+
>
|
|
214
|
+
<div className={`${CLASSNAME}__overlay`} />
|
|
215
|
+
|
|
216
|
+
<HeadingLevelProvider level={2}>
|
|
217
|
+
<ThemeProvider value={undefined}>
|
|
218
|
+
<div className={`${CLASSNAME}__container`} role="dialog" aria-modal="true" {...dialogProps}>
|
|
219
|
+
<ClickAwayProvider
|
|
220
|
+
callback={!shouldPreventCloseOnClickAway && onClose}
|
|
221
|
+
childrenRefs={clickAwayRefs}
|
|
222
|
+
parentRef={rootRef}
|
|
223
|
+
>
|
|
224
|
+
<section className={`${CLASSNAME}__wrapper`} ref={wrapperRef}>
|
|
225
|
+
{(header || headerChildContent) && (
|
|
226
|
+
<header
|
|
227
|
+
{...headerChildProps}
|
|
228
|
+
className={classNames(
|
|
229
|
+
`${CLASSNAME}__header`,
|
|
230
|
+
(forceHeaderDivider || hasTopIntersection) &&
|
|
231
|
+
`${CLASSNAME}__header--has-divider`,
|
|
232
|
+
headerChildProps?.className,
|
|
233
|
+
)}
|
|
234
|
+
>
|
|
235
|
+
{header}
|
|
236
|
+
{headerChildContent}
|
|
237
|
+
</header>
|
|
238
|
+
)}
|
|
239
|
+
|
|
240
|
+
<div
|
|
241
|
+
ref={mergeRefs(contentRef, localContentRef)}
|
|
242
|
+
className={`${CLASSNAME}__content`}
|
|
243
|
+
>
|
|
244
|
+
<div
|
|
245
|
+
className={`${CLASSNAME}__sentinel ${CLASSNAME}__sentinel--top`}
|
|
246
|
+
ref={setSentinelTop}
|
|
247
|
+
/>
|
|
248
|
+
|
|
249
|
+
{content}
|
|
250
|
+
|
|
251
|
+
<div
|
|
252
|
+
className={`${CLASSNAME}__sentinel ${CLASSNAME}__sentinel--bottom`}
|
|
253
|
+
ref={setSentinelBottom}
|
|
254
|
+
/>
|
|
255
|
+
</div>
|
|
256
|
+
|
|
257
|
+
{(footer || footerChildContent) && (
|
|
258
|
+
<footer
|
|
259
|
+
{...footerChildProps}
|
|
260
|
+
className={classNames(
|
|
261
|
+
`${CLASSNAME}__footer`,
|
|
262
|
+
(forceFooterDivider || hasBottomIntersection) &&
|
|
263
|
+
`${CLASSNAME}__footer--has-divider`,
|
|
264
|
+
footerChildProps?.className,
|
|
265
|
+
)}
|
|
266
|
+
>
|
|
267
|
+
{footer}
|
|
268
|
+
{footerChildContent}
|
|
269
|
+
</footer>
|
|
270
|
+
)}
|
|
271
|
+
|
|
272
|
+
{isLoading && (
|
|
273
|
+
<div className={`${CLASSNAME}__progress-overlay`}>
|
|
274
|
+
<Progress variant={ProgressVariant.circular} />
|
|
275
|
+
</div>
|
|
276
|
+
)}
|
|
277
|
+
</section>
|
|
278
|
+
</ClickAwayProvider>
|
|
279
|
+
</div>
|
|
280
|
+
</ThemeProvider>
|
|
281
|
+
</HeadingLevelProvider>
|
|
282
|
+
</div>
|
|
282
283
|
</Portal>
|
|
283
|
-
|
|
284
|
+
) : null;
|
|
284
285
|
});
|
|
285
286
|
Dialog.displayName = COMPONENT_NAME;
|
|
286
287
|
Dialog.className = CLASSNAME;
|
|
@@ -16,7 +16,8 @@ import { useCallbackOnEscape } from '@lumx/react/hooks/useCallbackOnEscape';
|
|
|
16
16
|
import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibility';
|
|
17
17
|
import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
|
|
18
18
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
import { Portal } from '@lumx/react/utils';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Defines the props of the component.
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import React, { useRef } from 'react';
|
|
2
|
-
import { createPortal } from 'react-dom';
|
|
3
2
|
|
|
4
3
|
import classNames from 'classnames';
|
|
5
4
|
import isFunction from 'lodash/isFunction';
|
|
@@ -13,7 +12,8 @@ import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibili
|
|
|
13
12
|
import { mergeRefs } from '@lumx/react/utils/react/mergeRefs';
|
|
14
13
|
import { useTheme } from '@lumx/react/utils/theme/ThemeContext';
|
|
15
14
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
16
|
-
|
|
15
|
+
|
|
16
|
+
import { Portal } from '@lumx/react/utils';
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Defines the props of the component.
|
|
@@ -99,39 +99,40 @@ export const Notification = forwardRef<NotificationProps, HTMLDivElement>((props
|
|
|
99
99
|
return null;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
className
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
<
|
|
122
|
-
|
|
123
|
-
<div className={`${CLASSNAME}__content`}>{content}</div>
|
|
124
|
-
{hasAction && (
|
|
125
|
-
<div className={`${CLASSNAME}__action`}>
|
|
126
|
-
<Button emphasis={Emphasis.medium} theme={theme} onClick={handleCallback}>
|
|
127
|
-
<span>{actionLabel}</span>
|
|
128
|
-
</Button>
|
|
102
|
+
return (
|
|
103
|
+
<Portal enabled={usePortal}>
|
|
104
|
+
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */}
|
|
105
|
+
<div
|
|
106
|
+
ref={mergeRefs(ref, rootRef)}
|
|
107
|
+
role="alert"
|
|
108
|
+
{...forwardedProps}
|
|
109
|
+
className={classNames(
|
|
110
|
+
className,
|
|
111
|
+
handleBasicClasses({
|
|
112
|
+
color,
|
|
113
|
+
hasAction,
|
|
114
|
+
isHidden: !isOpen,
|
|
115
|
+
prefix: CLASSNAME,
|
|
116
|
+
}),
|
|
117
|
+
)}
|
|
118
|
+
onClick={onClick}
|
|
119
|
+
style={{ ...style, zIndex }}
|
|
120
|
+
>
|
|
121
|
+
<div className={`${CLASSNAME}__icon`}>
|
|
122
|
+
<Icon icon={icon} size={Size.s} />
|
|
129
123
|
</div>
|
|
130
|
-
|
|
131
|
-
|
|
124
|
+
<div className={`${CLASSNAME}__content`}>{content}</div>
|
|
125
|
+
{hasAction && (
|
|
126
|
+
<div className={`${CLASSNAME}__action`}>
|
|
127
|
+
<Button emphasis={Emphasis.medium} theme={theme} onClick={handleCallback}>
|
|
128
|
+
<span>{actionLabel}</span>
|
|
129
|
+
</Button>
|
|
130
|
+
</div>
|
|
131
|
+
)}
|
|
132
|
+
</div>
|
|
133
|
+
);
|
|
134
|
+
</Portal>
|
|
132
135
|
);
|
|
133
|
-
|
|
134
|
-
return usePortal ? <Portal>{notification}</Portal> : notification;
|
|
135
136
|
});
|
|
136
137
|
Notification.displayName = COMPONENT_NAME;
|
|
137
138
|
Notification.className = CLASSNAME;
|
|
@@ -14,7 +14,7 @@ import { skipRender } from '@lumx/react/utils/react/skipRender';
|
|
|
14
14
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
15
15
|
|
|
16
16
|
import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
|
|
17
|
-
import { Portal } from '@lumx/react/utils
|
|
17
|
+
import { Portal } from '@lumx/react/utils';
|
|
18
18
|
import { useRestoreFocusOnClose } from './useRestoreFocusOnClose';
|
|
19
19
|
import { usePopoverStyle } from './usePopoverStyle';
|
|
20
20
|
import { Elevation, FitAnchorWidth, Offset, Placement, POPOVER_ZINDEX } from './constants';
|
|
@@ -93,11 +93,6 @@ const DEFAULT_PROPS: Partial<PopoverProps> = {
|
|
|
93
93
|
zIndex: POPOVER_ZINDEX,
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
/** Method to render the popover inside a portal if usePortal is true */
|
|
97
|
-
const renderPopover = (children: ReactNode, usePortal?: boolean): any => {
|
|
98
|
-
return usePortal ? <Portal>{children}</Portal> : children;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
96
|
// Inner component (must be wrapped before export)
|
|
102
97
|
const _InnerPopover = forwardRef<PopoverProps, HTMLDivElement>((props, ref) => {
|
|
103
98
|
const {
|
|
@@ -154,39 +149,38 @@ const _InnerPopover = forwardRef<PopoverProps, HTMLDivElement>((props, ref) => {
|
|
|
154
149
|
const clickAwayRefs = useRef([popoverRef, anchorRef]);
|
|
155
150
|
const mergedRefs = useMergeRefs<HTMLDivElement>(setPopperElement, ref, popoverRef);
|
|
156
151
|
|
|
157
|
-
return isOpen
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
: null;
|
|
152
|
+
return isOpen ? (
|
|
153
|
+
<Portal enabled={usePortal}>
|
|
154
|
+
<Component
|
|
155
|
+
{...forwardedProps}
|
|
156
|
+
ref={mergedRefs}
|
|
157
|
+
className={classNames(
|
|
158
|
+
className,
|
|
159
|
+
handleBasicClasses({
|
|
160
|
+
prefix: CLASSNAME,
|
|
161
|
+
theme,
|
|
162
|
+
elevation: Math.min(elevation || 0, 5),
|
|
163
|
+
position,
|
|
164
|
+
isInitializing: !styles.popover?.transform,
|
|
165
|
+
}),
|
|
166
|
+
)}
|
|
167
|
+
style={styles.popover}
|
|
168
|
+
{...attributes.popper}
|
|
169
|
+
>
|
|
170
|
+
{unmountSentinel}
|
|
171
|
+
<ClickAwayProvider callback={closeOnClickAway && onClose} childrenRefs={clickAwayRefs}>
|
|
172
|
+
{hasArrow && (
|
|
173
|
+
<div ref={setArrowElement} className={`${CLASSNAME}__arrow`} style={styles.arrow}>
|
|
174
|
+
<svg viewBox="0 0 14 14" aria-hidden>
|
|
175
|
+
<path d="M8 3.49C7.62 2.82 6.66 2.82 6.27 3.48L.04 14 14.04 14 8 3.49Z" />
|
|
176
|
+
</svg>
|
|
177
|
+
</div>
|
|
178
|
+
)}
|
|
179
|
+
<ThemeProvider value={theme}>{children}</ThemeProvider>
|
|
180
|
+
</ClickAwayProvider>
|
|
181
|
+
</Component>
|
|
182
|
+
</Portal>
|
|
183
|
+
) : null;
|
|
190
184
|
});
|
|
191
185
|
_InnerPopover.displayName = COMPONENT_NAME;
|
|
192
186
|
|
|
@@ -14,7 +14,7 @@ import { usePopper } from '@lumx/react/hooks/usePopper';
|
|
|
14
14
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
15
15
|
|
|
16
16
|
import { ARIA_LINK_MODES, TOOLTIP_ZINDEX } from '@lumx/react/components/tooltip/constants';
|
|
17
|
-
import { Portal } from '@lumx/react/utils
|
|
17
|
+
import { Portal } from '@lumx/react/utils';
|
|
18
18
|
import { useInjectTooltipRef } from './useInjectTooltipRef';
|
|
19
19
|
import { useTooltipOpen } from './useTooltipOpen';
|
|
20
20
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { Portal } from './Portal';
|
|
4
|
+
|
|
5
|
+
const TEST_ID = 'portal-child';
|
|
6
|
+
const PortalChild = () => <div data-testid={TEST_ID}>Hello from portal</div>;
|
|
7
|
+
|
|
8
|
+
describe(Portal, () => {
|
|
9
|
+
it('should render portal content in document.body by default', () => {
|
|
10
|
+
render(
|
|
11
|
+
<Portal>
|
|
12
|
+
<PortalChild />
|
|
13
|
+
</Portal>,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const child = screen.getByTestId(TEST_ID);
|
|
17
|
+
expect(child.parentElement).toBe(document.body);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should render inline when enabled is false', () => {
|
|
21
|
+
const { container } = render(
|
|
22
|
+
<div id="wrapper">
|
|
23
|
+
<Portal enabled={false}>
|
|
24
|
+
<PortalChild />
|
|
25
|
+
</Portal>
|
|
26
|
+
</div>,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const child = screen.getByTestId(TEST_ID);
|
|
30
|
+
expect(child.parentElement).toBe(container.querySelector('#wrapper'));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { PortalContext } from './PortalProvider';
|
|
4
|
+
|
|
5
|
+
export interface PortalProps {
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Render children in a portal outside the current DOM position
|
|
12
|
+
* (defaults to `document.body` but can be customized with the PortalContextProvider)
|
|
13
|
+
*/
|
|
14
|
+
export const Portal: React.FC<PortalProps> = ({ children, enabled = true }) => {
|
|
15
|
+
const init = React.useContext(PortalContext);
|
|
16
|
+
const context = React.useMemo(
|
|
17
|
+
() => {
|
|
18
|
+
return enabled ? init() : null;
|
|
19
|
+
},
|
|
20
|
+
// Only update on 'enabled'
|
|
21
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
22
|
+
[enabled],
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
React.useLayoutEffect(() => {
|
|
26
|
+
return context?.teardown;
|
|
27
|
+
}, [context?.teardown, enabled]);
|
|
28
|
+
|
|
29
|
+
if (!context?.container) {
|
|
30
|
+
return <>{children}</>;
|
|
31
|
+
}
|
|
32
|
+
return createPortal(children, context.container);
|
|
33
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Button } from '@lumx/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { PortalProvider } from './PortalProvider';
|
|
4
|
+
import { Portal } from './Portal';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
title: 'LumX components/PortalProvider',
|
|
8
|
+
component: PortalProvider,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const initShadowDOMPortal = () => {
|
|
12
|
+
const div = document.createElement('div');
|
|
13
|
+
document.body.appendChild(div);
|
|
14
|
+
const container = div.attachShadow({ mode: 'closed' });
|
|
15
|
+
const style = document.createElement('style');
|
|
16
|
+
style.innerText = `button { color: red; }`;
|
|
17
|
+
container.appendChild(style);
|
|
18
|
+
return { container, teardown: () => div.remove() };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Demonstrate how to customize portals to render into a custom shadow root
|
|
23
|
+
*/
|
|
24
|
+
export const RenderInShadowDOM = {
|
|
25
|
+
args: { enabled: true },
|
|
26
|
+
render: ({ enabled }) => (
|
|
27
|
+
<PortalProvider value={initShadowDOMPortal}>
|
|
28
|
+
<Portal enabled={enabled}>
|
|
29
|
+
<Button>My button {!enabled && 'not'} in a shadow DOM portal</Button>
|
|
30
|
+
</Portal>
|
|
31
|
+
</PortalProvider>
|
|
32
|
+
),
|
|
33
|
+
parameters: { chromatic: { disable: true } },
|
|
34
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { PortalInit, PortalProvider } from './PortalProvider';
|
|
4
|
+
import { Portal } from './Portal';
|
|
5
|
+
|
|
6
|
+
const TEST_ID = 'portal-child';
|
|
7
|
+
const PORTAL_CONTAINER_ID = 'portal-container';
|
|
8
|
+
|
|
9
|
+
const PortalChild = () => <div data-testid={TEST_ID}>Hello from portal</div>;
|
|
10
|
+
|
|
11
|
+
describe(PortalProvider, () => {
|
|
12
|
+
it('should render portal content in the provided container', () => {
|
|
13
|
+
const portalContainer = document.createElement('div');
|
|
14
|
+
portalContainer.id = PORTAL_CONTAINER_ID;
|
|
15
|
+
document.body.appendChild(portalContainer);
|
|
16
|
+
|
|
17
|
+
const getContainer: PortalInit = () => ({ container: portalContainer });
|
|
18
|
+
|
|
19
|
+
render(
|
|
20
|
+
<PortalProvider value={getContainer}>
|
|
21
|
+
<Portal>
|
|
22
|
+
<PortalChild />
|
|
23
|
+
</Portal>
|
|
24
|
+
</PortalProvider>,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const child = screen.getByTestId(TEST_ID);
|
|
28
|
+
expect(child.parentElement).toBe(portalContainer);
|
|
29
|
+
portalContainer.remove();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should call teardown on unmount', () => {
|
|
33
|
+
const teardownMock = jest.fn();
|
|
34
|
+
const portalContainer = document.createElement('div');
|
|
35
|
+
portalContainer.id = PORTAL_CONTAINER_ID;
|
|
36
|
+
document.body.appendChild(portalContainer);
|
|
37
|
+
|
|
38
|
+
const getContainer: PortalInit = () => ({
|
|
39
|
+
container: portalContainer,
|
|
40
|
+
teardown: teardownMock,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const { unmount } = render(
|
|
44
|
+
<PortalProvider value={getContainer}>
|
|
45
|
+
<Portal>
|
|
46
|
+
<PortalChild />
|
|
47
|
+
</Portal>
|
|
48
|
+
</PortalProvider>,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(teardownMock).not.toHaveBeenCalled();
|
|
52
|
+
unmount();
|
|
53
|
+
expect(teardownMock).toHaveBeenCalledTimes(1);
|
|
54
|
+
portalContainer.remove();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should render inline if provider does not return a container', () => {
|
|
58
|
+
const getContainer: PortalInit = () => ({}); // No container
|
|
59
|
+
|
|
60
|
+
const { container } = render(
|
|
61
|
+
<div id="wrapper">
|
|
62
|
+
<PortalProvider value={getContainer}>
|
|
63
|
+
<Portal>
|
|
64
|
+
<PortalChild />
|
|
65
|
+
</Portal>
|
|
66
|
+
</PortalProvider>
|
|
67
|
+
</div>,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const child = screen.getByTestId(TEST_ID);
|
|
71
|
+
expect(child.parentElement).toBe(container.querySelector('#wrapper'));
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type Container = DocumentFragment | Element;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Portal initializing function.
|
|
7
|
+
* If it does not provide a container, the Portal children will render in classic React tree and not in a portal.
|
|
8
|
+
*/
|
|
9
|
+
export type PortalInit = () => {
|
|
10
|
+
container?: Container;
|
|
11
|
+
teardown?: () => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const PortalContext = React.createContext<PortalInit>(() => ({ container: document.body }));
|
|
15
|
+
|
|
16
|
+
export interface PortalProviderProps {
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
value: PortalInit;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
23
|
+
*/
|
|
24
|
+
export const PortalProvider: React.FC<PortalProviderProps> = PortalContext.Provider;
|
package/src/utils/index.ts
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
* WARNING: All modules exported here are exposed to NPM in '@lumx/react/utils'.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
export
|
|
5
|
+
export { ClickAwayProvider } from './ClickAwayProvider';
|
|
6
|
+
export { Portal, type PortalProps, type PortalInit, PortalProvider, type PortalProviderProps } from './Portal';
|