diff --git a/projects/core/src/lib/mask.ts b/projects/core/src/lib/mask.ts index 5c802b5d2..a728fa37f 100644 --- a/projects/core/src/lib/mask.ts +++ b/projects/core/src/lib/mask.ts @@ -156,8 +156,13 @@ export class Maskito extends MaskHistory { } private updateSelectionRange([from, to]: SelectionRange): void { - if (this.element.selectionStart !== from || this.element.selectionEnd !== to) { - this.element.setSelectionRange?.(from, to); + const {element} = this; + + if ( + element.matches(':focus') && + (element.selectionStart !== from || element.selectionEnd !== to) + ) { + element.setSelectionRange?.(from, to); } } diff --git a/projects/core/src/lib/utils/dom/update-element.ts b/projects/core/src/lib/utils/dom/update-element.ts index 9e9062bed..e21cd8058 100644 --- a/projects/core/src/lib/utils/dom/update-element.ts +++ b/projects/core/src/lib/utils/dom/update-element.ts @@ -22,7 +22,10 @@ export function maskitoUpdateElement( const [from, to] = valueOrElementState.selection; element.value = valueOrElementState.value; - element.setSelectionRange?.(from, to); + + if (element.matches(':focus')) { + element.setSelectionRange?.(from, to); + } } element.dispatchEvent( diff --git a/projects/kit/src/lib/plugins/caret-guard.ts b/projects/kit/src/lib/plugins/caret-guard.ts index 17077a786..5201d4673 100644 --- a/projects/kit/src/lib/plugins/caret-guard.ts +++ b/projects/kit/src/lib/plugins/caret-guard.ts @@ -1,6 +1,6 @@ import {MaskitoPlugin} from '@maskito/core'; -import {clamp, getFocused} from '../utils'; +import {clamp} from '../utils'; export function maskitoCaretGuard( guard: ( @@ -17,7 +17,7 @@ export function maskitoCaretGuard( }; const listener = (): void => { - if (getFocused(document) !== element) { + if (!element.matches(':focus')) { return; } diff --git a/projects/kit/src/lib/utils/get-focused.ts b/projects/kit/src/lib/utils/get-focused.ts deleted file mode 100644 index 7f4afee20..000000000 --- a/projects/kit/src/lib/utils/get-focused.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Returns current active element, including shadow dom - * - * @return element or null - */ -export function getFocused({activeElement}: Document): Element | null { - if (!activeElement?.shadowRoot) { - return activeElement; - } - - let element = activeElement.shadowRoot.activeElement; - - while (element?.shadowRoot) { - element = element.shadowRoot.activeElement; - } - - return element; -} diff --git a/projects/kit/src/lib/utils/index.ts b/projects/kit/src/lib/utils/index.ts index 23a02ae52..4150e3ada 100644 --- a/projects/kit/src/lib/utils/index.ts +++ b/projects/kit/src/lib/utils/index.ts @@ -11,7 +11,6 @@ export * from './date/validate-date-string'; export * from './escape-reg-exp'; export * from './extract-affixes'; export * from './find-common-beginning-substr'; -export * from './get-focused'; export * from './identity'; export * from './is-empty'; export * from './pad-with-zeroes-until-valid';