Lines
21.05 %
Functions
15 %
Branches
100 %
//! Where the suggestion list opens: below or above its input, whichever
//! side of the visible area (the visual viewport, which an on-screen
//! keyboard shrinks) has room, never taller than that room.
use wasm_bindgen::prelude::*;
use web_sys::{
EventTarget, HtmlElement, HtmlInputElement, ScrollIntoViewOptions, ScrollLogicalPosition,
Window,
};
pub(crate) const MAX_HEIGHT: f64 = 200.0;
/// About three suggestions.
pub(crate) const MIN_HEIGHT: f64 = 108.0;
const MARGIN: f64 = 8.0;
const ABOVE: &str = "above";
/// A vertical extent in layout-viewport coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Span {
pub top: f64,
pub bottom: f64,
}
#[derive(Debug, PartialEq)]
pub(crate) struct Placement {
pub above: bool,
pub max_height: f64,
fn rooms(input: Span, view: Span) -> (f64, f64) {
(input.top - view.top, view.bottom - input.bottom)
/// Whether either side of `input` fits the smallest useful list.
pub(crate) fn roomy(input: Span, view: Span) -> bool {
let (above, below) = rooms(input, view);
above.max(below) - MARGIN >= MIN_HEIGHT
/// The side a list `list` pixels tall opens on, and its height cap.
pub(crate) fn placement(input: Span, view: Span, list: f64) -> Placement {
let opens_above = below < list.min(MAX_HEIGHT) && below < above;
let room = if opens_above { above } else { below } - MARGIN;
Placement {
above: opens_above,
max_height: room.clamp(MIN_HEIGHT, MAX_HEIGHT),
fn input_span(input: &HtmlInputElement) -> Span {
let rect = input.get_bounding_client_rect();
Span {
top: rect.top(),
bottom: rect.bottom(),
fn viewport(window: &Window) -> Span {
match window.visual_viewport() {
Some(view) => Span {
top: view.offset_top(),
bottom: view.offset_top() + view.height(),
},
None => Span {
top: 0.0,
bottom: window
.inner_height()
.ok()
.and_then(|h| h.as_f64())
.unwrap_or_default(),
fn center(input: &HtmlInputElement) {
let options = ScrollIntoViewOptions::new();
options.set_block(ScrollLogicalPosition::Center);
input.scroll_into_view_with_scroll_into_view_options(&options);
fn apply(popup: &HtmlElement, input: &HtmlInputElement, at: &Placement) -> Result<(), JsValue> {
let style = popup.style();
style.set_property("max-height", &format!("{}px", at.max_height))?;
match (at.above, popup.parent_element()) {
(true, Some(field)) => {
popup.class_list().add_1(ABOVE)?;
let bottom = field.client_height() - input.offset_top();
style.set_property("bottom", &format!("{bottom}px"))
_ => {
popup.class_list().remove_1(ABOVE)?;
style.remove_property("bottom").map(drop)
/// Fit the open `popup` of `input` into the visible area, first scrolling
/// the input to the middle when neither side has room.
pub(crate) fn fit(popup: &HtmlElement, input: &HtmlInputElement) -> Result<(), JsValue> {
let window = web_sys::window().ok_or("no window")?;
if popup.child_element_count() == 0 {
return Ok(());
if !roomy(input_span(input), viewport(&window)) {
center(input);
let at = placement(
input_span(input),
viewport(&window),
f64::from(popup.scroll_height()),
);
apply(popup, input, &at)
/// The targets and events that move or shrink the visible area.
pub(crate) fn viewport_events() -> Vec<(EventTarget, &'static str)> {
let Some(window) = web_sys::window() else {
return Vec::new();
let mut events = vec![(EventTarget::from(window.clone()), "scroll")];
if let Some(view) = window.visual_viewport() {
let view = EventTarget::from(view);
events.push((view.clone(), "resize"));
events.push((view, "scroll"));
events
#[cfg(test)]
#[path = "place_tests.rs"]
mod tests;