Skip to content

Commit

Permalink
Merge pull request #709 from strapi/enh/add-popover-placement
Browse files Browse the repository at this point in the history
Add placement props to Popover component
  • Loading branch information
HichamELBSI authored Oct 3, 2022
2 parents 67a281f + 04a3f73 commit 8c62558
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
34 changes: 32 additions & 2 deletions packages/strapi-design-system/src/Popover/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import { Portal } from '../Portal';

import { useIntersection } from '../helpers/useIntersection';

export const POPOVER_PLACEMENTS = [
'top',
'top-start',
'top-end',
'right',
'right-start',
'right-end',
'bottom',
'bottom-start',
'bottom-end',
'left',
'left-start',
'left-end',
];

const PopoverWrapper = styled(Box)`
box-shadow: ${({ theme }) => theme.shadows.filterShadow};
z-index: 4;
Expand Down Expand Up @@ -37,12 +52,22 @@ const PopoverScrollable = styled(Box)`
}
`;

const PopoverContent = ({ source, children, spacing, fullWidth, onReachEnd, intersectionId, centered, ...props }) => {
const PopoverContent = ({
source,
children,
spacing,
fullWidth,
placement,
onReachEnd,
intersectionId,
centered,
...props
}) => {
const popoverRef = React.useRef(null);
const [width, setWidth] = React.useState(undefined);
const { x, y, reference, floating, strategy } = useFloating({
strategy: 'fixed',
placement: centered ? 'bottom' : 'bottom-start',
placement: centered ? 'bottom' : placement,
middleware: [
offset({
mainAxis: spacing,
Expand Down Expand Up @@ -102,6 +127,7 @@ const popoverDefaultProps = {
intersectionId: undefined,
onReachEnd: undefined,
centered: false,
placement: 'bottom-start',
};

const popoverProps = {
Expand All @@ -122,6 +148,10 @@ const popoverProps = {
* The callback invoked after a scroll to the bottom of the popover content.
*/
onReachEnd: PropTypes.func,
/**
* The popover position
*/
placement: PropTypes.oneOf(POPOVER_PLACEMENTS),
/**
* A React ref. Used to defined the position of the popover.
*/
Expand Down
7 changes: 5 additions & 2 deletions packages/strapi-design-system/src/SimpleMenu/SimpleMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Typography } from '../Typography';
import { Box } from '../Box';
import { Flex } from '../Flex';
import { Button } from '../Button';
import { Popover } from '../Popover';
import { Popover, POPOVER_PLACEMENTS } from '../Popover';
import { getOptionStyle } from './utils';
import { useId } from '../helpers/useId';
import { KeyboardKeys } from '../helpers/keyboardKeys';
Expand Down Expand Up @@ -97,6 +97,7 @@ export const SimpleMenu = ({
onOpen = () => {},
onClose = () => {},
size,
popoverPlacement,
...props
}) => {
const menuButtonRef = useRef();
Expand Down Expand Up @@ -211,7 +212,7 @@ export const SimpleMenu = ({
{label}
</Component>
{visible && (
<Popover onBlur={handleBlur} source={menuButtonRef} spacing={4}>
<Popover onBlur={handleBlur} placement={popoverPlacement} source={menuButtonRef} spacing={4}>
<Box role="menu" as="ul" padding={1} id={menuId}>
{childrenClone}
</Box>
Expand All @@ -228,6 +229,7 @@ SimpleMenu.defaultProps = {
SimpleMenu.displayName = 'SimpleMenu';

SimpleMenu.defaultProps = {
popoverPlacement: 'bottom-start',
size: 'M',
};

Expand All @@ -238,6 +240,7 @@ SimpleMenu.propTypes = {
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.element]).isRequired,
onClose: PropTypes.func,
onOpen: PropTypes.func,
popoverPlacement: PropTypes.oneOf(POPOVER_PLACEMENTS),

/**
* Size of the trigger button.
Expand Down
7 changes: 5 additions & 2 deletions packages/strapi-design-system/src/v2/SimpleMenu/SimpleMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Box } from '../../Box';
import { Flex } from '../../Flex';
import { Button } from '../../Button';
import { BaseLink } from '../../BaseLink';
import { Popover } from '../../Popover';
import { Popover, POPOVER_PLACEMENTS } from '../../Popover';
import { getOptionStyle } from './utils';
import { useId } from '../../helpers/useId';
import { KeyboardKeys } from '../../helpers/keyboardKeys';
Expand Down Expand Up @@ -97,6 +97,7 @@ export const SimpleMenu = ({
onOpen = () => {},
onClose = () => {},
size,
popoverPlacement,
...props
}) => {
const menuButtonRef = useRef();
Expand Down Expand Up @@ -211,7 +212,7 @@ export const SimpleMenu = ({
{label}
</Component>
{visible && (
<Popover onBlur={handleBlur} source={menuButtonRef} spacing={4}>
<Popover onBlur={handleBlur} placement={popoverPlacement} source={menuButtonRef} spacing={4}>
<Box role="menu" as="ul" padding={1} id={menuId}>
{childrenClone}
</Box>
Expand All @@ -228,6 +229,7 @@ SimpleMenu.defaultProps = {
SimpleMenu.displayName = 'SimpleMenu';

SimpleMenu.defaultProps = {
popoverPlacement: 'bottom-start',
size: 'M',
};

Expand All @@ -238,6 +240,7 @@ SimpleMenu.propTypes = {
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.element]).isRequired,
onClose: PropTypes.func,
onOpen: PropTypes.func,
popoverPlacement: PropTypes.oneOf(POPOVER_PLACEMENTS),

/**
* Size of the trigger button.
Expand Down

0 comments on commit 8c62558

Please sign in to comment.