@@ -5,6 +5,10 @@ import CloseIcon from "../../styles/icons/cross.svg";
55import Link from "../Link/Link.jsx" ;
66
77// TODO: Check to make sure all pages are shown and properly sorted
8+ function isRTL ( ) {
9+ return document . documentElement . dir === "rtl" ;
10+ }
11+
812export default function SidebarMobile ( { isOpen, toggle, sections } ) {
913 const containerRef = useRef ( null ) ;
1014 const openerRef = useRef ( null ) ;
@@ -52,22 +56,19 @@ export default function SidebarMobile({ isOpen, toggle, sections }) {
5256
5357 const handleTouchEnd = useCallback ( ( event ) => {
5458 const threshold = 20 ;
59+ const deltaX = lastTouchPosition . current . x - initialTouchPosition . current . x ;
60+ const shouldClose = isRTL ( ) ? deltaX > threshold : deltaX < - threshold ;
61+ const shouldOpen = isRTL ( ) ? deltaX < - threshold : deltaX > threshold ;
5562
5663 // Free up all the inline styling
5764 containerRef . current . classList . remove ( "!duration-0" ) ;
5865 containerRef . current . style . transform = "" ;
5966
6067 // are we open?
61- if (
62- isOpenRef . current &&
63- initialTouchPosition . current . x - lastTouchPosition . current . x > threshold
64- ) {
68+ if ( isOpenRef . current && shouldClose ) {
6569 // this is in top level nav callback
6670 toggleRef . current ( false ) ;
67- } else if (
68- ! isOpenRef . current &&
69- lastTouchPosition . current . x - initialTouchPosition . current . x > threshold
70- ) {
71+ } else if ( ! isOpenRef . current && shouldOpen ) {
7172 toggleRef . current ( true ) ;
7273 event . preventDefault ( ) ;
7374 event . stopPropagation ( ) ;
@@ -80,28 +81,34 @@ export default function SidebarMobile({ isOpen, toggle, sections }) {
8081 const opener = openerRef . current ;
8182
8283 const handleTouchMove = ( event ) => {
83- const xDiff = initialTouchPosition . current . x - event . touches [ 0 ] . pageX ;
84+ const deltaX = event . touches [ 0 ] . pageX - initialTouchPosition . current . x ;
8485 const yDiff = initialTouchPosition . current . y - event . touches [ 0 ] . pageY ;
85- const factor = Math . abs ( yDiff / xDiff ) ;
86+ const xDistance = Math . abs ( deltaX ) ;
87+ const closeDistance = isRTL ( ) ? deltaX : - deltaX ;
88+ const factor = xDistance === 0 ? Infinity : Math . abs ( yDiff / xDistance ) ;
8689
8790 // Factor makes sure horizontal and vertical scroll dont take place together
88- if ( xDiff > 0 && factor < 0.8 ) {
91+ if ( closeDistance > 0 && factor < 0.8 ) {
8992 event . preventDefault ( ) ;
90- container . style . transform = `translateX(- ${ xDiff } px)` ;
93+ container . style . transform = `translateX(${ isRTL ( ) ? closeDistance : - closeDistance } px)` ;
9194 lastTouchPosition . current . x = event . touches [ 0 ] . pageX ;
9295 lastTouchPosition . current . y = event . touches [ 0 ] . pageY ;
9396 }
9497 } ;
9598
9699 const handleOpenerTouchMove = ( event ) => {
97- const xDiff = event . touches [ 0 ] . pageX - initialTouchPosition . current . x ;
100+ const deltaX = event . touches [ 0 ] . pageX - initialTouchPosition . current . x ;
98101 const yDiff = initialTouchPosition . current . y - event . touches [ 0 ] . pageY ;
99- const factor = Math . abs ( yDiff / xDiff ) ;
102+ const xDistance = Math . abs ( deltaX ) ;
103+ const openDistance = isRTL ( ) ? - deltaX : deltaX ;
104+ const factor = xDistance === 0 ? Infinity : Math . abs ( yDiff / xDistance ) ;
100105
101106 // Factor makes sure horizontal and vertical scroll dont take place together
102- if ( xDiff > 0 && xDiff < 295 && factor < 0.8 ) {
107+ if ( openDistance > 0 && openDistance < 295 && factor < 0.8 ) {
103108 event . preventDefault ( ) ;
104- container . style . transform = `translateX(calc(-100% + ${ xDiff } px))` ;
109+ container . style . transform = isRTL ( )
110+ ? `translateX(calc(100% - ${ openDistance } px))`
111+ : `translateX(calc(-100% + ${ openDistance } px))` ;
105112 lastTouchPosition . current . x = event . touches [ 0 ] . pageX ;
106113 lastTouchPosition . current . y = event . touches [ 0 ] . pageY ;
107114 }
0 commit comments