Implementing full screen mode in Angular


Immersive experiences can be crucial for engaging users with your Angular application and full screen mode allows you to maximize the visibility of your content, providing a focused and distraction-free environment. This article delves into various approaches to implementing fullscreen functionality in your Angular application, complete with code examples and considerations.

Understanding the Options

Two primary approaches exist for achieving fullscreen in Angular:

1. Browser API:

  • Leverages the built-in requestFullscreen() and exitFullscreen() methods.
  • Offers native browser fullscreen behavior.
  • Requires handling browser compatibility issues.

2. CSS Styling:

  • Utilizes height: 100vh and width: 100vw to encompass the viewport.
  • Simulates fullscreen without browser API involvement.
  • Doesn’t engage true full screen mode, lacking specific browser features.

Implementing Full Screen with the Browser API

1. Create a Service:

Create a service to encapsulate the full screen logic:

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class FullscreenService {
  private fullscreenElement: HTMLElement | null = null;
  private isFullscreen$ = new Subject<boolean>();

  isFullscreen(): Observable<boolean> {
    return this.isFullscreen$.asObservable();
  }

  toggleFullscreen(element: HTMLElement) {
    if (!this.fullscreenElement) {
      if (element.requestFullscreen) {
        element.requestFullscreen()
          .then(() => {
            this.fullscreenElement = element;
            this.isFullscreen$.next(true);
          })
          .catch(() => console.error('Failed to enter fullscreen'));
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen()
          .then(() => {
            this.fullscreenElement = element;
            this.isFullscreen$.next(true);
          })
          .catch(() => console.error('Failed to enter fullscreen'));
      }
    } else {
      if (document.exitFullscreen) {
        document.exitFullscreen()
          .then(() => {
            this.fullscreenElement = null;
            this.isFullscreen$.next(false);
          })
          .catch(() => console.error('Failed to exit fullscreen'));
      } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen()
          .then(() => {
            this.fullscreenElement = null;
            this.isFullscreen$.next(false);
          })
          .catch(() => console.error('Failed to exit fullscreen'));
      }
    }
  }
}

2. Integrate in Your Component:

Inject the service and utilize the toggleFullscreen method:

import { Component, OnInit } from '@angular/core';
import { FullscreenService } from './fullscreen.service';

@Component({
  selector: 'app-my-component',
  template: `
    <button (click)="toggleFullscreen(fullscreenElement)">
      {{ isFullscreen$ | async ? 'Exit Fullscreen' : 'Enter Fullscreen' }}
    </button>
    <div #fullscreenElement>
      </div>
  `
})
export class MyComponent implements OnInit {
  constructor(private fullscreenService: FullscreenService) {}

  ngOnInit() {
    this.fullscreenService.isFullscreen().subscribe(isFullscreen => {
      // Update UI based on fullscreen state
    });
  }

  toggleFullscreen(element: HTMLElement) {
    this.fullscreenService.toggleFullscreen(element);
  }
}

Creating a directive for full screen mode

For reusability and better encapsulation, consider a directive:

@Directive({
  selector: '[appFullscreen]'
})
export class FullscreenDirective {
  constructor(private elRef: ElementRef) {}

  toggleFullscreen() {
    if (this.elRef.nativeElement.requestFullscreen) {
      this.elRef.nativeElement.requestFullscreen();
    } else if (this.elRef.nativeElement.webkitRequestFullscreen) {
      this.elRef.nativeElement.webkitRequestFullscreen();
    }
  }
}

Implementing full screen with CSS

1. Apply Styles:

Set height: 100vh and width: 100vw to the desired element:

.fullscreen-element {
  height: 100vh;
  width: 100vw;
  overflow: hidden; /* Optional for content overflow */
}

2. Use in Your Component:

Apply the class to the element:

<div class="fullscreen-element"></div>

Handling keypress events:

Bind the (keyup) event to trigger fullscreen on specific keys (e.g., F11):

<div (keyup.f11)="toggleFullscreen()"></div>

Considerations and conclusion

  • Browser Compatibility: The Fullscreen API has broad support, but check for browser-specific prefixes (e.g., webkitRequestFullscreen).
  • CSS-based solution: is simpler but lacks true fullscreen behavior.
  • Exit Handling: Provide ways for users to exit (e.g., an exit button, ESC keypress).
  • Event Handling: Consider handling events like window resize and orientation changes when in fullscreen.
  • Styling Adjustments: Adapt your app’s UI for fullscreen mode, hiding unnecessary elements if needed.
  • Consider accessibility implications.

By following these guidelines and adapting the code examples to your specific needs, you can seamlessly integrate fullscreen functionality into your Angular application, enhancing the user experience and providing an immersive environment for your content. Remember to test thoroughly across different browsers and devices to ensure a consistent and enjoyable experience for all users.

0
Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.