Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
fix: Link search results should scroll into view when using arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor committed Jun 13, 2020
1 parent b9ed404 commit aef643f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/LinkEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class LinkEditor extends React.Component<Props, State> {
</ToolbarButton>

{showResults && (
<SearchResults>
<SearchResults id="link-search-results">
{results.map((result, index) => (
<LinkSearchResult
key={result.url}
Expand Down
23 changes: 21 additions & 2 deletions src/components/LinkSearchResult.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import scrollIntoView from "smooth-scroll-into-view-if-needed";
import styled, { withTheme } from "styled-components";
import { NextIcon } from "outline-icons";
import theme from "../theme";
Expand All @@ -12,9 +13,27 @@ type Props = {
theme: typeof theme;
};

function LinkSearchResult({ title, icon, theme, ...rest }: Props) {
function LinkSearchResult({ title, selected, icon, theme, ...rest }: Props) {
const ref = React.useCallback(
node => {
if (selected && node) {
scrollIntoView(node, {
scrollMode: "if-needed",
block: "center",
boundary: parent => {
// All the parent elements of your target are checked until they
// reach the #link-search-results. Prevents body and other parent
// elements from being scrolled
return parent.id !== "link-search-results";
},
});
}
},
[selected]
);

return (
<ListItem {...rest}>
<ListItem ref={ref} selected={selected} {...rest}>
<i>
<NextIcon color={theme.toolbarItem} />
</i>
Expand Down

0 comments on commit aef643f

Please sign in to comment.