Skip to content

Commit

Permalink
fix: reduce flickering
Browse files Browse the repository at this point in the history
Related to ParthJadhav#25.

Search results flickers because of three things (IMO):

- `handleInput` resets the `results` array, which immediately dropping
  list with `#if` directive in `SearchResult` component
- Changing styles on `focus` and `blur` are not immediate operation, that's why another
  kind of flickering is a transition between focused and non-focused
  state (gray background)
- `getIcon` function is another "bottleneck"; preservation of space for
  upcomming <img> tag is a key for rescue

Most "ugly" part of this code is manipulating with classes. Changing
classes through Svelte's `class:` binding leads to focus lost.

Unfortunately, manipulating classes through script cannot be made with
obfuscated selectors, that's why you can see `:global` in code. Maybe you
know better way to change classes without losing focus.
  • Loading branch information
Ty3uK committed Jan 5, 2023
1 parent b2a4446 commit 8d925ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/routes/App/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
});
const handleInput = async (event: any) => {
results = [];
if (event.target.value === "") {
footerText = "verve.app";
return;
Expand Down
18 changes: 14 additions & 4 deletions src/routes/App/lib/SearchResult.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
await appWindow.setSize(new LogicalSize(750, height));
if (results.length > 0 && results[0] !== "") {
const firstResult = document.getElementById(results[0]);
firstResult.classList.add('searchResultFocused');
await firstResult.focus();
}
});
Expand Down Expand Up @@ -58,6 +59,8 @@
else newIndex = (currentIndex + 1) % items.length;
}
if (current !== null && items[newIndex] !== null) {
items[newIndex].classList.add('searchResultFocused');
current.classList.remove('searchResultFocused');
current.blur();
items[newIndex].focus();
}
Expand Down Expand Up @@ -85,7 +88,12 @@
{#await getIcon(result
.split("/")
.pop()
.replace(/.app$/, "")) then { icon, fallbackIcon }}
.replace(/.app$/, ""))}
<span
class="appIcon"
alt=""
/>
{:then {icon, fallbackIcon}}
<img
class="appIcon"
src={icon}
Expand Down Expand Up @@ -123,12 +131,14 @@
order: 1;
flex-grow: 0;
}
.searchResult:focus {
background: var(--highlight-overlay);
outline: 0;
:global(.searchResultFocused) {
background: var(--highlight-overlay) !important;
outline: 0 !important;
}
.appIcon {
display: inline-flex;
width: 24px;
height: 24px;
margin-right: 8px;
Expand Down

0 comments on commit 8d925ad

Please sign in to comment.