Skip to content

Commit bb0f6f9

Browse files
committed
feat(CPopover, CTooltip): add fallbackPlacements property to allow specify the desired order of fallback placements
1 parent 6d2edc7 commit bb0f6f9

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

packages/coreui-react/src/components/popover/CPopover.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { Transition } from 'react-transition-group'
66
import { Placement } from '@popperjs/core'
77

88
import { usePopper } from '../../hooks'
9-
import { triggerPropType } from '../../props'
10-
import type { Triggers } from '../../types'
9+
import { fallbackPlacementsPropType, triggerPropType } from '../../props'
10+
import type { Placements, Triggers } from '../../types'
1111
import { isRTL } from '../../utils'
1212

1313
export interface CPopoverProps extends Omit<HTMLAttributes<HTMLDivElement>, 'title' | 'content'> {
@@ -35,6 +35,12 @@ export interface CPopoverProps extends Omit<HTMLAttributes<HTMLDivElement>, 'tit
3535
* @since 4.9.0-beta.1
3636
*/
3737
delay?: number | { show: number; hide: number }
38+
/**
39+
* Specify the desired order of fallback placements by providing a list of placements as an array. The placements should be prioritized based on preference.
40+
*
41+
* @since 4.9.0-beta.1
42+
*/
43+
fallbackPlacements?: Placements | Placements[]
3844
/**
3945
* Callback fired when the component requests to be hidden.
4046
*/
@@ -83,6 +89,7 @@ export const CPopover: FC<CPopoverProps> = ({
8389
className,
8490
content,
8591
delay = 0,
92+
fallbackPlacements = ['top', 'right', 'bottom', 'left'],
8693
offset = [0, 8],
8794
onHide,
8895
onShow,
@@ -101,6 +108,12 @@ export const CPopover: FC<CPopoverProps> = ({
101108

102109
const popperConfig = {
103110
modifiers: [
111+
{
112+
name: 'flip',
113+
options: {
114+
fallbackPlacements: fallbackPlacements,
115+
},
116+
},
104117
{
105118
name: 'offset',
106119
options: {
@@ -203,6 +216,7 @@ CPopover.propTypes = {
203216
hide: PropTypes.number.isRequired,
204217
}),
205218
]),
219+
fallbackPlacements: fallbackPlacementsPropType,
206220
offset: PropTypes.any, // TODO: find good proptype
207221
onHide: PropTypes.func,
208222
onShow: PropTypes.func,

packages/coreui-react/src/components/tooltip/CTooltip.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { Transition } from 'react-transition-group'
66
import { Placement } from '@popperjs/core'
77

88
import { usePopper } from '../../hooks'
9-
import { triggerPropType } from '../../props'
10-
import type { Triggers } from '../../types'
9+
import { fallbackPlacementsPropType, triggerPropType } from '../../props'
10+
import type { Placements, Triggers } from '../../types'
1111
import { isRTL } from '../../utils'
1212

1313
export interface CTooltipProps extends Omit<HTMLAttributes<HTMLDivElement>, 'content'> {
@@ -31,6 +31,12 @@ export interface CTooltipProps extends Omit<HTMLAttributes<HTMLDivElement>, 'con
3131
* @since 4.9.0-beta.1
3232
*/
3333
delay?: number | { show: number; hide: number }
34+
/**
35+
* Specify the desired order of fallback placements by providing a list of placements as an array. The placements should be prioritized based on preference.
36+
*
37+
* @since 4.9.0-beta.1
38+
*/
39+
fallbackPlacements?: Placements | Placements[]
3440
/**
3541
* Offset of the tooltip relative to its target.
3642
*/
@@ -79,6 +85,7 @@ export const CTooltip: FC<CTooltipProps> = ({
7985
className,
8086
content,
8187
delay = 0,
88+
fallbackPlacements = ['top', 'right', 'bottom', 'left'],
8289
offset = [0, 6],
8390
onHide,
8491
onShow,
@@ -96,6 +103,12 @@ export const CTooltip: FC<CTooltipProps> = ({
96103

97104
const popperConfig = {
98105
modifiers: [
106+
{
107+
name: 'flip',
108+
options: {
109+
fallbackPlacements: fallbackPlacements,
110+
},
111+
},
99112
{
100113
name: 'offset',
101114
options: {
@@ -195,6 +208,7 @@ CTooltip.propTypes = {
195208
hide: PropTypes.number.isRequired,
196209
}),
197210
]),
211+
fallbackPlacements: fallbackPlacementsPropType,
198212
offset: PropTypes.any, // TODO: find good proptype
199213
onHide: PropTypes.func,
200214
onShow: PropTypes.func,

packages/coreui-react/src/props.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export const colorPropType = PropTypes.oneOfType([
1616
PropTypes.string,
1717
])
1818

19+
export const fallbackPlacementsPropType = PropTypes.oneOfType([
20+
PropTypes.arrayOf(PropTypes.oneOf<Placements>(['top', 'bottom', 'right', 'left']).isRequired),
21+
PropTypes.oneOf<Placements>(['top', 'bottom', 'right', 'left']),
22+
])
23+
1924
export const placementPropType = PropTypes.oneOf<Placements>([
2025
'auto',
2126
'auto-start',

0 commit comments

Comments
 (0)