Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add React usage #70

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,56 @@ npm install devtools-detect

```html
<script type="module">
import devtools from './node_modules/devtools-detect/index.js';
import devtools from './node_modules/devtools-detect/index.js';

// Check if it's open
console.log('Is DevTools open:', devtools.isOpen);
// 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);
// 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);
});
// 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>
```

## React usage

```jsx
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;
}
```

```jsx
import {useDevToolsStatus} from './useDevToolsStatus.js';

export default function App() {
const isDevToolsOpen = useDevToolsStatus();
return isDevToolsOpen ? 'OPEN' : 'CLOSED';
}
```

## Support

- Chrome DevTools
Expand Down
Loading