Popovers
Popovers are built with https://popper.js.org/ via https://github.com/popperjs/react-popper.
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from 'react';
import { Button, Popover, PopoverHeader, PopoverBody } from 'reactstrap';
const Example = (props) => {
const [popoverOpen, setPopoverOpen] = useState(false);
const toggle = () => setPopoverOpen(!popoverOpen);
return (
<div>
<Button id="Popover1" type="button">
Launch Popover
</Button>
<Popover placement="bottom" isOpen={popoverOpen} target="Popover1" toggle={toggle}>
<PopoverHeader>Popover Title</PopoverHeader>
<PopoverBody>Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.</PopoverBody>
</Popover>
</div>
);
}
export default Example;
Properties
Popover.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
// space separated list of triggers (e.g. "click hover focus")
trigger: PropTypes.string,
// boolean to control the state of the popover
isOpen: PropTypes.bool,
// callback for toggling isOpen in the controlling component
toggle: PropTypes.func,
// boundaries for popper, can be scrollParent, window, viewport, or any DOM element
boundariesElement: PropTypes.oneOfType([PropTypes.string, DOMElement]),
target: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
DOMElement, // instanceof Element (https://developer.mozilla.org/en-US/docs/Web/API/Element)
]).isRequired,
// Where to inject the popper DOM node, default to body
container: PropTypes.oneOfType([PropTypes.string, PropTypes.func, DOMElement]),
className: PropTypes.string,
// Apply class to the popper component
popperClassName: PropTypes.string,
// Apply class to the inner-popover
innerClassName: PropTypes.string,
disabled: PropTypes.bool,
hideArrow: PropTypes.bool,
placementPrefix: PropTypes.string,
delay: PropTypes.oneOfType([
PropTypes.shape({ show: PropTypes.number, hide: PropTypes.number }),
PropTypes.number,
]),
placement: PropTypes.oneOf([
'auto',
'auto-start',
'auto-end',
'top',
'top-start',
'top-end',
'right',
'right-start',
'right-end',
'bottom',
'bottom-start',
'bottom-end',
'left',
'left-start',
'left-end',
]),
// Custom modifiers that are passed to Popper.js, see https://popper.js.org/popper-documentation.html#modifiers
modifiers: PropTypes.object,
// Whether the element the tooltip is pointing to has "position: fixed" styling. This is passed to Popper.js and
// will make the tooltip itself have "position: fixed" as well
positionFixed: PropTypes.bool,
offset: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
// Whether to show/hide the popover with a fade effect
// (default: true)
fade: PropTypes.bool,
// Whether to flip the direction of the popover if too close to
// the container edge
// (default: true)
flip: PropTypes.bool,
}
Popovers Trigger
Trigger each popover to see information about the trigger
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React from 'react';
import { Button, UncontrolledPopover, PopoverHeader, PopoverBody } from 'reactstrap';
const Example = (props) => {
return (
<div>
<Button id="PopoverFocus" type="button">
Launch Popover (Focus)
</Button>
{' '}
<Button id="PopoverClick" type="button">
Launch Popover (Click)
</Button>
{' '}
<Button id="PopoverLegacy" type="button">
Launch Popover (Legacy)
</Button>
<UncontrolledPopover trigger="focus" placement="bottom" target="PopoverFocus">
<PopoverHeader>Focus Trigger</PopoverHeader>
<PopoverBody>Focusing on the trigging element makes this popover appear. Blurring (clicking away) makes it disappear. You cannot select this text as the popover will disappear when you try.</PopoverBody>
</UncontrolledPopover>
<UncontrolledPopover trigger="click" placement="bottom" target="PopoverClick">
<PopoverHeader>Click Trigger</PopoverHeader>
<PopoverBody>Clicking on the triggering element makes this popover appear. Clicking on it again will make it disappear. You can select this text, but clicking away (somewhere other than the triggering element) will not dismiss this popover.</PopoverBody>
</UncontrolledPopover>
<UncontrolledPopover trigger="legacy" placement="bottom" target="PopoverLegacy">
<PopoverHeader>Legacy Trigger</PopoverHeader>
<PopoverBody>
Legacy is a reactstrap special trigger value (outside of bootstrap's spec/standard). Before reactstrap correctly supported click and focus, it had a hybrid which was very useful and has been brought back as trigger="legacy". One advantage of the legacy trigger is that it allows the popover text to be selected while also closing when clicking outside the triggering element and popover itself.</PopoverBody>
</UncontrolledPopover>
</div>
);
}
export default Example;
Popovers Placements
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React, { useState } from "react";
import { Button, Popover, PopoverHeader, PopoverBody } from "reactstrap";
const PopoverItem = props => {
const { id, item } = props;
const [popoverOpen, setPopoverOpen] = useState(false);
const toggle = () => setPopoverOpen(!popoverOpen);
return (
<span>
<Button
className="mr-1"
color="secondary"
id={"Popover-" + id}
type="button"
>
{item.text}
</Button>
<Popover
placement={item.placement}
isOpen={popoverOpen}
target={"Popover-" + id}
toggle={toggle}
>
<PopoverHeader>Popover Title</PopoverHeader>
<PopoverBody>
Sed posuere consectetur est at lobortis. Aenean eu leo quam.
Pellentesque ornare sem lacinia quam venenatis vestibulum.
</PopoverBody>
</Popover>
</span>
);
};
const PopoverExampleMulti = props => {
return (
<>
{[
{
placement: "top",
text: "Top"
},
{
placement: "bottom",
text: "Bottom"
},
{
placement: "left",
text: "Left"
},
{
placement: "right",
text: "Right"
}
].map((popover, i) => {
return <PopoverItem key={i} item={popover} id={i} />;
})}
</>
);
};
export default PopoverExampleMulti;
UncontrolledPopovers
/* eslint react/no-multi-comp: 0, react/prop-types: 0 */
import React from 'react';
import { Button, UncontrolledPopover, PopoverHeader, PopoverBody } from 'reactstrap';
export default function () {
return (
<div>
<Button id="UncontrolledPopover" type="button">
Launch Popover
</Button>
<UncontrolledPopover placement="bottom" target="UncontrolledPopover">
<PopoverHeader>Popover Title</PopoverHeader>
<PopoverBody>Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.</PopoverBody>
</UncontrolledPopover>
</div>
);
}
Repositioning Popovers
If you need to reposition a popover due to content changes or target placement changes, use the scheduleUpdate
function to manually reposition it. This function is exposed as a render prop for children
.
import React, { useState } from 'react';
import { Button, UncontrolledPopover, PopoverHeader, PopoverBody, Collapse } from 'reactstrap';
const PopoverContent = ({ scheduleUpdate }) => {
const [isOpen, setIsOpen] = useState(false);
return (
<>
<PopoverHeader>Schedule Update</PopoverHeader>
<PopoverBody>
<Button onClick={() => setIsOpen(!isOpen)}>Click me</Button>
<Collapse isOpen={isOpen} onEntered={scheduleUpdate} onExited={scheduleUpdate}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
</Collapse>
</PopoverBody>
</>
);
}
const Example = (props) => {
return (
<div className="text-center">
<Button id="ScheduleUpdateButton" type="button">
Open Popover
</Button>
<UncontrolledPopover trigger="click" placement="top" target="ScheduleUpdateButton">
{({ scheduleUpdate }) => (
<PopoverContent scheduleUpdate={scheduleUpdate} />
)}
</UncontrolledPopover>
</div>
);
}
export default Example;