Skip to content
Open
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
126 changes: 63 additions & 63 deletions useScroll/useScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {getRect, intersects} from '@enact/spotlight/src/utils';
import {assignPropertiesOf, constants, useScrollBase} from '@enact/ui/useScroll';
import utilDOM from '@enact/ui/useScroll/utilDOM';
import utilEvent from '@enact/ui/useScroll/utilEvent';
import {useContext, useRef} from 'react';
import {useCallback, useContext, useRef} from 'react';

import $L from '../internal/$L';
import {SharedState} from '../internal/SharedStateDecorator';
Expand Down Expand Up @@ -87,6 +87,10 @@ const useThemeScroll = (props, instances) => {
clearOverscrollEffect
} = useOverscrollEffect({}, instances);

const isContent = useCallback((element) => {
return (element && utilDOM.containsDangerously(scrollContentRef, element));
}, [scrollContentRef]);

const {handleWheel} = useEventWheel(props, {...instances, spottable: mutableRef});

const {calculateAndScrollTo, handleFocus, hasFocus} = useEventFocus(props, {...instances, spottable: mutableRef});
Expand All @@ -105,49 +109,13 @@ const useThemeScroll = (props, instances) => {
stopVoice
} = useEventVoice(props, instances);

const scrollbarProps = {
cbAlertScrollbarTrack: alertScrollbarTrack,
onInteractionForScroll,
scrollbarTrackCss
};

// Functions

function isContent (element) {
return (element && utilDOM.containsDangerously(scrollContentRef, element));
}

function scrollTo (opt) {
mutableRef.current.indexToFocus = (opt.focus && typeof opt.index === 'number') ? opt.index : null;
mutableRef.current.nodeToFocus = (opt.focus && opt.node instanceof Object && opt.node.nodeType === 1) ? opt.node : null;
}

function start (animate) {
if (scrollMode === 'native' && !animate) {
focusOnItem();
}
}

function stop () {
if (!props['data-spotlight-container-disabled']) {
themeScrollContentHandle.current.setContainerDisabled(false);
}

if (themeScrollContentHandle.current.pauseSpotlight) {
themeScrollContentHandle.current.pauseSpotlight(false);
}

focusOnItem();
mutableRef.current.lastScrollPositionOnFocus = null;
mutableRef.current.isWheeling = false;
stopVoice();
}

function scrollStopOnScroll () {
stop();
}
const alertScrollbarTrack = useCallback(() => {
const bounds = scrollContainerHandle.current.getScrollBounds();
scrollContainerHandle.current.showScrollbarTrack(bounds);
scrollContainerHandle.current.startHidingScrollbarTrack();
}, [scrollContainerHandle]);

function onInteractionForScroll ({inputType, isForward, isPagination, isVerticalScrollBar}) {
const onInteractionForScroll = useCallback(({inputType, isForward, isPagination, isVerticalScrollBar}) => {
const
{wheelDirection} = scrollContainerHandle.current,
bounds = scrollContainerHandle.current.getScrollBounds(),
Expand All @@ -163,15 +131,22 @@ const useThemeScroll = (props, instances) => {
}

scrollContainerHandle.current.scrollToAccumulatedTarget(direction * distance, isVerticalScrollBar, props.overscrollEffectOn[inputType]);
}
}, [props.overscrollEffectOn, scrollContainerHandle]);

function alertScrollbarTrack () {
const bounds = scrollContainerHandle.current.getScrollBounds();
scrollContainerHandle.current.showScrollbarTrack(bounds);
scrollContainerHandle.current.startHidingScrollbarTrack();
}
const scrollbarProps = {
cbAlertScrollbarTrack: alertScrollbarTrack,
onInteractionForScroll,
scrollbarTrackCss
};

