From 9b87f7436ff03e083b1b1b0dde780da31e7cb054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Cocchi?= Date: Sun, 6 Feb 2022 18:41:40 +0100 Subject: [PATCH] handle focus for iframe --- web/src/search-box.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/web/src/search-box.js b/web/src/search-box.js index 914cdd2..dcae44f 100644 --- a/web/src/search-box.js +++ b/web/src/search-box.js @@ -32,16 +32,34 @@ export class SearchBox extends Component { this.value = props.value this.onSearch = props.onSearch this.onReset = props.onReset + this.hasBeenFocused = false + this.focusInput = this.focusInput.bind(this) this.search = this.search.bind(this) this.clearSearch = this.clearSearch.bind(this) } - componentDidMount() { - // hack required for firefox + focusInput() { const inputInWebView = document.querySelector('.search-box input') if (inputInWebView && this.autofocus) { inputInWebView.focus() + this.hasBeenFocused = true + } + } + + componentDidMount() { + this.focusInput() + } + + componentDidUpdate() { + // check if we're in an iframe (Element desktop) + if (window.self !== window.top) { + // check if the iframe is not closed + if (document.querySelector('.search-box').offsetParent === null) { + this.focusInput() + } else { + this.hasBeenFocused = false + } } }