Skip to content

Commit

Permalink
refactor!: replace setNativeFocused(el) with el.focus()
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Aug 4, 2022
1 parent 7141e4b commit f93d4ab
Show file tree
Hide file tree
Showing 30 changed files with 47 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Directive, ElementRef, HostListener, Inject} from '@angular/core';
import {
setNativeFocused,
tuiClamp,
tuiGetClosestFocusable,
tuiIsNativeFocusedIn,
Expand Down Expand Up @@ -39,7 +38,7 @@ export class TuiToolbarNavigationManagerDirective {
: this.findNextTool(targetToolWrapper);

if (targetTool) {
setNativeFocused(targetTool);
targetTool.focus();
}
}

Expand Down
5 changes: 2 additions & 3 deletions projects/addon-editor/components/toolbar/toolbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '@taiga-ui/addon-editor/tokens';
import {
EMPTY_QUERY,
setNativeFocused,
tuiDefaultProp,
TuiHandler,
tuiIsNativeFocusedIn,
Expand Down Expand Up @@ -265,15 +264,15 @@ export class TuiToolbarComponent {
const firstButton = this.navigationManager?.findFirstFocusableTool();

if (firstButton) {
setNativeFocused(firstButton);
firstButton.focus();
}
}

private focusLast(): void {
const lastButton = this.navigationManager?.findFirstFocusableTool(true);

if (lastButton) {
setNativeFocused(lastButton);
lastButton.focus();
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions projects/cdk/directives/focus-trap/focus-trap.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@angular/core';
import {tuiContainsOrAfter} from '@taiga-ui/cdk/utils/dom';
import {
setNativeFocused,
tuiBlurNativeFocused,
tuiGetClosestFocusable,
tuiGetNativeFocused,
Expand Down Expand Up @@ -37,7 +36,7 @@ export class TuiFocusTrapDirective implements OnDestroy {
*/
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Promise.resolve().then(() => {
setNativeFocused(this.elementRef.nativeElement);
this.elementRef.nativeElement.focus();
});
}

Expand All @@ -59,7 +58,7 @@ export class TuiFocusTrapDirective implements OnDestroy {
);

if (focusable) {
setNativeFocused(focusable);
focusable.focus();
}
}

Expand All @@ -76,7 +75,7 @@ export class TuiFocusTrapDirective implements OnDestroy {
Promise.resolve().then(() => {
// TODO: iframe warning
if (this.activeElement instanceof HTMLElement) {
setNativeFocused(this.activeElement);
this.activeElement.focus();
}
});
}
Expand Down
1 change: 0 additions & 1 deletion projects/cdk/utils/focus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export * from './is-native-focused-in';
export * from './is-native-keyboard-focusable';
export * from './is-native-mouse-focusable';
export * from './move-focus';
export * from './set-native-focused';
export * from './set-native-mouse-focused';
3 changes: 1 addition & 2 deletions projects/cdk/utils/focus/move-focus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {tuiIsNativeFocused} from './is-native-focused';
import {setNativeFocused} from './set-native-focused';

/**
* Utility method for moving focus in a list of elements
Expand All @@ -17,7 +16,7 @@ export function tuiMoveFocus(
currentIndex += step;

while (currentIndex >= 0 && currentIndex < elements.length) {
setNativeFocused(elements[currentIndex]);
elements[currentIndex].focus();

if (tuiIsNativeFocused(elements[currentIndex])) {
return;
Expand Down
21 changes: 0 additions & 21 deletions projects/cdk/utils/focus/set-native-focused.ts

This file was deleted.

8 changes: 5 additions & 3 deletions projects/cdk/utils/focus/set-native-mouse-focused.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {setNativeFocused} from './set-native-focused';

/**
* Focuses or blurs element with mouse action imitation (to spoof {@link TuiFocusVisibleService})
*
Expand All @@ -25,5 +23,9 @@ export function tuiSetNativeMouseFocused(
element.dispatchEvent(event);
}

setNativeFocused(element, focused, preventScroll);
if (focused) {
element.focus({preventScroll});
} else {
element.blur();
}
}
3 changes: 1 addition & 2 deletions projects/cdk/utils/focus/tests/blur-native-focused.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {tuiBlurNativeFocused} from '../blur-native-focused';
import {setNativeFocused} from '../set-native-focused';

describe(`blurNativeFocused`, () => {
let element: HTMLInputElement;

beforeEach(() => {
element = document.createElement(`input`);
document.body.appendChild(element);
setNativeFocused(element, true);
element.focus();
});

afterAll(() => {
Expand Down
48 changes: 0 additions & 48 deletions projects/cdk/utils/focus/tests/set-native-focused.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@angular/core';
import {
EMPTY_QUERY,
setNativeFocused,
tuiGetClosestFocusable,
tuiItemsQueryListObservable,
tuiPreventDefault,
Expand Down Expand Up @@ -73,7 +72,7 @@ export class TuiDataListDropdownManagerDirective implements AfterViewInit {
event.stopPropagation();
}

setNativeFocused(element.nativeElement);
element.nativeElement.focus();
// TODO: iframe warning
dropdown.open = event instanceof MouseEvent;
}),
Expand Down Expand Up @@ -168,7 +167,7 @@ export class TuiDataListDropdownManagerDirective implements AfterViewInit {
);

if (item) {
setNativeFocused(item);
item.focus();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
TemplateRef,
} from '@angular/core';
import {
setNativeFocused,
TuiContextWithImplicit,
tuiDefaultProp,
TuiEventWith,
Expand Down Expand Up @@ -101,7 +100,7 @@ export class TuiOptionComponent<T = unknown> implements OnDestroy {
@HostListener(`mousemove.init`, [`$event`])
@HostListener(`mousemove.silent`, [`$event`])
onMouseMove({currentTarget}: TuiEventWith<MouseEvent, HTMLElement>): void {
setNativeFocused(currentTarget, true, true);
currentTarget.focus({preventScroll: true});
}

// Preventing focus loss upon focused option removal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
ViewChild,
} from '@angular/core';
import {
setNativeFocused,
TUI_FOCUSABLE_ITEM_ACCESSOR,
TuiActiveZoneDirective,
TuiContextWithImplicit,
Expand Down Expand Up @@ -233,7 +232,7 @@ export class TuiHostedDropdownComponent implements TuiFocusableElementAccessor {
return;
}

setNativeFocused(focusable);
focusable.focus();
event.preventDefault();
}

Expand All @@ -249,7 +248,7 @@ export class TuiHostedDropdownComponent implements TuiFocusableElementAccessor {
const host = this.nativeFocusableElement;

if (host !== null) {
setNativeFocused(host, true, true);
host.focus({preventScroll: true});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component, Input} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {EMPTY_ARRAY, setNativeFocused, TUI_DEFAULT_MATCHER} from '@taiga-ui/cdk';
import {EMPTY_ARRAY, TUI_DEFAULT_MATCHER} from '@taiga-ui/cdk';
import {TuiDataListComponent, tuiIsEditingKey} from '@taiga-ui/core';

interface Items<T> {
Expand Down Expand Up @@ -29,7 +29,7 @@ export class CustomListComponent<T> {

onKeyDown(key: string, element: HTMLElement | null): void {
if (element && tuiIsEditingKey(key)) {
setNativeFocused(element, true, true);
element.focus({preventScroll: true});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {setNativeFocused} from '@taiga-ui/cdk';
import {TuiInputComponent} from '@taiga-ui/kit';

@Component({
Expand All @@ -28,7 +27,7 @@ export class TuiActiveZoneExample1 {

onClick({nativeFocusableElement}: TuiInputComponent): void {
if (nativeFocusableElement) {
setNativeFocused(nativeFocusableElement);
nativeFocusableElement.focus();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Component, ElementRef, QueryList, ViewChildren} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {assets} from '@demo/utils';
import {EMPTY_QUERY, setNativeFocused, TuiBooleanHandler, tuiPure} from '@taiga-ui/cdk';
import {EMPTY_QUERY, TuiBooleanHandler, tuiPure} from '@taiga-ui/cdk';
import {TuiOptionComponent} from '@taiga-ui/core';
import {tuiGetWordRange} from '@taiga-ui/kit';

Expand Down Expand Up @@ -53,7 +53,7 @@ export class TuiDropdownSelectionExample2 {
}

event.preventDefault();
setNativeFocused(item.nativeElement);
item.nativeElement.focus();
}

filterItems(textarea: HTMLTextAreaElement): readonly User[] {
Expand All @@ -68,7 +68,7 @@ export class TuiDropdownSelectionExample2 {
const caret = value.indexOf(login) + login.length;

this.value = value;
setNativeFocused(textarea);
textarea.focus();
textarea.value = value;
textarea.setSelectionRange(caret, caret);
}
Expand Down
3 changes: 1 addition & 2 deletions projects/kit/components/combo-box/combo-box.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import {NgControl} from '@angular/forms';
import {
AbstractTuiNullableControl,
setNativeFocused,
TUI_STRICT_MATCHER,
TuiActiveZoneDirective,
TuiContextWithImplicit,
Expand Down Expand Up @@ -230,7 +229,7 @@ export class TuiComboBoxComponent<T>

private focusInput(preventScroll: boolean = false): void {
if (this.nativeFocusableElement) {
setNativeFocused(this.nativeFocusableElement, true, preventScroll);
this.nativeFocusableElement.focus({preventScroll});
}
}
}
3 changes: 1 addition & 2 deletions projects/kit/components/input-count/input-count.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import {NgControl} from '@angular/forms';
import {
AbstractTuiControl,
setNativeFocused,
TUI_FOCUSABLE_ITEM_ACCESSOR,
TUI_IS_MOBILE,
tuiClamp,
Expand Down Expand Up @@ -175,7 +174,7 @@ export class TuiInputCountComponent
}

event.preventDefault();
setNativeFocused(this.nativeFocusableElement);
this.nativeFocusableElement.focus();
}

onFocused(focused: boolean): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
DATE_FILLER_LENGTH,
DATE_RANGE_FILLER_LENGTH,
RANGE_SEPARATOR_CHAR,
setNativeFocused,
TUI_DATE_FORMAT,
TUI_DATE_SEPARATOR,
TUI_FIRST_DAY,
Expand Down Expand Up @@ -372,7 +371,7 @@ export class TuiInputDateRangeComponent

private focusInput(preventScroll: boolean = false): void {
if (this.nativeFocusableElement) {
setNativeFocused(this.nativeFocusableElement, true, preventScroll);
this.nativeFocusableElement.focus({preventScroll});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {NgControl} from '@angular/forms';
import {
AbstractTuiControl,
CHAR_PLUS,
setNativeFocused,
TUI_FOCUSABLE_ITEM_ACCESSOR,
TuiContextWithImplicit,
tuiDefaultProp,
Expand Down Expand Up @@ -190,7 +189,7 @@ export class TuiInputPhoneInternationalComponent
}

if (this.nativeFocusableElement) {
setNativeFocused(this.nativeFocusableElement);
this.nativeFocusableElement.focus();
}
}

Expand Down
Loading

0 comments on commit f93d4ab

Please sign in to comment.