handle autofocus search + setting on/off

This commit is contained in:
Kévin Cocchi 2022-02-06 12:18:11 +01:00
parent 41f0432e8d
commit fed4f08218
2 changed files with 29 additions and 2 deletions

View File

@ -15,7 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { html, render, Component } from "../lib/htm/preact.js" import { html, render, Component } from "../lib/htm/preact.js"
import { Spinner } from "./spinner.js" import { Spinner } from "./spinner.js"
import { SearchBox } from "./search-box.js" import { shouldAutofocusSearchBar, SearchBox } from "./search-box.js"
import * as widgetAPI from "./widget-api.js" import * as widgetAPI from "./widget-api.js"
import * as frequent from "./frequently-used.js" import * as frequent from "./frequently-used.js"
@ -128,6 +128,10 @@ class App extends Component {
}) })
} }
// End search
// Settings
setStickersPerRow(val) { setStickersPerRow(val) {
localStorage.mauStickersPerRow = val localStorage.mauStickersPerRow = val
document.documentElement.style.setProperty("--stickers-per-row", localStorage.mauStickersPerRow) document.documentElement.style.setProperty("--stickers-per-row", localStorage.mauStickersPerRow)
@ -147,6 +151,12 @@ class App extends Component {
} }
} }
setAutofocusSearchBar(checked) {
localStorage.mauAutofocusSearchBar = checked
}
// End settings
reloadPacks() { reloadPacks() {
this.imageObserver.disconnect() this.imageObserver.disconnect()
this.sectionObserver.disconnect() this.sectionObserver.disconnect()
@ -218,6 +228,9 @@ class App extends Component {
for (const entry of intersections) { for (const entry of intersections) {
const packID = entry.target.getAttribute("data-pack-id") const packID = entry.target.getAttribute("data-pack-id")
const navElement = document.getElementById(`nav-${packID}`) const navElement = document.getElementById(`nav-${packID}`)
if (!navElement) {
continue
}
if (entry.isIntersecting) { if (entry.isIntersecting) {
navElement.classList.add("visible") navElement.classList.add("visible")
const bb = navElement.getBoundingClientRect() const bb = navElement.getBoundingClientRect()
@ -321,6 +334,14 @@ const Settings = ({ app }) => html`
<option value="black">Black</option> <option value="black">Black</option>
</select> </select>
</div> </div>
<div>
Autofocus search bar:
<input
type="checkbox"
checked=${shouldAutofocusSearchBar()}
onChange=${evt => app.setAutofocusSearchBar(evt.target.checked)}
/>
</div>
</div> </div>
</section> </section>
` `

View File

@ -15,7 +15,13 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { html } from "../lib/htm/preact.js" import { html } from "../lib/htm/preact.js"
export function shouldAutofocusSearchBar() {
return localStorage.mauAutofocusSearchBar === 'true'
}
export const SearchBox = ({ onKeyUp, resetSearch, value }) => { export const SearchBox = ({ onKeyUp, resetSearch, value }) => {
const autofocus = shouldAutofocusSearchBar()
const className = `icon-display ${value ? 'reset-click-zone' : null}` const className = `icon-display ${value ? 'reset-click-zone' : null}`
const title = value ? 'Click to reset' : null const title = value ? 'Click to reset' : null
const onClick = value ? resetSearch : null const onClick = value ? resetSearch : null
@ -24,7 +30,7 @@ export const SearchBox = ({ onKeyUp, resetSearch, value }) => {
return html` return html`
<div class="search-box"> <div class="search-box">
<input value=${value} placeholder="Find stickers" onKeyUp=${onKeyUp} /> <input value=${value} placeholder="Find stickers" onKeyUp=${onKeyUp} autoFocus=${autofocus} />
<div class=${className} title=${title} onClick=${onClick}> <div class=${className} title=${title} onClick=${onClick}>
<span class="icon ${iconToDisplay}" /> <span class="icon ${iconToDisplay}" />
</div> </div>