Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create AnchoredOverlay Component #1715

Closed
Tracked by #1705
scurker opened this issue Oct 11, 2024 · 0 comments · Fixed by #1760
Closed
Tracked by #1705

Create AnchoredOverlay Component #1715

scurker opened this issue Oct 11, 2024 · 0 comments · Fixed by #1760
Assignees
Labels
rfc An issue proposing a new significant change rocket
Milestone

Comments

@scurker
Copy link
Member

scurker commented Oct 11, 2024

An overlay is a layered element that temporarily covers other content on the page. This component should be anchored to a target element that is positionally displayed along the target element according to a specified anchor position.

Props

interface AnchoredOverlayProps<T extend HTMLElement = HTMLElement> 
  extends PolymorphicComponent<T> {

  /** A target element or element ref to render the as the overlay anchor target. */
  target: ElementOrRef<T>

  /** How to anchor the overlay element relative to the target. */
  placement?: { ... }

  /** Determines if the overlay is currently shown */
  open?: boolean

  /** An optional offset number to position the anchor element from its anchored target. */
  offset?: number

  /** A callback function that is called when the overlay state changes. */
  onOpenChange?: (open: boolean) => void

}

Implementation

The AnchoredOverlay is a primitive component that primarily contains behaviors to position an overlay element relative to its target and does not contain any default styles or classnames and is intended to be used in composition with other components. The style attributes to position the popover should be implement using floating-ui and placement arguments:

import { useFloating } from '@floating-ui/react-dom'

function AnchoredOverlay({
  as = 'div',
  open,
  target,
  placement,
  children
}: AnchoredOverlayProps): JSX.Element {
  const overlayRef = useRef<HTMLElement>()
  useFloating({
    placement,
    open,
    elements: {
      reference: resolveElement(target),
      floating: overlayRef
    }
  }
  const Component = as

  return (
    <Component ref={overlayRef}>
      {children}
    </Component>
  )
}

Note

Cauldron is currently using popperjs to manage tooltips. floatingui is the rebranding of popperjs with a new api, part of the work involved here will be making any necessary changes to accommodate tooltip and popover to use the updated library.

Escape behaviors should happen automatically as part of this component. When escape is detected, the onOpenChange callback should be invoked.

@scurker scurker added the rfc An issue proposing a new significant change label Oct 11, 2024
@scurker scurker added this to the Q4 2024 milestone Oct 11, 2024
@scurker scurker changed the title Create Anchored Overlay Component Create AnchoredOverlay Component Oct 11, 2024
@scurker scurker added the rocket label Oct 25, 2024
@scurker scurker self-assigned this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfc An issue proposing a new significant change rocket
Projects
None yet
1 participant