Skip to content

Commit

Permalink
Document React usage (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
devbymak and sindresorhus authored Apr 23, 2024
1 parent 2fb9856 commit 38ab02f
Showing 1 changed file with 44 additions and 10 deletions.
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

0 comments on commit 38ab02f

Please sign in to comment.