Skip to content

feat: Implement shortcut to focus search #275

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

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
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
65 changes: 60 additions & 5 deletions src/components/DocSearch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 53 additions & 3 deletions src/components/DocSearch.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,40 @@ type options = {
inputSelector: string,
}

@bs.val @bs.scope("document")
external activeElement: option<Dom.element> = "activeElement"

@bs.val @bs.scope("window")
external docsearch: option<options => unit> = "docsearch"

type keyboardEventLike = {key: string, ctrlKey: bool, metaKey: bool}

@bs.val @bs.scope("window")
external addKeyboardEventListener: (string, keyboardEventLike => unit) => unit = "addEventListener"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, not sure if you are aware of this. You can set a fixed param like keydown via the as("keydown") _ placeholder:

Suggested change
external addKeyboardEventListener: (string, keyboardEventLike => unit) => unit = "addEventListener"
external addKeydownEventListener: (@as("keydown") _, keyboardEventLike => unit) => unit = "addEventListener"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will merge anyways, since this will be easy to refactor later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice didn't know this is possible! :)


@bs.val @bs.scope("window")
external removeKeyboardEventListener: (string, keyboardEventLike => unit) => unit =
"addEventListener"

@bs.send
external keyboardEventPreventDefault: keyboardEventLike => unit = "preventDefault"

type state =
| Active
| Inactive

@bs.get external isContentEditable: Dom.element => bool = "isContentEditable"
@bs.get external tagName: Dom.element => string = "tagName"
@bs.send external focus: Dom.element => unit = "focus"
@bs.send external blur: Dom.element => unit = "blur"
@bs.set external value: (Dom.element, string) => unit = "value"

@react.component
let make = () => {
// Used for the text input
let inputRef = React.useRef(Js.Nullable.null)
let (state, setState) = React.useState(_ => Inactive)

React.useEffect1(() => {
switch docsearch {
| Some(init) =>
Expand All @@ -27,12 +48,41 @@ let make = () => {
})
| None => ()
}

None
}, [])

// Used for the text input
let inputRef = React.useRef(Js.Nullable.null)
let (state, setState) = React.useState(_ => Inactive)
React.useEffect1(() => {
let isEditableTag = el =>
switch el->tagName {
| "TEXTAREA" | "SELECT" | "INPUT" => true
| _ => false
}

let focusSearch = e => {
switch activeElement {
| Some(el) when el->isEditableTag => ()
| Some(el) when el->isContentEditable => ()
| _ => {
setState(_ => Active)
inputRef.current->Js.Nullable.toOption->Belt.Option.forEach(focus)

e->keyboardEventPreventDefault
}
}
}

let handleGlobalKeyDown = e => {
switch e.key {
| "/" => focusSearch(e)
| "k" when e.ctrlKey || e.metaKey => focusSearch(e)
| _ => ()
}
}

addKeyboardEventListener("keydown", handleGlobalKeyDown)
Some(() => removeKeyboardEventListener("keydown", handleGlobalKeyDown))
}, [setState])

let focusInput = () =>
inputRef.current->Js.Nullable.toOption->Belt.Option.forEach(el => el->focus)
Expand Down