2022-11-19 12:36:58 -08:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
interface IStopPropagation {
|
2022-11-19 13:26:07 -08:00
|
|
|
/** Children to render within the bubble. */
|
2022-11-19 12:36:58 -08:00
|
|
|
children: React.ReactNode,
|
2022-11-19 13:26:07 -08:00
|
|
|
/** Whether to prevent mouse events from bubbling. (default: `true`) */
|
|
|
|
enabled?: boolean,
|
2022-11-19 12:36:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent mouse events from bubbling up.
|
|
|
|
*
|
|
|
|
* Why is this needed? Because `onClick`, `onMouseDown`, and `onMouseUp` are 3 separate events.
|
|
|
|
* To prevent a lot of code duplication, this component can stop all mouse events.
|
|
|
|
* Plus, placing it in the component tree makes it more readable.
|
|
|
|
*/
|
2022-11-19 13:26:07 -08:00
|
|
|
const StopPropagation: React.FC<IStopPropagation> = ({ children, enabled = true }) => {
|
2022-11-19 12:36:58 -08:00
|
|
|
|
|
|
|
const handler: React.MouseEventHandler<HTMLDivElement> = (e) => {
|
2022-11-19 13:26:07 -08:00
|
|
|
if (enabled) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2022-11-19 12:36:58 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
|
|
|
|
<div onClick={handler} onMouseDown={handler} onMouseUp={handler}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default StopPropagation;
|