|
1 | 1 | import {
|
2 |
| - ReactElement, |
3 |
| - ReactNode, |
| 2 | + type FC, |
| 3 | + type ReactElement, |
| 4 | + type ReactNode, |
4 | 5 | cloneElement,
|
5 | 6 | createContext,
|
6 | 7 | useContext,
|
@@ -38,28 +39,32 @@ const PopoverContext = createContext<PopoverContextValue | undefined>(
|
38 | 39 | undefined,
|
39 | 40 | );
|
40 | 41 |
|
41 |
| -export const Popover = (props: { |
| 42 | +interface PopoverProps { |
42 | 43 | children: ReactNode | ((popover: PopoverContextValue) => ReactNode); // Allows inline usage
|
43 | 44 | mode?: TriggerMode;
|
44 | 45 | isDefaultOpen?: boolean;
|
| 46 | +} |
| 47 | + |
| 48 | +export const Popover: FC<PopoverProps> = ({ |
| 49 | + children, |
| 50 | + mode, |
| 51 | + isDefaultOpen, |
45 | 52 | }) => {
|
46 | 53 | const hookId = useId();
|
47 |
| - const [isOpen, setIsOpen] = useState(props.isDefaultOpen ?? false); |
| 54 | + const [isOpen, setIsOpen] = useState(isDefaultOpen ?? false); |
48 | 55 | const triggerRef = useRef<HTMLElement>(null);
|
49 | 56 |
|
50 | 57 | const value: PopoverContextValue = {
|
51 | 58 | isOpen,
|
52 | 59 | setIsOpen,
|
53 | 60 | triggerRef,
|
54 | 61 | id: `${hookId}-popover`,
|
55 |
| - mode: props.mode ?? "click", |
| 62 | + mode: mode ?? "click", |
56 | 63 | };
|
57 | 64 |
|
58 | 65 | return (
|
59 | 66 | <PopoverContext.Provider value={value}>
|
60 |
| - {typeof props.children === "function" |
61 |
| - ? props.children(value) |
62 |
| - : props.children} |
| 67 | + {typeof children === "function" ? children(value) : children} |
63 | 68 | </PopoverContext.Provider>
|
64 | 69 | );
|
65 | 70 | };
|
@@ -102,14 +107,19 @@ export const PopoverTrigger = (props: { children: TriggerElement }) => {
|
102 | 107 |
|
103 | 108 | type Horizontal = "left" | "right";
|
104 | 109 |
|
105 |
| -export const PopoverContent = ( |
106 |
| - props: Omit<MuiPopoverProps, "open" | "onClose" | "anchorEl"> & { |
107 |
| - horizontal?: Horizontal; |
108 |
| - }, |
109 |
| -) => { |
| 110 | +type PopoverContentProps = Omit< |
| 111 | + MuiPopoverProps, |
| 112 | + "open" | "onClose" | "anchorEl" |
| 113 | +> & { |
| 114 | + horizontal?: Horizontal; |
| 115 | +}; |
| 116 | + |
| 117 | +export const PopoverContent: FC<PopoverContentProps> = ({ |
| 118 | + horizontal = "left", |
| 119 | + ...popoverProps |
| 120 | +}) => { |
110 | 121 | const popover = usePopover();
|
111 | 122 | const [isReady, setIsReady] = useState(false);
|
112 |
| - const horizontal = props.horizontal ?? "left"; |
113 | 123 | const hoverMode = popover.mode === "hover";
|
114 | 124 |
|
115 | 125 | // This is a hack to make sure the popover is not rendered until the trigger
|
@@ -143,7 +153,7 @@ export const PopoverContent = (
|
143 | 153 | }}
|
144 | 154 | {...horizontalProps(horizontal)}
|
145 | 155 | {...modeProps(popover)}
|
146 |
| - {...props} |
| 156 | + {...popoverProps} |
147 | 157 | id={popover.id}
|
148 | 158 | open={popover.isOpen}
|
149 | 159 | onClose={() => popover.setIsOpen(false)}
|
|
0 commit comments