gesturx 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +17 -1
  2. package/dist/index.d.ts +192 -0
  3. package/dist/index.js +627 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.umd.cjs +2 -0
  6. package/dist/index.umd.cjs.map +1 -0
  7. package/package.json +12 -1
  8. package/.github/workflows/publish.yml +0 -17
  9. package/.prettierrc.json +0 -3
  10. package/eslint.config.js +0 -18
  11. package/index.html +0 -13
  12. package/src/core/index.ts +0 -4
  13. package/src/core/manager.ts +0 -46
  14. package/src/core/pan/PanRecognizer.ts +0 -192
  15. package/src/core/pinch/PinchRecognizer.ts +0 -131
  16. package/src/core/recognizer/Recognizer.ts +0 -54
  17. package/src/core/recognizer/RecognizerEvent.ts +0 -39
  18. package/src/core/recognizer/mapToRecognizerEvent.ts +0 -142
  19. package/src/core/rotate/RotateRecognizer.ts +0 -115
  20. package/src/core/swipe/SwipeRecognizer.test.ts +0 -160
  21. package/src/core/swipe/SwipeRecognizer.ts +0 -82
  22. package/src/core/swipe/isRecognizedAsSwipe.ts +0 -13
  23. package/src/core/tap/TapRecognizer.test.ts +0 -567
  24. package/src/core/tap/TapRecognizer.ts +0 -172
  25. package/src/core/utils/events.ts +0 -186
  26. package/src/core/utils/geometry.ts +0 -115
  27. package/src/core/utils/utils.test.ts +0 -26
  28. package/src/core/utils/utils.ts +0 -58
  29. package/src/demo/App.tsx +0 -171
  30. package/src/demo/DebugBox.tsx +0 -10
  31. package/src/demo/useManager.ts +0 -40
  32. package/src/demo/useTap.tsx +0 -87
  33. package/src/index.css +0 -66
  34. package/src/main.tsx +0 -14
  35. package/src/theme.ts +0 -11
  36. package/src/vite-env.d.ts +0 -1
  37. package/tsconfig.json +0 -26
  38. package/tsconfig.node.json +0 -11
  39. package/vite.config.ts +0 -38
  40. package/vitest.config.ts +0 -8
  41. /package/{public → dist}/vite.svg +0 -0
