CoreGamix — Full-Stack Engineering & Developer ToolsCoreGamix — Full-Stack Engineering & Developer Tools
  • Angular
  • Web Development
    • React & TypeScript
    • Node.js
    • PHP & MySQL
  • Tech & AI News
  • Tools & Plugins
  • About
  • Contact Us
HomeWeb Development Angular Custom Angular 22 Snackbar: Build a Modern UI Service

Custom Angular 22 Snackbar: Build a Modern UI Service

Anil Mehra on July 4, 2026
Angular Web Development
Custom Angular 22 Snackbar UI examples
4 Min Read

Angular Material provides a fantastic default snackbar right out of the box. However, modern applications often require more than just basic string messages to effectively communicate with users. You might need custom styling, dynamic HTML injection, or even list support to neatly display a group of validation errors. By building a Custom Angular 22 Snackbar, you can achieve all of this while ensuring your application remains fully zoneless and perfectly safe for Server-Side Rendering (SSR). Let’s dive into how to architect this highly flexible UI service.

Handling SSR Safely for a Custom Angular 22 Snackbar

Handling Server Side Rendering safely in Angular 22

Opening a DOM-based overlay during Server-Side Rendering (SSR) will almost certainly cause fatal errors in your Node server. Because the Material overlay requires access to the document and window objects, we must explicitly check the platform environment before attempting to trigger it.

Since this customized overlay relies on these browser APIs to render, injecting a dedicated platform service is the cleanest way to ensure the code only fires in the browser context.

Read our complete guide to Angular Incremental Hydration & SSR

Here is the lightweight platform check service:

