| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 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, settingToFocus } =
- useSettingsModalContext()
- 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
- settingElt?.classList.add('ide-setting-highlighted')
- setTimeout(() => {
- settingElt?.classList.remove('ide-setting-highlighted')
- }, 2000)
- 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])
- }
|