go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/generic_libs/components/routed_tabs/routed_tabs.tsx (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { Tabs, TabsProps } from '@mui/material'; 16 import { useReducer } from 'react'; 17 import { Outlet } from 'react-router-dom'; 18 19 import { 20 ActiveTabContextProvider, 21 ActiveTabUpdaterContextProvider, 22 } from './context'; 23 import { reducer } from './reducer'; 24 25 /** 26 * A tabs implementation based on MUI tabs and react router. 27 * 28 * It's similar to MUI `<Tabs />` except that 29 * * `value` on `<RoutedTabs />` is managed automatically, and 30 * * its children must be `<RoutedTab />` instead of `<Tab />` from MUI, and 31 * * the tab content components must be mounted as child routes and should 32 * define its associated tab ID with `useTabId('tab-id')`, and 33 * * it contains an `<Outlet />`. 34 */ 35 export function RoutedTabs(props: Omit<TabsProps, 'value'>) { 36 const [state, dispatch] = useReducer(reducer, { activeTab: null }); 37 38 return ( 39 <ActiveTabUpdaterContextProvider value={dispatch}> 40 <ActiveTabContextProvider 41 value={{ activeTabId: state.activeTab?.id ?? null }} 42 > 43 <Tabs {...props} value={state.activeTab?.id ?? false} /> 44 <Outlet /> 45 </ActiveTabContextProvider> 46 </ActiveTabUpdaterContextProvider> 47 ); 48 }