@@ -56,48 +56,6 @@ export function getFocusNodeList(node: HTMLElement, includePositive = false) {
5656 return res ;
5757}
5858
59- let lastFocusElement = null ;
60-
61- /** @deprecated Do not use since this may failed when used in async */
62- export function saveLastFocusNode ( ) {
63- lastFocusElement = document . activeElement ;
64- }
65-
66- /** @deprecated Do not use since this may failed when used in async */
67- export function clearLastFocusNode ( ) {
68- lastFocusElement = null ;
69- }
70-
71- /** @deprecated Do not use since this may failed when used in async */
72- export function backLastFocusNode ( ) {
73- if ( lastFocusElement ) {
74- try {
75- // 元素可能已经被移动了
76- lastFocusElement . focus ( ) ;
77-
78- /* eslint-disable no-empty */
79- } catch ( e ) {
80- // empty
81- }
82- /* eslint-enable no-empty */
83- }
84- }
85-
86- export function limitTabRange ( node : HTMLElement , e : KeyboardEvent ) {
87- if ( e . keyCode === 9 ) {
88- const tabNodeList = getFocusNodeList ( node ) ;
89- const lastTabNode = tabNodeList [ e . shiftKey ? 0 : tabNodeList . length - 1 ] ;
90- const leavingTab =
91- lastTabNode === document . activeElement || node === document . activeElement ;
92-
93- if ( leavingTab ) {
94- const target = tabNodeList [ e . shiftKey ? tabNodeList . length - 1 : 0 ] ;
95- target . focus ( ) ;
96- e . preventDefault ( ) ;
97- }
98- }
99- }
100-
10159export interface InputFocusOptions extends FocusOptions {
10260 cursor ?: 'start' | 'end' | 'all' ;
10361}
@@ -137,3 +95,34 @@ export function triggerFocus(
13795 }
13896 }
13997}
98+
99+ // ======================================================
100+ // == Lock Focus ==
101+ // ======================================================
102+ let focusElements : HTMLElement [ ] = [ ] ;
103+
104+ function onWindowFocus ( e : FocusEvent ) {
105+ const lastElement = focusElements [ focusElements . length - 1 ] ;
106+
107+ console . log ( 'lock focus' , e . target , lastElement ) ;
108+ }
109+
110+ /**
111+ * Lock focus in the element.
112+ * It will force back to the first focusable element when focus leaves the element.
113+ */
114+ export function lockFocus ( element : HTMLElement ) : VoidFunction {
115+ // Refresh focus elements
116+ focusElements = focusElements . filter ( ele => ele !== element ) ;
117+ focusElements . push ( element ) ;
118+
119+ // Just add event since it will de-duplicate
120+ window . addEventListener ( 'focusin' , onWindowFocus , true ) ;
121+
122+ return ( ) => {
123+ focusElements = focusElements . filter ( ele => ele !== element ) ;
124+ if ( focusElements . length === 0 ) {
125+ window . removeEventListener ( 'focusin' , onWindowFocus , true ) ;
126+ }
127+ } ;
128+ }
0 commit comments