From 26ab8e282199da2646092b49d249646bedcae8e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Cocchi?= Date: Sun, 6 Feb 2022 16:25:38 +0100 Subject: [PATCH] properly handle mobile devices for autofocus --- web/src/index.js | 16 +++++++++++----- web/src/search-box.js | 18 +++++++++++++++--- web/src/user-agent-detect.js | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 web/src/user-agent-detect.js diff --git a/web/src/index.js b/web/src/index.js index 78105b8..7c5efed 100644 --- a/web/src/index.js +++ b/web/src/index.js @@ -15,7 +15,8 @@ // along with this program. If not, see . import { html, render, Component } from "../lib/htm/preact.js" import { Spinner } from "./spinner.js" -import { shouldAutofocusSearchBar, SearchBox } from "./search-box.js" +import { shouldAutofocusSearchBar, shouldDisplayAutofocusSearchBar, SearchBox } from "./search-box.js" +import { checkMobileSafari } from "./user-agent-detect.js" import * as widgetAPI from "./widget-api.js" import * as frequent from "./frequently-used.js" @@ -35,7 +36,12 @@ const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${ // We need to detect iOS webkit because it has a bug related to scrolling non-fixed divs // This is also used to fix scrolling to sections on Element iOS -const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/) +const isMobileSafari = checkMobileSafari() + +// We need to detect iOS webkit / Android because autofocusing a field does not open +// the device keyboard by design, making the option obsolete +const shouldAutofocusOption = shouldAutofocusSearchBar() +const displayAutofocusOption = shouldDisplayAutofocusSearchBar() const supportedThemes = ["light", "dark", "black"] @@ -338,14 +344,14 @@ const Settings = ({ app }) => html` -
+ ${displayAutofocusOption ? html`
Autofocus search bar: app.setAutofocusSearchBar(evt.target.checked)} /> -
+
` : null} ` diff --git a/web/src/search-box.js b/web/src/search-box.js index cce2032..914cdd2 100644 --- a/web/src/search-box.js +++ b/web/src/search-box.js @@ -14,11 +14,15 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . import { html, Component } from "../lib/htm/preact.js" +import { checkMobileSafari, checkAndroid } from "./user-agent-detect.js" -export function shouldAutofocusSearchBar() { - return localStorage.mauAutofocusSearchBar === 'true' +export function shouldDisplayAutofocusSearchBar() { + return !checkMobileSafari() && !checkAndroid() } +export function shouldAutofocusSearchBar() { + return localStorage.mauAutofocusSearchBar === 'true' && shouldDisplayAutofocusSearchBar() +} export class SearchBox extends Component { constructor(props) { @@ -33,6 +37,14 @@ export class SearchBox extends Component { this.clearSearch = this.clearSearch.bind(this) } + componentDidMount() { + // hack required for firefox + const inputInWebView = document.querySelector('.search-box input') + if (inputInWebView && this.autofocus) { + inputInWebView.focus() + } + } + componentWillReceiveProps(props) { this.value = props.value } @@ -63,7 +75,7 @@ export class SearchBox extends Component { placeholder="Find stickers …" value=${this.value} onKeyUp=${this.search} - autofocus=${this.autofocus} + autoFocus=${this.autofocus} />
diff --git a/web/src/user-agent-detect.js b/web/src/user-agent-detect.js new file mode 100644 index 0000000..474a479 --- /dev/null +++ b/web/src/user-agent-detect.js @@ -0,0 +1,18 @@ +export const getUserAgent = () => navigator.userAgent || navigator.vendor || window.opera + +export const checkiOSDevice = () => { + const agent = getUserAgent() + return agent.match(/(iPod|iPhone|iPad)/) +} + +export const checkMobileSafari = () => { + const agent = getUserAgent() + return agent.match(/(iPod|iPhone|iPad)/) && agent.match(/AppleWebKit/) +} + +export const checkAndroid = () => { + const agent = getUserAgent() + return agent.match(/android/i) +} + +export const checkMobileDevice = () => checkiOSDevice() || checkAndroid()