-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
320 additions
and
77 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 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
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,98 @@ | ||
import { | ||
ChangeDetectorRef, | ||
DestroyRef, | ||
ElementRef, | ||
NgZone, | ||
effect, | ||
inject, | ||
type Injector, | ||
type Type, | ||
} from '@angular/core'; | ||
import type { | ||
EventTypes, | ||
GestureKey, | ||
Handler, | ||
UserGestureConfig, | ||
} from '@use-gesture/vanilla'; | ||
import type { Recognizer } from '@use-gesture/vanilla/dist/declarations/src/Recognizer'; | ||
import { assertInjector } from 'ngxtension/assert-injector'; | ||
import { injectZonelessGesture } from './zoneless-gesture'; | ||
|
||
type InjectGestureFn< | ||
TGestureKey extends GestureKey, | ||
TGestureHandler extends Handler<TGestureKey, EventTypes[TGestureKey]>, | ||
TGestureConfig extends UserGestureConfig[TGestureKey] | ||
> = { | ||
( | ||
handler: ( | ||
state: Parameters<TGestureHandler>[0] & { cdr: ChangeDetectorRef } | ||
) => ReturnType<TGestureHandler>, | ||
options?: { | ||
injector?: Injector; | ||
zoneless?: boolean; | ||
config?: () => TGestureConfig; | ||
} | ||
): void; | ||
}; | ||
|
||
export type GestureInfer<TInjectGesture extends (...args: any[]) => void> = | ||
TInjectGesture extends InjectGestureFn< | ||
infer _GestureKey, | ||
infer _GestureHandler, | ||
infer GestureConfig | ||
> | ||
? { | ||
key: _GestureKey; | ||
handlerParameters: Parameters<TInjectGesture>[0]; | ||
state: Parameters<Parameters<TInjectGesture>[0]>[0]; | ||
config: GestureConfig; | ||
} | ||
: never; | ||
|
||
export function createGesture< | ||
TGestureKey extends GestureKey, | ||
TRecognizer extends Recognizer<TGestureKey> | ||
>(key: TGestureKey, gesture: Type<TRecognizer>) { | ||
type GestureHandler = Handler<TGestureKey, EventTypes[TGestureKey]>; | ||
type GestureConfig = UserGestureConfig[TGestureKey]; | ||
|
||
return function _injectGesture( | ||
handler, | ||
{ injector, config = () => ({}), zoneless } = {} | ||
) { | ||
return assertInjector(_injectGesture, injector, () => { | ||
const zonelessGesture = injectZonelessGesture(); | ||
const host = inject(ElementRef) as ElementRef<HTMLElement>; | ||
const zone = inject(NgZone); | ||
const cdr = inject(ChangeDetectorRef); | ||
|
||
zoneless ??= zonelessGesture; | ||
|
||
const ngHandler = (state: Parameters<GestureHandler>[0]) => { | ||
return handler(Object.assign(state, { cdr })); | ||
}; | ||
|
||
const gestureInstance = zoneless | ||
? zone.runOutsideAngular( | ||
() => new gesture(host.nativeElement, ngHandler) | ||
) | ||
: new gesture(host.nativeElement, ngHandler); | ||
|
||
effect(() => { | ||
if (zoneless) { | ||
zone.runOutsideAngular(() => { | ||
// @ts-expect-error | ||
gestureInstance.setConfig(config()); | ||
}); | ||
} else { | ||
// @ts-expect-error | ||
gestureInstance.setConfig(config()); | ||
} | ||
}); | ||
|
||
inject(DestroyRef).onDestroy( | ||
gestureInstance.destroy.bind(gestureInstance) | ||
); | ||
}); | ||
} as InjectGestureFn<TGestureKey, GestureHandler, GestureConfig>; | ||
} |
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,38 @@ | ||
import { | ||
Directive, | ||
EventEmitter, | ||
Injector, | ||
Input, | ||
Output, | ||
inject, | ||
signal, | ||
type OnInit, | ||
} from '@angular/core'; | ||
import { HoverGesture } from '@use-gesture/vanilla'; | ||
import { createGesture, type GestureInfer } from './gesture'; | ||
|
||
export const injectHover = createGesture('hover', HoverGesture); | ||
export type NgxInjectHover = GestureInfer<typeof injectHover>; | ||
|
||
@Directive({ | ||
selector: '[ngxHover]', | ||
standalone: true, | ||
}) | ||
export class NgxHover implements OnInit { | ||
private config = signal<NgxInjectHover['config']>({}); | ||
@Input('ngxHoverConfig') set _config(config: NgxInjectHover['config']) { | ||
this.config.set(config); | ||
} | ||
@Input('ngxHoverZoneless') zoneless?: boolean; | ||
@Output() ngxHover = new EventEmitter<NgxInjectHover['state']>(); | ||
|
||
private injector = inject(Injector); | ||
|
||
ngOnInit(): void { | ||
injectHover(this.ngxHover.emit.bind(this.ngxHover), { | ||
injector: this.injector, | ||
zoneless: this.zoneless, | ||
config: this.config, | ||
}); | ||
} | ||
} |
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,2 +1,7 @@ | ||
export * from './drag'; | ||
export * from './hover'; | ||
export * from './move'; | ||
export * from './pinch'; | ||
export * from './scroll'; | ||
export * from './wheel'; | ||
export * from './zoneless-gesture'; |
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,38 @@ | ||
import { | ||
Directive, | ||
EventEmitter, | ||
Injector, | ||
Input, | ||
Output, | ||
inject, | ||
signal, | ||
type OnInit, | ||
} from '@angular/core'; | ||
import { MoveGesture } from '@use-gesture/vanilla'; | ||
import { createGesture, type GestureInfer } from './gesture'; | ||
|
||
export const injectMove = createGesture('move', MoveGesture); | ||
export type NgxInjectMove = GestureInfer<typeof injectMove>; | ||
|
||
@Directive({ | ||
selector: '[ngxMove]', | ||
standalone: true, | ||
}) | ||
export class NgxMove implements OnInit { | ||
private config = signal<NgxInjectMove['config']>({}); | ||
@Input('ngxMoveConfig') set _config(config: NgxInjectMove['config']) { | ||
this.config.set(config); | ||
} | ||
@Input('ngxMoveZoneless') zoneless?: boolean; | ||
@Output() ngxMove = new EventEmitter<NgxInjectMove['state']>(); | ||
|
||
private injector = inject(Injector); | ||
|
||
ngOnInit(): void { | ||
injectMove(this.ngxMove.emit.bind(this.ngxMove), { | ||
injector: this.injector, | ||
zoneless: this.zoneless, | ||
config: this.config, | ||
}); | ||
} | ||
} |
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,38 @@ | ||
import { | ||
Directive, | ||
EventEmitter, | ||
Injector, | ||
Input, | ||
Output, | ||
inject, | ||
signal, | ||
type OnInit, | ||
} from '@angular/core'; | ||
import { PinchGesture } from '@use-gesture/vanilla'; | ||
import { createGesture, type GestureInfer } from './gesture'; | ||
|
||
export const injectPinch = createGesture('pinch', PinchGesture); | ||
export type NgxInjectPinch = GestureInfer<typeof injectPinch>; | ||
|
||
@Directive({ | ||
selector: '[ngxPinch]', | ||
standalone: true, | ||
}) | ||
export class NgxPinch implements OnInit { | ||
private config = signal<NgxInjectPinch['config']>({}); | ||
@Input('ngxPinchConfig') set _config(config: NgxInjectPinch['config']) { | ||
this.config.set(config); | ||
} | ||
@Input('ngxPinchZoneless') zoneless?: boolean; | ||
@Output() ngxPinch = new EventEmitter<NgxInjectPinch['state']>(); | ||
|
||
private injector = inject(Injector); | ||
|
||
ngOnInit(): void { | ||
injectPinch(this.ngxPinch.emit.bind(this.ngxPinch), { | ||
injector: this.injector, | ||
zoneless: this.zoneless, | ||
config: this.config, | ||
}); | ||
} | ||
} |
Oops, something went wrong.