function focusOnItem () {
// Functions

const scrollTo = useCallback((opt) => {
mutableRef.current.indexToFocus = (opt.focus && typeof opt.index === 'number') ? opt.index : null;
mutableRef.current.nodeToFocus = (opt.focus && opt.node instanceof Object && opt.node.nodeType === 1) ? opt.node : null;
}, []);

const focusOnItem = useCallback(() => {
let isItemFocused = false;

if (mutableRef.current.indexToFocus !== null && typeof themeScrollContentHandle.current.focusByIndex === 'function') {
Expand Down Expand Up @@ -210,9 +185,34 @@ const useThemeScroll = (props, instances) => {
if (Spotlight.getPointerMode() && !isItemFocused) {
Spotlight.focus(scrollContainerRef.current, {enterTo: 'topmost'});
}
}
}, [themeScrollContentHandle, scrollContainerRef]);

const start = useCallback((animate) => {
if (scrollMode === 'native' && !animate) {
focusOnItem();
}
}, [scrollMode, focusOnItem]);

function handleScroll (ev) {
const stop = useCallback(() => {
if (!props.spotlightContainerDisabled) {
themeScrollContentHandle.current.setContainerDisabled(false);
}

if (themeScrollContentHandle.current.pauseSpotlight) {
themeScrollContentHandle.current.pauseSpotlight(false);
}

focusOnItem();
mutableRef.current.lastScrollPositionOnFocus = null;
mutableRef.current.isWheeling = false;
stopVoice();
}, [focusOnItem, themeScrollContentHandle, stopVoice, props.spotlightContainerDisabled]);

const scrollStopOnScroll = useCallback(() => {
stop();
}, [stop]);

const handleScroll = useCallback((ev) => {
const
{scrollLeft: x, scrollTop: y} = ev,
{id} = props;
Expand All @@ -223,10 +223,10 @@ const useThemeScroll = (props, instances) => {
contextSharedState.set(ev, props);
contextSharedState.set(`${id}.scrollPosition`, {x, y});
}
}
}, [contextSharedState, props]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

props is always a new object, so props.id would be the right dep. It looks missing by accident. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, true. But if I add props.id I get this warning:
226:5 warning React Hook useCallback has a missing dependency: 'props'. Either include it or remove the dependency array react-hooks/exhaustive-deps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. It could be okay in this line if we define id as destructed variable from props, but it looks unclear what impact this way may have. I'll look into this.


// Callback for scroller updates; calculate and, if needed, scroll to new position based on focused item.
function handleScrollerUpdate () {
const handleScrollerUpdate = useCallback(() => {
if (scrollContainerHandle.current.scrollToInfo === null) {
const scrollHeight = scrollContainerHandle.current.getScrollBounds().scrollHeight;

Expand All @@ -239,33 +239,33 @@ const useThemeScroll = (props, instances) => {
// updated in calculateAndScrollTo but we might not have made it to that point), it will be
// out of date when we land back in this method next time.
scrollContainerHandle.current.bounds.scrollHeight = scrollContainerHandle.current.getScrollBounds().scrollHeight;
}
}, [calculateAndScrollTo, scrollContainerHandle]);

function handleResizeWindow () {
const handleResizeWindow = useCallback(() => {
const focusedItem = Spotlight.getCurrent();

if (focusedItem) {
focusedItem.blur();
}
}
}, []);

// FIXME setting event handlers directly to work on the V8 snapshot.
function addEventListeners (ref) { // `ref` is always `scrollContentRef`.
const addEventListeners = useCallback((ref) => { // `ref` is always `scrollContentRef`.
utilEvent('focusin').addEventListener(ref, handleFocus);

if (ref.current) {
addVoiceEventListener(ref);
}
}
}, [addVoiceEventListener, handleFocus]);

// FIXME setting event handlers directly to work on the V8 snapshot.
function removeEventListeners (ref) { // `ref` is always `scrollContentRef`.
const removeEventListeners = useCallback((ref) => { // `ref` is always `scrollContentRef`.
utilEvent('focusin').removeEventListener(ref, handleFocus);

if (ref.current) {
removeVoiceEventListener(ref);
}
}
}, [handleFocus, removeVoiceEventListener]);

// Return

Expand Down Expand Up @@ -366,9 +366,9 @@ const useScroll = (props) => {
wheelDirection: null
});

const setScrollContainerHandle = (handle) => {
const setScrollContainerHandle = useCallback((handle) => {
scrollContainerHandle.current = handle;
};
}, [scrollContainerHandle]);

// Hooks

Expand Down