1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import { ListContext } from '../useList';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export function useOptionContextStabilizer(value) {
|
21 | const listContext = React.useContext(ListContext);
|
22 | if (!listContext) {
|
23 | throw new Error('Option: ListContext was not found.');
|
24 | }
|
25 | const {
|
26 | getItemState,
|
27 | dispatch
|
28 | } = listContext;
|
29 | const {
|
30 | highlighted,
|
31 | selected,
|
32 | focusable
|
33 | } = getItemState(value);
|
34 |
|
35 |
|
36 |
|
37 | const localGetItemState = React.useCallback(itemValue => {
|
38 | if (itemValue !== value) {
|
39 | throw new Error(['Base UI Option: Tried to access the state of another Option.', 'This is unsupported when the Option uses the OptionContextStabilizer as a performance optimization.'].join('/n'));
|
40 | }
|
41 | return {
|
42 | highlighted,
|
43 | selected,
|
44 | focusable
|
45 | };
|
46 | }, [highlighted, selected, focusable, value]);
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | const localContextValue = React.useMemo(() => ({
|
52 | dispatch,
|
53 | getItemState: localGetItemState
|
54 | }), [dispatch, localGetItemState]);
|
55 | return {
|
56 | contextValue: localContextValue
|
57 | };
|
58 | } |
\ | No newline at end of file |