From 909aaf8d44ae0fc85570824210b08de20961c8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Cocchi?= Date: Sun, 6 Feb 2022 15:47:38 +0100 Subject: [PATCH] improve for mobile support --- web/src/index.js | 42 +++++++++++++------------ web/src/search-box.js | 72 ++++++++++++++++++++++++++++++++++--------- web/style/index.css | 2 +- web/style/index.sass | 34 +++++++++++--------- 4 files changed, 101 insertions(+), 49 deletions(-) diff --git a/web/src/index.js b/web/src/index.js index 6307a09..78105b8 100644 --- a/web/src/index.js +++ b/web/src/index.js @@ -41,7 +41,13 @@ const supportedThemes = ["light", "dark", "black"] const defaultState = { packs: [], + loading: true, + error: null, +} + +const defaultSearchState = { searchTerm: null, + filteredPacks: null } class App extends Component { @@ -50,8 +56,7 @@ class App extends Component { this.defaultTheme = params.get("theme") this.state = { ...defaultState, - loading: true, - error: null, + ...defaultSearchState, stickersPerRow: parseInt(localStorage.mauStickersPerRow || "4"), theme: localStorage.mauStickerThemeOverride || this.defaultTheme, frequentlyUsed: { @@ -101,31 +106,24 @@ class App extends Component { // Search resetSearch() { - this.setState({ searchTerm: defaultState.searchTerm }) + this.setState({ ...defaultSearchState }) } - searchStickers(e) { - if (e.key === "Escape") { - this.resetSearch() - return - } - + searchStickers(searchTerm) { const sanitizeString = s => s.toLowerCase().trim() - const searchTerm = sanitizeString(e.target.value) + const sanitizedSearch = sanitizeString(searchTerm) const allPacks = [this.state.frequentlyUsed, ...this.state.packs] const packsWithFilteredStickers = allPacks.map(pack => ({ ...pack, stickers: pack.stickers.filter(sticker => - sanitizeString(sticker.body).includes(searchTerm) || - sanitizeString(sticker.id).includes(searchTerm) + sanitizeString(sticker.body).includes(sanitizedSearch) || + sanitizeString(sticker.id).includes(sanitizedSearch) ), })) + const filteredPacks = packsWithFilteredStickers.filter(({ stickers }) => !!stickers.length) - this.setState({ - filteredPacks: packsWithFilteredStickers.filter(({ stickers }) => !!stickers.length), - searchTerm, - }) + this.setState({ searchTerm, filteredPacks }) } // End search @@ -284,8 +282,10 @@ class App extends Component { render() { const theme = `theme-${this.state.theme}` - const filterActive = !!this.state.searchTerm + + const filterActive = !!this.state.filteredPacks const packs = filterActive ? this.state.filteredPacks : [this.state.frequentlyUsed, ...this.state.packs] + const noPacksForSearch = filterActive && packs.length === 0 if (this.state.loading) { return html`
<${Spinner} size=${80} green />
` @@ -304,9 +304,13 @@ class App extends Component { ${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)} <${NavBarItem} pack=${{ id: "settings", title: "Settings" }} iconOverride="settings" /> - <${SearchBox} onKeyUp=${this.searchStickers} resetSearch=${this.resetSearch} value=${this.state.searchTerm} /> + <${SearchBox} + value=${this.state.searchTerm} + onSearch=${this.searchStickers} + onReset=${this.resetSearch} + />
this.packListRef = elem}> - ${filterActive && packs.length === 0 ? html`

No stickers match your search

` : null} + ${noPacksForSearch ? html`

No stickers match your search

` : null} ${packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)} <${Settings} app=${this}/>
diff --git a/web/src/search-box.js b/web/src/search-box.js index 13ae3cf..3360d6a 100644 --- a/web/src/search-box.js +++ b/web/src/search-box.js @@ -13,27 +13,71 @@ // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { html } from "../lib/htm/preact.js" +import { html, Component } from "../lib/htm/preact.js" export function shouldAutofocusSearchBar() { return localStorage.mauAutofocusSearchBar === 'true' } -export const SearchBox = ({ onKeyUp, resetSearch, value }) => { - const autofocus = shouldAutofocusSearchBar() +const SEARCHBOX_CLASSNAME = 'search-box' - const className = `icon-display ${value ? 'reset-click-zone' : null}` - const title = value ? 'Click to reset' : null - const onClick = value ? resetSearch : null +export class SearchBox extends Component { + constructor(props) { + super(props) - const iconToDisplay = `icon-${!value ? 'search' : 'reset'}` + this.autofocus = shouldAutofocusSearchBar() + this.value = props.value + this.onSearch = props.onSearch + this.onReset = props.onReset - return html` -