-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add KeyboardMixin and make other mixins use it (#2432)
- Loading branch information
Showing
11 changed files
with
234 additions
and
79 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
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,32 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2021 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
|
||
/** | ||
* A mixin that manages keyboard handling. | ||
* The mixin subscribes to the keyboard events while an actual implementation | ||
* for the event handlers is left to the client (a component or another mixin). | ||
*/ | ||
declare function KeyboardMixin<T extends new (...args: any[]) => {}>(base: T): T & KeyboardMixinConstructor; | ||
|
||
interface KeyboardMixinConstructor { | ||
new (...args: any[]): KeyboardMixin; | ||
} | ||
|
||
interface KeyboardMixin { | ||
/** | ||
* A handler for the `keydown` event. By default, it does nothing. | ||
* Override the method to implement your own behavior. | ||
*/ | ||
_onKeyDown(event: KeyboardEvent): void; | ||
|
||
/** | ||
* A handler for the `keyup` event. By default, it does nothing. | ||
* Override the method to implement your own behavior. | ||
*/ | ||
_onKeyUp(event: KeyboardEvent): void; | ||
} | ||
|
||
export { KeyboardMixinConstructor, KeyboardMixin }; |
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,51 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2021 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js'; | ||
|
||
const KeyboardMixinImplementation = (superclass) => | ||
class KeyboardMixinClass extends superclass { | ||
/** @protected */ | ||
ready() { | ||
super.ready(); | ||
|
||
this.addEventListener('keydown', (event) => { | ||
this._onKeyDown(event); | ||
}); | ||
|
||
this.addEventListener('keyup', (event) => { | ||
this._onKeyUp(event); | ||
}); | ||
} | ||
|
||
/** | ||
* A handler for the `keydown` event. By default, it does nothing. | ||
* Override the method to implement your own behavior. | ||
* | ||
* @param {KeyboardEvent} _event | ||
* @protected | ||
*/ | ||
_onKeyDown(_event) { | ||
// To be implemented. | ||
} | ||
|
||
/** | ||
* A handler for the `keyup` event. By default, it does nothing. | ||
* Override the method to implement your own behavior. | ||
* | ||
* @param {KeyboardEvent} _event | ||
* @protected | ||
*/ | ||
_onKeyUp(_event) { | ||
// To be implemented. | ||
} | ||
}; | ||
|
||
/** | ||
* A mixin that manages keyboard handling. | ||
* The mixin subscribes to the keyboard events while an actual implementation | ||
* for the event handlers is left to the client (a component or another mixin). | ||
*/ | ||
export const KeyboardMixin = dedupingMixin(KeyboardMixinImplementation); |
Oops, something went wrong.