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

Popup menu cleanup #697

Merged
merged 7 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore(popup-menu): don't leak resultsRef
Scrolling into view can be handled within the <PopupMenuList />
  • Loading branch information
nikku committed Nov 17, 2022
commit 83d27094ca6de1a72a15ab32cb380a507265c20b
26 changes: 0 additions & 26 deletions lib/features/popup-menu/PopupMenuComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default function PopupMenuComponent(props) {
}, [ search, originalEntries ]);

const inputRef = useRef();
const resultsRef = useRef();

const [ value, setValue ] = useState('');

Expand Down Expand Up @@ -118,20 +117,6 @@ export default function PopupMenuComponent(props) {
inputRef.current && inputRef.current.focus();
}, []);

// scroll to keyboard selected result
useLayoutEffect(() => {
const containerEl = resultsRef.current;

if (!containerEl)
return;

const selectedEl = containerEl.querySelector('.selected');

if (selectedEl) {
scrollIntoView(selectedEl);
}
}, [ selectedEntry ]);

// handle keyboard seleciton
const keyboardSelect = direction => {
const idx = entries.indexOf(selectedEntry);
Expand Down Expand Up @@ -227,7 +212,6 @@ export default function PopupMenuComponent(props) {
selectedEntry=${ selectedEntry }
setSelectedEntry=${ setSelectedEntry }
onSelect=${ onSelect }
resultsRef=${ resultsRef }
/>
</div>
`}
Expand Down Expand Up @@ -284,16 +268,6 @@ function PopupMenuWrapper(props) {
}

// helpers //////////////////////
function scrollIntoView(el) {
if (typeof el.scrollIntoViewIfNeeded === 'function') {
el.scrollIntoViewIfNeeded();
} else {
el.scrollIntoView({
scrollMode: 'if-needed',
block: 'nearest'
});
}
}

function getOverlayStyle(props) {
return {
Expand Down
34 changes: 32 additions & 2 deletions lib/features/popup-menu/PopupMenuList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
html,
useMemo
useMemo,
useLayoutEffect,
useRef
} from '../../ui';

import PopupMenuItem from './PopupMenuItem';
Expand All @@ -19,12 +21,27 @@ export default function PopupMenuList(props) {
selectedEntry,
setSelectedEntry,
onSelect,
resultsRef,
entries
} = props;

const resultsRef = useRef();

const groups = useMemo(() => groupEntries(entries), [ entries ]);

// scroll to selected result
useLayoutEffect(() => {
const containerEl = resultsRef.current;

if (!containerEl)
return;

const selectedEl = containerEl.querySelector('.selected');

if (selectedEl) {
scrollIntoView(selectedEl);
}
}, [ selectedEntry ]);

return (html`
<div class="results" ref=${ resultsRef }>
${ groups.map(group => html`
Expand Down Expand Up @@ -75,4 +92,17 @@ function groupEntries(entries) {
});

return groups;
}

// helpers ////////////////

function scrollIntoView(el) {
if (typeof el.scrollIntoViewIfNeeded === 'function') {
el.scrollIntoViewIfNeeded();
} else {
el.scrollIntoView({
scrollMode: 'if-needed',
block: 'nearest'
});
}
}