11import { ChevronRightIcon } from "lucide-react" ;
2- import { createContext , useContext , type ReactNode } from "react" ;
2+ import { createContext , useContext , useLayoutEffect , useState , type ReactNode } from "react" ;
33import { createPortal } from "react-dom" ;
44
55/**
@@ -10,6 +10,7 @@ import { createPortal } from "react-dom";
1010export interface PanelTabSlots {
1111 readonly breadcrumb : HTMLElement | null ;
1212 readonly actions : HTMLElement | null ;
13+ readonly registerActions : ( ) => ( ) => void ;
1314}
1415
1516export const PanelTabSlotsContext = createContext < PanelTabSlots | null > ( null ) ;
@@ -22,7 +23,25 @@ export function useInPanelTabBar(): boolean {
2223 return useContext ( PanelTabSlotsContext ) !== null ;
2324}
2425
25- function PanelTabPortal ( { slot, children } : { slot : keyof PanelTabSlots ; children : ReactNode } ) {
26+ export function usePanelTabActionsHost ( ) : {
27+ hasActions : boolean ;
28+ registerActions : ( ) => ( ) => void ;
29+ } {
30+ const [ count , setCount ] = useState ( 0 ) ;
31+ const registerActions = ( ) => {
32+ setCount ( ( current ) => current + 1 ) ;
33+ return ( ) => setCount ( ( current ) => current - 1 ) ;
34+ } ;
35+ return { hasActions : count > 0 , registerActions } ;
36+ }
37+
38+ function PanelTabPortal ( {
39+ slot,
40+ children,
41+ } : {
42+ slot : "breadcrumb" | "actions" ;
43+ children : ReactNode ;
44+ } ) {
2645 const slots = useContext ( PanelTabSlotsContext ) ;
2746 if ( ! slots ) return children ;
2847 const element = slots [ slot ] ;
@@ -49,5 +68,8 @@ export function PanelTabBreadcrumb({ children }: { children: ReactNode }) {
4968 * bar they render in place.
5069 */
5170export function PanelTabActions ( { children } : { children : ReactNode } ) {
71+ const registerActions = useContext ( PanelTabSlotsContext ) ?. registerActions ;
72+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Tell the tab bar before paint, so its actions notch never draws empty.
73+ useLayoutEffect ( ( ) => registerActions ?.( ) , [ registerActions ] ) ;
5274 return < PanelTabPortal slot = "actions" > { children } </ PanelTabPortal > ;
5375}
0 commit comments