import { Injectable, inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

@Injectable({ providedIn: 'root' })
export class PlatformService {
  private platformId = inject(PLATFORM_ID);

  get isBrowser(): boolean {
    return isPlatformBrowser(this.platformId);
  }

  get isServer(): boolean {
    return isPlatformServer(this.platformId);
  }
}

The Core Service Logic of Our Custom Angular 22 Snackbar

Now that we have our platform checks in place, we can build the actual service wrapper. This service acts as a clean abstraction over MatSnackBar, injecting our custom data payload and handling the SSR check automatically.

By centralizing this logic, deploying this enhanced notification system anywhere in your application becomes a seamless one-liner. It takes the boilerplate out of your components while keeping your application architecture pristine.

import { Injectable, inject } from '@angular/core';
import { PlatformService } from './platform.service';
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
import { SnackbarComponent } from '../../ui/snackbar/snackbar.component';

export type SnackbarType = 'success' | 'error' | 'warning' | 'info';

export interface SnackbarData {
  type: SnackbarType;
  message?: string;
  html?: string;
  list?: string[];
  title?: string;
}

@Injectable({ providedIn: 'root' })
export class SnackbarService {
  private snackBar = inject(MatSnackBar);
  private platformService = inject(PlatformService);

  show(data: SnackbarData, config?: MatSnackBarConfig) {
    if (!this.platformService.isBrowser) {
      return; // SSR Safety!
    }

    this.snackBar.openFromComponent(SnackbarComponent, {
      data,
      duration: config?.duration ?? 5000,
      panelClass: ['app-snackbar', `app-snackbar-${data.type}`],
      horizontalPosition: config?.horizontalPosition ?? 'center',
      verticalPosition: config?.verticalPosition ?? 'bottom',
      ...config
    });
  }

  success(message: string, title?: string) {
    this.show({ type: 'success', message, title });
  }
  
  // Extendable with error(), warning(), and info() methods...
}

Component Template using Modern Control Flow for a Custom Angular 22 Snackbar

Angular 22 provides powerful new @if and @for control blocks natively in the template. This makes conditional rendering faster, cleaner, and much more readable than the older *ngIf structural directives.

Thanks to these control flow blocks, it is incredibly easy to conditionally render optional titles, standard text messages, injected HTML, or even bulleted lists natively inside our dynamic component depending on the data payload passed to it.

<div class="snackbar-container">
  <div class="snackbar-icon">
    <mat-icon>{{ getIcon() }}</mat-icon>
  </div>
  <div class="snackbar-content">
    @if (data.title) {
      <div class="snackbar-title">{{ data.title }}</div>
    }
    
    @if (data.message) {
      <div class="snackbar-message">{{ data.message }}</div>
    }

    @if (data.html) {
      <div class="snackbar-html" [innerHTML]="data.html"></div>
    }

    @if (data.list && data.list.length > 0) {
      <ul class="snackbar-list">
        @for (item of data.list; track item) {
          <li>{{ item }}</li>
        }
      </ul>
    }
  </div>
  <div class="snackbar-actions">
    <button mat-icon-button (click)="dismiss()">
      <mat-icon>close</mat-icon>
    </button>
  </div>
</div>

Styling Without Encapsulation Restrictions

To properly style Material Design components from the ground up, you will often need to set encapsulation: ViewEncapsulation.None inside your component decorator. This allows you to easily override the default MDC surface styles that Material applies.

To prevent these global styles from leaking out and affecting the rest of your app, simply scope your CSS by targeting the panelClass we provided in the service (for example, using .app-snackbar-success & { background-color: #2e7d32; } in SCSS). By doing this, adding a Custom Angular 22 Snackbar layout becomes both safe and highly maintainable.

Conclusion

Building a Custom Angular 22 Snackbar gives you the ultimate flexibility to handle rich user notifications—like rendering safe HTML and structural lists—while ensuring your application remains strictly zoneless and SSR-safe. By effectively wrapping the default Angular Material component, you protect your server-side rendering pipelines and create a scalable UI solution that your entire development team will love using. Upgrade your notifications today and take full advantage of Angular’s modern feature set!

Learn how to set up NgRx with Angular 22

Official Documentation for Angular Material Snackbar

Anil Mehra on July 4, 2026 Angular Web Development
previous article
Next article

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

About the Author

Anil Mehra

Anil Mehra

Software Engineer · Google AI Certified

Anil Mehra is a Software Engineer specializing in Angular (NgRx, SignalStore, RxJS), React, TypeScript, Node.js, PHP, and MySQL. Holding the Google AI Professional Certificate, he bridges production web architecture with practical AI-powered workflows.

Google AI Professional Certificate official badge

Google AI Professional Certificate

Google · Coursera Credential ↗

About & Consulting · Instagram · LinkedIn

Recent Guides

  • WordPress Block Bindings API guide banner showing custom PHP sources connected to Gutenberg block editor Attributes sidebar
    Block Bindings API in WordPress Plugins: PHP Sources, Editor UI & Batched LoopsDecember 2, 2026
  • WordPress autoloaded options architecture card showing wp_options payload reduction from 1.84 MB to 186 KB and TTFB drop
    WordPress Autoloaded Options: Fix Plugin Bloat, TTFB Spikes & Cache EvictionsDecember 2, 2026
  • claude sonnet 4.5 retirement migration guide banner
    Claude Sonnet 4.5 Retirement Hits Today: How I Migrated Node.js Calls to Sonnet 5.5November 30, 2026
  • speculation rules api instant prerender LCP guide banner
    Speculation Rules API Guide: Cut Route LCP to 38ms Without Phantom AnalyticsNovember 30, 2026

Engineering Topics

  • Angular 13
  • Frontend Development 4
  • Node.js 3
  • PHP & MySQL 2
  • React & TypeScript 2
  • SEO & Performance 2
  • Tech & AI News 4
  • Web Development 21
  • Wordpress Plugins 3
Read next
Angular Incremental Hydration: The Ultimate SSR Guide 4 Min
Angular Incremental Hydration: The Ultimate SSR Guide
Anil Mehra on August 25, 2026
Mastering Angular incremental hydration transforms web performance instantly. Specifically, web developers often...
Building Accessible Angular Aria Components Fast 4 Min
Building Accessible Angular Aria Components Fast
Anil Mehra on August 25, 2026
Creating inclusive web applications requires excellent tooling. Therefore, developers need robust solutions for web...
Zoneless Angular 22 Apps: Practical Migration & Performance Guide 3 Min
Zoneless Angular 22 Apps: Practical Migration & Performance Guide
Anil Mehra on August 25, 2026
Building high-speed web platforms requires modern techniques. Specifically, creating Zoneless Angular 22 Apps...
CoreGamix — Full-Stack Engineering & Developer ToolsCoreGamix — Full-Stack Engineering & Developer Tools

contact

support@coregamix.com

© 2026 CoreGamix. All Rights Reserved.  |  About  |  Privacy Policy  |  Terms of Use  |  Contact

Back to top