From 802ef11493bcfc554958b1472c7d493561990ac3 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Wed, 14 Feb 2024 16:58:28 +1100 Subject: [PATCH] fix: speedup resolution for auto-guards --- src/Trap.js | 6 +++- stories/Group.js | 78 +++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/Trap.js b/src/Trap.js index f330667..d48858f 100644 --- a/src/Trap.js +++ b/src/Trap.js @@ -130,7 +130,11 @@ const activateTrap = () => { } } - if (document) { + if (document + // element was changed + && activeElement !== document.activeElement + // fast check for any auto-guard + && document.querySelector('[data-focus-auto-guard]')) { const newActiveElement = document && document.activeElement; const allNodes = expandFocusableNodes(workingArea); const focusedIndex = allNodes.map(({ node }) => node).indexOf(newActiveElement); diff --git a/stories/Group.js b/stories/Group.js index c97c153..7e762b7 100644 --- a/stories/Group.js +++ b/stories/Group.js @@ -1,84 +1,94 @@ -import * as React from "react"; -import { Component } from "react"; -import FocusLock, {InFocusGuard} from "../src/index"; +import * as React from 'react'; +import { Component } from 'react'; +import FocusLock, { InFocusGuard } from '../src/index'; const styles = { - fontFamily: "sans-serif", - textAlign: "center", - fontSize: "16px" + fontFamily: 'sans-serif', + textAlign: 'center', + fontSize: '16px', }; const bg = { - backgroundColor: '#FEE' + backgroundColor: '#FEE', }; class Trap1 extends Component { state = { - disabled: true + disabled: true, }; toggle = () => { setTimeout(() => { - this.setState({disabled: !this.state.disabled}); + this.setState({ disabled: !this.state.disabled }); }, 10); }; render() { - const {disabled} = this.state; + const { disabled } = this.state; return (
-
+
- {!disabled && + {!disabled && ( + - link somethere
+ link somethere + {' '} +
+ ) } some text outsite -
+
some text outsite - {!disabled && + {!disabled && ( + - link somethere
+ link somethere + {' '} +
+ ) }
- ) + ); } } export class ShardGroupCase extends React.Component { ref1 = React.createRef(); + ref2 = React.createRef(); + state = { - disabled: true + disabled: true, }; toggle = () => { setTimeout(() => { - this.setState({disabled: !this.state.disabled}); + this.setState({ disabled: !this.state.disabled }); }, 10); }; render() { - const {disabled} = this.state; + const { disabled } = this.state; return (
-
- +
+
- - - + {/* */} + {/* */} + {/* */} @@ -86,26 +96,32 @@ export class ShardGroupCase extends React.Component { - {!disabled && + {!disabled && ( + - link somethere
+ link somethere + {' '} +
+ ) } some text outsite -
+
some text outsite
- link somethere
+ link somethere + {' '} +
- ) + ); } } -export const GroupCase = () =>
; \ No newline at end of file +export const GroupCase = () =>
;