A platform for community discussion. Free, open, simple.
FIX: Add composer to scroll-lock on iOS devices (#41366)
Bannon Tanner40996dc89baf
diff --git a/frontend/discourse/app/lib/body-scroll-lock.js b/frontend/discourse/app/lib/body-scroll-lock.jsindex 4c9ae7994d0..d0e0c79e803 100644--- a/frontend/discourse/app/lib/body-scroll-lock.js+++ b/frontend/discourse/app/lib/body-scroll-lock.js@@ -5,12 +5,24 @@*/const isServer = () => typeof window === "undefined";+const isDiscourseIOS = () => {+ if (typeof document === "undefined") {+ return false;+ }++ const html = document.documentElement;+ return (+ html.classList.contains("ios-device") ||+ html.classList.contains("ipados-device")+ );+};const detectOS = (ua) => {+ const uaProvided = Boolean(ua);ua = ua || navigator.userAgent;const ipad = /(iPad).*OS\s([\d_]+)/.test(ua);const iphone = !ipad && /(iPhone\sOS)\s([\d_]+)/.test(ua);const android = /(Android);?[\s/]+([\d.]+)?/.test(ua);- const ios = iphone || ipad;+ const ios = iphone || ipad || (!uaProvided && isDiscourseIOS());return { ios, android };};function getEventListenerOptions(options) {diff --git a/frontend/discourse/app/lib/composer/composer-position.js b/frontend/discourse/app/lib/composer/composer-position.jsindex e8b5c3ed65d..c294cec56b9 100644--- a/frontend/discourse/app/lib/composer/composer-position.js+++ b/frontend/discourse/app/lib/composer/composer-position.js@@ -1,4 +1,5 @@import { later } from "@ember/runloop";+import { lock, unlock } from "discourse/lib/body-scroll-lock";import { applyBehaviorTransformer } from "discourse/lib/transformer";export function setupComposerPosition(editor) {@@ -6,6 +7,52 @@ export function setupComposerPosition(editor) {// for Safari iOS/iPad and Firefox on Android// The fixes here go together with styling in base/compose.cssconst html = document.documentElement;+ const isMobileOrIpad =+ html.classList.contains("mobile-device") ||+ html.classList.contains("ipados-device");+ const isIOS = html.classList.contains("ios-device");+ const isIpadOS = html.classList.contains("ipados-device");++ let editorFocused = document.activeElement === editor;+ let scrollLocked = false;+ let scrollLockTargets = null;++ function shouldLockScroll() {+ const ipadHardwareKeyboard =+ isIpadOS && !html.classList.contains("keyboard-visible");+ return editorFocused && isIOS && !ipadHardwareKeyboard;+ }++ function getAllowedScrollTargets() {+ const replyControl = document.getElementById("reply-control");+ return [+ editor,+ replyControl?.querySelector(".d-editor-preview-wrapper"),+ replyControl?.querySelector(".d-editor-button-bar__wrap"),+ ].filter(Boolean);+ }++ function refreshScrollLock() {+ if (shouldLockScroll() && !scrollLocked) {+ scrollLockTargets = getAllowedScrollTargets();+ lock(scrollLockTargets);+ scrollLocked = true;+ } else if (!shouldLockScroll() && scrollLocked) {+ unlock(scrollLockTargets);+ scrollLockTargets = null;+ scrollLocked = false;+ }+ }++ function onFocus() {+ editorFocused = true;+ refreshScrollLock();+ }++ function onBlur() {+ editorFocused = false;+ refreshScrollLock();+ }function editorTouchMove(event) {// This is an alternative to locking up the body@@ -21,24 +68,48 @@ export function setupComposerPosition(editor) {});}- if (- html.classList.contains("mobile-device") ||- html.classList.contains("ipados-device")- ) {+ let classObserver;+ if (isMobileOrIpad) {window.addEventListener("scroll", correctScrollPosition);correctScrollPosition();editor.addEventListener("touchmove", editorTouchMove);}+ if (isIOS) {+ editor.addEventListener("focus", onFocus);+ editor.addEventListener("blur", onBlur);+ refreshScrollLock();+ }++ if (isIpadOS) {+ classObserver = new MutationObserver(refreshScrollLock);+ classObserver.observe(html, {+ attributes: true,+ attributeFilter: ["class"],+ });+ }+// destructorreturn () => {- if (- html.classList.contains("mobile-device") ||- html.classList.contains("ipados-device")- ) {+ if (isMobileOrIpad) {window.removeEventListener("scroll", correctScrollPosition);editor.removeEventListener("touchmove", editorTouchMove);}++ if (isIOS) {+ editor.removeEventListener("focus", onFocus);+ editor.removeEventListener("blur", onBlur);++ if (scrollLocked) {+ unlock(scrollLockTargets);+ scrollLockTargets = null;+ scrollLocked = false;+ }+ }++ if (isIpadOS) {+ classObserver?.disconnect();+ }};}diff --git a/frontend/discourse/select-kit/components/select-kit/select-kit-collection.gjs b/frontend/discourse/select-kit/components/select-kit/select-kit-collection.gjsindex 85c3723c43e..7a795e1fd5b 100644--- a/frontend/discourse/select-kit/components/select-kit/select-kit-collection.gjs+++ b/frontend/discourse/select-kit/components/select-kit/select-kit-collection.gjs@@ -17,7 +17,7 @@ export default class SelectKitCollection extends Component {}const isChildOfLock = getLockState().lockedElements.some((locked) =>- locked.targetElement.contains(element)+ (locked.targetElement ?? locked).contains(element));if (isChildOfLock) {