A platform for community discussion. Free, open, simple.
FIX: prevents body scroll in input focus (#35497)
Joffrey JAFFEUX0a75c18f5736
diff --git a/app/assets/javascripts/discourse/app/modifiers/prevent-scroll-on-focus.js b/app/assets/javascripts/discourse/app/modifiers/prevent-scroll-on-focus.jsnew file mode 100644index 00000000000..15b85cc1e1e--- /dev/null+++ b/app/assets/javascripts/discourse/app/modifiers/prevent-scroll-on-focus.js@@ -0,0 +1,74 @@+import { registerDestructor } from "@ember/destroyable";+import { service } from "@ember/service";+import Modifier from "ember-modifier";+import { bind } from "discourse/lib/decorators";++/**+ * Modifier that prevents body scroll when focusing an input on iOS.+ *+ * This is a workaround for an iOS bug where the body scrolls when focusing+ * an input element near the keyboard, causing unwanted viewport scrolling.+ *+ * On non-iOS platforms, this modifier does nothing.+ */+export default class PreventScrollOnFocus extends Modifier {+ @service capabilities;++ constructor(owner, args) {+ super(owner, args);+ registerDestructor(this, (instance) => instance.cleanup());+ }++ modify(element) {+ if (!this.capabilities.isIOS) {+ return;+ }++ this.element = element;+ this.element.addEventListener("focus", this.preventScrollOnFocus);+ }++ /**+ * Focus event handler that prevents iOS scroll behavior.+ *+ * Creates a temporary off-screen clone of the input element, focuses the+ * clone first, then focuses the actual element. This technique prevents+ * iOS from scrolling the viewport when the input receives focus.+ *+ * The clone is positioned far off-screen and scaled to zero to ensure+ * it's not visible or interactable. After focusing both elements in+ * sequence, the clone is removed.+ *+ * Skips the clone process if:+ * - The relatedTarget is already a clone (to prevent infinite loops)+ * - The input type is "password" (to prevent issues with password managers)+ *+ * @param {FocusEvent} event - The focus event+ */+ @bind+ preventScrollOnFocus(event) {+ if (event.relatedTarget?.matches("[data-ios-input-clone]")) {+ return;+ }++ if (event.target.type === "password") {+ return;+ }++ const clone = event.target.cloneNode(false);+ clone.removeAttribute("id");+ clone.dataset.iosInputClone = true;+ clone.style.setProperty("position", "fixed");+ clone.style.setProperty("left", "0");+ clone.style.setProperty("top", "0");+ clone.style.setProperty("transform", "translateY(-3000px) scale(0)");+ document.documentElement.appendChild(clone);+ clone.focus({ preventScroll: true });+ event.target.focus({ preventScroll: true });+ clone.remove();+ }++ cleanup() {+ this.element?.removeEventListener("focus", this.preventScrollOnFocus);+ }+}diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjsindex 45588f37b08..425c68a5ea6 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer.gjs@@ -43,6 +43,7 @@ import { waitForClosedKeyboard } from "discourse/lib/wait-for-keyboard";import DAutocompleteModifier, {SKIP,} from "discourse/modifiers/d-autocomplete";+import preventScrollOnFocus from "discourse/modifiers/prevent-scroll-on-focus";import { i18n } from "discourse-i18n";import DButton from "discourse/plugins/chat/discourse/components/chat/composer/button";import ChatComposerDropdown from "discourse/plugins/chat/discourse/components/chat-composer-dropdown";@@ -56,7 +57,6 @@ import TextareaInteractor from "discourse/plugins/chat/discourse/lib/textarea-inconst CHAT_PRESENCE_KEEP_ALIVE = 5 * 1000; // 5 secondsexport default class ChatComposer extends Component {- @service capabilities;@service site;@service siteSettings;@service store;@@ -353,22 +353,8 @@ export default class ChatComposer extends Component {}@action- onTextareaFocusIn(event) {+ onTextareaFocusIn() {this.isFocused = true;-- if (!this.capabilities.isIOS) {- return;- }-- // hack to prevent the whole viewport to move on focus input- const textarea = event.target;- textarea.style.transform = "translateY(-99999px)";- textarea.focus();- window.requestAnimationFrame(() => {- window.requestAnimationFrame(() => {- textarea.style.transform = "";- });- });}@action@@ -750,6 +736,7 @@ export default class ChatComposer extends Component {{{didInsert this.setupTextareaInteractor}}{{on "input" this.onInput}}{{on "keydown" this.onKeyDown}}+ {{preventScrollOnFocus}}{{on "focusin" this.onTextareaFocusIn}}{{on "focusout" this.onTextareaFocusOut}}{{didInsert this.setupAutocomplete}}