import React, {forwardRef, useEffect, useState} from 'react'; import debounce from 'lodash/debounce'; import type {ITab, TabsProps} from '../../typings/hyper'; import {decorate, getTabProps} from '../utils/plugins'; import DropdownButton from './new-tab'; import Tab_ from './tab'; const Tab = decorate(Tab_, 'Tab'); const isMac = /Mac/.test(navigator.userAgent); const Tabs = forwardRef((props, ref) => { const {tabs = [], borderColor, onChange, onClose, fullScreen} = props; const [shouldFocusCounter, setShouldFocusCounter] = useState({ index: 0, when: undefined as Date | undefined }); const scrollToActiveTab = debounce((currTabs: ITab[]) => { const activeTab = currTabs.findIndex((t) => t.isActive); setShouldFocusCounter({ index: activeTab, when: new Date() }); }, 100); useEffect(() => { scrollToActiveTab(tabs); }, [tabs, tabs.length]); const hide = !isMac && tabs.length === 1; const handleWheel = (event: React.WheelEvent) => { // A plain vertical mouse wheel only ever reports deltaY. macOS trackpads // generate real deltaX for horizontal swipes, but a wheel mouse (common // on Linux/Windows) doesn't, so without this the tab list never scrolls. if (event.deltaX === 0 && event.deltaY !== 0) { event.currentTarget.scrollLeft += event.deltaY; } }; return ( ); }); Tabs.displayName = 'Tabs'; export default Tabs;