Skip to content

Commit

Permalink
fix(scrolltoitem): scrollToItem accepts IOItem as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyshlyaev177 committed Sep 11, 2021
1 parent 780ce5d commit a5f5011
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
11 changes: 3 additions & 8 deletions example-nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ function App() {

if (!itemSelected) {
// NOTE: center item on select
scrollToItem(
getItemById(itemId)?.entry?.target,
'smooth',
'center',
'nearest'
);
scrollToItem(getItemById(itemId), 'smooth', 'center', 'nearest');
}
};

Expand All @@ -113,7 +108,7 @@ function App() {
scrollToItem,
}: scrollVisibilityApiType) => {
// NOTE: scroll to item, auto/smooth for animation
// scrollToItem(getItemById('test15')?.entry?.target, 'auto');
// scrollToItem(getItemById('test15'), 'auto');
// NOTE: or restore exact position by pixels
// scrollContainer.current.scrollLeft = position;
},
Expand Down Expand Up @@ -168,9 +163,9 @@ function App() {
function LeftArrow() {
const { initComplete, isFirstItemVisible, scrollPrev } =
React.useContext(VisibilityContext);

// NOTE initComplete is a hack for prevent blinking on init
// Can get visibility of item only after it's rendered

return (
<Arrow
disabled={!initComplete || (initComplete && isFirstItemVisible)}
Expand Down
9 changes: 6 additions & 3 deletions src/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import createApi from './createApi';
import ItemsMap from './ItemsMap';
import { observerEntriesToItems } from './helpers';
import { observerOptions } from './settings';
import { IOItem } from './types';

const setup = (ratio = [0.3, 1, 0.7]) => {
const items = new ItemsMap();
Expand Down Expand Up @@ -195,9 +196,11 @@ describe('createApi', () => {
test('item exists', async () => {
const { items, visibleItems } = setup([1, 1, 0.3]);

const item = document.createElement('div');
const item = {
entry: { target: document.createElement('div') },
} as unknown as IOItem;
const scrollIntoView = jest.fn();
item.scrollIntoView = scrollIntoView;
item.entry.target.scrollIntoView = scrollIntoView;

createApi(items, visibleItems).scrollToItem(item);

Expand Down Expand Up @@ -229,7 +232,7 @@ describe('createApi', () => {
const { items, visibleItems } = setup([1, 1, 0.3]);

expect(() =>
createApi(items, visibleItems).scrollToItem(null as unknown as Element)
createApi(items, visibleItems).scrollToItem(undefined)
).not.toThrow();
});
});
Expand Down
6 changes: 2 additions & 4 deletions src/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ export default function createApi(
behavior: ScrollBehavior = 'smooth',
inline: ScrollLogicalPosition = 'end',
block: ScrollLogicalPosition = 'nearest'
): void =>
scrollToItem(getPrevItem()?.entry?.target, behavior, inline, block);
): void => scrollToItem(getPrevItem(), behavior, inline, block);

const scrollNext = (
behavior: ScrollBehavior = 'smooth',
inline: ScrollLogicalPosition = 'start',
block: ScrollLogicalPosition = 'nearest'
): void =>
scrollToItem(getNextItem()?.entry?.target, behavior, inline, block);
): void => scrollToItem(getNextItem(), behavior, inline, block);

return {
// centerVisibleItem,
Expand Down
10 changes: 8 additions & 2 deletions src/helpers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
scrollToItem,
} from './helpers';
import { observerOptions } from './settings';
import { IOItem } from './types';

describe('getNodesFromRefs', () => {
test('should return empty array if refs are null', () => {
Expand Down Expand Up @@ -99,7 +100,9 @@ describe('scrollToItem', () => {
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;
window.requestAnimationFrame = requestAnimationFrameMock;

const item = document.createElement('div');
const item = {
entry: { target: document.createElement('div') },
} as unknown as IOItem;

scrollToItem(item);

Expand All @@ -117,7 +120,10 @@ describe('scrollToItem', () => {
const requestAnimationFrameMock = jest.fn((fn) => fn?.());
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;
window.requestAnimationFrame = requestAnimationFrameMock;
const item = document.createElement('div');

const item = {
entry: { target: document.createElement('div') },
} as unknown as IOItem;

scrollToItem(item, 'auto', 'center', 'end');

Expand Down
9 changes: 6 additions & 3 deletions src/helpers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import type { Refs, Item } from './types';
import type { Refs, Item, IOItem } from './types';
import { observerOptions } from './settings';

export const getNodesFromRefs = (refs: Refs): HTMLElement[] => {
Expand Down Expand Up @@ -31,15 +31,18 @@ export function observerEntriesToItems(
});
}
export function scrollToItem(
item?: Element,
// TODO: remove Element
_item?: IOItem | Element,
behavior: ScrollBehavior = 'smooth',
inline: ScrollLogicalPosition = 'end',
block: ScrollLogicalPosition = 'nearest'
) {
const item = (_item as IOItem)?.entry?.target || _item;

if (item) {
window &&
window.requestAnimationFrame(() =>
item.scrollIntoView({
item!.scrollIntoView({
block,
behavior,
inline,
Expand Down

0 comments on commit a5f5011

Please sign in to comment.