-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Custom dialogs and file structure (#9)
1. Use custom dialog implementation to increase compatibility since, for now, the <dialog> element doesn't provide the compatibility we need 2. Revamp file structure to make long files shorter and more readable Refs: fliegwerk/fliegdoc#26 Closes: fliegwerk/fliegdoc#26
- Loading branch information
Showing
27 changed files
with
529 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tabWidth": 1, | ||
"singleQuote": true, | ||
"useTabs": true, | ||
"endOfLine": "lf" | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ResultSelectionParams } from '../lib/result-selection-params'; | ||
|
||
export interface DialogContentParams extends ResultSelectionParams { | ||
reset: Function; | ||
searchFn: Function; | ||
onClose: Function; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { h } from 'preact'; | ||
import { Ref, useRef, useState } from 'preact/hooks'; | ||
import SearchResult from './search-result'; | ||
import { useOnChangeCallback } from '../hooks/use-on-change-callback'; | ||
import { Preface } from './preface'; | ||
import { useOnSubmitCallback } from '../hooks/use-on-submit-callback'; | ||
import { Form } from './form'; | ||
import { DialogContentParams } from './dialog-content-params'; | ||
|
||
export default function DialogContent(props: DialogContentParams) { | ||
const [results, setResults] = useState< | ||
{ url: string; text: string; title: string }[] | ||
>([]); | ||
const [query, setQuery] = useState(''); | ||
const listRef = useRef<HTMLUListElement>(null); | ||
const onChange = useOnChangeCallback(setQuery, setResults, props, listRef); | ||
const onSubmit = useOnSubmitCallback(results, props, onChange); | ||
|
||
return ( | ||
<div> | ||
<Preface /> | ||
{getForm({ | ||
onSubmit: onSubmit, | ||
query: query, | ||
onChange: onChange, | ||
props: props, | ||
results: results, | ||
})} | ||
{getResultList(listRef, results, props)} | ||
</div> | ||
); | ||
} | ||
|
||
function getForm({ | ||
onSubmit, | ||
query, | ||
onChange, | ||
props, | ||
results, | ||
}: { | ||
onSubmit: (evt: Event) => void; | ||
query: string; | ||
onChange: (value: string) => void; | ||
props: DialogContentParams; | ||
results: { url: string; text: string; title: string }[]; | ||
}) { | ||
return ( | ||
<Form | ||
onSubmit={onSubmit} | ||
query={query} | ||
onChange={onChange} | ||
index={props.index} | ||
up={props.up} | ||
down={props.down} | ||
resultsLength={results.length} | ||
/> | ||
); | ||
} | ||
|
||
function getResultList( | ||
listRef: Ref<HTMLUListElement>, | ||
results: { url: string; text: string; title: string }[], | ||
props: DialogContentParams | ||
) { | ||
return ( | ||
<ul ref={listRef}> | ||
{results.map((result, index) => ( | ||
<SearchResult | ||
key={result.url} | ||
active={index === props.index} | ||
result={result} | ||
/> | ||
))} | ||
</ul> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ResultSelectionParams } from '../lib/result-selection-params'; | ||
|
||
export interface FormParams extends ResultSelectionParams { | ||
onSubmit: (evt: Event) => void; | ||
query: string; | ||
onChange: (value: string) => void; | ||
resultsLength: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { SearchInput } from './search-input'; | ||
import { h } from 'preact'; | ||
import { FormParams } from './form-params'; | ||
import { useFormKeyDownHandler } from '../hooks/use-form-key-down-handler'; | ||
|
||
export function Form(props: FormParams) { | ||
const onKeyDown = useFormKeyDownHandler(props); | ||
|
||
return ( | ||
<form id="search-form" onSubmit={props.onSubmit}> | ||
<SearchInput | ||
value={props.query} | ||
onInput={(evt) => props.onChange((evt.target as HTMLInputElement).value)} | ||
onKeyDown={onKeyDown} | ||
/> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { h } from 'preact'; | ||
|
||
export function Preface() { | ||
return ( | ||
<div> | ||
<h2>Search</h2> | ||
<p> | ||
Press <kbd>ESC</kbd> to exit, <kbd>↓</kbd>, <kbd>↑</kbd> to | ||
navigate, and <kbd>Return</kbd> accept. | ||
</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { h } from 'preact'; | ||
import { JSXInternal } from 'preact/src/jsx'; | ||
import TargetedEvent = JSXInternal.TargetedEvent; | ||
import { useEffect, useRef } from 'preact/hooks'; | ||
|
||
export function SearchInput(props: { | ||
value: string; | ||
onInput: (evt: TargetedEvent<HTMLInputElement, Event>) => void; | ||
onKeyDown: (evt: KeyboardEvent) => void; | ||
}) { | ||
const ref = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
if (ref.current) ref.current.focus(); | ||
}, [ref]); | ||
|
||
return ( | ||
<input | ||
id="search-input" | ||
autoComplete="off" | ||
type="search" | ||
autoFocus | ||
value={props.value} | ||
onInput={props.onInput} | ||
onKeyDown={props.onKeyDown} | ||
ref={ref} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Fragment, h } from 'preact'; | ||
import { useState } from 'preact/hooks'; | ||
import DialogContent from './dialog-content'; | ||
import { registerWindowSearchFunction } from '../lib/registerWindowSearchFunction'; | ||
import { useOpenShortcutListener } from '../hooks/use-open-shortcut-listener'; | ||
import { useDialogState } from '../hooks/use-dialog-state'; | ||
|
||
const defaultSearch = () => []; | ||
|
||
function getDialog( | ||
onClose: () => void, | ||
props: { results: any[]; search: string }, | ||
index: number, | ||
setIndex: (value: ((prevState: number) => number) | number) => void | ||
) { | ||
return ( | ||
<div class="search-dialog shadowed"> | ||
<DialogContent | ||
onClose={onClose} | ||
searchFn={(window as any)[props.search] ?? defaultSearch} | ||
index={index} | ||
up={() => setIndex(index - 1)} | ||
down={() => setIndex(index + 1)} | ||
reset={() => setIndex(0)} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
const SearchPopup = (props: { results: any[]; search: string }) => { | ||
const [index, setIndex] = useState(0); | ||
let { isOpen, onOpen, onClose } = useDialogState(); | ||
registerWindowSearchFunction(props, setIndex, onOpen); | ||
useOpenShortcutListener(setIndex, onOpen, onClose); | ||
|
||
return ( | ||
<div id="search-wrapper"> | ||
{isOpen && ( | ||
<Fragment> | ||
<div class="backdrop" onClick={onClose}> | ||
| ||
</div> | ||
{getDialog(onClose, props, index, setIndex)} | ||
</Fragment> | ||
)} | ||
</div> | ||
); | ||
}; | ||
export default SearchPopup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { h } from 'preact'; | ||
import { useLayoutEffect, useRef } from 'preact/hooks'; | ||
|
||
export default function SearchResult({ | ||
active, | ||
result, | ||
}: { | ||
active: boolean; | ||
result: any; | ||
}) { | ||
const ref = useRef<HTMLAnchorElement>(null); | ||
useLayoutEffect(() => { | ||
if (ref.current && active) { | ||
ref.current.scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'nearest', | ||
}); | ||
} | ||
}, [active, ref]); | ||
|
||
return ( | ||
<li | ||
key={result.url} | ||
aria-type={'options'} | ||
aria-selected={active} | ||
className={active ? 'active' : ''} | ||
> | ||
<a href={result.url} ref={ref}> | ||
<p> | ||
<small>{result.url}</small> | ||
</p> | ||
<h5>{result.name}</h5> | ||
<p>{result.text}</p> | ||
</a> | ||
</li> | ||
); | ||
} |
Oops, something went wrong.