package/src/demo/App.tsx DELETED
@@ -1,171 +0,0 @@
1
- import { useEffect, useState } from "react"
2
- import { PanRecognizer } from "../core/pan/PanRecognizer"
3
- import { Box, Stack, Text, useToast } from "@chakra-ui/react"
4
- import { ArrowForwardIcon } from "@chakra-ui/icons"
5
- import { DebugBox } from "./DebugBox"
6
- import { useManager } from "./useManager"
7
- import { useTap } from "./useTap"
8
-
9
- function App() {
10
- const toast = useToast()
11
- const [container, setContainer] = useState<HTMLElement | undefined>()
12
- const { manager } = useManager(container)
13
- const { tapDebug } = useTap(manager)
14
- const [boxAngle, setBoxAngle] = useState(0)
15
- const [boxScale, setBoxScale] = useState(1)
16
- const [numberOfFingers, setNumberOfFingers] = useState(0)
17
- const [rotation, setRotation] = useState(0)
18
- const [scale, setScale] = useState(1)
19
- const [distance, setDistance] = useState(0)
20
-
21
- useEffect(() => {
22
- const rotateSub = manager.events$.subscribe((e) => {
23
- if (e.type === "rotate") {
24
- setBoxAngle((state) => state + e.deltaAngle)
25
- setRotation(e.angle)
26
- }
27
-
28
- if (e.type === "rotateEnd") {
29
- setRotation(0)
30
- }
31
- })
32
-
33
- const pinchSub = manager.events$.subscribe((e) => {
34
- if (e.type === "pinchMove") {
35
- setBoxScale((value) => value * e.deltaDistanceScale)
36
- setScale(e.scale)
37
- setDistance(e.distance)
38
- }
39
-
40
- if (e.type === "pinchEnd") {
41
- setScale(1)
42
- setDistance(0)
43
- }
44
- })
45
-
46
- const swipeSub = manager.events$.subscribe((e) => {
47
- if (e.type === "swipe") {
48
- toast({
49
- title: "Swipe",
50
- description: (
51
- <Stack>
52
- <Text display="flex" gap={4}>
53
- Direction:{" "}
54
- <ArrowForwardIcon
55
- boxSize={6}
56
- transform={`rotate(${e.angle}deg)`}
57
- />{" "}
58
- </Text>
59
- <Text display="flex" gap={4}>
60
- Velocity: X: <b>{e.velocityX.toFixed(2)}</b> / Y:{" "}
61
- <b>{e.velocityY.toFixed(2)} </b>
62
- </Text>
63
- </Stack>
64
- ),
65
- })
66
- }
67
- })
68
-
69
- /**
70
- * To track active fingers, listen for the fingers$ observable on one of
71
- * your pan recognizer which is configured in a way to track all fingers.
72
- */
73
- const fingerTrackingSub = manager.recognizers
74
- .find(
75
- (recognizer): recognizer is PanRecognizer =>
76
- recognizer instanceof PanRecognizer,
77
- )
78
- ?.fingers$.subscribe((fingers) => {
79
- setNumberOfFingers(fingers)
80
- })
81
-
82
- /**
83
- * Moving a box on x,y axis.
84
- * We track the deltaX/Y values.
85
- */
86
- let latestBoxPosition = { x: 0, y: 0 }
87
- const sub3 = manager.events$.subscribe((event) => {
88
- if (
89
- event.type === "panMove" ||
90
- event.type === "panEnd" ||
91
- event.type === "panStart"
92
- ) {
93
- const boxElement = document.getElementById(`boxContainer`)
94
-
95
- if (boxElement) {
96
- const domRect = boxElement.getBoundingClientRect()
97
-
98
- if (event.type === "panStart") {
99
- latestBoxPosition.x = domRect.left
100
- latestBoxPosition.y = domRect.top
101
- }
102
-
103
- boxElement.style.left = `${latestBoxPosition.x + event.deltaX}px`
104
- boxElement.style.top = `${latestBoxPosition.y + event.deltaY}px`
105
- }
106
- }
107
- })
108
-
109
- /**
110
- * track center.
111
- * We use the center value, which indicate the true center
112
- * no matter how many fingers
113
- */
114
- const sub = manager.events$.subscribe((event) => {
115
- const boxElement = document.getElementById(`boxCenter`)
116
-
117
- if (boxElement) {
118
- boxElement.style.left = `${event.center.x - boxElement.offsetWidth / 2}px`
119
- boxElement.style.top = `${event.center.y - boxElement.offsetHeight / 2}px`
120
- }
121
- })
122
-
123
- return () => {
124
- sub.unsubscribe()
125
- swipeSub.unsubscribe()
126
- fingerTrackingSub?.unsubscribe()
127
- sub3.unsubscribe()
128
- rotateSub.unsubscribe()
129
- pinchSub.unsubscribe()
130
- }
131
- }, [manager])
132
-
133
- return (
134
- <>
135
- <Stack position="absolute" right={0} top={0} pr={2} pt={2} zIndex={1}>
136
- <DebugBox>fingers: {numberOfFingers}</DebugBox>
137
- {tapDebug}
138
- <DebugBox>
139
- rotation:{" "}
140
- <ArrowForwardIcon boxSize={6} transform={`rotate(${rotation}deg)`} />{" "}
141
- </DebugBox>
142
- <DebugBox>
143
- pinch:
144
- <Text>
145
- {scale.toFixed(1)}% {distance.toFixed(0)}px
146
- </Text>
147
- </DebugBox>
148
- </Stack>
149
- <Box
150
- id="container"
151
- ref={(element) => {
152
- if (element) {
153
- setContainer(element)
154
- }
155
- }}
156
- >
157
- <Box id="boxContainer">
158
- <div
159
- id="box"
160
- style={{
161
- transform: `rotate(${boxAngle}deg) scale(${boxScale})`,
162
- }}
163
- ></div>
164
- </Box>
165
- <div id="boxCenter"></div>
166
- </Box>
167
- </>
168
- )
169
- }
170
-
171
- export default App
@@ -1,10 +0,0 @@
1
- import { Box } from "@chakra-ui/react"
2
- import { ReactNode } from "react"
3
-
4
- export const DebugBox = ({ children }: { children: ReactNode }) => {
5
- return (
6
- <Box bgColor="white" p={1} px={2}>
7
- {children}
8
- </Box>
9
- )
10
- }
@@ -1,40 +0,0 @@
1
- import { useEffect, useState } from "react"
2
- import {
3
- PanRecognizer,
4
- SwipeRecognizer,
5
- TapRecognizer,
6
- createManager,
7
- } from "../core"
8
- import { RotateRecognizer } from "../core/rotate/RotateRecognizer"
9
- import { PinchRecognizer } from "../core/pinch/PinchRecognizer"
10
-
11
- export const useManager = (container?: HTMLElement) => {
12
- const [manager] = useState(() => {
13
- const panRecognizer = new PanRecognizer()
14
- const swipeRecognizer = new SwipeRecognizer()
15
- const rotateRecognizer = new RotateRecognizer()
16
- const pinchRecognizer = new PinchRecognizer()
17
- const tapRecognizer = new TapRecognizer({
18
- maxTaps: 3,
19
- failWith: [panRecognizer],
20
- })
21
-
22
- return createManager({
23
- recognizers: [
24
- tapRecognizer,
25
- panRecognizer,
26
- swipeRecognizer,
27
- rotateRecognizer,
28
- pinchRecognizer,
29
- ],
30
- })
31
- })
32
-
33
- useEffect(() => {
34
- if (container) {
35
- manager.initialize(container)
36
- }
37
- }, [container, manager])
38
-
39
- return { manager }
40
- }
@@ -1,87 +0,0 @@
1
- import { useEffect, useState } from "react"
2
- import { Manager } from "../core/manager"
3
- import {
4
- FormControl,
5
- FormLabel,
6
- NumberDecrementStepper,
7
- NumberIncrementStepper,
8
- NumberInput,
9
- NumberInputField,
10
- NumberInputStepper,
11
- Stack,
12
- Text,
13
- useToast,
14
- } from "@chakra-ui/react"
15
- import { DebugBox } from "./DebugBox"
16
- import { TapRecognizer } from "../core"
17
-
18
- const TapDebug = ({
19
- value,
20
- onChange,
21
- }: {
22
- value: number
23
- onChange: (value: number) => void
24
- }) => {
25
- return (
26
- <DebugBox>
27
- <Stack>
28
- <Text>Tap:</Text>
29
- <FormControl>
30
- <FormLabel>max taps</FormLabel>
31
- <NumberInput
32
- defaultValue={value}
33
- min={1}
34
- max={20}
35
- onChange={(valueString) => onChange(parseInt(valueString))}
36
- maxWidth={100}
37
- >
38
- <NumberInputField />
39
- <NumberInputStepper>
40
- <NumberIncrementStepper />
41
- <NumberDecrementStepper />
42
- </NumberInputStepper>
43
- </NumberInput>
44
- </FormControl>
45
- </Stack>
46
- </DebugBox>
47
- )
48
- }
49
-
50
- export const useTap = (manager: Manager) => {
51
- const toast = useToast()
52
- const [maxTaps, setMaxTaps] = useState(3)
53
- const tapRecognizer = manager.recognizers.find(
54
- (recognizer) => recognizer instanceof TapRecognizer,
55
- )
56
-
57
- useEffect(() => {
58
- const clickSub = manager.events$.subscribe((e) => {
59
- if (e.type === "tap") {
60
- toast({
61
- title: "Click",
62
- description: (
63
- <Stack>
64
- <Text display="flex" gap={4}>
65
- Taps: <b>{e.taps}</b>
66
- </Text>
67
- </Stack>
68
- ),
69
- })
70
- }
71
- })
72
-
73
- return () => {
74
- clickSub.unsubscribe()
75
- }
76
- }, [manager, toast])
77
-
78
- useEffect(() => {
79
- tapRecognizer?.update({ maxTaps })
80
- }, [maxTaps, tapRecognizer])
81
-
82
- const tapDebug = (
83
- <TapDebug value={maxTaps} onChange={(value) => setMaxTaps(value)} />
84
- )
85
-
86
- return { tapDebug }
87
- }
package/src/index.css DELETED
@@ -1,66 +0,0 @@
1
- :root {
2
- font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3
- line-height: 1.5;
4
- font-weight: 400;
5
-
6
- color-scheme: light dark;
7
- color: rgba(255, 255, 255, 0.87);
8
-
9
- font-synthesis: none;
10
- text-rendering: optimizeLegibility;
11
- -webkit-font-smoothing: antialiased;
12
- -moz-osx-font-smoothing: grayscale;
13
- }
14
-
15
- html {
16
- height: 100%;
17
- }
18
-
19
- body {
20
- margin: 0;
21
- display: flex;
22
- min-width: 100vw;
23
- min-height: 100%;
24
- }
25
-
26
- #root {
27
- flex: 1;
28
- display: flex;
29
- }
30
-
31
- #container {
32
- display: flex;
33
- justify-content: center;
34
- align-items: center;
35
- flex: 1;
36
- position: relative;
37
- overflow: hidden;
38
- }
39
-
40
- .container {
41
- position: relative;
42
- }
43
-
44
- #boxContainer {
45
- height: 150px;
46
- width: 150px;
47
- position: absolute;
48
- left: 0;
49
- top: 0;
50
- border: 1px solid white;
51
- }
52
-
53
- #box {
54
- height: 148px;
55
- width: 148px;
56
- background-color: red;
57
- }
58
-
59
- #boxCenter {
60
- height: 10px;
61
- width: 10px;
62
- background-color: white;
63
- position: absolute;
64
- left: 0;
65
- top: 0;
66
- }
package/src/main.tsx DELETED
@@ -1,14 +0,0 @@
1
- import React from "react"
2
- import ReactDOM from "react-dom/client"
3
- import App from "./demo/App.tsx"
4
- import "./index.css"
5
- import { ChakraProvider } from "@chakra-ui/react"
6
- import { theme } from "./theme.ts"
7
-
8
- ReactDOM.createRoot(document.getElementById("root")!).render(
9
- <React.StrictMode>
10
- <ChakraProvider theme={theme}>
11
- <App />
12
- </ChakraProvider>
13
- </React.StrictMode>,
14
- )
package/src/theme.ts DELETED
@@ -1,11 +0,0 @@
1
- import { extendTheme } from "@chakra-ui/react"
2
-
3
- export const theme = extendTheme({
4
- styles: {
5
- global: {
6
- body: {
7
- bg: `#242424`
8
- }
9
- }
10
- }
11
- })
package/src/vite-env.d.ts DELETED
@@ -1 +0,0 @@
1
- /// <reference types="vite/client" />
package/tsconfig.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "useDefineForClassFields": true,
5
- "lib": ["ES2020", "DOM", "DOM.Iterable"],
6
- "module": "ESNext",
7
- "skipLibCheck": true,
8
-
9
- /* Bundler mode */
10
- "moduleResolution": "bundler",
11
- "allowImportingTsExtensions": true,
12
- "noUncheckedIndexedAccess": true,
13
- "resolveJsonModule": true,
14
- "isolatedModules": true,
15
- "noEmit": true,
16
- "jsx": "react-jsx",
17
-
18
- /* Linting */
19
- "strict": true,
20
- "noUnusedLocals": true,
21
- "noUnusedParameters": true,
22
- "noFallthroughCasesInSwitch": true
23
- },
24
- "include": ["src"],
25
- "references": [{ "path": "./tsconfig.node.json" }]
26
- }
@@ -1,11 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "composite": true,
4
- "skipLibCheck": true,
5
- "module": "ESNext",
6
- "moduleResolution": "bundler",
7
- "allowSyntheticDefaultImports": true,
8
- "strict": true
9
- },
10
- "include": ["vite.config.ts"]
11
- }
package/vite.config.ts DELETED
@@ -1,38 +0,0 @@
1
- import { defineConfig } from "vite"
2
- import dts from "vite-plugin-dts"
3
- import { resolve } from "path"
4
- import externals from "rollup-plugin-node-externals"
5
- import react from "@vitejs/plugin-react"
6
-
7
- export default defineConfig(({ mode }) => {
8
- const libMode = process.env.LIB_MODE === "true"
9
-
10
- return {
11
- build: {
12
- minify: false,
13
- ...(libMode && {
14
- lib: {
15
- entry: resolve(__dirname, `src/core/index.ts`),
16
- name: "gesturx",
17
- fileName: "index",
18
- },
19
- }),
20
- emptyOutDir: mode !== `development`,
21
- sourcemap: true,
22
- },
23
- plugins: [
24
- react(),
25
- {
26
- enforce: `pre`,
27
- ...externals({
28
- peerDeps: libMode,
29
- deps: libMode,
30
- devDeps: libMode,
31
- }),
32
- },
33
- dts({
34
- entryRoot: "src",
35
- }),
36
- ],
37
- }
38
- })
package/vitest.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from "vitest/config"
2
-
3
- export default defineConfig(() => ({
4
- test: {
5
- environment: "happy-dom",
6
- coverage: {},
7
- },
8
- }))
File without changes