Skip to content

Commit a18aa4a

Browse files
authored
HELP-7947 popover trigger refs (#330)
* added trigger props to Container component instead of trying to clone it to children * 3.2.35
1 parent c92ab50 commit a18aa4a

File tree

4 files changed

+51
-48
lines changed

4 files changed

+51
-48
lines changed

lib/components/Popover/Popover.stories.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Box from "../Box";
55
import Grid from "../Grid";
66
import Flex from "../Flex";
77
import StyledLink from "../StyledLink";
8+
import Spacer from "../Spacer";
89

910
export default {
1011
title: "Components/Popover",
@@ -95,20 +96,22 @@ export const textAlignment = () => (
9596
);
9697

9798
export const DisplayInlineBlock = () => (
98-
<>
99-
<Popover text="Description that explains child element" inlineBlock>
100-
<Button>Inline Block Set</Button>
101-
</Popover>
102-
<Popover text="Description that explains child element" inlineBlock>
103-
<Button>Inline Block Set</Button>
104-
</Popover>
105-
<Popover text="Description that explains child element">
106-
<Button>Not Inline</Button>
107-
</Popover>
108-
<Popover direction="top" text="Description that explains child element">
109-
<Button>Not Inline</Button>
110-
</Popover>
111-
</>
99+
<Flex flexDirection="column">
100+
<Spacer mt="r">
101+
<Popover text="Description that explains child element" inlineBlock>
102+
<Button>Inline Block Set</Button>
103+
</Popover>
104+
<Popover text="Description that explains child element" inlineBlock>
105+
<Button>Inline Block Set</Button>
106+
</Popover>
107+
<Popover text="Because it is display: block, the triggering container is wider than the button">
108+
<Button>Not Inline</Button>
109+
</Popover>
110+
<Popover direction="top" text="Description that explains child element">
111+
<Button>Not Inline</Button>
112+
</Popover>
113+
</Spacer>
114+
</Flex>
112115
);
113116

114117
export const tooltipExample = () => (

lib/components/Popover/index.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { cloneElement, useState } from "react";
1+
import React, { useState } from "react";
22
import {
33
useFloating,
44
autoUpdate,
@@ -18,12 +18,12 @@ import themeGet from "@styled-system/theme-get";
1818
import styled from "styled-components";
1919
import Icon from "../Icon";
2020
import { PropTypes } from "prop-types";
21-
import Box from "../Box";
2221
import {
2322
getFloatingUiRootElement,
2423
getFloatingUiZIndex,
2524
isRenderedInReactSelectMenu
2625
} from "../../utils/floatingUiHelpers";
26+
import { layout, space } from "styled-system";
2727

2828
const DIRECTIONS_MAP = {
2929
topLeft: "top-start",
@@ -36,6 +36,15 @@ const DIRECTIONS_MAP = {
3636
bottomRight: "bottom-end"
3737
};
3838

39+
const Container = styled.div`
40+
${space}
41+
${layout}
42+
43+
display: ${(props) =>
44+
props.inlineBlock ? "inline-block !important" : "block !important"};
45+
position: relative;
46+
`;
47+
3948
const TooltipControl = styled.div`
4049
border: none;
4150
background: none;
@@ -267,6 +276,7 @@ export default function Popover({
267276
enableSelectAll,
268277
variant,
269278
ariaLabel,
279+
inlineBlock,
270280
...props
271281
}) {
272282
const [visible, setVisible] = useState(false);
@@ -310,32 +320,6 @@ export default function Popover({
310320
tabIndex: "0"
311321
};
312322

313-
let triggerComponent = null;
314-
315-
if (variant === "tooltip") {
316-
triggerComponent = (
317-
<TooltipControl {...triggerProps} active={visible} tabIndex="0">
318-
<Icon
319-
transform="grow-4"
320-
icon={["fas", "question-circle"]}
321-
fontSize="2"
322-
/>
323-
</TooltipControl>
324-
);
325-
} else if (children?.type?.$$typeof === Symbol.for("react.forward_ref")) {
326-
triggerComponent = cloneElement(children, triggerProps);
327-
} else {
328-
triggerComponent = (
329-
<div
330-
{...triggerProps}
331-
style={{ display: "inline-block" }}
332-
data-testid={`${children?.props?.["data-testid"] || "popover"}-trigger`}
333-
>
334-
{children}
335-
</div>
336-
);
337-
}
338-
339323
const directionClass =
340324
context.placement === DIRECTIONS_MAP[direction]
341325
? direction
@@ -363,7 +347,23 @@ export default function Popover({
363347
const containsLinks = refs.floating?.current?.querySelectorAll("a").length;
364348

365349
return (
366-
<Box {...props} aria-describedby={context.floatingId}>
350+
<Container
351+
inlineBlock={inlineBlock}
352+
data-testid={`${children?.props?.["data-testid"] || "popover"}-trigger`}
353+
{...props}
354+
{...triggerProps}
355+
aria-describedby={context.floatingId}
356+
>
357+
{variant === "tooltip" && (
358+
<TooltipControl {...triggerProps} active={visible} tabIndex="0">
359+
<Icon
360+
transform="grow-4"
361+
icon={["fas", "question-circle"]}
362+
fontSize="2"
363+
/>
364+
</TooltipControl>
365+
)}
366+
367367
{text &&
368368
(visible ? (
369369
<FloatingPortal
@@ -395,8 +395,8 @@ export default function Popover({
395395
**/
396396
))}
397397

398-
{triggerComponent}
399-
</Box>
398+
{children}
399+
</Container>
400400
);
401401
}
402402

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.34",
3+
"version": "3.2.35",
44
"engines": {
55
"node": "20.12.2"
66
},

0 commit comments

Comments
 (0)