Files
Verso/services/web/frontend/js/features/settings/hooks/use-focus-on-setting.tsx
T
Davinder SinghandCopybot f27c99ea4b Tearing down of old Editor (File tree) (#31784)
* merging ide-redesign/components/file-tree into features/file-tree

* moving ide-redesign/contexts/settings-modal-context   to features/settings/contexts

* use-collapsible-file-tree.tsx → features/file-tree/hooks

* use-focus-on-setting.tsx → features/settings/hooks

* use-project-notification-preferences.ts → features/settings/hooks

* use-rail-overflow.tsx→ features/ide-react/hooks

* deleting use-switch-enable-new-editor-state.ts

* use-toolbar-menu-editor-commands.tsx → features/source-editor/hooks

* npm run extract-translations

* modifying the test to target correct buttons and removing a test for old component

* adding a test back and modifying it

* changing the test

GitOrigin-RevId: baa1e9a992c88b84313eea82161354d4958cf1ef
2026-03-06 09:14:01 +00:00

57 lines
1.6 KiB
TypeScript

import { useEditorLeftMenuContext } from '@/features/editor-left-menu/components/editor-left-menu-context'
import { useEffect, useState } from 'react'
import { useSettingsModalContext } from '../context/settings-modal-context'
/**
* A hook to scroll to and focus on a specific setting in the settings modal
*/
export default function useFocusOnSetting() {
const { activeTab, setActiveTab, settingToTabMap } = useSettingsModalContext()
const { settingToFocus } = useEditorLeftMenuContext()
const [eltToScrollTo, setEltToScrollTo] = useState<{
tab: string | undefined
element: HTMLElement | null
} | null>(null)
useEffect(() => {
if (settingToFocus) {
const newActiveTab = settingToTabMap.get(settingToFocus)
const settingElt: HTMLDivElement | null = document.querySelector(
`#setting-${settingToFocus}`
)
const settingToFocusElt: HTMLElement | null =
settingElt?.querySelector('input, select, button') ?? settingElt
setActiveTab(newActiveTab)
setEltToScrollTo({ tab: newActiveTab, element: settingToFocusElt })
}
// clear the focus setting
window.dispatchEvent(
new CustomEvent('ui.focus-setting', { detail: undefined })
)
}, [settingToFocus, activeTab, setActiveTab, settingToTabMap])
// Scroll to the focused setting, once the correct tab is open
useEffect(() => {
if (!eltToScrollTo) {
return
}
const { tab, element } = eltToScrollTo
if (tab === activeTab) {
element?.scrollIntoView({
block: 'center',
behavior: 'smooth',
})
element?.focus()
setEltToScrollTo(null)
}
}, [eltToScrollTo, activeTab])
}