Skip to content

Commit e6e8ad7

Browse files
committed
refactor: improve RiPopover API with trigger and className props for better extensibility
Refactor RiPopover to replace `button` with `trigger` and className props while maintaining backwards compatibility. simplify RiPopover trigger rendering with standalone prop, so the trigger can be any custom element that can receive `ref` and pass to it DOM element
1 parent 191540f commit e6e8ad7

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

redisinsight/ui/src/components/base/popover/RiPopover.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const RiPopover = ({
1818
panelClassName,
1919
className,
2020
maxWidth = '100%',
21+
standalone = false,
2122
...props
2223
}: RiPopoverProps) => {
2324
// Warn if both button and trigger are provided
@@ -41,15 +42,20 @@ export const RiPopover = ({
4142
const activeClassName = className ?? panelClassName
4243

4344
// Render trigger element
44-
// For new API (trigger): React elements render directly, scalars wrap in span
45-
// For old API (button): Always wrap in span (backwards compatibility)
46-
const triggerElement =
47-
trigger !== undefined && React.isValidElement(activeTrigger) ? (
48-
activeTrigger
49-
) : (
50-
<span className={anchorClassName}>{activeTrigger}</span>
51-
)
45+
// If standalone is true, the trigger will be standalone and will not be wrapped in a span
46+
// for this to work properly, ether base trigger element is `div`, `span` etc. (base dom element)
47+
// or a component that forwards ref
48+
const triggerElement = standalone ? (
49+
activeTrigger
50+
) : (
51+
<span className={anchorClassName}>{activeTrigger}</span>
52+
)
5253

54+
const placement =
55+
anchorPosition && anchorPositionMap[anchorPosition]?.placement
56+
const align = anchorPosition && anchorPositionMap[anchorPosition]?.align
57+
// TODO: maybe use wrapped popover instead of inline style?!
58+
const padding = panelPaddingSize && panelPaddingSizeMap[panelPaddingSize]
5359
return (
5460
<Popover
5561
{...props}
@@ -66,11 +72,11 @@ export const RiPopover = ({
6672
className={activeClassName}
6773
maxWidth={maxWidth}
6874
style={{
69-
padding: panelPaddingSize && panelPaddingSizeMap[panelPaddingSize],
75+
padding,
7076
}}
7177
autoFocus={ownFocus}
72-
placement={anchorPosition && anchorPositionMap[anchorPosition]?.placement}
73-
align={anchorPosition && anchorPositionMap[anchorPosition]?.align}
78+
placement={placement}
79+
align={align}
7480
>
7581
{triggerElement}
7682
</Popover>

redisinsight/ui/src/components/base/popover/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ export type RiPopoverProps = Omit<
2727
anchorPosition?: AnchorPosition
2828
panelPaddingSize?: PanelPaddingSize
2929
anchorClassName?: string
30-
/* @deprecated - use @see{className} - this is popover content wrapper class name */
30+
/* @deprecated - use @see {@link className} - this is popover content wrapper class name */
3131
panelClassName?: string
32-
// new preferred prop for popover content wrapper class name (optional)
32+
/* new preferred prop for popover content wrapper class name (optional) */
3333
className?: string
3434
'data-testid'?: string
35+
/* if true, the trigger will be standalone and will not be wrapped in a span */
36+
standalone?: boolean
3537
}

0 commit comments

Comments
 (0)