@llselect/core
Version:
Low-level select: a minimal, flexible, framework-agnostic replacement for the native HTML select element.
240 lines • 12.7 kB
TypeScript
/**
* Scroll and zoom gestures while the popup is open: the types of the
* `behaviorOfScrollGestureFn` and `behaviorOfZoomGestureFn` settings, and the
* listeners that sort each gesture and carry out the behavior a setting picks.
* - The presets that fill the setting live in `@llselect/core/behavior-presets`.
* - `base.ts` only wires the setting, the protected methods and these
* listeners.
*/
import type { LLSelectOutsideClickBehavior } from './base.js';
/**
* What a scroll gesture is. See {@link LLSelectScrollGesture}.
* - `'wheel'`: a `wheel` event, which a mouse wheel and a touchpad both give.
* A `wheel` with `ctrlKey` is a zoom gesture instead; see
* {@link LLSelectZoomGesture}.
* - `'touch-start'`: one finger touching down.
* - `'touch-drag'`: one finger moving while it touches.
* - `'touch-end'`: one finger lifting. The library does not send it.
* @group Behaviors
*/
export type LLSelectScrollGestureKind = 'wheel' | 'touch-start' | 'touch-drag' | 'touch-end';
/**
* Where a scroll gesture happens. See {@link LLSelectScrollGesture}.
* - `'popup'`: inside the popup, its header, footer and filter input included.
* - `'trigger'`: inside the root element, outside the popup.
* - `'outside'`: anywhere else on the page.
* @group Behaviors
*/
export type LLSelectScrollGestureZone = 'popup' | 'trigger' | 'outside';
/**
* One scroll gesture while the popup is open, as `behaviorOfScrollGestureFn`
* receives it.
* - A touch counts as a scroll gesture only while one finger touches. From
* the moment a second finger lands until every finger lifts, the touch is
* a zoom gesture, which `behaviorOfZoomGestureFn` answers; see
* {@link LLSelectZoomGesture}.
* - A pinch starts with one finger, so the touch-down of that finger is a
* gesture like any other.
* - A touch keeps the zone where it touched down, wherever it moves later.
* @group Behaviors
*/
export interface LLSelectScrollGesture {
/** What the gesture is. */
readonly kind: LLSelectScrollGestureKind;
/** Where the gesture happens. */
readonly zone: LLSelectScrollGestureZone;
}
/**
* What the select component does with one scroll gesture: keep the popup
* open or close it, and let the page scroll or block it.
* - `'leave-popup-open-and-allow-page-scroll'`: nothing happens; the browser
* scrolls as it would without the select component. Over the popup list,
* the browser may still stop such a scroll at the end of the list, which
* has the CSS property `overscroll-behavior: contain`.
* - `'leave-popup-open-and-block-page-scroll'`: the popup stays open, and
* the gesture scrolls nothing.
* - `'close-popup-and-allow-page-scroll'`: the popup closes, with the close
* reason `'scroll-gesture'`, and the gesture scrolls as usual.
* - `'close-popup-and-block-page-scroll'`: the popup closes, with the close
* reason `'scroll-gesture'`, and the gesture scrolls nothing.
* - A `'touch-start'` scrolls nothing by itself, so its page-scroll half acts
* only when the popup closes there.
* - That half then holds for the rest of that touch, until a second finger
* lands or a mouse or a pen presses.
* - While the popup stays open, each move of the touch asks again, as a
* `'touch-drag'`.
* - A close picked for a `'touch-drag'` blocks that one move at most. The
* library sees no later move of that touch and blocks none of them.
* - When a touch-down closes the popup, the click of that tap would reopen
* the popup, or land on what the popup covered. So the library swallows
* that click when the touch-down was:
* - on the trigger, except on a button of the trigger buttons strip, whose
* click does that button's action;
* - inside the popup;
* - outside the select component, while `behaviorOfOutsideClick` is
* `'block'`.
* - Only a click near the touch-down, within one second of the lift, counts
* as the tap's click.
* - A click from the keyboard or from `click()` never counts as the tap's
* click.
* - A later touch, or the press of a mouse or a pen, ends the wait for the
* tap's click.
* @group Behaviors
*/
export type LLSelectScrollGestureBehavior = 'leave-popup-open-and-allow-page-scroll' | 'leave-popup-open-and-block-page-scroll' | 'close-popup-and-allow-page-scroll' | 'close-popup-and-block-page-scroll';
/**
* Where a zoom gesture happens. See {@link LLSelectZoomGesture}.
* - `'popup'`: inside the popup, its header, footer and filter input included.
* - `'trigger'`: inside the root element, outside the popup.
* - `'outside'`: anywhere else on the page.
* - The scroll gestures have a type of their own with the same values,
* {@link LLSelectScrollGestureZone}, so that each type can change alone.
* @group Behaviors
*/
export type LLSelectZoomGestureZone = 'popup' | 'trigger' | 'outside';
/**
* One zoom gesture while the popup is open, as `behaviorOfZoomGestureFn`
* receives it.
* - On a touchscreen, a touch becomes a zoom gesture when a second finger
* lands, and stays one until every finger lifts.
* - Its zone is where the first finger touched down. If both fingers land
* in one event, the zone is where that event happens.
* - Every `wheel` with `ctrlKey` is a zoom gesture of its own, whose zone is
* where the pointer is. Ctrl with a mouse wheel gives such a wheel, and so
* does a touchpad pinch in Chromium; the page cannot tell the two apart.
* @group Behaviors
*/
export interface LLSelectZoomGesture {
/** Where the zoom happens. */
readonly zone: LLSelectZoomGestureZone;
}
/**
* What the select component does with one zoom gesture: keep the popup open
* or close it, and let the page zoom or block it.
* - `'leave-popup-open-and-allow-page-zoom'`: nothing happens; the browser
* zooms as it would without the select component.
* - `'leave-popup-open-and-block-page-zoom'`: the popup stays open, and the
* gesture does not zoom the page.
* - `'close-popup-and-allow-page-zoom'`: the popup closes, with the close
* reason `'zoom-gesture'`, and the page zooms as usual.
* - `'close-popup-and-block-page-zoom'`: the popup closes, with the close
* reason `'zoom-gesture'`, and the gesture does not zoom the page.
* - On a touchscreen, the answer holds until every finger lifts, also after
* a close.
* - For a `wheel` with `ctrlKey`, the answer covers that one event. The next
* one asks again while the popup is open.
* - The library blocks a zoom by canceling its events. A browser may still
* zoom when it sends them as not cancelable.
* @group Behaviors
*/
export type LLSelectZoomGestureBehavior = 'leave-popup-open-and-allow-page-zoom' | 'leave-popup-open-and-block-page-zoom' | 'close-popup-and-allow-page-zoom' | 'close-popup-and-block-page-zoom';
/**
* A set of behavior settings, spread into the settings of a select
* component. The presets of `@llselect/core/behavior-presets` have this
* shape.
* @group Behaviors
*/
export interface LLSelectBehaviorPreset {
/** See `behaviorOfOutsideClick` in `LLSelectBaseSettings`. */
readonly behaviorOfOutsideClick: LLSelectOutsideClickBehavior;
/** See `behaviorOfScrollGestureFn` in `LLSelectBaseSettings`. */
readonly behaviorOfScrollGestureFn: (gesture: LLSelectScrollGesture) => LLSelectScrollGestureBehavior;
/** See `behaviorOfZoomGestureFn` in `LLSelectBaseSettings`. */
readonly behaviorOfZoomGestureFn: (gesture: LLSelectZoomGesture) => LLSelectZoomGestureBehavior;
}
/**
* The mark that `defaultBehaviorOfScrollGesture` carries. The main bundle and
* the presets bundle each hold a copy of that function, so the library tests
* for the mark, not for the identity.
* @internal
*/
export declare const DEFAULT_BEHAVIOR_OF_SCROLL_GESTURE_MARK: unique symbol;
/**
* The mark that `defaultBehaviorOfZoomGesture` carries, for the reason of
* {@link DEFAULT_BEHAVIOR_OF_SCROLL_GESTURE_MARK}.
* @internal
*/
export declare const DEFAULT_BEHAVIOR_OF_ZOOM_GESTURE_MARK: unique symbol;
/**
* What the listeners need from the select component.
* @internal
*/
export interface GestureHost {
readonly rootEl: HTMLElement;
readonly popupEl: HTMLElement;
/** Whether scroll gestures outside the select component reach `behaviorFor`: the protected `shouldListenForScrollGesturesOutside`. */
readonly scrollOutside: boolean;
/** Whether zoom gestures outside the select component reach `zoomBehaviorFor`: the protected `shouldListenForZoomGesturesOutside`. */
readonly zoomOutside: boolean;
/** The behavior for one scroll gesture: the protected `behaviorOfScrollGesture`. */
behaviorFor(gesture: LLSelectScrollGesture): LLSelectScrollGestureBehavior;
/** Closes the popup with the close reason `'scroll-gesture'`. */
closeFor(gesture: LLSelectScrollGesture): void;
/** The behavior for one zoom gesture: the protected `behaviorOfZoomGesture`. */
zoomBehaviorFor(gesture: LLSelectZoomGesture): LLSelectZoomGestureBehavior;
/** Closes the popup with the close reason `'zoom-gesture'`. */
closeForZoom(gesture: LLSelectZoomGesture): void;
/** Whether a click with this composed path would toggle the popup: a click on the trigger wrapper, outside the buttons of the trigger buttons strip. */
clickToggles(path: readonly EventTarget[]): boolean;
/** Whether a click outside the select component is swallowed while the popup is open: `behaviorOfOutsideClick: 'block'`. */
readonly swallowsOutsideClick: boolean;
/** Takes the function that ends a touch hold early, for `destroy()`. */
holdStarted(release: () => void): void;
/** Told when that hold has ended, so the select component drops `release`. */
holdEnded(release: () => void): void;
}
/**
* A tap's click may land at most this far from its touch-down, in CSS pixels.
* - `base.ts` binds the click swallow of a blocked outside press with it too.
* @internal
*/
export declare const TAP_CLICK_RADIUS: number;
/**
* A tap's click may still come this long after the touch lifts, in
* milliseconds.
* - Browsers can hold the click back by about 300 ms on a page without a
* mobile viewport.
* - `base.ts` waits as long in the click swallow of a blocked outside press.
* @internal
*/
export declare const CLICK_WAIT_MS = 1000;
/**
* Attaches the scroll-gesture and zoom-gesture listeners for one open cycle;
* the returned function detaches them.
* - On the popup: a `wheel` listener and the touch listeners, which see the
* zone `'popup'`. Chromium 156 makes a wheel over the top-layer popup
* cancelable only for a non-passive listener on the popup itself, not on
* an ancestor such as the root element.
* - On the root element: the same listeners, which see the zone `'trigger'`
* and skip the gestures of the popup.
* - On the document, only while `host.scrollOutside` or `host.zoomOutside`
* is set: the same listeners in the capture phase, which see the zone
* `'outside'` and pass each group only the gestures it listens for there.
* - A non-passive `wheel` or `touchmove` listener there makes every scroll
* of the page wait for script, so the default behaviors, which leave the
* outside alone, do not attach them.
* - The `touchstart` listener there is non-passive only while
* `host.zoomOutside` is set, since only a zoom cancels a touch-down.
* - A wheel or a drag inside the popup that something in the popup can still
* scroll is left to the browser and does not reach `behaviorFor`. A
* touch-down always reaches it, since it has no direction yet.
* - A `wheel` with `ctrlKey` reaches `zoomBehaviorFor` instead of
* `behaviorFor`, and so does a touch from the moment a second finger lands
* until every finger lifts. The touch asks once, and its answer holds for
* the rest of it.
* - A pinch-zoomed page gets the same calls as a page that is not zoomed.
* - The hold of a zoom that closed the popup and blocks the zoom lasts until
* every finger lifts, or until `destroy()`; see `holdZoomTouch`.
* - In a closed shadow root around the select component, the document sees
* no deeper than the shadow host:
* - The document's listeners read every gesture there as `'outside'`,
* before the listeners inside see it.
* - The hold of a one-finger close that the document's listeners start
* sits on the shadow host.
* - If the close removed the node that the touch started on, the shadow
* host may miss the rest of the touch, as inferred from the code.
* - The next touch-down still ends that hold.
* @internal
*/
export declare function attachGestureListeners(host: GestureHost): () => void;
//# sourceMappingURL=behaviors.d.ts.map