@molecule/app-overlay-dom
v1.1.0
Published
DOM utility for document-scoped overlay focus, dismissal and scroll locking across Molecule web UI kits
Readme
@molecule/app-overlay-dom
DOM overlay utility shared by the React, Vue, Svelte, Solid and Angular UI kits.
OverlayLayer registers a mounted surface with its document's overlay stack
using OverlayLayerOptions from @molecule/app-ui. One stack coordinates
Escape, backdrop dismissal, modal focus and body scroll locks across roots
and renderers. A renderer can supply a logical parent for its portals.
Otherwise the stack resolves ownership from the anchor's DOM ancestry.
Renderers supply their own portal, hydration and focus scheduling. Call
sync(options, parent) when the surface or options change and release()
when the view unmounts. Anchored dropdowns can also share document-local
positioning and outside-pointer detection.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Type
utility
Installation
npm install @molecule/app-overlay-dom @molecule/app-uiAPI
Types
FocusScheduler
Schedules DOM focus at the point where a renderer has finished writing its view.
type FocusScheduler = (focus: () => void, transition: 'open' | 'close') => voidClasses
OverlayLayer
A renderer-owned registration for one overlay, reusable across open intervals.
class OverlayLayer {
private registration?: { stack: DocumentLayers; entry: Entry }
/**
* Creates a registration owned by one mounted overlay view.
*
* @param afterRender - Schedules focus if the renderer is still writing DOM.
*/
constructor(private readonly afterRender: FocusScheduler = (focus) => focus()) {}
/**
* Registers an open surface, updates live options, or releases a closed layer.
*
* @param options - The current renderer-neutral overlay options.
* @param parent - The logical parent when the renderer can provide one.
*/
sync(options: OverlayLayerOptions, parent?: OverlayLayer): void {
const surface = options.open ? domElement(options.surface.current) : null
if (!surface) {
this.release()
return
}
const stack = layersForDocument(surface.ownerDocument)
if (this.registration?.stack === stack) {
stack.update(this.registration.entry, options, surface, parent)
} else {
this.release()
const entry = stack.open(this, options, surface, parent)
this.registration = { stack, entry }
stack.focusOnOpen(entry, this.afterRender)
}
}
/** Releases keys, scroll and focus ownership. Safe to call after closing. */
release(): void {
const registration = this.registration
this.registration = undefined
registration?.stack.remove(registration.entry, this.afterRender)
}
/** Requests dismissal when this layer owns the backdrop. */
dismissBackdrop(): void {
this.registration?.stack.backdrop(this)
}
}Functions
readOverlayBox(element)
Reads a browser element's bounds synchronously, including SVG and foreign documents.
function readOverlayBox(element: Element): ElementBoxreadOverlayAnchorWidth(anchor)
HTML layout width, which excludes transforms. SVG anchors have no layout width.
function readOverlayAnchorWidth(anchor: Element): number | undefinedmeasureOverlayPosition(anchor, surface, geometry)
Reads a web overlay's placement in page coordinates, with scrollbars excluded from the bounds.
function measureOverlayPosition(
anchor: Element,
surface: Element,
geometry: OverlayAnchorGeometry,
): OverlayPositionanchor— Mounted anchor.surface— Mounted overlay surface.geometry— Placement, offset, padding and collision policy.
Returns: Position relative to the document portal.
measureTooltipPosition(anchor, surface, placement)
Places a tooltip inside its own viewport with the kit's 8px gap and margin.
function measureTooltipPosition(
anchor: Element,
surface: Element,
placement: TooltipPlacement,
): OverlayPositioncalculateDropdownPosition(trigger, menu, placement, align, offset, scroll)
Calculates absolute menu offsets from viewport rectangles and page scroll.
function calculateDropdownPosition(
trigger: DOMRectReadOnly,
menu: DOMRectReadOnly,
placement: Placement,
align: Align,
offset?: number,
{ scrollX, scrollY }?: Pick<Window, 'scrollX' | 'scrollY'>,
): { top: number; left: number }trigger— The trigger rectangle.menu— The menu rectangle.placement— The desired side and optional edge of the trigger.align— The alignment for top and bottom placements.offset— The gap between the trigger and menu in CSS pixels.scroll— The page scroll in the trigger's window.
Returns: The top and left offsets in CSS pixels.
measureDropdownPosition(trigger, menu, placement, align)
Measures a dropdown in the coordinate space of its trigger's document.
function measureDropdownPosition(
trigger: Element,
menu: Element,
placement: Placement,
align: Align,
): { top: number; left: number }trigger— The mounted trigger.menu— The mounted menu.placement— The desired dropdown placement.align— The alignment within the placement axis.
Returns: The top and left offsets in CSS pixels.
isOutsideOverlay(event, trigger, surface)
Checks whether a dispatched pointer event reached either part of an anchored overlay.
function isOutsideOverlay(event: Event, trigger: Element, surface: Element): booleanevent— The event on the owner document.trigger— The mounted trigger.surface— The mounted menu.
Returns: Whether the event originated outside both elements.
domElement(handle)
Reads a renderer's DOM handle, including one from a different window.
function domElement(handle: unknown): FocusElement | nullhandle— The element handle supplied by the renderer.
Returns: The DOM element, or null if there is no DOM handle.
focusStaysWithin(event)
Whether blur moved between controls in the same surface.
function focusStaysWithin(event: Pick<FocusEvent, 'currentTarget' | 'relatedTarget'>): booleanInjection Notes
Requirements
Peer dependencies:
@molecule/app-ui^1.9.0
Runtime Dependencies
@molecule/app-ui
