Skip to content

useScrollToItem - add offsetType to statics #1144

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

Merged
merged 3 commits into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion generatedTypes/hooks/useScrollToItem/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ export declare type ScrollToItemResultProps<T extends ScrollToSupportedViews> =
*/
focusIndex: (index: number, animated?: boolean) => void;
};
declare const useScrollToItem: <T extends ScrollToSupportedViews>(props: ScrollToItemProps<T>) => ScrollToItemResultProps<T>;
declare const useScrollToItem: {
<T extends ScrollToSupportedViews>(props: ScrollToItemProps<T>): ScrollToItemResultProps<T>;
offsetType: typeof OffsetType;
};
export default useScrollToItem;
4 changes: 2 additions & 2 deletions src/components/tabController/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {Constants} from '../../helpers';
import {LogService} from '../../services';
import FadedScrollView from './FadedScrollView';

import useScrollToItem, {OffsetType} from '../../hooks/useScrollToItem';
import {useScrollToItem} from '../../hooks';

const {Code, Value, interpolate, block, set} = Reanimated;

Expand Down Expand Up @@ -201,7 +201,7 @@ const TabBar = (props: Props) => {
const {scrollViewRef: tabBar, onItemLayout, itemsWidths, focusIndex} = useScrollToItem({
itemsCount: itemsCount.current,
selectedIndex,
offsetType: centerSelected ? OffsetType.CENTER : OffsetType.DYNAMIC
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
});

const indicatorOffsets = useMemo((): number[] => {
Expand Down
16 changes: 9 additions & 7 deletions src/hooks/useScrollToItem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type ScrollToItemResultProps<T extends ScrollToSupportedViews> = Pick<Scr
*/
itemsWidths: number[];
/**
* Use in order to focus the item with the specified index
* Use in order to focus the item with the specified index (use when the selectedIndex is not changed)
*/
focusIndex: (index: number, animated?: boolean) => void;
};
Expand Down Expand Up @@ -95,22 +95,22 @@ const useScrollToItem = <T extends ScrollToSupportedViews>(props: ScrollToItemPr
const centeredOffsets = [];
let currentCenterOffset = outerSpacing;
const leftOffsets = [];
leftOffsets.push(0);
leftOffsets.push(outerSpacing - innerSpacing);
const rightOffsets = [];
rightOffsets.push(-Constants.screenWidth + itemsWidths[0]);
rightOffsets.push(-Constants.screenWidth + itemsWidths[0] + outerSpacing + innerSpacing);
while (index < itemsCount) {
centeredOffsets[index] = currentCenterOffset - screenCenter + itemsWidths[index] / 2;
++index;
currentCenterOffset += itemsWidths[index - 1] + innerSpacing;
leftOffsets[index] = leftOffsets[index - 1] + itemsWidths[index - 1];
rightOffsets[index] = rightOffsets[index - 1] + itemsWidths[index];
leftOffsets[index] = leftOffsets[index - 1] + itemsWidths[index - 1] + innerSpacing;
rightOffsets[index] = rightOffsets[index - 1] + itemsWidths[index] + innerSpacing;
}

if (addOffsetMargin) {
index = 1;
index = 1;
while (index < itemsCount - 1) {
leftOffsets[index] -= itemsWidths[index - 1];
rightOffsets[index] += itemsWidths[index + 1];
rightOffsets[index] += itemsWidths[index + 1] + innerSpacing;
++index;
}
}
Expand Down Expand Up @@ -155,4 +155,6 @@ const useScrollToItem = <T extends ScrollToSupportedViews>(props: ScrollToItemPr
};
};

useScrollToItem.offsetType = OffsetType;

export default useScrollToItem;