Skip to content

Commit 1f871fb

Browse files
authored
HELP-8042 slowness due to floating UI (#334)
1 parent bf81a58 commit 1f871fb

File tree

4 files changed

+145
-69
lines changed

4 files changed

+145
-69
lines changed

lib/components/ActionsMenu/index.js

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ const StyledActionsMenuContainer = styled.div`
6868
opacity: 0;
6969
visibility: hidden;
7070
71+
&.hack-for-legacy-tests {
72+
position: absolute;
73+
pointer-events: none;
74+
opacity: 0;
75+
visibility: hidden;
76+
height: 0;
77+
width: 0;
78+
padding: 0;
79+
}
80+
7181
&.visible {
7282
visibility: visible;
7383
opacity: 1;
@@ -288,19 +298,31 @@ export const ActionsMenuBody = ({
288298
const triggerRef = useMergeRefs([actionMenu.refs.setReference, childrenRef]);
289299
const ref = useMergeRefs([actionMenu.refs.setFloating]);
290300

291-
const triggerProps = {
292-
ariaLabel,
293-
"aria-label": ariaLabel,
294-
onFocus: onTriggerFocus,
295-
id,
296-
...actionMenu.getReferenceProps({
297-
...props,
298-
onClick: onToggle,
299-
ref: triggerRef,
300-
"data-state": actionMenu.open ? "open" : "closed",
301-
"data-testid": dataTestId
302-
})
303-
};
301+
const triggerProps = useMemo(
302+
() => ({
303+
ariaLabel,
304+
"aria-label": ariaLabel,
305+
onFocus: onTriggerFocus,
306+
id,
307+
...actionMenu.getReferenceProps({
308+
...props,
309+
onClick: onToggle,
310+
ref: triggerRef,
311+
"data-state": actionMenu.open ? "open" : "closed",
312+
"data-testid": dataTestId
313+
})
314+
}),
315+
[
316+
ariaLabel,
317+
onTriggerFocus,
318+
id,
319+
actionMenu,
320+
onToggle,
321+
props,
322+
triggerRef,
323+
dataTestId
324+
]
325+
);
304326

305327
let triggerComponent = (
306328
<Control {...triggerProps}>
@@ -328,25 +350,21 @@ export const ActionsMenuBody = ({
328350
[closeOnClick, closeMenu, id, actionMenu]
329351
);
330352

331-
const visible = actionMenu.context.open;
353+
const style = useMemo(
354+
() => ({
355+
...actionMenu.floatingStyles,
356+
zIndex: getFloatingUiZIndex(actionMenu.refs.reference)
357+
}),
358+
[actionMenu.floatingStyles, actionMenu.refs.reference]
359+
);
332360

333-
const ActionMenuContent = (
334-
<StyledActionsMenuContainer
335-
ref={ref}
336-
style={{
337-
...actionMenu.floatingStyles,
338-
zIndex: getFloatingUiZIndex(actionMenu.refs.reference)
339-
}}
340-
aria-labelledby={actionMenu.labelId}
341-
{...actionMenu.getFloatingProps(props)}
342-
className={`actionMenu-content ${visible ? "visible" : ""}`}
343-
aria-hidden={visible ? "false" : "true"}
344-
data-testid={`${dataTestId}__menu`}
345-
>
346-
<Menu menuWidth={menuWidth} isOpen={toggleState}>
347-
{children}
348-
</Menu>
349-
</StyledActionsMenuContainer>
361+
const menuDataTestId = useMemo(() => `${dataTestId}__menu`, [dataTestId]);
362+
363+
const { getFloatingProps } = actionMenu;
364+
365+
const floatingProps = useMemo(
366+
() => getFloatingProps(props),
367+
[getFloatingProps, props]
350368
);
351369

352370
const component = (
@@ -359,7 +377,19 @@ export const ActionsMenuBody = ({
359377
root={getFloatingUiRootElement(actionMenu.refs.reference)}
360378
>
361379
<FloatingFocusManager context={actionMenu.context} modal={true}>
362-
{ActionMenuContent}
380+
<StyledActionsMenuContainer
381+
aria-labelledby={actionMenu.labelId}
382+
data-testid={menuDataTestId}
383+
{...floatingProps}
384+
style={style}
385+
className="actionMenu-content visible"
386+
aria-hidden="false"
387+
ref={ref}
388+
>
389+
<Menu menuWidth={menuWidth} isOpen={toggleState}>
390+
{children}
391+
</Menu>
392+
</StyledActionsMenuContainer>
363393
</FloatingFocusManager>
364394
</FloatingPortal>
365395
) : (
@@ -369,7 +399,15 @@ export const ActionsMenuBody = ({
369399
but in a hidden state ensures that tests pass. * Ideally, we would
370400
update all the tests in teamform-app-ui to open the ActionsMenu *
371401
before assertion. **/
372-
ActionMenuContent
402+
<StyledActionsMenuContainer
403+
aria-labelledby={actionMenu.labelId}
404+
data-testid={menuDataTestId}
405+
className="actionMenu-content hack-for-legacy-tests"
406+
>
407+
<Menu menuWidth={menuWidth} isOpen={toggleState}>
408+
{children}
409+
</Menu>
410+
</StyledActionsMenuContainer>
373411
)}
374412
</Wrapper>
375413
</ActionMenuContext.Provider>

lib/components/Popover/index.js

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useMemo } from "react";
22
import {
33
useFloating,
44
autoUpdate,
@@ -79,9 +79,15 @@ const StyledPopover = styled.div`
7979
box-shadow: ${themeGet("shadows.boxDefault")};
8080
user-select: ${(props) => (props.enableSelectAll ? "all" : "auto")};
8181
82-
pointer-events: none;
83-
opacity: 0;
84-
visibility: hidden;
82+
&.hack-for-legacy-tests {
83+
position: absolute;
84+
pointer-events: none;
85+
opacity: 0;
86+
visibility: hidden;
87+
height: 0;
88+
width: 0;
89+
padding: 0;
90+
}
8591
8692
&.visible {
8793
opacity: 1;
@@ -312,37 +318,42 @@ export default function Popover({
312318
role
313319
]);
314320

315-
const triggerProps = {
316-
...getReferenceProps({ ref: refs.setReference }),
317-
tabIndex: "0"
318-
};
321+
const triggerProps = useMemo(
322+
() => ({
323+
...getReferenceProps({ ref: refs.setReference }),
324+
tabIndex: "0"
325+
}),
326+
[getReferenceProps, refs.setReference]
327+
);
319328

320-
const directionClass =
321-
context.placement === DIRECTIONS_MAP[direction]
322-
? direction
323-
: context.placement;
329+
const directionClass = useMemo(
330+
() =>
331+
context.placement === DIRECTIONS_MAP[direction]
332+
? direction
333+
: context.placement,
334+
[context.placement, direction]
335+
);
324336

325-
const Popover = (
326-
<StyledPopover
327-
textAlign={textAlign}
328-
width={width}
329-
enableSelectAll={enableSelectAll}
330-
className={`Tooltip popover ${
331-
visible ? "visible" : ""
332-
} ${directionClass}`}
333-
ref={refs.setFloating}
334-
style={{
335-
...floatingStyles,
336-
zIndex: getFloatingUiZIndex(context.refs.reference)
337-
}}
338-
{...getFloatingProps()}
339-
ariaLabel={ariaLabel}
340-
>
341-
{text}
342-
</StyledPopover>
337+
const style = useMemo(
338+
() => ({
339+
...floatingStyles,
340+
zIndex: getFloatingUiZIndex(context.refs.reference)
341+
}),
342+
[floatingStyles, context.refs.reference]
343343
);
344+
344345
const containsLinks = refs.floating?.current?.querySelectorAll("a").length;
345346

347+
const visiblePopoverClassName = useMemo(
348+
() => `Tooltip popover visible ${directionClass}`,
349+
[directionClass]
350+
);
351+
352+
const floatingProps = useMemo(
353+
() => getFloatingProps(props),
354+
[getFloatingProps, props]
355+
);
356+
346357
return (
347358
<Container
348359
inlineBlock={inlineBlock}
@@ -374,21 +385,48 @@ export default function Popover({
374385
isRenderedInReactSelectMenu(context.refs.reference) && -1
375386
}
376387
>
377-
{Popover}
388+
<StyledPopover
389+
className={visiblePopoverClassName}
390+
ref={refs.setFloating}
391+
textAlign={textAlign}
392+
width={width}
393+
enableSelectAll={enableSelectAll}
394+
ariaLabel={ariaLabel}
395+
style={style}
396+
{...floatingProps}
397+
>
398+
{text}
399+
</StyledPopover>
378400
</FloatingFocusManager>
379401
) : (
380-
Popover
402+
<StyledPopover
403+
className={visiblePopoverClassName}
404+
ref={refs.setFloating}
405+
textAlign={textAlign}
406+
width={width}
407+
enableSelectAll={enableSelectAll}
408+
ariaLabel={ariaLabel}
409+
style={style}
410+
{...floatingProps}
411+
>
412+
{text}
413+
</StyledPopover>
381414
)}
382415
</FloatingPortal>
383416
) : (
384-
Popover
385417
/*
386418
* HACK: Fixing all the broken tests in teamform-app-ui is too time consuming
387419
* right this moment with a lot of the tests asserting against Popover items.
388420
* Rendering the markup even when closed but in a hidden state ensures that tests pass.
389421
* Ideally, we would update all the tests in teamform-app-ui to open the Popover
390422
* before assertion.
391423
**/
424+
<StyledPopover
425+
ariaLabel={ariaLabel}
426+
className="Tooltip popover hack-for-legacy-tests"
427+
>
428+
{text}
429+
</StyledPopover>
392430
))}
393431

394432
{children}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "orcs-design-system",
3-
"version": "3.2.36",
3+
"version": "3.2.37",
44
"engines": {
55
"node": "20.12.2"
66
},

0 commit comments

Comments
 (0)