Skip to content

Commit 25dc5eb

Browse files
2nikhiltomsojinantony01
authored andcommitted
docs: updates menuTarget prop usage docs (carbon-design-system#19246)
* docs: updates menuTarget prop usage docs * chore: clean up * chore: doc format change
1 parent 62af26a commit 25dc5eb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/react/src/components/MenuButton/MenuButton.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,28 @@ components with focus management.
115115
Pass the `menuTarget` prop to render the `Menu` inside the modal or any specific
116116
element where you want it to render:
117117

118+
## Managing the `menuTarget` prop with React refs
119+
Because React does not trigger re-renders when a ref's `.current` value changes,
120+
you must capture the ref value in state to update the `menuTarget` prop.
121+
```tsx
122+
const Component = () => {
123+
const containerRef = useRef<HTMLDivElement>(null);
124+
const [target, setTarget] = useState<HTMLDivElement | undefined>(undefined);
125+
useEffect(() => {
126+
if (containerRef.current) {
127+
setTarget(containerRef.current);
128+
}
129+
}, []);
130+
return (
131+
<div ref={containerRef}>
132+
<MenuButton label="Actions" menuTarget={target}>
133+
<MenuItem label="First action" />
134+
</MenuButton>
135+
</div>
136+
);
137+
};
138+
```
139+
118140
## Experimental Auto Align
119141

120142
<Canvas

0 commit comments

Comments
 (0)