= (args) => {
onClick={handleClick}
backgroundColor={BackgroundColor.primaryAlternative}
style={{ width: 200, height: 200 }}
- color={Color.primaryInverse}
+ color={TextColor.primaryInverse}
as="button"
>
Click to toggle popover
@@ -751,7 +752,7 @@ export const OnPressEscKey: StoryFn = (args) => {
onClick={handleClick}
backgroundColor={BackgroundColor.primaryAlternative}
style={{ width: 200, height: 200 }}
- color={Color.primaryInverse}
+ color={TextColor.primaryInverse}
as="button"
>
Click to open
@@ -835,7 +836,7 @@ export const MouseEventDemo: StoryFn = (args) => {
onMouseLeave={handleMouseLeave}
backgroundColor={BackgroundColor.primaryAlternative}
style={{ width: 200, height: 200 }}
- color={Color.primaryInverse}
+ color={TextColor.primaryInverse}
>
Hover
@@ -872,7 +873,7 @@ export const OnFocusBlur: StoryFn = (args) => {
onBlur={handleClose}
backgroundColor={BackgroundColor.primaryAlternative}
style={{ width: 200, height: 200 }}
- color={Color.primaryInverse}
+ color={TextColor.primaryInverse}
as="button"
>
Focus to open
diff --git a/ui/components/component-library/popover/popover.tsx b/ui/components/component-library/popover/popover.tsx
index 992f48f75..1ad1bd02a 100644
--- a/ui/components/component-library/popover/popover.tsx
+++ b/ui/components/component-library/popover/popover.tsx
@@ -10,124 +10,139 @@ import {
Display,
JustifyContent,
} from '../../../helpers/constants/design-system';
-import Box from '../../ui/box/box';
-import { PopoverPosition, PopoverProps, PopoverRole } from '.';
-export const Popover = ({
- children,
- className = '',
- position = PopoverPosition.Auto,
- role = PopoverRole.Tooltip,
- hasArrow = false,
- matchWidth,
- preventOverflow = false,
- offset = [0, 8],
- flip = false,
- referenceHidden = true,
- referenceElement,
- isOpen,
- title,
- isPortal = false,
- arrowProps,
- onPressEscKey,
- ...props
-}: PopoverProps) => {
- const [popperElement, setPopperElement] = useState(null);
- const [arrowElement, setArrowElement] = useState(null);
+import { Box } from '..';
+import type { BoxProps, PolymorphicRef } from '../box';
- // Define Popper options
- const { styles, attributes } = usePopper(referenceElement, popperElement, {
- placement: position,
- modifiers: [
- {
- name: 'preventOverflow',
- enabled: position === PopoverPosition.Auto ? true : preventOverflow,
- },
- {
- name: 'flip',
- enabled: position === PopoverPosition.Auto ? true : flip,
- },
- {
- name: 'arrow',
- enabled: hasArrow,
- options: {
- element: arrowElement,
- },
- },
- {
- name: 'offset',
- options: {
- offset,
- },
- },
- ],
- });
+import {
+ PopoverProps,
+ PopoverComponent,
+ PopoverPosition,
+ PopoverRole,
+} from './popover.types';
- // Define width to match reference element or auto
- const contentStyle = {
- width: matchWidth ? referenceElement?.clientWidth : 'auto',
- };
+export const Popover: PopoverComponent = React.forwardRef(
+ (
+ {
+ children,
+ className = '',
+ position = PopoverPosition.Auto,
+ role = PopoverRole.Tooltip,
+ hasArrow = false,
+ matchWidth,
+ preventOverflow = false,
+ offset = [0, 8],
+ flip = false,
+ referenceHidden = true,
+ referenceElement,
+ isOpen,
+ title,
+ isPortal = false,
+ arrowProps,
+ onPressEscKey,
+ ...props
+ }: PopoverProps,
+ ref?: PolymorphicRef,
+ ) => {
+ const [popperElement, setPopperElement] = useState(
+ null,
+ );
+ const [arrowElement, setArrowElement] = useState(null);
- // Esc key press
- const handleEscKey = (event: KeyboardEvent) => {
- if (event.key === 'Escape') {
- // Close the popover when the "Esc" key is pressed
- if (onPressEscKey) {
- onPressEscKey();
- }
- }
- };
-
- useEffect(() => {
- document.addEventListener('keydown', handleEscKey);
-
- return () => {
- document.removeEventListener('keydown', handleEscKey);
- };
- }, [onPressEscKey]);
-
- const PopoverContent = (
-
- {children}
- {hasArrow && (
-
- )}
-
- );
+ {
+ name: 'flip',
+ enabled: position === PopoverPosition.Auto ? true : flip,
+ },
+ {
+ name: 'arrow',
+ enabled: hasArrow,
+ options: {
+ element: arrowElement,
+ },
+ },
+ {
+ name: 'offset',
+ options: {
+ offset,
+ },
+ },
+ ],
+ });
- return (
- <>
- {isPortal
- ? isOpen && createPortal(PopoverContent, document.body)
- : isOpen && PopoverContent}
- >
- );
-};
+ // Define width to match reference element or auto
+ const contentStyle = {
+ width: matchWidth ? referenceElement?.clientWidth : 'auto',
+ };
+
+ // Esc key press
+ const handleEscKey = (event: KeyboardEvent) => {
+ if (event.key === 'Escape') {
+ // Close the popover when the "Esc" key is pressed
+ if (onPressEscKey) {
+ onPressEscKey();
+ }
+ }
+ };
+
+ useEffect(() => {
+ document.addEventListener('keydown', handleEscKey);
+
+ return () => {
+ document.removeEventListener('keydown', handleEscKey);
+ };
+ }, [onPressEscKey]);
+
+ const PopoverContent = (
+ )}
+ style={{ ...styles.popper, ...contentStyle, ...props.style }}
+ >
+ {children}
+ {hasArrow && (
+ )}
+ />
+ )}
+
+ );
+
+ return (
+ <>
+ {isPortal
+ ? isOpen && createPortal(PopoverContent, document.body)
+ : isOpen && PopoverContent}
+ >
+ );
+ },
+);
diff --git a/ui/components/component-library/popover/popover.types.ts b/ui/components/component-library/popover/popover.types.ts
index 5321f88d7..674aa3586 100644
--- a/ui/components/component-library/popover/popover.types.ts
+++ b/ui/components/component-library/popover/popover.types.ts
@@ -1,4 +1,9 @@
-import type { BoxProps } from '../../ui/box/box.d';
+import React from 'react';
+import type {
+ StyleUtilityProps,
+ PolymorphicComponentPropWithRef,
+ BoxProps,
+} from '../box';
export enum PopoverPosition {
Auto = 'auto',
@@ -21,7 +26,7 @@ export enum PopoverRole {
Dialog = 'dialog',
}
-export interface PopoverProps extends BoxProps {
+export interface PopoverStyleUtilityProps extends StyleUtilityProps {
/**
* The contents within the Popover
*/
@@ -46,7 +51,7 @@ export interface PopoverProps extends BoxProps {
/**
* Pass any `BoxProps` to the Popover arrow
*/
- arrowProps?: BoxProps;
+ arrowProps?: BoxProps<'div'>;
/**
* Boolean to control the width of the Popover to match the width of the reference element
* Default: false
@@ -92,3 +97,10 @@ export interface PopoverProps extends BoxProps {
*/
onPressEscKey?: () => void;
}
+
+export type PopoverProps =
+ PolymorphicComponentPropWithRef;
+
+export type PopoverComponent = (
+ props: PopoverProps,
+) => React.ReactElement | null;