Skip to content

Commit 310385a

Browse files
committed
some more docs
1 parent c232d79 commit 310385a

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

special-pages/pages/new-tab/app/tabs/TabsProvider.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import { h, createContext } from 'preact';
22
import { useContext, useEffect } from 'preact/hooks';
3-
import { TabsService } from './tabs.service';
43
import { CustomizerThemesContext } from '../customizer/CustomizerProvider.js';
54
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+
*/
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
*/
1428
export 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+
*/
2451
export function useTabState() {
2552
const tabs = useContext(TabsStateContext);
2653
const current = useComputed(() => tabs.value.tabId);

0 commit comments

Comments
 (0)