Skip to content

Commit af6f18a

Browse files
committed
fixup! fixup! Add controlled popover
1 parent 785822d commit af6f18a

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/components/Popover/Popover.jsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,31 @@ export const Popover = React.forwardRef((props, ref) => {
1212
const {
1313
placement,
1414
children,
15-
popoverHelperId,
15+
popoverTargetId,
1616
portalId,
1717
...restProps
1818
} = props;
1919

2020
const PopoverEl = (
2121
<>
22-
{!!popoverHelperId && <div className={styles.helper} id={popoverHelperId} popover="auto" />}
22+
{/**
23+
* This hack is needed because the default behavior of the Popover API is to place the popover into a
24+
* top-layer. It is currently not possible to position an element in the top-layer relative to a normal element.
25+
* This will create a hidden browser popover, then with CSS it will open and close the RUI popover.
26+
*/}
27+
{!!popoverTargetId && (
28+
<div
29+
className={styles.helper}
30+
id={popoverTargetId}
31+
popover="auto"
32+
/>
33+
)}
2334
<div
2435
{...transferProps(restProps)}
2536
className={classNames(
2637
styles.root,
2738
ref && styles.isRootControlled,
28-
popoverHelperId && styles.controlledPopover,
39+
popoverTargetId && styles.controlledPopover,
2940
getRootSideClassName(placement, styles),
3041
getRootAlignmentClassName(placement, styles),
3142
)}
@@ -46,7 +57,7 @@ export const Popover = React.forwardRef((props, ref) => {
4657

4758
Popover.defaultProps = {
4859
placement: 'bottom',
49-
popoverHelperId: null,
60+
popoverTargetId: null,
5061
portalId: null,
5162
};
5263

@@ -78,7 +89,7 @@ Popover.propTypes = {
7889
* This sets the ID of the internal helper element for the popover.
7990
* Assign the same ID to `popovertarget` of a trigger to make it open and close.
8091
*/
81-
popoverHelperId: PropTypes.string,
92+
popoverTargetId: PropTypes.string,
8293
/**
8394
* If set, popover is rendered in the React Portal with that ID.
8495
*/

src/components/Popover/Popover.module.scss

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@
4949
}
5050
}
5151

52-
// Controlled popover
52+
/**
53+
* Controlled popover
54+
* Hide the popover by default. This is needed because the popover is
55+
* controlled via CSS with the help of the helper popover. The popover can't
56+
* be displayed directly, because relative positioning doesn't work with
57+
* elements on the top-layer, so this CSS hack is needed.
58+
*/
5359
.controlledPopover {
5460
display: none;
5561
}
5662

63+
// Hide the popover helper element
5764
.helper {
5865
position: fixed;
5966
inset: unset;
@@ -67,6 +74,7 @@
6774
pointer-events: none;
6875
}
6976

77+
// If the popover helper is open, show the actual popover
7078
.helper:popover-open ~ .controlledPopover {
7179
display: block;
7280
}

src/components/Popover/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ React.createElement(() => {
286286

287287
## Controlled Popover
288288

289-
To make it easier to implement the popover in your app, you can set the
290-
`popoverHelperId` prop to the same ID as the `popovertarget` of a trigger.
291-
This uses the browser Popover API and will control the popover by closing it
292-
when the trigger or backdrop is pressed.
289+
Popover API can be used to control visibility of Popover component. You need to
290+
set `id` on the trigger element and matching `popoverTargetId` attribute on the
291+
Popover component. This leverages the browser's Popover API to control the
292+
popover, automatically closing it when the trigger or the backdrop is pressed.
293293

294294
```docoff-react-preview
295295
React.createElement(() => {
@@ -308,7 +308,7 @@ React.createElement(() => {
308308
label="Want to see a popover? Click me!"
309309
popovertarget="my-popover-helper"
310310
/>
311-
<Popover id="my-popover" popoverHelperId="my-popover-helper">
311+
<Popover id="my-popover" popoverTargetId="my-popover-helper">
312312
Hello there!
313313
</Popover>
314314
</PopoverWrapper>

0 commit comments

Comments
 (0)