Skip to content

Commit

Permalink
fix: popper strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
devharris7 committed Mar 22, 2021
1 parent 58265b7 commit e0abdf7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/components/Popover/Popper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';
import { usePopper } from 'react-popper';
import { popoverPlacements } from './constants';
import { popoverPlacements, popoverStrategies } from './constants';

// default arrow position in pixel from the corresponding popover edge
const DEFAULT_ARROW_POSITION = 12;
Expand Down Expand Up @@ -47,6 +47,7 @@ const Popper = ({
arrowStyles,
dts,
placement: popperPlacement,
strategy,
popoverClass,
popoverContent,
title,
Expand Down Expand Up @@ -82,7 +83,8 @@ const Popper = ({
const { styles, attributes, update } = usePopper(refElement, popperElement, {
modifiers: defaultModifiers.concat(modifiers),
placement: popperPlacement,
wrapperStyles,
strategy,
...wrapperStyles,
});

const calculatedArrowStyles = renderArrowStyles(popperPlacement, arrowStyles, popperElement);
Expand Down Expand Up @@ -123,6 +125,7 @@ Popper.propTypes = {
dts: PropTypes.string,
modifiers: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]), // eslint-disable-line react/forbid-prop-types
placement: PropTypes.oneOf(popoverPlacements),
strategy: PropTypes.oneOf(popoverStrategies),
popoverClass: PropTypes.string,
popoverContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
refElement: PropTypes.instanceOf(Element),
Expand Down
5 changes: 4 additions & 1 deletion src/components/Popover/WithRef.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import Popper from './Popper';
import { themes, popoverPlacements } from './constants';
import { themes, popoverPlacements, popoverStrategies } from './constants';
import './styles.scss';

const WithRef = ({
Expand All @@ -17,6 +17,7 @@ const WithRef = ({
modifiers,
wrapperStyles,
placement,
strategy,
isOpen,
arrowStyles,
getContainer,
Expand All @@ -39,6 +40,7 @@ const WithRef = ({
boundariesElement={boundariesElement}
arrowStyles={arrowStyles}
placement={placement}
strategy={strategy}
modifiers={modifiers}
popperRef={popperRef}
/>,
Expand All @@ -56,6 +58,7 @@ WithRef.propTypes = {
wrapperStyles: PropTypes.object, // eslint-disable-line react/forbid-prop-types
modifiers: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]), // eslint-disable-line react/forbid-prop-types
placement: PropTypes.oneOf(popoverPlacements),
strategy: PropTypes.oneOf(popoverStrategies),
popoverContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
isOpen: PropTypes.bool,
popperRef: PropTypes.func,
Expand Down
7 changes: 7 additions & 0 deletions src/components/Popover/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export const themes = ['light', 'dark', 'warn', 'error', 'info', 'success'];

const basePlacements = ['auto', 'top', 'right', 'bottom', 'left'];
export const popoverPlacements = _.flatMap(basePlacements, trigger => [trigger, `${trigger}-start`, `${trigger}-end`]);

/**
* Describes the positioning strategy to use.
* By default, it is absolute.
* If your reference element is in a fixed container, use the fixed strategy
*/
export const popoverStrategies = ['absolute', 'fixed'];
5 changes: 4 additions & 1 deletion src/components/Popover/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import { themes, popoverPlacements } from './constants';
import { themes, popoverPlacements, popoverStrategies } from './constants';
import WithRef from './WithRef';
import './styles.scss';

Expand Down Expand Up @@ -66,6 +66,7 @@ const Popover = props => {
getContainer={props.getContainer}
arrowStyles={props.arrowStyles}
placement={props.placement}
strategy={props.strategy}
modifiers={props.modifiers}
isOpen={isPopoverOpen}
popperRef={popperRef}
Expand All @@ -86,6 +87,7 @@ Popover.propTypes = {
wrapperStyles: PropTypes.object, // eslint-disable-line react/forbid-prop-types
modifiers: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]), // eslint-disable-line react/forbid-prop-types
placement: PropTypes.oneOf(popoverPlacements),
strategy: PropTypes.oneOf(popoverStrategies),
popoverContent: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
children: PropTypes.node.isRequired,
triggers: PropTypes.oneOfType([triggerPropTypes, PropTypes.arrayOf(triggerPropTypes)]),
Expand All @@ -98,6 +100,7 @@ Popover.propTypes = {
Popover.defaultProps = {
theme: 'light',
placement: 'auto',
strategy: 'absolute',
triggers: 'hover',
isOpen: false,
};
Expand Down
22 changes: 22 additions & 0 deletions www/containers/props.json
Original file line number Diff line number Diff line change
Expand Up @@ -3202,6 +3202,19 @@
"computed": false
}
},
"strategy": {
"type": {
"name": "enum",
"computed": true,
"value": "popoverStrategies"
},
"required": false,
"description": "",
"defaultValue": {
"value": "'absolute'",
"computed": false
}
},
"popoverContent": {
"type": {
"name": "union",
Expand Down Expand Up @@ -3334,6 +3347,15 @@
"required": false,
"description": ""
},
"strategy": {
"type": {
"name": "enum",
"computed": true,
"value": "popoverStrategies"
},
"required": false,
"description": ""
},
"popoverClass": {
"type": {
"name": "string"
Expand Down

0 comments on commit e0abdf7

Please sign in to comment.