fullscreen-toggle
Version:
A lightweight utility to toggle fullscreen mode in web applications. Works with all JavaScript frameworks including React, Angular, Vue, and vanilla JS.
251 lines (193 loc) • 5.67 kB
Markdown
A lightweight, cross-browser utility to toggle fullscreen mode in web applications. Works seamlessly with all JavaScript frameworks including React, Angular, Vue, and vanilla JavaScript.
```bash
npm install fullscreen-toggle
```
```javascript
import { toggleFullScreen } from 'fullscreen-toggle';
// Enter fullscreen
toggleFullScreen(true);
// Exit fullscreen
toggleFullScreen(false);
```
```javascript
import { toggleFullScreen } from 'fullscreen-toggle';
// Using async/await
try {
const success = await toggleFullScreen(true);
if (success) {
console.log('Entered fullscreen successfully!');
} else {
console.log('Failed to enter fullscreen');
}
} catch (error) {
console.error('Error:', error);
}
// Using .then()
toggleFullScreen(true)
.then(success => {
if (success) {
console.log('Fullscreen activated!');
}
});
```
```jsx
import React, { useState } from 'react';
import { toggleFullScreen, isFullScreen, onFullScreenChange } from 'fullscreen-toggle';
function App() {
const [fullscreen, setFullscreen] = useState(false);
useEffect(() => {
// Listen for fullscreen changes
const cleanup = onFullScreenChange((isFs) => {
setFullscreen(isFs);
});
return cleanup; // Cleanup on unmount
}, []);
const handleToggle = async () => {
const success = await toggleFullScreen(!fullscreen);
if (!success) {
alert('Fullscreen not supported or failed');
}
};
return (
<div>
<button onClick={handleToggle}>
{fullscreen ? 'Exit Fullscreen' : 'Enter Fullscreen'}
</button>
</div>
);
}
```
```vue
<template>
<div>
<button @click="handleToggle">
{{ isFs ? 'Exit Fullscreen' : 'Enter Fullscreen' }}
</button>
</div>
</template>
<script>
import { toggleFullScreen, isFullScreen, onFullScreenChange } from 'fullscreen-toggle';
export default {
data() {
return {
isFs: false,
cleanup: null
};
},
mounted() {
this.cleanup = onFullScreenChange((isFullScreen) => {
this.isFs = isFullScreen;
});
},
beforeDestroy() {
if (this.cleanup) {
this.cleanup();
}
},
methods: {
async handleToggle() {
const success = await toggleFullScreen(!this.isFs);
if (!success) {
alert('Fullscreen not supported or failed');
}
}
}
};
</script>
```
```typescript
import { Component, OnInit, OnDestroy } from '@angular/core';
import { toggleFullScreen, isFullScreen, onFullScreenChange } from 'fullscreen-toggle';
@Component({
selector: 'app-fullscreen',
template: `
<button (click)="handleToggle()">
{{ isFullscreen ? 'Exit Fullscreen' : 'Enter Fullscreen' }}
</button>
`
})
export class FullscreenComponent implements OnInit, OnDestroy {
isFullscreen = false;
private cleanup?: () => void;
ngOnInit() {
this.cleanup = onFullScreenChange((isFs) => {
this.isFullscreen = isFs;
});
}
ngOnDestroy() {
if (this.cleanup) {
this.cleanup();
}
}
async handleToggle() {
const success = await toggleFullScreen(!this.isFullscreen);
if (!success) {
alert('Fullscreen not supported or failed');
}
}
}
```
```javascript
import { toggleFullScreen, isFullScreenSupported } from 'fullscreen-toggle';
const button = document.getElementById('fullscreen-btn');
if (isFullScreenSupported()) {
button.addEventListener('click', async () => {
const isCurrentlyFullscreen = isFullScreen();
const success = await toggleFullScreen(!isCurrentlyFullscreen);
if (success) {
button.textContent = isCurrentlyFullscreen ? 'Enter Fullscreen' : 'Exit Fullscreen';
} else {
console.error('Failed to toggle fullscreen');
}
});
} else {
button.disabled = true;
button.textContent = 'Fullscreen not supported';
}
```
Toggles fullscreen mode.
- **goFullScreen**: `true` to enter fullscreen, `false` to exit
- **Returns**: Promise that resolves to `true` if successful, `false` otherwise
Checks if the browser is currently in fullscreen mode.
- **Returns**: `true` if in fullscreen, `false` otherwise
Checks if the Fullscreen API is supported by the browser.
- **Returns**: `true` if supported, `false` otherwise
Adds an event listener for fullscreen state changes.
- **callback**: Function called when fullscreen state changes
- **Returns**: Cleanup function to remove the event listener
- ✅ Chrome 15+
- ✅ Firefox 64+
- ✅ Safari 16.4+
- ✅ Edge 12+
- ✅ Opera 15+
- ✅ Internet Explorer 11
- 🚀 **Lightweight**: Less than 2KB gzipped
- 🌐 **Cross-browser**: Handles vendor prefixes automatically
- 📱 **Framework agnostic**: Works with React, Vue, Angular, and vanilla JS
- 💪 **TypeScript**: Full TypeScript support with type definitions
- 🔄 **Promise-based**: Modern async/await support
- 📡 **Event handling**: Listen for fullscreen changes
- 🛡️ **Error handling**: Graceful fallbacks and error handling
MIT
Pull requests are welcome! Please read our contributing guidelines first.
If you find any issues or have feature requests, please open an issue on our [GitHub repository](https://github.com/yourusername/fullscreen-toggle/issues).