DetectTab

A comprehensive tab detection and management library for web applications with full TypeScript support

Visible
Tab Visibility
Focused
Tab Focus
0s
Visible Time
0s
Hidden Time

Live Demo

Try switching tabs, minimizing the window, or clicking elsewhere to see DetectTab in action!

πŸ“Š Time Statistics

0s
Total Visible
0s
Total Hidden
0s
Current State
0
State Changes

πŸ“ Event Log

Quick Start

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();
    }
}

Features

Everything you need for professional tab management

πŸ‘οΈ

Visibility Detection

Real-time monitoring of tab visibility using the Page Visibility API with graceful fallbacks

🎯

Focus Management

Track when your tab gains or loses focus for enhanced user experience

⏱️

Time Tracking

Comprehensive time statistics for visible, hidden, and current state durations

πŸ”§

TypeScript Support

Full TypeScript definitions for excellent IDE support and type safety

⚑

Performance Optimized

Built-in debouncing and efficient event handling for optimal performance

πŸ’Ύ

Persistence

Optional localStorage integration for tracking data across browser sessions

🌐

Browser Compatible

Works across all modern browsers with graceful degradation for older ones

πŸ“¦

Zero Dependencies

Lightweight library with no external dependencies for minimal bundle impact