
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
@custom-react-hooks/use-mouse
Advanced tools
The `useMouse` hook is designed for tracking the mouse position within a specified element in React applications. It's enhanced to provide additional features such as offsetting the mouse position and avoiding edges of the screen, which is useful for tool
The useMouse hook is designed for tracking the mouse position within a specified element in React applications. It's enhanced to provide additional features such as offsetting the mouse position and avoiding edges of the screen, which is useful for tooltips and other floating elements.
npm install @custom-react-hooks/use-mouse
or
yarn add @custom-react-hooks/use-mouse
npm install @custom-react-hooks/all
or
yarn add @custom-react-hooks/all
The useMouse hook must be imported using a named import as shown below:
Named Import:
import { useMouse } from '@custom-react-hooks/use-mouse';
This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.
import React, { useRef } from 'react';
import { useMouse } from '@custom-react-hooks/all';
const Tooltip = ({ mousePosition, isVisible }) => {
const { window, document } = useFrame();
const tooltipRef = useRef(null);
const positionStyles = useMemo(() => {
const style = { display: isVisible ? 'block' : 'none' };
if (tooltipRef.current) {
const tooltipRect = tooltipRef.current.getBoundingClientRect();
let newX = mousePosition.x;
let newY = mousePosition.y;
if (newX + tooltipRect.width > window.innerWidth) {
newX -= tooltipRect.width;
}
if (newY + tooltipRect.height > window.innerHeight) {
newY -= tooltipRect.height;
}
style.left = `${newX}px`;
style.top = `${newY}px`;
}
return style;
}, [mousePosition, isVisible]);
return (
<div
ref={tooltipRef}
style={positionStyles}
>
X: <span>{mousePosition.x}</span>,
Y: <span>{mousePosition.y}</span>
</div>
);
};
const MouseComponent = () => {
const boxRef = useRef(null);
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
const tooltipOptions = {
offsetX: 15,
offsetY: 15,
avoidEdges: true,
tooltipWidth: 120,
tooltipHeight: 50,
};
const mousePosition = useMouse(boxRef, tooltipOptions);
const handleMouseEnter = () => setIsTooltipVisible(true);
const handleMouseLeave = () => setIsTooltipVisible(false);
return (
<div className="center">
<div
ref={boxRef}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
Hover over this box
</div>
<Tooltip
mousePosition={mousePosition}
isVisible={isTooltipVisible}
/>
</div>
);
};
export default MouseComponent;
In this TypeScript example, the useMouse hook is used to track the mouse position within a div element and adjust the position of a tooltip or pop-up element accordingly.
ref: A React RefObject pointing to the target element that the mouse position will be relative to.options: Configuration object for the mouse position adjustments with the following properties:
offsetX: The horizontal offset added to the mouse position.offsetY: The vertical offset added to the mouse position.avoidEdges: Boolean that indicates whether the tooltip position should be adjusted to avoid the edges of the screen or the target element.tooltipWidth: (Optional) The width of the tooltip element, used in calculating edge avoidance.tooltipHeight: (Optional) The height of the tooltip element, used in calculating edge avoidance.relativeToWindow: (Optional) Boolean that indicates whether the mouse position should be calculated relative to the window instead of the target element. If not provided, it defaults to false.x: The calculated x-coordinate for the tooltip position.y: The calculated y-coordinate for the tooltip position.position: A string indicating the tooltip's position relative to the cursor. Possible values are 'bottomRight', 'bottomLeft', 'topRight', and 'topLeft'.Contributions to enhance useMouse are welcome. Please feel free to submit issues or pull requests to the repository.
FAQs
The `useMouse` hook is designed for tracking the mouse position within a specified element in React applications. It's enhanced to provide additional features such as offsetting the mouse position and avoiding edges of the screen, which is useful for tool
The npm package @custom-react-hooks/use-mouse receives a total of 502 weekly downloads. As such, @custom-react-hooks/use-mouse popularity was classified as not popular.
We found that @custom-react-hooks/use-mouse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.