Skip to content

Commit

Permalink
feat(button): click signal with detail
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Mar 2, 2023
1 parent db1e138 commit 03c2dc1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
5 changes: 4 additions & 1 deletion core/type/src/event-signal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export type ClickSignalType = {
import type {Stringifyable} from './type-helper.js';

export type ClickSignalType<T extends Stringifyable = Stringifyable> = {
readonly x: number;
readonly y: number;
readonly altKey: boolean;
readonly ctrlKey: boolean;
readonly metaKey: boolean;
detail: T;
};
6 changes: 5 additions & 1 deletion ui/ui-kit/src/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {eventTrigger} from '@alwatr/signal';

import {AlwatrSurface} from '../card/surface.js';

import type {ClickSignalType} from '@alwatr/type';
import type {ClickSignalType, Stringifyable} from '@alwatr/type';

declare global {
interface HTMLElementTagNameMap {
Expand Down Expand Up @@ -48,6 +48,9 @@ export class AlwatrButton extends AlwatrSurface {
@property({attribute: 'signal-id'})
signalId?: string;

@property({attribute: false})
detail?: Stringifyable;

override connectedCallback(): void {
super.connectedCallback();
this.setAttribute('stated', '');
Expand Down Expand Up @@ -81,6 +84,7 @@ export class AlwatrButton extends AlwatrSurface {
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
detail: this.detail,
});
}
}
Expand Down
20 changes: 13 additions & 7 deletions ui/ui-kit/src/button/icon-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {eventTrigger} from '@alwatr/signal';

import {AlwatrSurface} from '../card/surface.js';

import type {StringifyableRecord, ClickSignalType} from '@alwatr/type';
import type {StringifyableRecord, ClickSignalType, Stringifyable} from '@alwatr/type';

declare global {
interface HTMLElementTagNameMap {
Expand All @@ -18,15 +18,20 @@ export interface IconButtonContent extends StringifyableRecord {
*/
icon: string;

/**
* Flip icon on rtl
*/
flipRtl?: true;

/**
* Unique name for identify click event over signal.
*/
clickSignalId?: string;

/**
* Flip icon on rtl
* Dispatched signal with ClickSignalType and this detail.
*/
flipRtl?: true;
clickDetail?: Stringifyable;
}

/**
Expand Down Expand Up @@ -94,15 +99,16 @@ export class AlwatrStandardIconButton extends AlwatrSurface {
}

protected _click(event: MouseEvent): void {
const signalId = this.content?.clickSignalId;
this._logger.logMethodArgs('_click', {signalId});
if (signalId) {
eventTrigger.dispatch<ClickSignalType>(signalId, {
const clickSignalId = this.content?.clickSignalId;
this._logger.logMethodArgs('_click', {signalId: clickSignalId});
if (clickSignalId) {
eventTrigger.dispatch<ClickSignalType>(clickSignalId, {
x: event.clientX,
y: event.clientY,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
detail: this.content?.clickDetail,
});
}
}
Expand Down

0 comments on commit 03c2dc1

Please sign in to comment.