Skip to content

Commit

Permalink
fix: correct position for mention dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Jul 19, 2024
1 parent f6d9b08 commit ea8f727
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
29 changes: 15 additions & 14 deletions projects/demo/src/app/pages/mention/examples/1/mention/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<tui-data-list (mouseleave.capture)="$event.stopPropagation()">
<button
*ngFor="let item of getFilteredItems(items, mentionSuggestions); let i = index"
tuiOption
[tuiAutoFocus]="i == 0"
(click)="setMention.emit(item)"
(keydown.enter)="setMention.emit(item)"
>
{{ item.name }}
<tui-avatar
size="s"
[src]="item.avatar || (item.name | tuiInitials)"
></tui-avatar>
</button>
<tui-data-list>
<div #container>
<button
*ngFor="let item of getFilteredItems(items, mentionSuggestions)"
tuiOption
(click)="setMention.emit(item)"
(keydown.enter)="setMention.emit(item)"
>
{{ item.name }}
<tui-avatar
size="s"
[src]="item.avatar || (item.name | tuiInitials)"
></tui-avatar>
</button>
</div>
</tui-data-list>
21 changes: 21 additions & 0 deletions projects/demo/src/app/pages/mention/examples/1/mention/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
HostListener,
Input,
Output,
ViewChild,
} from '@angular/core';
import {tuiPure} from '@taiga-ui/cdk';

Expand All @@ -19,6 +22,9 @@ export interface User {
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MentionsComponent {
@ViewChild('container', {read: ElementRef})
private readonly container?: ElementRef<HTMLDivElement>;

@Input()
mentionSuggestions?: string;

Expand Down Expand Up @@ -48,4 +54,19 @@ export class MentionsComponent {
)
: items;
}

@HostListener('window:keydown.arrowUp', ['$event', 'false'])
@HostListener('window:keydown.arrowDown', ['$event', 'true'])
protected down(event: Event, isDown: boolean): void {
const buttons = Array.from(this.el?.querySelectorAll('button') ?? []);
const button = isDown ? buttons[0] : buttons[buttons.length - 1];

if (!this.el?.contains(event.target as any)) {
button.focus();
}
}

private get el(): HTMLDivElement | null {
return this.container?.nativeElement ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class TuiDropdownToolbarDirective extends TuiDropdownSelectionDirective {
range.commonAncestorContainer.parentElement?.closest('tui-dropdown');

this.range =
contained && tuiIsTextNode(range.commonAncestorContainer)
(contained && tuiIsTextNode(range.commonAncestorContainer)) ||
range.commonAncestorContainer?.nodeName === 'P'
? range
: this.range;

Expand Down

0 comments on commit ea8f727

Please sign in to comment.