Skip to content

Commit

Permalink
Refactor and prevent selection within scroll margin
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed Sep 13, 2024
1 parent 9286168 commit 288d3cc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
22 changes: 22 additions & 0 deletions src/Playroom/Snippets/Snippets.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ export const snippetsContainer = style([
{
listStyle: 'none',
top: toolbarItemSize,
/*
These pseudo-elements create a buffer area at the top and bottom of the list, the same size as the scroll margin.
This prevents auto-scrolling when the cursor enters a snippet in the scroll margin, by preventing the element from being selected.
*/
'::before': {
content: '',
position: 'fixed',
top: toolbarItemSize,
left: 0,
right: 0,
height: vars.space[snippetsBorderSpace],
zIndex: 1,
},
'::after': {
content: '',
position: 'fixed',
bottom: 0,
left: 0,
right: 0,
height: vars.space[snippetsBorderSpace],
zIndex: 1,
},
},
]);

Expand Down
30 changes: 13 additions & 17 deletions src/Playroom/Snippets/Snippets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import type { PlayroomProps } from '../Playroom';
import type { Snippet } from '../../../utils';
import SearchField from './SearchField/SearchField';
import { Text } from '../Text/Text';
import { Stack } from '../Stack/Stack';

import * as styles from './Snippets.css';
import { Stack } from '../Stack/Stack';

type HighlightIndex = number | null;
type ReturnedSnippet = Snippet | null;
Expand All @@ -20,8 +20,8 @@ interface Props {

const getLabel = (snippet: Snippet) => `${snippet.group}\n${snippet.name}`;

function getSnippetId(group: string, name: string, index: number) {
return `${group}_${name}_${index}`;
function getSnippetId(snippet: Snippet, index: number) {
return `${snippet.group}_${snippet.name}_${index}`;
}

const filterSnippetsForTerm = (snippets: Props['snippets'], term: string) =>
Expand Down Expand Up @@ -58,20 +58,16 @@ export default ({ snippets, onHighlight, onClose }: Props) => {
[searchTerm, snippets]
);

const highlightedItemId =
typeof highlightedIndex === 'number'
? getSnippetId(
filteredSnippets[highlightedIndex].group,
filteredSnippets[highlightedIndex].name,
highlightedIndex
)
: null;

const highlightedItem = highlightedItemId
? document.getElementById(highlightedItemId)
: null;
if (
typeof highlightedIndex === 'number' &&
filteredSnippets[highlightedIndex]
) {
const highlightedItem = document.getElementById(
getSnippetId(filteredSnippets[highlightedIndex], highlightedIndex)
);

highlightedItem?.scrollIntoView({ block: 'nearest' });
highlightedItem?.scrollIntoView({ block: 'nearest' });
}

useEffect(() => {
debouncedPreview(
Expand Down Expand Up @@ -140,7 +136,7 @@ export default ({ snippets, onHighlight, onClose }: Props) => {
return (
<li
ref={isHighlighted ? highlightedEl : undefined}
id={getSnippetId(snippet.group, snippet.name, index)}
id={getSnippetId(snippet, index)}
key={`${snippet.group}_${snippet.name}_${index}`}
className={classnames(styles.snippet, {
[styles.highlight]: isHighlighted,
Expand Down

0 comments on commit 288d3cc

Please sign in to comment.