Skip to content

Commit 644cdbc

Browse files
committed
feat: implement pointer event gestures
1 parent fdec958 commit 644cdbc

File tree

15 files changed

+291
-465
lines changed

15 files changed

+291
-465
lines changed

src/extracted/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type { Direction } from './toDirection'
5555
export { toHookApi } from './toHookApi'
5656
export type { HookApi } from './toHookApi'
5757

58-
export { toMousePoint, toTouchMovePoint, toTouchEndPoint } from './toPoints'
58+
export { toPointerPoint, toTouchMovePoint, toTouchEndPoint } from './toPoints'
5959

6060
export { toPolarCoordinates } from './toPolarCoordinates'
6161
export type { PolarCoordinates } from './toPolarCoordinates'

src/extracted/storePointerMoveMetadata.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { RecognizeableEffect } from '../classes'
22
import { createClone } from '../pipes/any'
33
import { toDirection } from './toDirection'
44
import type { Direction } from './toDirection'
5-
import { toMousePoint, toTouchMovePoint, toTouchEndPoint } from './toPoints'
5+
import { toPointerPoint, toTouchMovePoint, toTouchEndPoint } from './toPoints'
66
import { toPolarCoordinates } from './toPolarCoordinates'
77
import type { PolarCoordinates } from './toPolarCoordinates'
88
import type { PointerStartMetadata } from './storePointerStartMetadata'
@@ -67,6 +67,10 @@ export function storePointerMoveMetadata<
6767
| 'touchmove'
6868
| 'touchend'
6969
| 'mouseover'
70+
| 'pointerdown'
71+
| 'pointermove'
72+
| 'pointerup'
73+
| 'pointerover'
7074
),
7175
Metadata extends PointerMoveMetadata & PointerStartMetadata & PointerTimeMetadata
7276
> ({ event, api }: {
@@ -86,7 +90,7 @@ export function storePointerMoveMetadata<
8690
{ x: startX, y: startY } = metadata.points.start,
8791
{ x: newX, y: newY } = (() => {
8892
if (event instanceof MouseEvent) {
89-
return toMousePoint(event)
93+
return toPointerPoint(event)
9094
}
9195

9296
if (event instanceof TouchEvent) {

src/extracted/storePointerStartMetadata.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { RecognizeableEffect } from '../classes'
22
import { createClone } from '../pipes/any'
3-
import { toMousePoint, toTouchMovePoint } from './toPoints'
3+
import { toPointerPoint, toTouchMovePoint } from './toPoints'
44

55
export type PointerStartMetadata = {
66
points: {
@@ -17,7 +17,7 @@ const initialMetadata: PointerStartMetadata = {
1717
}
1818

1919
export function storePointerStartMetadata<
20-
Type extends 'mousedown' | 'touchstart' | 'mouseover',
20+
Type extends 'mousedown' | 'touchstart' | 'mouseover' | 'pointerdown' | 'pointerover',
2121
Metadata extends PointerStartMetadata
2222
> ({ event, api }: {
2323
event: MouseEvent | TouchEvent,
@@ -26,8 +26,8 @@ export function storePointerStartMetadata<
2626
const { getMetadata } = api,
2727
metadata = getMetadata()
2828

29-
const point = event instanceof MouseEvent
30-
? toMousePoint(event)
29+
const point = (event instanceof MouseEvent || event instanceof PointerEvent)
30+
? toPointerPoint(event)
3131
: toTouchMovePoint(event)
3232

3333
if (!metadata.points) metadata.points = createClone<typeof metadata.points>()(initialMetadata.points)

src/extracted/storePointerTimeMetadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const initialMetadata: PointerTimeMetadata = {
2222
velocity: 0,
2323
}
2424

25-
export function storePointerTimeMetadata<Type extends 'mousedown' | 'touchstart' | 'mouseover'> ({
25+
export function storePointerTimeMetadata<Type extends 'mousedown' | 'touchstart' | 'pointerdown' | 'mouseover' | 'pointerover'> ({
2626
event,
2727
api,
2828
getShouldStore,

src/extracted/toPoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function toMousePoint (event: MouseEvent): { x: number, y: number } {
1+
export function toPointerPoint (event: MouseEvent): { x: number, y: number } {
22
return {
33
x: event.clientX,
44
y: event.clientY,

src/factories/createMousepress.ts

Lines changed: 0 additions & 157 deletions
This file was deleted.
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ import type { RecognizeableEffect } from '../classes'
33
import { toHookApi, storePointerStartMetadata, storePointerTimeMetadata, storePointerMoveMetadata } from '../extracted'
44
import type { PointerStartMetadata, PointerTimeMetadata, HookApi, PointerMoveMetadata } from '../extracted'
55

6-
export type HoverType = 'mouseover' | 'mouseout' | 'touchstart'
6+
export type PointerhoverType = 'pointerover' | 'pointerout'
77

8-
export type HoverMetadata = (
8+
export type PointerhoverMetadata = (
99
& PointerStartMetadata
1010
& PointerMoveMetadata
1111
& PointerTimeMetadata
1212
)
1313

14-
export type HoverOptions = {
14+
export type PointerhoverOptions = {
1515
minDuration?: number,
16-
onOver?: HoverHook,
17-
onOut?: HoverHook,
16+
onOver?: PointerhoverHook,
17+
onOut?: PointerhoverHook,
1818
}
1919

20-
export type HoverHook = (api: HoverHookApi) => any
20+
export type PointerhoverHook = (api: PointerhoverHookApi) => any
2121

22-
export type HoverHookApi = HookApi<HoverType, HoverMetadata>
22+
export type PointerhoverHookApi = HookApi<PointerhoverType, PointerhoverMetadata>
2323

24-
const defaultOptions: HoverOptions = {
24+
const defaultOptions: PointerhoverOptions = {
2525
minDuration: 0,
2626
}
2727

2828
/**
29-
* [Docs](https://baleada.dev/docs/logic/factories/hover)
29+
* [Docs](https://baleada.dev/docs/logic/factories/pointerhover)
3030
*/
31-
export function createHover (options: HoverOptions = {}) {
31+
export function createPointerhover (options: PointerhoverOptions = {}) {
3232
const {
3333
minDuration,
3434
onOver,
@@ -39,18 +39,18 @@ export function createHover (options: HoverOptions = {}) {
3939
}
4040

4141
let request: number
42-
let mouseStatus: 'entered' | 'exited' = 'exited'
42+
let pointerStatus: 'entered' | 'exited' = 'exited'
4343

44-
const mouseover: RecognizeableEffect<'mouseover', HoverMetadata> = (event, api) => {
45-
if (mouseStatus === 'exited') {
46-
mouseStatus = 'entered'
44+
const pointerover: RecognizeableEffect<'pointerover', PointerhoverMetadata> = (event, api) => {
45+
if (pointerStatus === 'exited') {
46+
pointerStatus = 'entered'
4747

4848
storePointerStartMetadata({ event, api })
4949
storePointerMoveMetadata({ event, api })
5050
storePointerTimeMetadata({
5151
event,
5252
api,
53-
getShouldStore: () => mouseStatus === 'entered',
53+
getShouldStore: () => pointerStatus === 'entered',
5454
setRequest: newRequest => request = newRequest,
5555
// @ts-expect-error
5656
recognize,
@@ -64,7 +64,7 @@ export function createHover (options: HoverOptions = {}) {
6464
onOver?.(toHookApi(api))
6565
}
6666

67-
const recognize: RecognizeableEffect<'mouseover', HoverMetadata> = (event, api) => {
67+
const recognize: RecognizeableEffect<'pointerover', PointerhoverMetadata> = (event, api) => {
6868
const { getMetadata, recognized } = api,
6969
metadata = getMetadata()
7070

@@ -73,8 +73,8 @@ export function createHover (options: HoverOptions = {}) {
7373
}
7474
}
7575

76-
const mouseout: RecognizeableEffect<'mouseout', HoverMetadata> = (event, api) => {
77-
const { denied, listenInjection: { optionsByType: { mouseout: { target } } } } = api
76+
const pointerout: RecognizeableEffect<'pointerout', PointerhoverMetadata> = (event, api) => {
77+
const { denied, listenInjection: { optionsByType: { pointerout: { target } } } } = api
7878

7979
if (
8080
event.target !== target
@@ -84,36 +84,31 @@ export function createHover (options: HoverOptions = {}) {
8484
return
8585
}
8686

87-
if (mouseStatus === 'entered') {
87+
if (pointerStatus === 'entered') {
8888
denied()
8989
stop()
90-
mouseStatus = 'exited'
90+
pointerStatus = 'exited'
9191
}
9292

9393
onOut?.(toHookApi(api))
9494
}
9595

96-
const touchstart: RecognizeableEffect<'touchstart', HoverMetadata> = event => {
97-
event.preventDefault()
98-
}
99-
10096
return {
101-
mouseover: {
102-
effect: mouseover,
97+
pointerover: {
98+
effect: pointerover,
10399
stop,
104100
},
105-
mouseout,
106-
touchstart,
101+
pointerout,
107102
}
108103
}
109104

110-
export class Hover extends Listenable<HoverType, HoverMetadata> {
111-
constructor (options?: HoverOptions) {
105+
export class Pointerhover extends Listenable<PointerhoverType, PointerhoverMetadata> {
106+
constructor (options?: PointerhoverOptions) {
112107
super(
113-
'recognizeable' as HoverType,
108+
'recognizeable' as PointerhoverType,
114109
{
115110
recognizeable: {
116-
effects: createHover(options),
111+
effects: createPointerhover(options),
117112
},
118113
}
119114
)

0 commit comments

Comments
 (0)