Can I make the children of the Checkbox not activate the checkbox? #3621
Replies: 1 comment
-
|
Yes — the trick is to stop the click event from propagating to the Wrap your clickable text content so that it stops propagation: <Checkbox.Root>
<Checkbox.Indicator />
<span
onClick={(e) => {
e.stopPropagation();
// open your ToS modal here
setShowModal(true);
}}
>
I agree to the <a href="#">Terms of Service</a>
</span>
</Checkbox.Root>Alternatively, if you want a cleaner separation, do not put the text inside <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
<Checkbox.Root id="tos">
<Checkbox.Indicator />
</Checkbox.Root>
<span>
I agree to the{" "}
<button onClick={() => setShowModal(true)}>Terms of Service</button>
</span>
</div>This way clicking the text never reaches the checkbox. The checkbox only toggles when you click directly on it. The key insight is that Radix's |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using this for a ToS checkbox. The user clicks on the text in the children of the Checkbox to open a modal for the user to read. This works, however clicking on the text will check the box. I want the box to only be checked/unchecked when clicking the box itself, not the text or label. Is that possible?
Beta Was this translation helpful? Give feedback.
All reactions