* Gets the directions in which an element node should be scrolled, * assuming that the user's pointer is already within it scrollable region. * @param element Element for which we should calculate the scroll direction. * @param clientRect Bounding client rectangle of the element. * @param direct
( element: HTMLElement, clientRect: DOMRect, direction: Direction, pointerX: number, pointerY: number, )
| 848 | * @param pointerY Position of the user's pointer along the y axis. |
| 849 | */ |
| 850 | function getElementScrollDirections( |
| 851 | element: HTMLElement, |
| 852 | clientRect: DOMRect, |
| 853 | direction: Direction, |
| 854 | pointerX: number, |
| 855 | pointerY: number, |
| 856 | ): [AutoScrollVerticalDirection, AutoScrollHorizontalDirection] { |
| 857 | const computedVertical = getVerticalScrollDirection(clientRect, pointerY); |
| 858 | const computedHorizontal = getHorizontalScrollDirection(clientRect, pointerX); |
| 859 | let verticalScrollDirection = AutoScrollVerticalDirection.NONE; |
| 860 | let horizontalScrollDirection = AutoScrollHorizontalDirection.NONE; |
| 861 | |
| 862 | // Note that we here we do some extra checks for whether the element is actually scrollable in |
| 863 | // a certain direction and we only assign the scroll direction if it is. We do this so that we |
| 864 | // can allow other elements to be scrolled, if the current element can't be scrolled anymore. |
| 865 | // This allows us to handle cases where the scroll regions of two scrollable elements overlap. |
| 866 | if (computedVertical) { |
| 867 | const scrollTop = element.scrollTop; |
| 868 | |
| 869 | if (computedVertical === AutoScrollVerticalDirection.UP) { |
| 870 | if (scrollTop > 0) { |
| 871 | verticalScrollDirection = AutoScrollVerticalDirection.UP; |
| 872 | } |
| 873 | } else if (element.scrollHeight - scrollTop > element.clientHeight) { |
| 874 | verticalScrollDirection = AutoScrollVerticalDirection.DOWN; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if (computedHorizontal) { |
| 879 | const scrollLeft = element.scrollLeft; |
| 880 | |
| 881 | if (direction === 'rtl') { |
| 882 | if (computedHorizontal === AutoScrollHorizontalDirection.RIGHT) { |
| 883 | // In RTL `scrollLeft` will be negative when scrolled. |
| 884 | if (scrollLeft < 0) { |
| 885 | horizontalScrollDirection = AutoScrollHorizontalDirection.RIGHT; |
| 886 | } |
| 887 | } else if (element.scrollWidth + scrollLeft > element.clientWidth) { |
| 888 | horizontalScrollDirection = AutoScrollHorizontalDirection.LEFT; |
| 889 | } |
| 890 | } else { |
| 891 | if (computedHorizontal === AutoScrollHorizontalDirection.LEFT) { |
| 892 | if (scrollLeft > 0) { |
| 893 | horizontalScrollDirection = AutoScrollHorizontalDirection.LEFT; |
| 894 | } |
| 895 | } else if (element.scrollWidth - scrollLeft > element.clientWidth) { |
| 896 | horizontalScrollDirection = AutoScrollHorizontalDirection.RIGHT; |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | return [verticalScrollDirection, horizontalScrollDirection]; |
| 902 | } |
no test coverage detected