From e2fffe70b3f04fd5c222734b2bdaf67c2d07f1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Cocchi?= Date: Wed, 13 Sep 2023 11:52:30 +0200 Subject: [PATCH] (Try to) autofocus the search bar when opening the picker --- web/src/index.js | 26 ++++++++++++++++++++++++-- web/src/search-box.js | 22 ++++++++++++++++++++++ web/src/user-agent-detect.js | 18 ++++++++++++++++++ web/src/widget-api.js | 10 +++++++--- web/style/index.css | 2 +- web/style/index.sass | 7 +++++++ 6 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 web/src/user-agent-detect.js diff --git a/web/src/index.js b/web/src/index.js index 413d0c0..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 { 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"] @@ -128,6 +134,8 @@ class App extends Component { // End search + // Settings + setStickersPerRow(val) { localStorage.mauStickersPerRow = val document.documentElement.style.setProperty("--stickers-per-row", localStorage.mauStickersPerRow) @@ -147,6 +155,12 @@ class App extends Component { } } + setAutofocusSearchBar(checked) { + localStorage.mauAutofocusSearchBar = checked + } + + // End settings + reloadPacks() { this.imageObserver.disconnect() this.sectionObserver.disconnect() @@ -330,6 +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 e9d6ed2..ea50c77 100644 --- a/web/src/search-box.js +++ b/web/src/search-box.js @@ -14,11 +14,28 @@ // 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 shouldDisplayAutofocusSearchBar() { + return !checkMobileSafari() && !checkAndroid() +} + +export function shouldAutofocusSearchBar() { + return localStorage.mauAutofocusSearchBar === 'true' && shouldDisplayAutofocusSearchBar() +} + +export function focusSearchBar() { + const inputInWebView = document.querySelector('.search-box input') + if (inputInWebView && shouldAutofocusSearchBar()) { + inputInWebView.focus() + } +} export class SearchBox extends Component { constructor(props) { super(props) + this.autofocus = shouldAutofocusSearchBar() this.value = props.value this.onSearch = props.onSearch this.onReset = props.onReset @@ -27,6 +44,10 @@ export class SearchBox extends Component { this.clearSearch = this.clearSearch.bind(this) } + componentDidMount() { + focusSearchBar() + } + componentWillReceiveProps(props) { this.value = props.value } @@ -57,6 +78,7 @@ export class SearchBox extends Component { placeholder="Find stickers …" value=${this.value} onKeyUp=${this.search} + 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() diff --git a/web/src/widget-api.js b/web/src/widget-api.js index fa72165..433aa61 100644 --- a/web/src/widget-api.js +++ b/web/src/widget-api.js @@ -13,6 +13,8 @@ // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +import { focusSearchBar } from "./search-box.js" + let widgetId = null window.onmessage = event => { @@ -33,10 +35,12 @@ window.onmessage = event => { widgetId = request.widgetId } - let response + let response = {} - if (request.action === "visibility") { - response = {} + if (request.action === "visibility") { // visibility of the widget changed + if (request.visible) { + focusSearchBar() // we have to re-focus the search bar when appropriate + } } else if (request.action === "capabilities") { response = { capabilities: ["m.sticker"] } } else { diff --git a/web/style/index.css b/web/style/index.css index cfa66a7..9a45fe1 100644 --- a/web/style/index.css +++ b/web/style/index.css @@ -1 +1 @@ -*{font-family:sans-serif}body{margin:0}h1{font-size:1rem}:root{--stickers-per-row: 4;--sticker-size: calc(100vw / var(--stickers-per-row))}main{color:var(--text-color)}main.spinner{margin-top:5rem}main.error,main.empty{margin:2rem}main.empty{text-align:center}main.has-content{position:fixed;top:0;left:0;right:0;bottom:0;display:grid;grid-template-rows:calc(12vw + 2px) min-content auto}main.theme-light{--highlight-color: #eee;--search-box-color: var(--highlight-color);--text-color: black;background-color:#fff}main.theme-dark{--highlight-color: #444;--search-box-color: #383e4b;--text-color: white;background-color:#22262e}main.theme-black{--highlight-color: #222;--search-box-color: var(--highlight-color);--text-color: white;background-color:#000}.icon{width:100%;height:100%;background-color:var(--text-color);mask-size:contain;-webkit-mask-size:contain;mask-image:var(--icon-image);-webkit-mask-image:var(--icon-image)}.icon.icon-settings{--icon-image: url(../res/settings.svg)}.icon.icon-recent{--icon-image: url(../res/recent.svg)}.icon.icon-search{--icon-image: url(../res/search.svg)}.icon.icon-reset{--icon-image: url(../res/reset.svg)}nav{display:flex;overflow-x:auto}nav>a{border-bottom:2px solid transparent}nav>a.visible{border-bottom-color:green}nav>a>div.sticker{width:12vw;height:12vw}div.pack-list,nav{scrollbar-width:none}div.pack-list::-webkit-scrollbar,nav::-webkit-scrollbar{display:none}div.pack-list{overflow-y:auto}div.pack-list.ios-safari-hack{position:fixed;top:calc(calc(12vw + 2px) + calc(2 * 0.7rem + 2 * 0.5rem + 1rem));bottom:0;left:0;right:0;-webkit-overflow-scrolling:touch}div.search-empty{margin:1.2rem;text-align:center}section.stickerpack{margin-top:.75rem}section.stickerpack>div.sticker-list{display:flex;flex-wrap:wrap}section.stickerpack>h1{margin:0 0 0 .75rem}div.sticker{display:flex;padding:4px;cursor:pointer;position:relative;width:var(--sticker-size);height:var(--sticker-size);box-sizing:border-box}div.sticker:hover{background-color:var(--highlight-color)}div.sticker>img{display:none;width:100%;object-fit:contain}div.sticker>img.visible{display:initial}div.sticker>.icon{width:70%;height:70%;margin:15%}div.search-box{display:flex;margin:.5rem;padding:0;border-radius:.4rem;background-color:var(--search-box-color);opacity:.5;transition:all .1s;border:1px solid transparent}div.search-box input,div.search-box .icon-display{color:var(--text-color);outline:none;border:none;height:1rem;margin:0;padding:.7rem;background-color:transparent;-webkit-tap-highlight-color:transparent}div.search-box .icon-display{width:1rem}div.search-box .icon-display.reset-click-zone{cursor:pointer}div.search-box .icon-display .icon{display:block;width:1rem;height:1rem}div.search-box .icon-display .icon-search{opacity:.5}div.search-box input{flex-grow:1;font-size:1rem}div.settings-list{display:flex;flex-direction:column}div.settings-list>*{margin:.5rem}div.settings-list button{padding:.5rem;border-radius:.25rem}div.settings-list input:not([type=checkbox]){width:100%} +*{font-family:sans-serif}body{margin:0}h1{font-size:1rem}:root{--stickers-per-row: 4;--sticker-size: calc(100vw / var(--stickers-per-row))}main{color:var(--text-color)}main.spinner{margin-top:5rem}main.error,main.empty{margin:2rem}main.empty{text-align:center}main.has-content{position:fixed;top:0;left:0;right:0;bottom:0;display:grid;grid-template-rows:calc(12vw + 2px) min-content auto}main.theme-light{--highlight-color: #eee;--search-box-color: var(--highlight-color);--text-color: black;background-color:#fff}main.theme-dark{--highlight-color: #444;--search-box-color: #383e4b;--text-color: white;background-color:#22262e}main.theme-black{--highlight-color: #222;--search-box-color: var(--highlight-color);--text-color: white;background-color:#000}.icon{width:100%;height:100%;background-color:var(--text-color);mask-size:contain;-webkit-mask-size:contain;mask-image:var(--icon-image);-webkit-mask-image:var(--icon-image)}.icon.icon-settings{--icon-image: url(../res/settings.svg)}.icon.icon-recent{--icon-image: url(../res/recent.svg)}.icon.icon-search{--icon-image: url(../res/search.svg)}.icon.icon-reset{--icon-image: url(../res/reset.svg)}nav{display:flex;overflow-x:auto}nav>a{border-bottom:2px solid transparent}nav>a.visible{border-bottom-color:green}nav>a>div.sticker{width:12vw;height:12vw}div.pack-list,nav{scrollbar-width:none}div.pack-list::-webkit-scrollbar,nav::-webkit-scrollbar{display:none}div.pack-list{overflow-y:auto}div.pack-list.ios-safari-hack{position:fixed;top:calc(calc(12vw + 2px) + calc(2 * 0.7rem + 2 * 0.5rem + 1rem));bottom:0;left:0;right:0;-webkit-overflow-scrolling:touch}div.search-empty{margin:1.2rem;text-align:center}section.stickerpack{margin-top:.75rem}section.stickerpack>div.sticker-list{display:flex;flex-wrap:wrap}section.stickerpack>h1{margin:0 0 0 .75rem}div.sticker{display:flex;padding:4px;cursor:pointer;position:relative;width:var(--sticker-size);height:var(--sticker-size);box-sizing:border-box}div.sticker:hover{background-color:var(--highlight-color)}div.sticker>img{display:none;width:100%;object-fit:contain}div.sticker>img.visible{display:initial}div.sticker>.icon{width:70%;height:70%;margin:15%}div.search-box{display:flex;margin:.5rem;padding:0;border-radius:.4rem;background-color:var(--search-box-color);opacity:.5;transition:all .1s;border:1px solid transparent}div.search-box:focus-within{opacity:1;border:1px solid var(--text-color)}div.search-box:not(:focus-within):hover{opacity:.7}div.search-box input,div.search-box .icon-display{color:var(--text-color);outline:none;border:none;height:1rem;margin:0;padding:.7rem;background-color:transparent;-webkit-tap-highlight-color:transparent}div.search-box .icon-display{width:1rem}div.search-box .icon-display.reset-click-zone{cursor:pointer}div.search-box .icon-display .icon{display:block;width:1rem;height:1rem}div.search-box .icon-display .icon-search{opacity:.5}div.search-box input{flex-grow:1;font-size:1rem}div.settings-list{display:flex;flex-direction:column}div.settings-list>*{margin:.5rem}div.settings-list button{padding:.5rem;border-radius:.25rem}div.settings-list input:not([type=checkbox]){width:100%} diff --git a/web/style/index.sass b/web/style/index.sass index 5885de9..caa7162 100644 --- a/web/style/index.sass +++ b/web/style/index.sass @@ -178,6 +178,13 @@ div.search-box transition: all .1s border: 1px solid transparent + &:focus-within + opacity: 1 + border: 1px solid var(--text-color) + + &:not(:focus-within):hover + opacity: .7 + input,.icon-display color: var(--text-color) outline: none