@@ -12,6 +12,17 @@ function needsCustomInput(option: SelectorOption): boolean {
1212 return option . customInput === true || isOtherOption ( option . id ) ;
1313}
1414
15+ function isUserInInteractiveElement ( ) : boolean {
16+ const el = document . activeElement ;
17+ return (
18+ el instanceof HTMLElement &&
19+ ( el . tagName === "INPUT" ||
20+ el . tagName === "TEXTAREA" ||
21+ el . tagName === "SELECT" ||
22+ el . getAttribute ( "contenteditable" ) === "true" )
23+ ) ;
24+ }
25+
1526interface UseActionSelectorStateProps {
1627 options : SelectorOption [ ] ;
1728 multiSelect : boolean ;
@@ -76,7 +87,9 @@ export function useActionSelectorState({
7687 isEditing && selectedOption && needsCustomInput ( selectedOption ) ;
7788
7889 useEffect ( ( ) => {
79- containerRef . current ?. focus ( ) ;
90+ if ( ! isUserInInteractiveElement ( ) ) {
91+ containerRef . current ?. focus ( ) ;
92+ }
8093 } , [ ] ) ;
8194
8295 useEffect ( ( ) => {
@@ -107,7 +120,7 @@ export function useActionSelectorState({
107120 } , [ activeStep , checkedOptions , customInput , onStepAnswer ] ) ;
108121
109122 const restoreStepAnswer = useCallback (
110- ( step : number ) => {
123+ ( step : number , { autoFocus = true } : { autoFocus ?: boolean } = { } ) => {
111124 const saved = stepAnswers . get ( step ) ;
112125 if ( saved ) {
113126 setCheckedOptions ( new Set ( saved . selectedIds ) ) ;
@@ -121,13 +134,17 @@ export function useActionSelectorState({
121134 }
122135 setSelectedIndex ( 0 ) ;
123136 setIsEditing ( false ) ;
124- containerRef . current ?. focus ( ) ;
137+ if ( autoFocus ) {
138+ containerRef . current ?. focus ( ) ;
139+ }
125140 } ,
126141 [ initialSelections , stepAnswers ] ,
127142 ) ;
128143
129144 useEffect ( ( ) => {
130- restoreStepAnswer ( activeStep ) ;
145+ restoreStepAnswer ( activeStep , {
146+ autoFocus : ! isUserInInteractiveElement ( ) ,
147+ } ) ;
131148 } , [ activeStep , restoreStepAnswer ] ) ;
132149
133150 useEffect ( ( ) => {
0 commit comments