Install devtools-detect via npm
mainInstall the package using npm to detect if browser DevTools are open and determine their orientation.
npm install devtools-detectrepository·main·Indexed 24 days ago
https://github.com/sindresorhus/devtools-detectA utility for detecting whether browser DevTools are open and determining their orientation. It provides a devtools object with isOpen and orientation properties, and dispatches a devtoolschange event on the window/globalThis object when the state changes.
Install the package using npm to detect if browser DevTools are open and determine their orientation.
npm install devtools-detectBe aware of the following limitations when using this package:
To use DevTools detection in React, create a custom hook that listens to the devtoolschange event and updates local state. This allows your components to reactively render based on whether DevTools is open.
import {useState, useEffect} from 'react';
import devtoolsDetect from 'devtools-detect';
export function useDevToolsStatus() {
const [isDevToolsOpen, setIsDevToolsOpen] = useState(devtoolsDetect.isOpen);
useEffect(() => {
const handleChange = event => {
setIsDevToolsOpen(event.detail.isOpen);
};
window.addEventListener('devtoolschange', handleChange);
return () => {
window.removeEventListener('devtoolschange', handleChange);
};
}, []);
return isDevToolsOpen;
}Import devtools to check the current state of DevTools or listen for changes via the devtoolschange event on the window object.
devtools.isOpen: A boolean indicating if DevTools is open.devtools.orientation: The orientation of DevTools (undefined if not open).devtoolschange event: Dispatched on window when DevTools is opened, closed, or changes orientation. The event detail contains { isOpen, orientation }.<script type="module">
import devtools from './node_modules/devtools-detect/index.js';
// Check if it's open
console.log('Is DevTools open:', devtools.isOpen);
// Check it's orientation, `undefined` if not open
console.log('DevTools orientation:', devtools.orientation);
// Get notified when it's opened/closed or orientation changes
window.addEventListener('devtoolschange', event => {
console.log('Is DevTools open:', event.detail.isOpen);
console.log('DevTools orientation:', event.detail.orientation);
});
</script>The default export is a devtools object that provides real-time information about the state of the browser's developer tools. You can check devtools.isOpen to see if the tools are currently open and devtools.orientation to see if they are docked 'horizontal' or 'vertical'.
Note that the library automatically polls for changes every 500ms.
The library dispatches a devtoolschange event on the globalThis object whenever the state of the developer tools changes (e.g., when they are opened or closed, or when their orientation changes).
You can listen for this event to react to changes in real-time. The event's detail property contains an object with isOpen (boolean) and orientation ('horizontal' | 'vertical' | undefined).