-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add keyboard shortcut component (#136)
* feat: add keyboard shortcut component * chore: add docs * chore: bump version
- Loading branch information
Showing
8 changed files
with
146 additions
and
23 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,30 @@ | ||
--- | ||
id: ng-keyboard-shortcut | ||
title: ng-keyboard-shortcut | ||
sidebar_position: 2 | ||
--- | ||
|
||
```<ng-keyboard-shortcut>``` component can help you create a single shortcut, in a more declarative way. | ||
Each component added can be bound to one shortcut, and will emit using (fire) Output binding when the shortcut is clicked. | ||
Can be useful to create multiple shortcut, each shortcut will be added to the Help menu automatically as long as you provide a description and a label. | ||
|
||
|
||
|
||
## Example | ||
|
||
```html | ||
|
||
<ng-keyboard-shortcut *ngIf="enableShortcut" [key]="'shift + k'" (fire)="onShiftPlusKClicked($event)"></ng-keyboard-shortcut> | ||
``` | ||
|
||
|
||
## Inputs: | ||
| Name | Type | Default | Description | | ||
|----------|:-------------:|-----------------: |:-------------:| | ||
| key | ```string / string[] ```| none |List or single key (sequencese or combinations) to bind to. | | ||
| description | `string` | `""` | the description to show in the help menu | | ||
| label | `string` | `""` | the label to group the key in the help menu | | ||
| preventDefault | `boolean` | `false` | whether to prevent default of the original key fired | | ||
| target | `HTMLElement` | `undefined` | Limit the event to a specific target | | ||
| allowIn | `AllowIn` | `[]` | Keys are disable for Input, Select, ContentEditable and Textarea, this can be used to allow the shortcut to work in those elements | | ||
| throttleTime | `number` | `0` | Throttle the key event | |
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
24 changes: 24 additions & 0 deletions
24
libs/ng-keyboard-shortcuts/src/lib/keyboard-shortcut.component.spec.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,24 @@ | ||
import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
|
||
import { KeyboardShortcutComponent } from "./keyboard-shortcut.component"; | ||
|
||
describe("NgKeyboardShortcutComponent", () => { | ||
let component: KeyboardShortcutComponent; | ||
let fixture: ComponentFixture<KeyboardShortcutComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [KeyboardShortcutComponent] | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(KeyboardShortcutComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it("should create", () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
libs/ng-keyboard-shortcuts/src/lib/keyboard-shortcut.component.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,60 @@ | ||
import { | ||
Component, | ||
OnInit, | ||
ChangeDetectionStrategy, | ||
Input, | ||
OnDestroy, | ||
OnChanges, | ||
SimpleChanges, | ||
AfterViewInit, | ||
Output, | ||
EventEmitter | ||
} from '@angular/core'; | ||
import {AllowIn, ShortcutEventOutput} from './ng-keyboard-shortcuts.interfaces'; | ||
import {KeyboardShortcutsService} from './ng-keyboard-shortcuts.service'; | ||
|
||
@Component({ | ||
selector: "ng-keyboard-shortcut", | ||
template: "<ng-content ></ng-content>", | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class KeyboardShortcutComponent implements AfterViewInit, OnDestroy, OnChanges { | ||
constructor(private keyboard: KeyboardShortcutsService) {} | ||
private clearId; | ||
|
||
@Input() description: string; | ||
// @Input() encapsulate = true; | ||
@Input() label: string; | ||
@Input() preventDefault: boolean; | ||
@Input() allowIn: AllowIn[]; | ||
@Input() key: string | string[]; | ||
@Input() target: HTMLElement; | ||
@Input() throttleTime: number; | ||
|
||
@Output() fire = new EventEmitter(); | ||
|
||
|
||
ngOnDestroy(): void { | ||
this.keyboard.remove(this.clearId); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
|
||
} | ||
|
||
ngAfterViewInit(): void { | ||
this.clearId = this.keyboard.add({ | ||
description: this.description, | ||
label: this.label, | ||
preventDefault: this.preventDefault, | ||
allowIn: this.allowIn, | ||
target: this.target, | ||
key: this.key, | ||
throttleTime: this.throttleTime, | ||
command: (event: ShortcutEventOutput) => { | ||
this.fire.emit(event); | ||
} | ||
}) | ||
} | ||
|
||
} |
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
b46754a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ng-keyboard-shortcuts – ./
ng-keyboard-shortcuts-git-master-omridevk.vercel.app
ng-keyboard-shortcuts.vercel.app
ng-keyboard-shortcuts-omridevk.vercel.app