react ~8 KB 0 deps v0.3.1 ↗ GitHub ↗

@mshafiqyajid/react-popover

Headless popover hook and styled component. Click or hover triggered, rich ReactNode content, optional title header, arrow, full placement set with -start/-end alignment, flip + shift + offset + collisionPadding, portal positioning.

Playground #

Props
TSX
import { PopoverStyled } from "@mshafiqyajid/react-popover/styled";
import "@mshafiqyajid/react-popover/styles.css";

<PopoverStyled
  title="More info"
  content="This is the popover content."
/>

Install #

npm install @mshafiqyajid/react-popover

Quick start #

import { PopoverStyled } from "@mshafiqyajid/react-popover/styled";
import "@mshafiqyajid/react-popover/styles.css";

<PopoverStyled
  title="Keyboard shortcuts"
  content={
    <div>
      <p>⌘K — Command palette</p>
      <p>⌘S — Save</p>
    </div>
  }
>
  <button>Show shortcuts</button>
</PopoverStyled>

Headless #

import { usePopover } from "@mshafiqyajid/react-popover";

const { triggerProps, popoverProps, isOpen, close } = usePopover({
  trigger: "click",
  closeOnEsc: true,
});

return (
  <>
    <button {...triggerProps}>Open</button>
    {isOpen && (
      <div {...popoverProps} className="my-popover">
        <p>Content</p>
        <button onClick={close}>Close</button>
      </div>
    )}
  </>
);

The hook delegates positioning to the consumer — pair with your own floating-UI library or absolute layout. PopoverStyled uses Floating UI internally.

API #

PropTypeDefaultDescription
contentReactNodePopover body content
titlestringOptional header title
placementtop | bottom | left | right (each with optional -start / -end)"bottom"Preferred side and alignment
trigger"click" | "hover""click"How to open
size"sm" | "md" | "lg""md"Width
showArrowbooleantrueShow arrow pointing to trigger
offsetnumber8Gap between trigger and popover
collisionPaddingnumber8Viewport edge margin for flip / shift
flipbooleantrueAuto-flip to opposite side near edges
shiftbooleantruePush back into view along the cross-axis
strategy"absolute" | "fixed""absolute"Positioning strategy
closeOnOutsideClickbooleantrueClose on outside click
closeOnEscbooleantrueClose on Escape

data-placement #

The popover's data-placement attribute reflects the resolved (post-flip) placement. Use it to style side-specific arrow direction, radius, etc.

Edit this page on GitHub