-
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.
Closes #91 - [x] drag - [ ] move - [ ] hover - [ ] scroll - [ ] wheel - [ ] pinch - [ ] multiple
- Loading branch information
Showing
20 changed files
with
281 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Component, signal } from '@angular/core'; | ||
import type { Vector2 } from '@use-gesture/vanilla'; | ||
import { | ||
NgxDrag, | ||
injectDrag, | ||
provideZonelessGesture, | ||
type NgxDragState, | ||
} from 'ngxtension/gestures'; | ||
|
||
@Component({ | ||
selector: 'app-box', | ||
standalone: true, | ||
template: ` | ||
<span style="position: absolute; top: -1rem; "> | ||
{{ position() }} | ||
</span> | ||
`, | ||
host: { | ||
class: 'draggable-box pink-draggable-box', | ||
}, | ||
}) | ||
export class Box { | ||
position = signal<Vector2>([0, 0]); | ||
|
||
constructor() { | ||
injectDrag( | ||
({ target, active, offset: [ox, oy], cdr }) => { | ||
const el = target as HTMLElement; | ||
this.position.set([ox, oy]); | ||
el.style.transform = `translate(${ox}px, ${oy}px)`; | ||
if (!active) { | ||
cdr.detectChanges(); | ||
} | ||
}, | ||
{ config: () => ({ from: this.position }) } | ||
); | ||
} | ||
} | ||
|
||
@Component({ | ||
standalone: true, | ||
imports: [Box, NgxDrag], | ||
template: ` | ||
<app-box /> | ||
<div | ||
class="draggable-box blue-draggable-box" | ||
(ngxDrag)="onDrag($event)" | ||
[ngxDragConfig]="{ from: position }" | ||
></div> | ||
`, | ||
providers: [provideZonelessGesture()], | ||
}) | ||
export default class Drag { | ||
position = signal<Vector2>([0, 0]); | ||
|
||
onDrag({ target, active, offset: [ox, oy], cdr }: NgxDragState) { | ||
const el = target as HTMLElement; | ||
this.position.set([ox, oy]); | ||
el.style.transform = `translate(${ox}px, ${oy}px)`; | ||
if (!active) { | ||
cdr.detectChanges(); | ||
} | ||
} | ||
} |
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,3 @@ | ||
# ngxtension/gestures | ||
|
||
Secondary entry point of `ngxtension`. It can be used by importing from `ngxtension/gestures`. |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
} | ||
} |
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,33 @@ | ||
{ | ||
"name": "ngxtension/gestures", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"projectType": "library", | ||
"sourceRoot": "libs/ngxtension/gestures/src", | ||
"targets": { | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/ngxtension/jest.config.ts", | ||
"testPathPattern": ["gestures"], | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": [ | ||
"libs/ngxtension/gestures/**/*.ts", | ||
"libs/ngxtension/gestures/**/*.html" | ||
] | ||
} | ||
} | ||
} | ||
} |
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, | ||
Directive, | ||
ElementRef, | ||
EventEmitter, | ||
Injector, | ||
Input, | ||
NgZone, | ||
Output, | ||
effect, | ||
inject, | ||
runInInjectionContext, | ||
signal, | ||
type OnInit, | ||
} from '@angular/core'; | ||
import { | ||
DragGesture, | ||
type DragConfig, | ||
type DragState, | ||
type EventTypes, | ||
type Handler, | ||
} from '@use-gesture/vanilla'; | ||
import { assertInjector } from 'ngxtension/assert-injector'; | ||
import { injectZonelessGesture } from './zoneless-gesture'; | ||
|
||
type DragHandler = Handler<'drag', EventTypes['drag']>; | ||
export type NgxDragState = Parameters<DragHandler>[0] & { | ||
cdr: ChangeDetectorRef; | ||
}; | ||
type NgxDragHandler = (state: NgxDragState) => ReturnType<DragHandler>; | ||
|
||
export function injectDrag( | ||
handler: NgxDragHandler, | ||
{ | ||
injector, | ||
zoneless, | ||
config = () => ({}), | ||
}: { injector?: Injector; zoneless?: boolean; config?: () => DragConfig } = {} | ||
) { | ||
injector = assertInjector(injectDrag, injector); | ||
return runInInjectionContext(injector, () => { | ||
const zonelessGesture = injectZonelessGesture(); | ||
const host = inject(ElementRef) as ElementRef<HTMLElement>; | ||
const zone = inject(NgZone); | ||
const cdr = inject(ChangeDetectorRef); | ||
|
||
zoneless ??= zonelessGesture; | ||
|
||
const ngHandler = (state: DragState) => { | ||
Object.assign(state, { cdr }); | ||
return handler(state as NgxDragState); | ||
}; | ||
|
||
let dragGesture: DragGesture; | ||
|
||
dragGesture = zoneless | ||
? zone.runOutsideAngular( | ||
() => new DragGesture(host.nativeElement, ngHandler) | ||
) | ||
: new DragGesture(host.nativeElement, ngHandler); | ||
|
||
effect(() => { | ||
if (zoneless) { | ||
zone.runOutsideAngular(() => { | ||
dragGesture.setConfig(config()); | ||
}); | ||
} else { | ||
dragGesture.setConfig(config()); | ||
} | ||
}); | ||
|
||
inject(DestroyRef).onDestroy(dragGesture.destroy.bind(dragGesture)); | ||
}); | ||
} | ||
|
||
@Directive({ | ||
selector: '[ngxDrag]', | ||
standalone: true, | ||
}) | ||
export class NgxDrag implements OnInit { | ||
private config = signal<DragConfig>({}); | ||
@Input('ngxDragConfig') set _config(config: DragConfig) { | ||
this.config.set(config); | ||
} | ||
@Input('ngxDragZoneless') zoneless?: boolean; | ||
@Output() ngxDrag = new EventEmitter<NgxDragState>(); | ||
|
||
private injector = inject(Injector); | ||
|
||
ngOnInit(): void { | ||
injectDrag(this.ngxDrag.emit.bind(this.ngxDrag), { | ||
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,2 @@ | ||
export * from './drag'; | ||
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,9 @@ | ||
import { createInjectionToken } from 'ngxtension/create-injection-token'; | ||
|
||
const [injectZonelessGesture, provideFn] = createInjectionToken(() => false); | ||
|
||
function provideZonelessGesture() { | ||
return provideFn(true); | ||
} | ||
|
||
export { injectZonelessGesture, provideZonelessGesture }; |
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
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
Oops, something went wrong.