-
Notifications
You must be signed in to change notification settings - Fork 10
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
2 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type {LoggerMixinInterface} from './logging.js'; | ||
import type {Constructor} from '@alwatr/type'; | ||
|
||
export declare class ToggleMixinInterface extends LoggerMixinInterface { | ||
private _selected: boolean; | ||
get selected(): boolean; | ||
set selected(value: boolean); | ||
protected _click(event: MouseEvent): void | ||
} | ||
|
||
export function ToggleMixin<T extends Constructor<LoggerMixinInterface>>( | ||
superClass: T, | ||
): Constructor<ToggleMixinInterface> & T { | ||
class ToggleMixinClass extends superClass { | ||
private _selected = false; | ||
|
||
get selected(): boolean { | ||
return this._selected; | ||
} | ||
set selected(value: boolean) { | ||
if (this._selected === value) return; | ||
this._logger.logProperty('selected', value); | ||
this._selected = value; | ||
if (value) { | ||
this.setAttribute('selected', ''); | ||
} | ||
else { | ||
this.removeAttribute('selected'); | ||
} | ||
} | ||
|
||
override connectedCallback(): void { | ||
super.connectedCallback(); | ||
this.addEventListener('click', this._click); | ||
} | ||
override disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
this.removeEventListener('click', this._click); | ||
} | ||
|
||
/** | ||
* On host click event. | ||
*/ | ||
protected _click(event: MouseEvent): void { | ||
this._logger.logMethod('_click'); | ||
if (event.metaKey || event.altKey || event.shiftKey) return; | ||
this.selected = !this.selected; | ||
} | ||
} | ||
|
||
return ToggleMixinClass as unknown as Constructor<ToggleMixinInterface> & T; | ||
} |