mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-07 16:19:02 +02:00
f27c99ea4b
* 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
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { useCallback, useState } from 'react'
|
|
import { RailElement } from '../util/rail-types'
|
|
import { useResizeObserver } from '@/shared/hooks/use-resize-observer'
|
|
|
|
const useRailOverflow = (railTabs: RailElement[]) => {
|
|
const [tabsInRail, setTabsInRail] = useState<RailElement[]>(railTabs)
|
|
const [tabsInOverflow, setTabsInOverflow] = useState<RailElement[]>([])
|
|
|
|
const handleResize = useCallback(
|
|
(element: Element) => {
|
|
const height = (element as HTMLElement).offsetHeight
|
|
|
|
const tabHeight =
|
|
(element.querySelector('.ide-rail-tab-link')?.clientHeight ?? 0) + 4 // 4px gap between tabs
|
|
|
|
const numTabsToFit = Math.floor(height / tabHeight)
|
|
|
|
if (numTabsToFit >= railTabs.length) {
|
|
setTabsInRail(railTabs)
|
|
setTabsInOverflow([])
|
|
} else {
|
|
const sliceIndex = Math.max(numTabsToFit - 1, 0)
|
|
setTabsInRail(railTabs.slice(0, sliceIndex))
|
|
setTabsInOverflow(railTabs.slice(sliceIndex))
|
|
}
|
|
},
|
|
[railTabs]
|
|
)
|
|
|
|
const { elementRef: tabWrapperRef } = useResizeObserver(handleResize)
|
|
|
|
return { tabsInRail, tabsInOverflow, tabWrapperRef }
|
|
}
|
|
|
|
export default useRailOverflow
|