|
1 | 1 | 'use client'; |
2 | 2 | import * as React from 'react'; |
3 | | -import { useMergedRefs } from '@base-ui-components/utils/useMergedRefs'; |
| 3 | +import { useStableCallback } from '@base-ui-components/utils/useStableCallback'; |
4 | 4 | import { useControlled } from '@base-ui-components/utils/useControlled'; |
5 | | -import { FloatingEvents, useFloatingTree } from '../../floating-ui-react'; |
| 5 | +import { useFloatingTree } from '../../floating-ui-react'; |
6 | 6 | import { MenuCheckboxItemContext } from './MenuCheckboxItemContext'; |
7 | 7 | import { REGULAR_ITEM, useMenuItem } from '../item/useMenuItem'; |
8 | 8 | import { useCompositeListItem } from '../../composite/list/useCompositeListItem'; |
9 | 9 | import { useMenuRootContext } from '../root/MenuRootContext'; |
10 | 10 | import { useRenderElement } from '../../utils/useRenderElement'; |
11 | 11 | import { useBaseUiId } from '../../utils/useBaseUiId'; |
12 | | -import type { BaseUIComponentProps, HTMLProps, NonNativeButtonProps } from '../../utils/types'; |
| 12 | +import type { BaseUIComponentProps, NonNativeButtonProps } from '../../utils/types'; |
13 | 13 | import { itemMapping } from '../utils/stateAttributesMapping'; |
14 | 14 | import { useMenuPositionerContext } from '../positioner/MenuPositionerContext'; |
15 | 15 | import { createChangeEventDetails } from '../../utils/createBaseUIEventDetails'; |
16 | 16 | import type { MenuRoot } from '../root/MenuRoot'; |
17 | 17 |
|
18 | | -const InnerMenuCheckboxItem = React.memo( |
19 | | - React.forwardRef(function InnerMenuCheckboxItem( |
20 | | - componentProps: InnerMenuCheckboxItemProps, |
21 | | - forwardedRef: React.ForwardedRef<Element>, |
22 | | - ) { |
23 | | - const { |
24 | | - checked: checkedProp, |
25 | | - defaultChecked, |
26 | | - onCheckedChange, |
27 | | - className, |
28 | | - closeOnClick, |
29 | | - disabled = false, |
30 | | - highlighted, |
31 | | - id, |
32 | | - menuEvents, |
33 | | - itemProps, |
34 | | - render, |
35 | | - allowMouseUpTriggerRef, |
36 | | - typingRef, |
37 | | - nativeButton, |
38 | | - nodeId, |
39 | | - ...elementProps |
40 | | - } = componentProps; |
41 | | - |
42 | | - const [checked, setChecked] = useControlled({ |
43 | | - controlled: checkedProp, |
44 | | - default: defaultChecked ?? false, |
45 | | - name: 'MenuCheckboxItem', |
46 | | - state: 'checked', |
47 | | - }); |
48 | | - |
49 | | - const { getItemProps, itemRef } = useMenuItem({ |
50 | | - closeOnClick, |
51 | | - disabled, |
52 | | - highlighted, |
53 | | - id, |
54 | | - menuEvents, |
55 | | - allowMouseUpTriggerRef, |
56 | | - typingRef, |
57 | | - nativeButton, |
58 | | - nodeId, |
59 | | - itemMetadata: REGULAR_ITEM, |
60 | | - }); |
61 | | - |
62 | | - const state: MenuCheckboxItem.State = React.useMemo( |
63 | | - () => ({ |
64 | | - disabled, |
65 | | - highlighted, |
66 | | - checked, |
67 | | - }), |
68 | | - [disabled, highlighted, checked], |
69 | | - ); |
70 | | - |
71 | | - const element = useRenderElement('div', componentProps, { |
72 | | - state, |
73 | | - stateAttributesMapping: itemMapping, |
74 | | - props: [ |
75 | | - itemProps, |
76 | | - { |
77 | | - role: 'menuitemcheckbox', |
78 | | - 'aria-checked': checked, |
79 | | - onClick(event: React.MouseEvent) { |
80 | | - const details = createChangeEventDetails('item-press', event.nativeEvent); |
81 | | - |
82 | | - onCheckedChange?.(!checked, details); |
83 | | - |
84 | | - if (details.isCanceled) { |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - setChecked((currentlyChecked) => !currentlyChecked); |
89 | | - }, |
90 | | - }, |
91 | | - elementProps, |
92 | | - getItemProps, |
93 | | - ], |
94 | | - ref: [itemRef, forwardedRef], |
95 | | - }); |
96 | | - |
97 | | - return ( |
98 | | - <MenuCheckboxItemContext.Provider value={state}>{element}</MenuCheckboxItemContext.Provider> |
99 | | - ); |
100 | | - }), |
101 | | -); |
102 | | - |
103 | 18 | /** |
104 | 19 | * A menu item that toggles a setting on or off. |
105 | 20 | * Renders a `<div>` element. |
106 | 21 | * |
107 | 22 | * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu) |
108 | 23 | */ |
109 | 24 | export const MenuCheckboxItem = React.forwardRef(function MenuCheckboxItem( |
110 | | - props: MenuCheckboxItem.Props, |
| 25 | + componentProps: MenuCheckboxItem.Props, |
111 | 26 | forwardedRef: React.ForwardedRef<Element>, |
112 | 27 | ) { |
113 | | - const { id: idProp, label, closeOnClick = false, nativeButton = false, ...other } = props; |
| 28 | + const { |
| 29 | + render, |
| 30 | + className, |
| 31 | + id: idProp, |
| 32 | + label, |
| 33 | + nativeButton = false, |
| 34 | + disabled = false, |
| 35 | + closeOnClick = false, |
| 36 | + checked: checkedProp, |
| 37 | + defaultChecked, |
| 38 | + onCheckedChange, |
| 39 | + ...elementProps |
| 40 | + } = componentProps; |
114 | 41 |
|
115 | | - const itemRef = React.useRef<HTMLElement>(null); |
116 | 42 | const listItem = useCompositeListItem({ label }); |
117 | | - const mergedRef = useMergedRefs(forwardedRef, listItem.ref, itemRef); |
118 | | - |
119 | | - const { itemProps, activeIndex, allowMouseUpTriggerRef, typingRef } = useMenuRootContext(); |
120 | 43 | const menuPositionerContext = useMenuPositionerContext(true); |
121 | | - |
122 | 44 | const id = useBaseUiId(idProp); |
123 | | - |
124 | | - const highlighted = listItem.index === activeIndex; |
125 | 45 | const { events: menuEvents } = useFloatingTree()!; |
126 | 46 |
|
127 | | - // This wrapper component is used as a performance optimization. |
128 | | - // MenuCheckboxItem reads the context and re-renders the actual MenuCheckboxItem |
129 | | - // only when it needs to. |
| 47 | + const { store } = useMenuRootContext(); |
| 48 | + const highlighted = store.useState('isActive', listItem.index); |
| 49 | + const itemProps = store.useState('itemProps'); |
| 50 | + |
| 51 | + const [checked, setChecked] = useControlled({ |
| 52 | + controlled: checkedProp, |
| 53 | + default: defaultChecked ?? false, |
| 54 | + name: 'MenuCheckboxItem', |
| 55 | + state: 'checked', |
| 56 | + }); |
| 57 | + |
| 58 | + const { getItemProps, itemRef } = useMenuItem({ |
| 59 | + closeOnClick, |
| 60 | + disabled, |
| 61 | + highlighted, |
| 62 | + id, |
| 63 | + menuEvents, |
| 64 | + store, |
| 65 | + nativeButton, |
| 66 | + nodeId: menuPositionerContext?.floatingContext.nodeId, |
| 67 | + itemMetadata: REGULAR_ITEM, |
| 68 | + }); |
| 69 | + |
| 70 | + const state: MenuCheckboxItem.State = React.useMemo( |
| 71 | + () => ({ |
| 72 | + disabled, |
| 73 | + highlighted, |
| 74 | + checked, |
| 75 | + }), |
| 76 | + [disabled, highlighted, checked], |
| 77 | + ); |
| 78 | + |
| 79 | + const handleClick = useStableCallback((event: React.MouseEvent) => { |
| 80 | + const details = createChangeEventDetails('item-press', event.nativeEvent); |
| 81 | + |
| 82 | + onCheckedChange?.(!checked, details); |
| 83 | + |
| 84 | + if (details.isCanceled) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + setChecked((currentlyChecked) => !currentlyChecked); |
| 89 | + }); |
| 90 | + |
| 91 | + const element = useRenderElement('div', componentProps, { |
| 92 | + state, |
| 93 | + stateAttributesMapping: itemMapping, |
| 94 | + props: [ |
| 95 | + itemProps, |
| 96 | + { |
| 97 | + role: 'menuitemcheckbox', |
| 98 | + 'aria-checked': checked, |
| 99 | + onClick: handleClick, |
| 100 | + }, |
| 101 | + elementProps, |
| 102 | + getItemProps, |
| 103 | + ], |
| 104 | + ref: [itemRef, forwardedRef, listItem.ref], |
| 105 | + }); |
130 | 106 |
|
131 | 107 | return ( |
132 | | - <InnerMenuCheckboxItem |
133 | | - {...other} |
134 | | - id={id} |
135 | | - ref={mergedRef} |
136 | | - highlighted={highlighted} |
137 | | - menuEvents={menuEvents} |
138 | | - itemProps={itemProps} |
139 | | - allowMouseUpTriggerRef={allowMouseUpTriggerRef} |
140 | | - typingRef={typingRef} |
141 | | - closeOnClick={closeOnClick} |
142 | | - nativeButton={nativeButton} |
143 | | - nodeId={menuPositionerContext?.floatingContext.nodeId} |
144 | | - /> |
| 108 | + <MenuCheckboxItemContext.Provider value={state}>{element}</MenuCheckboxItemContext.Provider> |
145 | 109 | ); |
146 | 110 | }); |
147 | 111 |
|
148 | | -interface InnerMenuCheckboxItemProps extends MenuCheckboxItem.Props { |
149 | | - highlighted: boolean; |
150 | | - itemProps: HTMLProps; |
151 | | - menuEvents: FloatingEvents; |
152 | | - allowMouseUpTriggerRef: React.RefObject<boolean>; |
153 | | - typingRef: React.RefObject<boolean>; |
154 | | - closeOnClick: boolean; |
155 | | - nativeButton: boolean; |
156 | | - nodeId: string | undefined; |
157 | | -} |
158 | | - |
159 | 112 | export type MenuCheckboxItemState = { |
160 | 113 | /** |
161 | 114 | * Whether the checkbox item should ignore user interaction. |
|
0 commit comments