From 5c873260407ddd96d69be31e5468d927b276bbb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Cocchi?= Date: Wed, 13 Sep 2023 11:51:41 +0200 Subject: [PATCH] Add a clear search button to search bar --- web/res/reset.svg | 1 + web/src/index.js | 67 ++++++++++++++++++++++++++----------------- web/src/search-box.js | 59 +++++++++++++++++++++++++++++++------ web/style/index.css | 2 +- web/style/index.sass | 53 ++++++++++++++++++++++------------ 5 files changed, 126 insertions(+), 56 deletions(-) create mode 100644 web/res/reset.svg diff --git a/web/res/reset.svg b/web/res/reset.svg new file mode 100644 index 0000000..be0a47f --- /dev/null +++ b/web/res/reset.svg @@ -0,0 +1 @@ + diff --git a/web/src/index.js b/web/src/index.js index a215c46..413d0c0 100644 --- a/web/src/index.js +++ b/web/src/index.js @@ -41,10 +41,13 @@ const supportedThemes = ["light", "dark", "black"] const defaultState = { packs: [], - filtering: { - searchTerm: "", - packs: [], - }, + loading: true, + error: null, +} + +const defaultSearchState = { + searchTerm: null, + filteredPacks: null } class App extends Component { @@ -52,9 +55,8 @@ class App extends Component { super(props) this.defaultTheme = params.get("theme") this.state = { - packs: defaultState.packs, - loading: true, - error: null, + ...defaultState, + ...defaultSearchState, stickersPerRow: parseInt(localStorage.mauStickersPerRow || "4"), theme: localStorage.mauStickerThemeOverride || this.defaultTheme, frequentlyUsed: { @@ -63,7 +65,6 @@ class App extends Component { stickerIDs: frequent.get(), stickers: [], }, - filtering: defaultState.filtering, } if (!supportedThemes.includes(this.state.theme)) { this.state.theme = "light" @@ -77,6 +78,7 @@ class App extends Component { this.packListRef = null this.navRef = null this.searchStickers = this.searchStickers.bind(this) + this.resetSearch = this.resetSearch.bind(this) this.sendSticker = this.sendSticker.bind(this) this.navScroll = this.navScroll.bind(this) this.reloadPacks = this.reloadPacks.bind(this) @@ -101,28 +103,31 @@ class App extends Component { localStorage.mauFrequentlyUsedStickerCache = JSON.stringify(stickers.map(sticker => [sticker.id, sticker])) } - searchStickers(e) { + // Search + + resetSearch() { + this.setState({ ...defaultSearchState }) + } + + 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({ - filtering: { - ...this.state.filtering, - searchTerm, - packs: packsWithFilteredStickers.filter(({ stickers }) => !!stickers.length), - }, - }) + this.setState({ searchTerm, filteredPacks }) } + // End search + setStickersPerRow(val) { localStorage.mauStickersPerRow = val document.documentElement.style.setProperty("--stickers-per-row", localStorage.mauStickersPerRow) @@ -145,10 +150,8 @@ class App extends Component { reloadPacks() { this.imageObserver.disconnect() this.sectionObserver.disconnect() - this.setState({ - packs: defaultState.packs, - filtering: defaultState.filtering, - }) + this.setState({ packs: defaultState.packs }) + this.resetSearch() this._loadPacks(true) } @@ -215,6 +218,9 @@ class App extends Component { for (const entry of intersections) { const packID = entry.target.getAttribute("data-pack-id") const navElement = document.getElementById(`nav-${packID}`) + if (!navElement) { + continue + } if (entry.isIntersecting) { navElement.classList.add("visible") const bb = navElement.getBoundingClientRect() @@ -258,6 +264,7 @@ class App extends Component { const sticker = this.stickersByID.get(id) frequent.add(id) this.updateFrequentlyUsed() + this.resetSearch() widgetAPI.sendSticker(sticker) } @@ -267,8 +274,10 @@ class App extends Component { render() { const theme = `theme-${this.state.theme}` - const filterActive = !!this.state.filtering.searchTerm - const packs = filterActive ? this.state.filtering.packs : [this.state.frequentlyUsed, ...this.state.packs] + + 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 />
` @@ -287,9 +296,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} /> + <${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 ba2ed5d..e9d6ed2 100644 --- a/web/src/search-box.js +++ b/web/src/search-box.js @@ -13,14 +13,55 @@ // // 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 const SearchBox = ({ onKeyUp, placeholder = 'Find stickers' }) => { - const component = html` - - ` - return component +export class SearchBox extends Component { + constructor(props) { + super(props) + + this.value = props.value + this.onSearch = props.onSearch + this.onReset = props.onReset + + this.search = this.search.bind(this) + this.clearSearch = this.clearSearch.bind(this) + } + + componentWillReceiveProps(props) { + this.value = props.value + } + + search(e) { + if (e.key === "Escape") { + this.clearSearch() + return + } + this.onSearch(e.target.value) + } + + clearSearch() { + this.onReset() + } + + render() { + const isEmpty = !this.value + + const className = `icon-display ${isEmpty ? null : 'reset-click-zone'}` + const title = isEmpty ? null : 'Click to reset' + const onClick = isEmpty ? null : this.clearSearch + const iconToDisplay = `icon-${isEmpty ? 'search' : 'reset'}` + + return html` + + ` + } } diff --git a/web/style/index.css b/web/style/index.css index e33e450..cfa66a7 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.icon-search{--icon-image: url(../res/search.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{position:relative;display:flex}div.search-box>input[type=text]{flex-grow:1;background-color:var(--search-box-color);outline:none;border:none;border-radius:.25rem;height:1rem;padding:.7rem;padding-right:calc(1rem + 0.7rem);margin:.5rem;font-size:1rem;color:var(--text-color)}div.search-box>span.icon{display:flex;position:absolute;top:calc(50% - 1rem / 2);right:1rem;width:1rem;height:1rem;box-sizing:border-box}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{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 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 c079f65..5885de9 100644 --- a/web/style/index.sass +++ b/web/style/index.sass @@ -92,9 +92,12 @@ main.theme-black &.icon-recent --icon-image: url(../res/recent.svg) - &.icon.icon-search + &.icon-search --icon-image: url(../res/search.svg) + &.icon-reset + --icon-image: url(../res/reset.svg) + nav display: flex overflow-x: auto @@ -166,30 +169,42 @@ div.sticker margin: 15% div.search-box - position: relative display: flex + margin: $search-box-input-margin + padding: 0 + border-radius: .4rem + background-color: var(--search-box-color) + opacity: .5 + transition: all .1s + border: 1px solid transparent - >input[type="text"] - flex-grow: 1 - background-color: var(--search-box-color) + input,.icon-display + color: var(--text-color) outline: none border: none - border-radius: .25rem height: $search-box-input-height + margin: 0 padding: $search-box-input-padding - padding-right: calc(#{$search-box-icon-size} + #{$search-box-input-padding}) - margin: $search-box-input-margin - font-size: 1rem - color: var(--text-color) + background-color: transparent + -webkit-tap-highlight-color: transparent - >span.icon - display: flex - position: absolute - top: calc(50% - #{$search-box-icon-size} / 2) - right: $search-box-icon-size - width: $search-box-icon-size - height: $search-box-icon-size - box-sizing: border-box + .icon-display + width: $search-box-input-height + + &.reset-click-zone + cursor: pointer + + .icon + display: block + width: $search-box-icon-size + height: $search-box-icon-size + + .icon-search + opacity: .5 + + input + flex-grow: 1 + font-size: 1rem div.settings-list display: flex @@ -202,5 +217,5 @@ div.settings-list padding: .5rem border-radius: .25rem - input + input:not([type="checkbox"]) width: 100%