-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tanstank-react-router and close sidebar onSwipeLeft
- Loading branch information
Showing
16 changed files
with
335 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as useWordleStore } from './store' | ||
export { useSwipe } from './swipe' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
interface SwipeOptions { | ||
left?: () => void | ||
right?: () => void | ||
up?: () => void | ||
down?: () => void | ||
} | ||
|
||
const useSwipe = ({ left, right, up, down }: SwipeOptions = {}) => { | ||
const touchCoordsRef = useRef({ touchStart: { x: 0, y: 0, time: Date.now() } }) | ||
const fnsRef = useRef({ up, down, left, right }) | ||
fnsRef.current = { | ||
up, | ||
left, | ||
down, | ||
right, | ||
} | ||
useEffect(() => { | ||
const handleTouchStart = (e: TouchEvent) => { | ||
touchCoordsRef.current.touchStart.x = e.targetTouches[0].clientX | ||
touchCoordsRef.current.touchStart.y = e.targetTouches[0].clientY | ||
touchCoordsRef.current.touchStart.time = Date.now() | ||
} | ||
const handleTouchEnd = (e: TouchEvent) => { | ||
const threshold = 150 | ||
const swipeSpeed = 1 // sec; | ||
const touchEndX = e.changedTouches[0].clientX | ||
const touchEndY = e.changedTouches[0].clientY | ||
const touchStartX = touchCoordsRef.current.touchStart.x | ||
const touchStartY = touchCoordsRef.current.touchStart.y | ||
const elapsedTime = (Date.now() - touchCoordsRef.current.touchStart.time) / 1000 | ||
if (elapsedTime > swipeSpeed) { | ||
return | ||
} | ||
const xDistance = touchStartX - touchEndX | ||
const yDistance = touchStartY - touchEndY | ||
|
||
if (Math.abs(xDistance) < threshold && Math.abs(yDistance) < threshold) { | ||
return | ||
} | ||
|
||
if (Math.abs(xDistance) >= Math.abs(yDistance)) { | ||
xDistance > 0 ? fnsRef.current.left?.() : fnsRef.current.right?.() | ||
} else { | ||
yDistance > 0 ? fnsRef.current.down?.() : fnsRef.current.up?.() | ||
} | ||
} | ||
window.addEventListener('touchstart', handleTouchStart) | ||
window.addEventListener('touchend', handleTouchEnd) | ||
return () => { | ||
window.removeEventListener('touchstart', handleTouchStart) | ||
window.removeEventListener('touchend', handleTouchEnd) | ||
} | ||
}) | ||
} | ||
|
||
export { | ||
useSwipe | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* prettier-ignore-start */ | ||
|
||
/* eslint-disable */ | ||
|
||
// @ts-nocheck | ||
|
||
// noinspection JSUnusedGlobalSymbols | ||
|
||
// This file is auto-generated by TanStack Router | ||
|
||
// Import Routes | ||
|
||
import { Route as rootRoute } from './routes/__root' | ||
import { Route as HelperImport } from './routes/helper' | ||
import { Route as IndexImport } from './routes/index' | ||
|
||
// Create/Update Routes | ||
|
||
const HelperRoute = HelperImport.update({ | ||
path: '/helper', | ||
getParentRoute: () => rootRoute, | ||
} as any) | ||
|
||
const IndexRoute = IndexImport.update({ | ||
path: '/', | ||
getParentRoute: () => rootRoute, | ||
} as any) | ||
|
||
// Populate the FileRoutesByPath interface | ||
|
||
declare module '@tanstack/react-router' { | ||
interface FileRoutesByPath { | ||
'/': { | ||
id: '/' | ||
path: '/' | ||
fullPath: '/' | ||
preLoaderRoute: typeof IndexImport | ||
parentRoute: typeof rootRoute | ||
} | ||
'/helper': { | ||
id: '/helper' | ||
path: '/helper' | ||
fullPath: '/helper' | ||
preLoaderRoute: typeof HelperImport | ||
parentRoute: typeof rootRoute | ||
} | ||
} | ||
} | ||
|
||
// Create and export the route tree | ||
|
||
export const routeTree = rootRoute.addChildren({ IndexRoute, HelperRoute }) | ||
|
||
/* prettier-ignore-end */ | ||
|
||
/* ROUTE_MANIFEST_START | ||
{ | ||
"routes": { | ||
"__root__": { | ||
"filePath": "__root.tsx", | ||
"children": [ | ||
"/", | ||
"/helper" | ||
] | ||
}, | ||
"/": { | ||
"filePath": "index.tsx" | ||
}, | ||
"/helper": { | ||
"filePath": "helper.tsx" | ||
} | ||
} | ||
} | ||
ROUTE_MANIFEST_END */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createRouter } from '@tanstack/react-router' | ||
import { routeTree } from './routeTree.gen' | ||
|
||
|
||
const router = createRouter({ | ||
routeTree, | ||
basepath: '/better-wordle', | ||
trailingSlash: 'never', | ||
}) | ||
|
||
declare module '@tanstack/react-router' { | ||
interface Register { | ||
router: typeof router | ||
} | ||
} | ||
|
||
export default router |
Oops, something went wrong.