Skip to content

Commit

Permalink
fix: add custom event for msg outer click
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanenkoStud committed Dec 11, 2023
1 parent db5b3c0 commit bb02305
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ export class SelectionContainerController<U extends SelectableElement> {
private handleSelectionStartBound: (e: MouseEvent|TouchEvent) => void;
private handleSelectionEndBound: (e: MouseEvent|TouchEvent) => void;
private handleIgnoreSelectionBound: () => void;
private handleClickBound: (e: Event) => void;
private pressTimer: number | null = null;
private ignored = false;
private selected = false;

constructor(
private host: ReactiveControllerHost & SelectableContainer<U>,
Expand All @@ -60,7 +58,6 @@ export class SelectionContainerController<U extends SelectableElement> {
this.handleSelectionStartBound = this.handleSelectionStart.bind(this);
this.handleSelectionEndBound = this.handleSelectionEnd.bind(this);
this.handleIgnoreSelectionBound = this.handleIgnoreSelection.bind(this);
this.handleClickBound = this.handleClick.bind(this);
}

hostUpdate() {
Expand All @@ -72,7 +69,6 @@ export class SelectionContainerController<U extends SelectableElement> {
this.defaultSlot.removeEventListener('touchstart', this.handleSelectionStartBound,);
this.defaultSlot.removeEventListener('touchmove', this.handleIgnoreSelectionBound,);
this.defaultSlot.removeEventListener('touchend', this.handleSelectionEndBound);
this.defaultSlot.removeEventListener('click', this.handleClickBound);
}
}

Expand All @@ -84,7 +80,6 @@ export class SelectionContainerController<U extends SelectableElement> {
this.defaultSlot?.addEventListener('touchstart', this.handleSelectionStartBound, { passive: true });
this.defaultSlot?.addEventListener('touchmove', this.handleIgnoreSelectionBound, { passive: true });
this.defaultSlot?.addEventListener('touchend', this.handleSelectionEndBound);
this.defaultSlot?.addEventListener('click', this.handleClickBound);
}

private _updateSelected(selectedElement: SelectableElement) {
Expand All @@ -108,7 +103,6 @@ export class SelectionContainerController<U extends SelectableElement> {
const e = new CustomEvent(this.eventNames.select, {...CUSTOM_EVENT_INIT, detail});
this.host.dispatchEvent(e);
if (!e.defaultPrevented) this._updateSelected(selectedElement);
this.selected = true;
}

private getSelectable(target: HTMLElement|EventTarget|null): U | null {
Expand Down Expand Up @@ -150,11 +144,4 @@ export class SelectionContainerController<U extends SelectableElement> {
clearTimeout(this.pressTimer);
this.ignored = true;
}

private handleClick(e: Event) {
if(this.selected) {
e.preventDefault();
}
this.selected = false;
}
}
9 changes: 5 additions & 4 deletions packages/kite-chat-component/src/kite-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
ContextMenuAction,
KitePointerAnchorElement,
} from './components';
import {KiteMsgElement} from './kite-msg';
import {KiteMsgElement, MsgOutsideClick} from './kite-msg';

console.debug('kite-chat loaded');

Expand Down Expand Up @@ -220,7 +220,7 @@ export class KiteChatElement extends
>
</kite-chat-header>
<main
@click=${this._contextMenu}
@kite-msg.outsideclick=${this._contextMenu}
@scroll=${() => this.contextMenu.hide()}
class="relative flex flex-1 overflow-hidden flex-col-reverse bg-slate-300/50 snap-y overflow-y-auto outline-none border-none"
>
Expand Down Expand Up @@ -357,8 +357,9 @@ export class KiteChatElement extends
return !e.defaultPrevented;
}

private _contextMenu(event: PointerEvent) {
if(event.defaultPrevented) return;
private _contextMenu(wrapperEvent: CustomEvent<MsgOutsideClick>) {
const event = wrapperEvent.detail;
if(event.defaultPrevented || this.selectedElements.length > 0) return;
const msgElement = (event.target as HTMLElement).closest(KiteMsgElement.TAG) as KiteMsgElement | null;
if(!msgElement) {
return;
Expand Down
31 changes: 28 additions & 3 deletions packages/kite-chat-component/src/kite-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const hhmmLocalizedFormat = new Intl.DateTimeFormat(
}
);

const CUSTOM_EVENT_INIT = {
bubbles: true,
composed: true,
};

export type MsgOutsideClick = PointerEvent;

/**
* Styled chat message component. Presence of the <pre>status</pre> attribute means outgoing message.
* @attr status
Expand Down Expand Up @@ -100,10 +107,28 @@ export class KiteMsgElement extends LitElement {
this.selected = false;
}

private handleClick(event: Event) {
const path = event.composedPath();
const isInsideMessageContainer = path.some((node) => node instanceof HTMLElement && node.classList.contains('message-container'));

if (!isInsideMessageContainer) {
// Click occurred on the host but not on .message-container
this.dispatchEvent(new CustomEvent('kite-msg.outsideclick', {...CUSTOM_EVENT_INIT, detail: event}));
}
}

override connectedCallback() {
super.connectedCallback();
this.addEventListener('click', this.handleClick.bind(this));
}

override disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('click', this.handleClick.bind(this));
}

override render() {
return html` <div class="message-container" @click=${(e: MouseEvent) => {
e.stopPropagation();
}}><slot></slot>${this._renderStatus()}${this._renderTimestamp()}</div>`;
return html` <div class="message-container"><slot></slot>${this._renderStatus()}${this._renderTimestamp()}</div>`;
}

private _renderStatus() {
Expand Down

0 comments on commit bb02305

Please sign in to comment.