A comprehensive tab detection and management library for web applications with full TypeScript support
Try switching tabs, minimizing the window, or clicking elsewhere to see DetectTab in action!
Get started with DetectTab in just a few lines of code
# Install via npm
npm install detect-tab
# Install via yarn
yarn add detect-tab
# Install via pnpm
pnpm add detect-tab
import { DetectTab } from 'detect-tab';
// Create instance
const tabDetector = new DetectTab();
// Check current state
console.log('Is visible:', tabDetector.isVisible());
console.log('Is focused:', tabDetector.isFocused());
// Listen for state changes
tabDetector.onStateChange((state, info) => {
if (state === 'hidden') {
pauseAnimations();
} else {
resumeAnimations();
}
});
// Listen for focus changes
tabDetector.onFocusChange((focused, info) => {
console.log('Tab focused:', focused);
});
import { DetectTab, TabEvent } from 'detect-tab';
// Advanced configuration
const tabDetector = new DetectTab({
persistent: true, // Save data across sessions
debounceTime: 100, // Debounce events
debug: true // Enable debugging
});
// Performance optimization
tabDetector.onStateChange((state) => {
if (state === 'hidden') {
// Pause expensive operations
clearInterval(animationTimer);
reduceUpdateFrequency();
} else {
// Resume normal operations
startAnimations();
normalUpdateFrequency();
}
});
// Analytics integration
tabDetector.on(TabEvent.VISIBILITY_CHANGE, (info) => {
analytics.track('tab_visibility_changed', {
visible: info.visible,
timeInState: info.timeInState
});
});
import {
DetectTab,
TabState,
TabInfo,
DetectTabOptions
} from 'detect-tab';
class TabManager {
private detector: DetectTab;
constructor(options?: DetectTabOptions) {
this.detector = new DetectTab(options);
this.setupListeners();
}
private setupListeners(): void {
this.detector.onStateChange((state: TabState, info: TabInfo) => {
this.handleStateChange(state, info);
});
}
private handleStateChange(state: TabState, info: TabInfo): void {
switch (state) {
case TabState.VISIBLE:
this.onVisible(info);
break;
case TabState.HIDDEN:
this.onHidden(info);
break;
}
}
public getStats() {
return this.detector.getTimeStats();
}
}
Everything you need for professional tab management
Real-time monitoring of tab visibility using the Page Visibility API with graceful fallbacks
Track when your tab gains or loses focus for enhanced user experience
Comprehensive time statistics for visible, hidden, and current state durations
Full TypeScript definitions for excellent IDE support and type safety
Built-in debouncing and efficient event handling for optimal performance
Optional localStorage integration for tracking data across browser sessions
Works across all modern browsers with graceful degradation for older ones
Lightweight library with no external dependencies for minimal bundle impact