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

refactor: Custom dialogs and file structure #9

Merged
merged 13 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 1,
"singleQuote": true,
"useTabs": true,
"endOfLine": "lf"
}
70 changes: 47 additions & 23 deletions dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions dist/oss-licenses.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
"license": "MIT",
"licenseText": "Copyright JS Foundation and other contributors\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
},
{
"name": "dialog-polyfill",
"version": "0.5.5",
"author": "The Chromium Authors",
"repository": "https://github.com/GoogleChrome/dialog-polyfill",
"source": "https://registry.npmjs.org/dialog-polyfill/-/dialog-polyfill-0.5.5.tgz",
"license": "BSD-3-Clause",
"licenseText": "// Copyright (c) 2013 The Chromium Authors. All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n// * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n// * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n// * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
},
{
"name": "preact",
"version": "10.5.9",
Expand Down
39 changes: 20 additions & 19 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.0.1",
"scripts": {
"build": "webpack",
"start": "webpack --watch"
"start": "webpack --watch",
"style": "prettier -w src/**/*.*"
},
"author": "Pablo Klaschka",
"license": "MIT",
Expand All @@ -13,6 +14,7 @@
"awesome-typescript-loader": "^5.2.1",
"css-loader": "^5.0.1",
"extract-text-webpack-plugin": "^3.0.2",
"prettier": "^2.2.1",
"style-loader": "^2.0.0",
"terser-webpack-plugin": "^5.1.1",
"ts-loader": "^8.0.14",
Expand All @@ -22,7 +24,6 @@
"webpack-license-plugin": "^4.1.2"
},
"dependencies": {
"dialog-polyfill": "^0.5.6",
"preact": "^10.5.11",
"preact-custom-element": "^4.2.1"
}
Expand Down
7 changes: 7 additions & 0 deletions src/components/dialog-content-params.tsx
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;
}
42 changes: 42 additions & 0 deletions src/components/dialog-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {h} from 'preact';
import {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) {
pklaschka marked this conversation as resolved.
Show resolved Hide resolved
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 />
<Form
onSubmit={onSubmit}
query={query}
onChange={onChange}
index={props.index}
up={props.up}
down={props.down}
resultsLength={results.length}
/>
<ul ref={listRef}>
{results.map((result, index) => (
<SearchResult
key={result.url}
active={index === props.index}
result={result}
/>
))}
</ul>
</div>
);
}
8 changes: 8 additions & 0 deletions src/components/form-params.tsx
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;
}
18 changes: 18 additions & 0 deletions src/components/form.tsx
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) {
pklaschka marked this conversation as resolved.
Show resolved Hide resolved
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>
);
}
13 changes: 13 additions & 0 deletions src/components/preface.tsx
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>&darr;</kbd>, <kbd>&uarr;</kbd> to
navigate, and <kbd>Return</kbd> accept.
</p>
</div>
);
}
29 changes: 29 additions & 0 deletions src/components/search-input.tsx
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}
/>
);
}
38 changes: 38 additions & 0 deletions src/components/search-popup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 = () => [];

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>
<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>
</Fragment>
)}
</div>
);
};
export default SearchPopup;
37 changes: 37 additions & 0 deletions src/components/search-result.tsx
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>
);
}
Loading