1
1
import { h , createContext } from 'preact' ;
2
2
import { useContext , useEffect } from 'preact/hooks' ;
3
- import { TabsService } from './tabs.service' ;
4
3
import { CustomizerThemesContext } from '../customizer/CustomizerProvider.js' ;
5
4
import { 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
+ */
6
11
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 ) ) ) ;
8
18
9
19
/**
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
+ *
10
24
* @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
13
27
*/
14
28
export function TabsProvider ( { children, service } ) {
15
29
const tabs = useSignal ( service . snapshot ( ) ) ;
@@ -21,6 +35,19 @@ export function TabsProvider({ children, service }) {
21
35
return < TabsStateContext . Provider value = { tabs } > { children } </ TabsStateContext . Provider > ;
22
36
}
23
37
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
+ */
24
51
export function useTabState ( ) {
25
52
const tabs = useContext ( TabsStateContext ) ;
26
53
const current = useComputed ( ( ) => tabs . value . tabId ) ;
0 commit comments