Skip to content

Commit

Permalink
Add loading animation, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynetics committed Jul 6, 2024
1 parent 41b4017 commit cabb921
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
7 changes: 5 additions & 2 deletions javascript/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {FunctionComponent, render} from "preact"
import habitat from "preact-habitat"
import Omnibar, {buildItemStyle} from "omnibar2"
import {Globals} from "csstype"
import {useItemAction, useOmnibarExtensions} from "./hooks"
import {useDelayedLoadingStyle, useItemAction, useOmnibarExtensions} from "./hooks"
import {useHotkey, useModal, useToggleFocus} from "./hooks"
import {AppArgs, INPUT_DATA_ID, Item, ModalArg} from "./types"
import {iconClass} from "./icon"
Expand All @@ -27,6 +27,7 @@ const App: FunctionComponent<AppArgs> = (args) => {
const RailsOmnibar: FunctionComponent<AppArgs & ModalArg> = (args) => {
const extensions = useOmnibarExtensions(args)
const itemAction = useItemAction(args.itemModal)
const loadingStyle = useDelayedLoadingStyle();

return (
<>
Expand All @@ -35,10 +36,12 @@ const RailsOmnibar: FunctionComponent<AppArgs & ModalArg> = (args) => {
extensions={extensions}
maxResults={args.maxResults}
onAction={itemAction}
onQueryStart={() => loadingStyle.startTimer()}
onQueryEnd={() => loadingStyle.stop()}
placeholder={args.placeholder}
children={(props) => renderItem({...props, modal: args.modal})}
showEmpty
style={args.modal ? MODAL_ROW_STYLE : ROW_STYLE}
style={{...(args.modal ? MODAL_ROW_STYLE : ROW_STYLE), ...loadingStyle.style}}
/>
{args.itemModal.modal}
</>
Expand Down
1 change: 1 addition & 0 deletions javascript/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./use_delayed_loading_style"
export * from "./use_hotkey"
export * from "./use_item_action"
export * from "./use_modal"
Expand Down
26 changes: 26 additions & 0 deletions javascript/src/hooks/use_delayed_loading_style.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {useState} from "preact/hooks"

export const useDelayedLoadingStyle = () => {
const [enabled, setEnabled] = useState(false)
const [timer, setTimer] = useState<NodeJS.Timer | null>(null)

const startTimer = () => {
if (timer) clearTimeout(timer)
setTimer(setTimeout(() => setEnabled(true), 100))
}

const stop = () => {
if (timer) clearTimeout(timer)
setEnabled(false)
}

return { style : enabled ? LOADING_STYLE : {}, startTimer, stop }
}

const LOADING_STYLE = {
background: "repeating-linear-gradient(90deg, #000F, #0004 50%, #000F)",
backgroundClip: "text",
backgroundPosition: "40000px",
color: "#0001",
transition: "background 100s ease-out, color 0.15s ease-in",
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@heroicons/react": "^2.0.14",
"fuzzysort": "^2.0.4",
"omnibar2": "^2.4.5",
"omnibar2": "^2.5.0",
"preact": "^10.11.3",
"preact-habitat": "^3.3.0",
"react-modal": "^3.16.1",
Expand Down

0 comments on commit cabb921

Please sign in to comment.