Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(search): add global / shortcut to focus search bar #3916

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 57 additions & 24 deletions site/search/Autocomplete.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect } from "react"
import React, { useEffect, useRef, useState } from "react"
import { render } from "react-dom"
import {
AutocompleteApi,
AutocompleteSource,
Render,
autocomplete,
Expand All @@ -27,6 +28,7 @@ import {
} from "./searchClient.js"
import { queryParamsToStr } from "@ourworldindata/utils"
import { SiteAnalytics } from "../SiteAnalytics.js"
import Mousetrap from "mousetrap"

const siteAnalytics = new SiteAnalytics()

Expand Down Expand Up @@ -242,11 +244,17 @@ export function Autocomplete({
detachedMediaQuery?: string
panelClassName?: string
}) {
const containerRef = useRef<HTMLDivElement>(null)

const [search, setSearch] = useState<AutocompleteApi<BaseItem> | null>(null)

useEffect(() => {
if (!containerRef.current) return

const search = autocomplete({
placeholder,
detachedMediaQuery,
container: AUTOCOMPLETE_CONTAINER_ID,
container: containerRef.current,
classNames: {
panel: panelClassName,
},
Expand Down Expand Up @@ -282,31 +290,56 @@ export function Autocomplete({
plugins: [recentSearchesPlugin],
})

const container = document.querySelector(AUTOCOMPLETE_CONTAINER_ID)
if (container) {
const input = container.querySelector<HTMLInputElement>("input")
if (input) {
const inputId = input.id
const button = container.querySelector(
`label[for='${inputId}'] button`
)
// Disable the button on mount. We know there's no input because the element is created by JS
// and thus isn't persisted between navigations
button?.setAttribute("disabled", "true")
setSearch(search)

const input =
containerRef.current.querySelector<HTMLInputElement>("input")
if (input) {
const inputId = input.id
const button = containerRef.current.querySelector(
`label[for='${inputId}'] button`
)
// Disable the button on mount. We know there's no input because the element is created by JS
// and thus isn't persisted between navigations
button?.setAttribute("disabled", "true")

input.addEventListener("input", () => {
const isFormValid = input.checkValidity()
if (isFormValid) {
button?.removeAttribute("disabled")
} else {
button?.setAttribute("disabled", "true")
}
})
}
input.addEventListener("input", () => {
const isFormValid = input.checkValidity()
if (isFormValid) {
button?.removeAttribute("disabled")
} else {
button?.setAttribute("disabled", "true")
}
})
}

return () => search.destroy()
}, [onActivate, onClose, placeholder, detachedMediaQuery, panelClassName])
}, [
onActivate,
onClose,
placeholder,
detachedMediaQuery,
panelClassName,
containerRef,
])

// Register a global shortcut to open the search box on typing "/"
useEffect(() => {
marcelgerber marked this conversation as resolved.
Show resolved Hide resolved
if (!search) return

Mousetrap.bind("/", (e) => {
e.preventDefault() // don't type "/" into input
search.setIsOpen(true)

const input =
containerRef.current?.querySelector<HTMLInputElement>("input")
input?.focus()
})

return () => {
Mousetrap.unbind("/")
}
}, [search, containerRef])

return <div className={className} id="autocomplete" />
return <div className={className} ref={containerRef} id="autocomplete" />
}