Skip to content

Commit

Permalink
chore: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Jul 15, 2024
1 parent e3cce99 commit fa54099
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {TuiKeyboardService} from '@taiga-ui/addon-mobile/services';
import {TuiActiveZone} from '@taiga-ui/cdk/directives/active-zone';
import type {TuiSwipeEvent} from '@taiga-ui/cdk/directives/swipe';
import {TuiSwipe} from '@taiga-ui/cdk/directives/swipe';
import {tuiInjectElement, tuiIsElement, tuiIsNodeIn} from '@taiga-ui/cdk/utils/dom';
import {tuiInjectElement, tuiIsElement} from '@taiga-ui/cdk/utils/dom';
import {tuiGetNativeFocused} from '@taiga-ui/cdk/utils/focus';
import {tuiPx} from '@taiga-ui/cdk/utils/miscellaneous';
import {tuiFadeIn, tuiSlideInTop} from '@taiga-ui/core/animations';
Expand Down Expand Up @@ -92,12 +92,10 @@ export class TuiDropdownMobileComponent implements OnDestroy, AfterViewInit {

protected onClick(event: MouseEvent): void {
if (
!this.el.contains(event.target as Node) &&
// TODO: find a better way to check if the click is inside interactive element in textfield
!(
tuiIsNodeIn(event.target as Node, 'tui-svg') ||
(tuiIsElement(event.target) && event.target.tagName === 'button')
)
tuiIsElement(event.target) &&
!this.el.contains(event.target) &&
(!this.dropdown.el.contains(event.target) ||
event.target.matches('input,textarea'))
) {
event.stopPropagation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tui-dropdown-mobile._sheet {
overflow: auto;
background: var(--tui-service-backdrop);
/* stylelint-disable-next-line */
box-shadow: 0 -50vh 5rem 5rem var(--tui-service-backdrop);
box-shadow: 0 -80vh 0 5rem var(--tui-service-backdrop);
overflow-y: scroll;
scroll-snap-type: y mandatory;
overscroll-behavior: none;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Directive, Input, Output} from '@angular/core';
import {BehaviorSubject} from 'rxjs';

/**
* @deprecated TODO: remove in v.5 when legacy controls are dropped
*/
@Directive({
standalone: true,
selector:
'[tuiDropdownOpen]:not([tuiDropdown]),[tuiDropdownOpenChange]:not([tuiDropdown])',
})
export class TuiDropdownOpenLegacy {
@Output()
public readonly tuiDropdownOpenChange = new BehaviorSubject(false);

@Input()
public set tuiDropdownOpen(open: boolean) {
this.tuiDropdownOpenChange.next(open);
}
}
19 changes: 8 additions & 11 deletions projects/core/directives/dropdown/dropdown-open.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function shouldClose(this: TuiDropdownOpen, event: Event | KeyboardEvent): boole

@Directive({
standalone: true,
selector: '[tuiDropdownOpen],[tuiDropdownOpenChange]',
selector: '[tuiDropdown][tuiDropdownOpen],[tuiDropdown][tuiDropdownOpenChange]',
providers: [TuiDropdownDriver, tuiAsDriver(TuiDropdownDriver)],
hostDirectives: [
TuiObscured,
Expand All @@ -61,8 +61,7 @@ export class TuiDropdownOpen implements OnChanges {
@ContentChild('tuiDropdownHost', {descendants: true, read: ElementRef})
private readonly dropdownHost?: ElementRef<HTMLElement>;

// TODO: Remove optional after refactor is complete
private readonly directive = inject(TuiDropdownDirective, {optional: true});
private readonly directive = inject(TuiDropdownDirective);
private readonly el = tuiInjectElement();
private readonly obscured = inject(TuiObscured);

Expand All @@ -72,8 +71,7 @@ export class TuiDropdownOpen implements OnChanges {
fromEvent(this.el, 'focusin').pipe(
map(tuiGetActualTarget),
filter(
(target) =>
!this.host.contains(target) || !this.directive?.dropdownBoxRef,
(target) => !this.host.contains(target) || !this.directive.dropdownBoxRef,
),
),
)
Expand All @@ -93,12 +91,7 @@ export class TuiDropdownOpen implements OnChanges {
public readonly driver = inject(TuiDropdownDriver);

public get dropdown(): HTMLElement | undefined {
return this.directive?.dropdownBoxRef?.location.nativeElement;
}

// TODO: make it private when all legacy controls will be deleted from @taiga-ui/legacy (5.0)
public get focused(): boolean {
return tuiIsNativeFocusedIn(this.host) || tuiIsNativeFocusedIn(this.dropdown);
return this.directive.dropdownBoxRef?.location.nativeElement;
}

public ngOnChanges(): void {
Expand Down Expand Up @@ -168,6 +161,10 @@ export class TuiDropdownOpen implements OnChanges {
return tuiIsElementEditable(this.host);
}

private get focused(): boolean {
return tuiIsNativeFocusedIn(this.host) || tuiIsNativeFocusedIn(this.dropdown);
}

private update(open: boolean): void {
if (open && !this.tuiDropdownEnabled) {
return;
Expand Down
2 changes: 2 additions & 0 deletions projects/core/directives/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {TuiDropdownContext} from './dropdown-context.directive';
import {TuiDropdownHover} from './dropdown-hover.directive';
import {TuiDropdownManual} from './dropdown-manual.directive';
import {TuiDropdownOpen} from './dropdown-open.directive';
import {TuiDropdownOpenLegacy} from './dropdown-open-legacy.directive';
import {TuiDropdownOptionsDirective} from './dropdown-options.directive';
import {TuiDropdownPortal} from './dropdown-portal.directive';
import {TuiDropdownPosition} from './dropdown-position.directive';
Expand All @@ -17,6 +18,7 @@ export const TuiDropdown = [
TuiDropdownDirective,
TuiDropdownComponent,
TuiDropdownOpen,
TuiDropdownOpenLegacy,
TuiDropdownPortal,
TuiDropdownManual,
TuiDropdownHover,
Expand Down
1 change: 1 addition & 0 deletions projects/core/directives/dropdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './dropdown-hover.directive';
export * from './dropdown-hover.options';
export * from './dropdown-manual.directive';
export * from './dropdown-open.directive';
export * from './dropdown-open-legacy.directive';
export * from './dropdown-options.directive';
export * from './dropdown-portal.directive';
export * from './dropdown-position.directive';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
[stringify]="stringify"
[tuiDropdownOpen]="open()"
[tuiTextfieldCleaner]="true"
[tuiTextfieldLabelOutside]="true"
[(ngModel)]="selected"
(tuiDropdownOpenChange)="open.set($event)"
>
Expand Down
3 changes: 2 additions & 1 deletion projects/legacy/components/combo-box/combo-box.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export class TuiComboBoxComponent<T>

public get focused(): boolean {
return (
tuiIsNativeFocused(this.nativeFocusableElement) || !!this.dropdown?.focused
tuiIsNativeFocused(this.nativeFocusableElement) ||
!!this.dropdown?.tuiDropdownOpen
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export class TuiInputPhoneComponent

public get focused(): boolean {
return (
tuiIsNativeFocused(this.nativeFocusableElement) || !!this.dropdown?.focused
tuiIsNativeFocused(this.nativeFocusableElement) ||
!!this.dropdown?.tuiDropdownOpen
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class TuiInputTagComponent
}

public get focused(): boolean {
return tuiIsNativeFocusedIn(this.el) || !!this.dropdown?.focused;
return tuiIsNativeFocusedIn(this.el) || !!this.dropdown?.tuiDropdownOpen;
}

public onTagEdited(value: string, index: number): void {
Expand Down
3 changes: 2 additions & 1 deletion projects/legacy/components/input/input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export class TuiInputComponent

public get focused(): boolean {
return (
tuiIsNativeFocused(this.nativeFocusableElement) || !!this.dropdown?.focused
tuiIsNativeFocused(this.nativeFocusableElement) ||
!!this.dropdown?.tuiDropdownOpen
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class TuiMultiSelectComponent<T>
}

public get focused(): boolean {
return !!this.input?.focused || !!this.dropdown?.focused;
return !!this.input?.focused || !!this.dropdown?.tuiDropdownOpen;
}

public onValueChange(value: readonly T[]): void {
Expand Down
3 changes: 2 additions & 1 deletion projects/legacy/components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export class TuiSelectComponent<T>

public get focused(): boolean {
return (
tuiIsNativeFocused(this.nativeFocusableElement) || !!this.dropdown?.focused
tuiIsNativeFocused(this.nativeFocusableElement) ||
!!this.dropdown?.tuiDropdownOpen
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Directive, inject} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom';
import {tuiGetClosestFocusable} from '@taiga-ui/cdk/utils/focus';
import {TuiDropdownOpen} from '@taiga-ui/core/directives/dropdown';
import {TuiDropdownOpen, TuiDropdownOpenLegacy} from '@taiga-ui/core/directives/dropdown';
import {distinctUntilChanged} from 'rxjs';

@Directive({
Expand All @@ -12,28 +12,26 @@ import {distinctUntilChanged} from 'rxjs';
export class TuiLegacyDropdownOpenMonitorDirective {
private readonly el = tuiInjectElement();
private readonly host = inject(TuiDropdownOpen, {self: true});
private readonly external = inject(TuiDropdownOpen, {
skipSelf: true,
private readonly external = inject(TuiDropdownOpenLegacy, {
optional: true,
});

constructor() {
if (this.external && !this.external['directive']) {
this.host.driver
.pipe(distinctUntilChanged(), takeUntilDestroyed())
.subscribe((open) => this.external?.toggle(open));
this.external.driver
.pipe(distinctUntilChanged(), takeUntilDestroyed())
.subscribe((value) => {
if (value) {
tuiGetClosestFocusable({
initial: this.el,
root: this.el,
})?.focus();
}
this.host.driver
.pipe(distinctUntilChanged(), takeUntilDestroyed())
.subscribe((open) => this.external?.tuiDropdownOpenChange.next(open));

this.host.toggle(value);
});
}
this.external?.tuiDropdownOpenChange
.pipe(distinctUntilChanged(), takeUntilDestroyed())
.subscribe((open) => {
if (open) {
tuiGetClosestFocusable({
initial: this.el,
root: this.el,
})?.focus();
}

this.host.toggle(open);
});
}
}

0 comments on commit fa54099

Please sign in to comment.