Skip to content

Commit

Permalink
feat(icon-button): new content prop with click signal event
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Feb 7, 2023
1 parent c3a74ec commit bb5dae6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
7 changes: 7 additions & 0 deletions core/type/src/event-signal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ClickSignalType = {
readonly x: number;
readonly y: number;
readonly altKey: boolean;
readonly ctrlKey: boolean;
readonly metaKey: boolean;
};
1 change: 1 addition & 0 deletions core/type/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './storage.js';
export * from './global.js';
export * from './i18n.js';
export * from './type-helper.js';
export * from './event-signal.js';

Alwatr.registeredList.push({
name: '@alwatr/type',
Expand Down
67 changes: 56 additions & 11 deletions ui/ui-kit/src/button/icon-button.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import {css, customElement, html, property} from '@alwatr/element';
import {css, customElement, html, property, type PropertyValues} from '@alwatr/element';
import {eventTrigger} from '@alwatr/signal';

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

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

import '@alwatr/icon';

declare global {
interface HTMLElementTagNameMap {
'alwatr-icon-button': AlwatrStandardIconButton;
}
}

export interface IconButtonContent extends StringifyableRecord {
/**
* Icon name.
*/
icon: string;

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

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

/**
* Alwatr standard icon button element.
*
Expand Down Expand Up @@ -47,23 +68,47 @@ export class AlwatrStandardIconButton extends AlwatrSurface {
`,
];

@property()
icon?: string;
@property({type: Object, attribute: false})
content?: IconButtonContent;

@property({type: Boolean, attribute: 'flip-rtl'})
flipRtl = false;
constructor() {
super();
this._click = this._click.bind(this);
}

override connectedCallback(): void {
this.setAttribute('stated', '');
super.connectedCallback();
this.addEventListener('click', this._click);
}

override disconnectedCallback(): void {
super.disconnectedCallback();
this.removeEventListener('click', this._click);
}

protected override shouldUpdate(changedProperties: PropertyValues<this>): boolean {
return super.shouldUpdate(changedProperties) && this.content != null;
}

override render(): unknown {
super.render();
return html`<alwatr-icon
.name=${this.icon}
?flip-rtl=${this.flipRtl}
></alwatr-icon>`;
this._logger.logMethod('render');
if (this.content == null) return;
return html`<alwatr-icon .name=${this.content.icon} ?flip-rtl=${this.content.flipRtl}></alwatr-icon>`;
}

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

Expand Down
2 changes: 1 addition & 1 deletion ui/ui-kit/src/card/icon-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class AlwatrIconBox extends AlwatrSurface {
`,
];

@property({type: Object})
@property({type: Object, attribute: false})
content?: IconBoxContent;

protected override update(changedProperties: PropertyValues<this>): void {
Expand Down
6 changes: 3 additions & 3 deletions uniquely/soffit-pwa/src/director/l18e-loader.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {setL18eLoader} from '@alwatr/i18n';

import {logger} from './logger';
import {logger} from './logger.js';

import type {L18eContext} from '@alwatr/type';

setL18eLoader((locale) => {
const language = locale.language;
logger.logMethodArgs('l18eLoader', {language});
return language === 'en'
? import('../l18r/en.json', {assert: {type: 'json'}}) as Promise<L18eContext>
: import('../l18r/fa.json', {assert: {type: 'json'}}) as Promise<L18eContext>;
? import('../l18r/en.json', {assert: {type: 'json'}}) as unknown as Promise<L18eContext>
: import('../l18r/fa.json', {assert: {type: 'json'}}) as unknown as Promise<L18eContext>;
});

0 comments on commit bb5dae6

Please sign in to comment.