11import { h , createContext } from 'preact' ;
22import { useContext , useEffect } from 'preact/hooks' ;
3- import { TabsService } from './tabs.service' ;
43import { CustomizerThemesContext } from '../customizer/CustomizerProvider.js' ;
54import { signal , useComputed , useSignal } from '@preact/signals' ;
5+ import { TabsService } from './tabs.service' ;
6+
7+ /**
8+ * @template T
9+ * @typedef {import('@preact/signals').ReadonlySignal<T> } ReadonlySignal<T>
10+ */
611
7- const TabsStateContext = createContext ( signal ( /** @type {import('../../types/new-tab').Tabs } */ ( TabsService . DEFAULT ) ) ) ;
12+ /**
13+ * @typedef {import("preact").ComponentChild } ComponentChild
14+ * @typedef {import('../../types/new-tab').Tabs } Tabs
15+ */
16+
17+ const TabsStateContext = createContext ( signal ( /** @type {Tabs } */ ( TabsService . DEFAULT ) ) ) ;
818
919/**
20+ * Global state provider for tab information.
21+ *
22+ * This exposes a signal to the Tabs object. use the hook below to access the individual fields.
23+ *
1024 * @param {object } props
11- * @param {import("preact"). ComponentChild } props.children
12- * @param {import('./tabs.service'). TabsService } props.service
25+ * @param {ComponentChild } props.children
26+ * @param {TabsService } props.service
1327 */
1428export function TabsProvider ( { children, service } ) {
1529 const tabs = useSignal ( service . snapshot ( ) ) ;
@@ -21,6 +35,19 @@ export function TabsProvider({ children, service }) {
2135 return < TabsStateContext . Provider value = { tabs } > { children } </ TabsStateContext . Provider > ;
2236}
2337
38+ /**
39+ * Exposes 2 signals - one for the current tab ID and one for the list of tabIds.
40+ *
41+ * In a component, if you want to trigger a re-render based on the current tab, you can
42+ * access the .value field directly.
43+ *
44+ * ```js
45+ * const { current } = useTabState();
46+ * return <MyComponent key={current.value} />
47+ * ```
48+ *
49+ * @returns {{current: ReadonlySignal<string>, all: ReadonlySignal<string[]>} }
50+ */
2451export function useTabState ( ) {
2552 const tabs = useContext ( TabsStateContext ) ;
2653 const current = useComputed ( ( ) => tabs . value . tabId ) ;
0 commit comments