From 5ab501c92fc22845ef45f55d4461ace99bc1e4b2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 11 Oct 2022 12:15:46 +0200 Subject: [PATCH] LibWeb: Scroll elements into view when they become focused This makes both user-interactive (tab keys) and programmatic focus changes scroll the viewport if necessary to reveal the newly focused element. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 4 ++++ Userland/Libraries/LibWeb/DOM/Element.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index e80db840ff..2774d01ee3 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1427,6 +1427,10 @@ void Document::set_focused_element(Element* element) if (m_layout_root) m_layout_root->set_needs_display(); + + // Scroll the viewport if necessary to make the newly focused element visible. + if (m_focused_element) + m_focused_element->scroll_into_view(); } void Document::set_active_element(Element* element) diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index f3b8736d77..f45fad6203 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -160,7 +160,7 @@ public: WebIDL::ExceptionOr insert_adjacent_text(String const& where, String const& data); // https://w3c.github.io/csswg-drafts/cssom-view-1/#dom-element-scrollintoview - void scroll_into_view(Optional>); + void scroll_into_view(Optional> = {}); protected: Element(Document&, DOM::QualifiedName);