Skip to content

Commit

Permalink
[@mantine/core] Popover: Fix infinite loop when component is used wit…
Browse files Browse the repository at this point in the history
…h Preact (#2752)

Co-authored-by: Jérémie van der Sande <jeremie.van-der-sande@ubisoft.com>
  • Loading branch information
jvdsande and Jérémie van der Sande committed Oct 22, 2022
1 parent 450840a commit 36b9504
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/mantine-core/src/Popover/Popover.tsx
@@ -1,6 +1,6 @@
/* eslint-disable react/no-unused-prop-types */

import React, { useRef, useState } from 'react';
import React, { useRef, useState, useCallback } from 'react';
import { useId, useClickOutside } from '@mantine/hooks';
import {
useMantineTheme,
Expand Down Expand Up @@ -209,6 +209,22 @@ export function Popover(props: PopoverProps) {
dropdownNode,
]);

const reference = useCallback(
(node: HTMLElement) => {
setTargetNode(node);
popover.floating.reference(node);
},
[popover.floating.reference]
);

const floating = useCallback(
(node: HTMLElement) => {
setDropdownNode(node);
popover.floating.floating(node);
},
[popover.floating.floating]
);

return (
<StylesApiProvider
classNames={classNames}
Expand All @@ -221,14 +237,8 @@ export function Popover(props: PopoverProps) {
returnFocus,
disabled,
controlled: popover.controlled,
reference: (node) => {
setTargetNode(node as HTMLElement);
popover.floating.reference(node);
},
floating: (node) => {
setDropdownNode(node);
popover.floating.floating(node);
},
reference,
floating,
x: popover.floating.x,
y: popover.floating.y,
arrowX: popover.floating?.middlewareData?.arrow?.x,
Expand Down

0 comments on commit 36b9504

Please sign in to comment.