Skip to content

Commit

Permalink
added trigger
Browse files Browse the repository at this point in the history
 props to Container component instead of trying to clone it to children
  • Loading branch information
KitoC committed Aug 23, 2024
1 parent c92ab50 commit 4130c27
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
31 changes: 17 additions & 14 deletions lib/components/Popover/Popover.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Box from "../Box";
import Grid from "../Grid";
import Flex from "../Flex";
import StyledLink from "../StyledLink";
import Spacer from "../Spacer";

export default {
title: "Components/Popover",
Expand Down Expand Up @@ -95,20 +96,22 @@ export const textAlignment = () => (
);

export const DisplayInlineBlock = () => (
<>
<Popover text="Description that explains child element" inlineBlock>
<Button>Inline Block Set</Button>
</Popover>
<Popover text="Description that explains child element" inlineBlock>
<Button>Inline Block Set</Button>
</Popover>
<Popover text="Description that explains child element">
<Button>Not Inline</Button>
</Popover>
<Popover direction="top" text="Description that explains child element">
<Button>Not Inline</Button>
</Popover>
</>
<Flex flexDirection="column">
<Spacer mt="r">
<Popover text="Description that explains child element" inlineBlock>
<Button>Inline Block Set</Button>
</Popover>
<Popover text="Description that explains child element" inlineBlock>
<Button>Inline Block Set</Button>
</Popover>
<Popover text="Because it is display: block, the triggering container is wider than the button">
<Button>Not Inline</Button>
</Popover>
<Popover direction="top" text="Description that explains child element">
<Button>Not Inline</Button>
</Popover>
</Spacer>
</Flex>
);

export const tooltipExample = () => (
Expand Down
62 changes: 31 additions & 31 deletions lib/components/Popover/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { cloneElement, useState } from "react";
import React, { useState } from "react";
import {
useFloating,
autoUpdate,
Expand All @@ -18,12 +18,12 @@ import themeGet from "@styled-system/theme-get";
import styled from "styled-components";
import Icon from "../Icon";
import { PropTypes } from "prop-types";
import Box from "../Box";
import {
getFloatingUiRootElement,
getFloatingUiZIndex,
isRenderedInReactSelectMenu
} from "../../utils/floatingUiHelpers";
import { layout, space } from "styled-system";

const DIRECTIONS_MAP = {
topLeft: "top-start",
Expand All @@ -36,6 +36,15 @@ const DIRECTIONS_MAP = {
bottomRight: "bottom-end"
};

const Container = styled.div`
${space}
${layout}
display: ${(props) =>
props.inlineBlock ? "inline-block !important" : "block !important"};
position: relative;
`;

const TooltipControl = styled.div`
border: none;
background: none;
Expand Down Expand Up @@ -267,6 +276,7 @@ export default function Popover({
enableSelectAll,
variant,
ariaLabel,
inlineBlock,
...props
}) {
const [visible, setVisible] = useState(false);
Expand Down Expand Up @@ -310,32 +320,6 @@ export default function Popover({
tabIndex: "0"
};

let triggerComponent = null;

if (variant === "tooltip") {
triggerComponent = (
<TooltipControl {...triggerProps} active={visible} tabIndex="0">
<Icon
transform="grow-4"
icon={["fas", "question-circle"]}
fontSize="2"
/>
</TooltipControl>
);
} else if (children?.type?.$$typeof === Symbol.for("react.forward_ref")) {
triggerComponent = cloneElement(children, triggerProps);
} else {
triggerComponent = (
<div
{...triggerProps}
style={{ display: "inline-block" }}
data-testid={`${children?.props?.["data-testid"] || "popover"}-trigger`}
>
{children}
</div>
);
}

const directionClass =
context.placement === DIRECTIONS_MAP[direction]
? direction
Expand Down Expand Up @@ -363,7 +347,23 @@ export default function Popover({
const containsLinks = refs.floating?.current?.querySelectorAll("a").length;

return (
<Box {...props} aria-describedby={context.floatingId}>
<Container
inlineBlock={inlineBlock}
data-testid={`${children?.props?.["data-testid"] || "popover"}-trigger`}
{...props}
{...triggerProps}
aria-describedby={context.floatingId}
>
{variant === "tooltip" && (
<TooltipControl {...triggerProps} active={visible} tabIndex="0">
<Icon
transform="grow-4"
icon={["fas", "question-circle"]}
fontSize="2"
/>
</TooltipControl>
)}

{text &&
(visible ? (
<FloatingPortal
Expand Down Expand Up @@ -395,8 +395,8 @@ export default function Popover({
**/
))}

{triggerComponent}
</Box>
{children}
</Container>
);
}

Expand Down

0 comments on commit 4130c27

Please sign in to comment.