UNPKG

@llselect/core

Version:

Low-level select: a minimal, flexible, framework-agnostic replacement for the native HTML select element.

271 lines • 12.9 kB
/** Minimal rect shape consumed by {@link computePosition}, in layout-viewport coordinates. */ export interface AnchorRect { top: number; left: number; right: number; bottom: number; width: number; height: number; } /** * Whether the floating element sits below or above the anchor. * @group Positioning */ export type LLSelectPlacement = 'below' | 'above'; /** * How the floating element decides its width. See `LLSelectBaseSettings` * (`popupWidthPolicy` field) for the user-facing contract. * @group Positioning */ export type LLSelectWidthPolicy = 'fit-content' | 'match-trigger-wrapper'; /** Input to the pure positioning calculation. */ export interface PositionInput { anchorRect: AnchorRect; viewportWidth: number; viewportHeight: number; /** * Origin of the VISIBLE viewport in layout-viewport coordinates, those of * `position: fixed`. * - `0` unless the visual viewport is panned inside the layout viewport, * under pinch zoom or while the on-screen keyboard shrinks it * (`visualViewport.offsetLeft/offsetTop`). * - The anchor rect is in the same coordinates, so clamping against * `[0, viewportWidth]` alone would drag the floating element toward the * layout origin. * - Default `0`. */ viewportLeft?: number; /** See {@link PositionInput.viewportLeft}. Default `0`. */ viewportTop?: number; /** Measured height of the floating element. Pass 0 if unknown. */ floatingHeight: number; /** Width policy. Optional; default `'fit-content'`. */ widthPolicy?: LLSelectWidthPolicy; /** * Floating element's natural (max-content) width in px. Only consulted when * `widthPolicy === 'fit-content'`. Default `0`. */ floatingNaturalWidth?: number; /** * Writing direction of the anchor's context. Only consulted when * `widthPolicy === 'fit-content'`: `'rtl'` right-aligns the floating element * to the anchor and grows LEFTWARD (the mirror of ltr). Default `'ltr'`. * `'match-trigger-wrapper'` is position-identical in both directions. */ direction?: 'ltr' | 'rtl'; /** * Placement currently in effect, for stickiness across repositions of one * open cycle. When set and the content still fits on that side, it is kept * even if the other side would also fit - so a transient content shrink * (e.g. a filter query matching nothing) does not flip the popup back and * forth. Omit / `undefined` (first placement) to pick fresh. */ currentPlacement?: LLSelectPlacement | undefined; /** * Distance in px between the anchor's edge and the floating element's edge * next to it. * - The same below and above the anchor. * - A negative value counts as `0`. * - Default `4`. */ distanceFromAnchor?: number; /** * Room in px that the floating element leaves to the edges of the visible * window. * - Below the anchor, the room is to the bottom edge; above it, to the top * edge. * - Under `'fit-content'`, it is the margin on both sides as well, and * there at most half the window's width. * - A negative value counts as `0`. * - Default `8`. */ distanceFromViewportEdge?: number; } /** Result of {@link computePosition}: coordinates and chosen placement. */ export interface PositionResult { top: number; left: number; width: number; /** Maximum height the floating element may occupy. */ maxHeight: number; placement: LLSelectPlacement; } /** * Package-internal: the default of `PositionInput.distanceFromAnchor`, and of * the `popupDistanceFromTriggerWrapper` setting. */ export declare const DEFAULT_DISTANCE_FROM_ANCHOR = 4; /** * Package-internal: the default of `PositionInput.distanceFromViewportEdge`, * and of the `popupDistanceFromViewportEdge` setting. */ export declare const DEFAULT_DISTANCE_FROM_VIEWPORT_EDGE = 8; /** * Compute where to place the floating element relative to the anchor. * - The window here is the visible window, in layout-viewport coordinates, * those of `position: fixed`. * - The anchor rect and the result use the same coordinates. * - Across, it spans `[viewportLeft, viewportLeft + viewportWidth]`. * - Down, it spans `[viewportTop, viewportTop + viewportHeight]`. * - Both offsets are 0 unless the visual viewport is panned. See * {@link PositionInput.viewportLeft}. * - Vertically, the floating element goes below the anchor if it fits there. * - Otherwise it goes above the anchor, if it fits there. * - If it fits on neither side, it takes the side with more room. * - `maxHeight` limits it to the room on its side. * - A `currentPlacement` that still fits is kept. * - Keeping it means that a list that shrinks and regrows does not make the * popup jump sides. * - The floating element keeps a gap of `distanceFromAnchor` from the anchor. * - Below the anchor, its room ends `distanceFromViewportEdge` above the * bottom of the window. * - Above the anchor, its room starts the same distance below the top of the * window. * - A negative value of either distance counts as 0. * - It also stays inside the window vertically when the anchor is outside it: * - If the anchor's top lies below the window, the bottom of the window * stands in for that top. * - An anchor under the on-screen keyboard is such a case. * - If the anchor's bottom lies above the window, the top of the window * stands in for that bottom. * - The floating element then no longer touches the anchor. * - While the anchor's edge next to the floating element is inside the * window, the placement is exactly the one without the rule above. * - If the chosen side has no room at all, `maxHeight` is 0. * - If, in addition, the anchor's edge next to the floating element lies * outside the window, `top` is kept inside the window. * - Horizontally, `widthPolicy === 'fit-content'`, the default, makes `width` * the larger of the anchor's width and `floatingNaturalWidth`. * - That width is at most the window's width minus two side margins. * - A side margin is `distanceFromViewportEdge`, and at most half the * window's width, so the two margins never cross. * - In ltr, the floating element starts at the anchor's left edge. * - In ltr, it grows rightward. * - In rtl, it starts at the anchor's right edge. * - In rtl, it grows leftward. * - If it then crosses a side margin, it shifts back inside the margins. * - `widthPolicy === 'match-trigger-wrapper'` copies the anchor's `width`. * - That policy also copies the anchor's `left`. * - That policy ignores `direction`. * - That policy has no horizontal clamp. */ export declare function computePosition(input: PositionInput): PositionResult; /** * Package-internal (tests): whether the element has containment of any kind, * from `contain` or from what `content-visibility` and `container-type` apply. */ export declare function hasContainment(s: CSSStyleDeclaration): boolean; /** Package-internal test hook: forget the measured `filter` behavior. */ export declare function resetFilterProbe(): void; /** * Package-internal (tests): whether an element with this computed style is * the containing block of its fixed-position descendants, and so of its * absolute ones as well. The list is the one DESIGN.md "Hidden-trigger check" * keeps. `filterHoldsFixedFn` answers for `filter`, the one counted property * whose answer differs between engines; it runs only when `filter` is set. */ export declare function createsContainingBlock(s: CSSStyleDeclaration, filterHoldsFixedFn: () => boolean): boolean; /** * Whether `anchor` is currently hidden: fully outside the layout viewport, or * fully outside an ancestor's overflow clip that applies to it (DESIGN.md * "Hidden-trigger check"). * - Exposed so a caller can refuse to open a popup against an off-screen * trigger BEFORE building a positioner, rather than opening and then * hiding re-entrantly. * - The layout viewport here is the client box of `<html>`, or of `<body>` * in quirks mode, which leaves out the scrollbars. */ export declare function isAnchorHidden(anchor: HTMLElement): boolean; /** Controls the lifecycle of an active positioner. */ export interface Positioner { /** Force a re-position now. Normally called automatically. */ reposition(): void; /** * Stop tracking and clear all inline styles + `data-placement` from the * floating element. Idempotent. Call once when the floating element is * dismissed. */ detach(): void; } /** Options passed to {@link createPositioner}. */ export interface PositionerOptions { /** * Called when the anchor is found hidden: fully outside the layout * viewport, or fully clipped away by an ancestor (see `isAnchorHidden`). * - The layout viewport here is the client box of `<html>`, or of `<body>` * in quirks mode, which leaves out the scrollbars. * - The check runs at the first placement and after every `scroll` event * outside the floating element that the capture listeners see: of the * document, of any element in it, or inside a shadow root on the * anchor's path. * - A resize, a pan of the visual viewport and the public `reposition()` * never run it. * - Typical use: close the floating element so it does not hang in space * without a visible trigger. */ onHide?: () => void; /** * Width policy. Default `'fit-content'`, matching the `popupWidthPolicy` * setting default. */ widthPolicy?: LLSelectWidthPolicy; /** * Distance in px between the anchor and the floating element. * - The contract is that of {@link PositionInput.distanceFromAnchor}. * - Default `4`, the default of the `popupDistanceFromTriggerWrapper` * setting. */ distanceFromAnchor?: number; /** * Room in px that the floating element leaves to the edges of the visible * window. * - The contract is that of {@link PositionInput.distanceFromViewportEdge}. * - Default `8`, the default of the `popupDistanceFromViewportEdge` * setting. */ distanceFromViewportEdge?: number; /** * The floating element's inner scroll container (the popup list). Under an * active `maxHeight` clamp the floating element's overflow is absorbed as * this element's internal scrolling, so `offsetHeight` alone under-reports * the natural height; its `scrollHeight - clientHeight` restores the * difference WITHOUT lifting the clamp to re-measure (a lift-and-restore * would clamp this element's scrollTop to 0 mid-frame - losing the * scrolled-to-chosen position - and caused a visible window-scroll jolt on * Firefox). Omit when the floating element has no inner scroller. * * INVARIANT: this element must have no author-set height cap of its own - * the positioner owns the popup's `maxHeight`. The reconstruction adds back * ALL of its overflow, so an independent `max-height` on the inner list * (theme or consumer CSS) is read as extra natural height and can pick a * side as if the popup were taller than it can render. Shipped themes honor * this; consumer themes must clamp the popup, not the inner list. */ innerScrollEl?: HTMLElement; } /** * Attach a positioner that keeps `floating` placed relative to `anchor`. * * - Sets `floating` to `position: fixed`. * - Listens for `scroll` in the capture phase, on `window` and on every * shadow root of the anchor's flat-tree path. The shadow roots are read * once, at attach. * - Listens for `resize` on `window` and on `visualViewport`, for `scroll` * on `visualViewport`, and watches both elements with a `ResizeObserver`. * - Ignores a scroll inside `floating`, which cannot move the anchor. * - At the first placement and after every `scroll` event outside * `floating` that the capture listeners see, checks whether the anchor is * hidden (`isAnchorHidden`). If it is and `onHide` is provided, calls * `onHide` and skips the style updates. * - The first placement runs before any listener or observer is registered. * If `onHide` throws there, the positioner leaves nothing behind. * - Otherwise, and on every resize and every pan of the visual viewport, * applies the coordinates from {@link computePosition} and sets * `data-placement` on `floating` for CSS hooks. * - A pan of the visual viewport never runs the hidden check. * * Caller is responsible for calling `detach()` when the floating element is * dismissed; otherwise listeners leak. */ export declare function createPositioner(anchor: HTMLElement, floating: HTMLElement, options?: PositionerOptions): Positioner; //# sourceMappingURL=positioning.d.ts.map