Skip to content

Commit

Permalink
feat(js): scroll to top when query changes (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Feb 19, 2021
1 parent dd28098 commit 706939c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ export function autocomplete<TItem extends BaseItem>(
setPanelPosition();
}

// We scroll to the top of the panel whenever the query changes (i.e. new
// results come in) so that users don't have to.
if (state.query !== prevState.query) {
const scrollablePanels = document.querySelectorAll(
'.aa-Panel--Scrollable'
);
scrollablePanels.forEach((scrollablePanel) => {
if (scrollablePanel.scrollTop !== 0) {
scrollablePanel.scrollTop = 0;
}
});
}

debouncedRender({ state });
};

Expand Down
4 changes: 3 additions & 1 deletion packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export function renderPanel<TItem extends BaseItem>(
</section>
));

const children = <div className="aa-PanelLayout">{sections}</div>;
const children = (
<div className="aa-PanelLayout aa-Panel--Scrollable">{sections}</div>
);

render({ children, state, sections, createElement, Fragment }, dom.panel);
}
4 changes: 3 additions & 1 deletion packages/autocomplete-theme-classic/src/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ body {
height: 100%;
margin-top: var(--aa-spacing-half);
max-height: var(--aa-panel-max-height);
overflow-y: auto;
padding-bottom: var(--aa-spacing-half);
padding-top: var(--aa-spacing-half);
position: relative;
Expand All @@ -221,6 +220,9 @@ body {
overflow: hidden;
}
}
.aa-Panel--Scrollable {
overflow-y: auto;
}

// when a query isn't resolved yet
&.aa-Panel--stalled {
Expand Down
5 changes: 3 additions & 2 deletions packages/website/docs/autocomplete-theme-classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ autocomplete({

## CSS utility classes

- `aa-ActiveOnly`: displays an element only when the item is active
- `aa-TouchOnly`: displays an element only on touch devices.
- `.aa-Panel--Scrollable`: declares the scrollable container of the panel
- `.aa-ActiveOnly`: displays an element only when the item is active
- `.aa-TouchOnly`: displays an element only on touch devices.

## Dark mode

Expand Down

0 comments on commit 706939c

Please sign in to comment.