Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic popover with React - How to make it sticky to the button

Following the Ionic documentation, I am trying to get the popover sticky to the button (like on their own example). Unfortunately I do not know how to achieve this... Thanks

import React, { useState } from 'react';
import { IonPopover, IonButton } from '@ionic/react';

export const PopoverExample: React.FC = () => {
  const [showPopover, setShowPopover] = useState(false);

  return (
    <>
      <IonPopover
        isOpen={showPopover}
        onDidDismiss={e => setShowPopover(false)}
      >
        <p>This is popover content</p>
      </IonPopover>
      <IonButton onClick={() => setShowPopover(true)}>Show Popover</IonButton>
    </>
  );
};
like image 496
Fewl Avatar asked Dec 08 '22 11:12

Fewl


1 Answers

You also need to include an event in the showPopover hook -

const [showPopover, setShowPopover] = useState<{open: boolean, event: Event | undefined}>({
  open: false,
  event: undefined,
});

<IonPopover
    isOpen={showPopover.open}
    event={showPopover.event}
    onDidDismiss={e => setShowPopover({open: false, event: undefined})}
>
   <p>This is popover content</p>
</IonPopover>
<IonButton onClick={(e) => setShowPopover({open: true, event: e.nativeEvent})}>Click</IonButton>
like image 51
James Brightman Avatar answered Dec 11 '22 07:12

James Brightman