All files / element-ng/theme si-theme-signals.ts

85.71% Statements 6/7
50% Branches 1/2
100% Functions 2/2
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33              152x                       152x 7x     6x   6x 3x            
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { isPlatformBrowser } from '@angular/common';
import { inject, PLATFORM_ID, signal, Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { fromEvent, map } from 'rxjs';
 
import { SI_THEME_DOM_TARGET } from './si-theme-dom-target';
 
/**
 * Returns a signal that reflects whether the current Element theme is dark (`true`) or light
 * (`false`). The signal reacts to `theme-switch` events dispatched on `window`.
 *
 * @experimental
 * @returns A boolean signal — `true` when the active theme is dark
 * @throws When called outside of an Angular injection context
 */
export const injectIsDarkTheme = (): Signal<boolean> => {
  Iif (!isPlatformBrowser(inject(PLATFORM_ID))) {
    return signal(false);
  }
  const targetDom = inject(SI_THEME_DOM_TARGET);
 
  return toSignal(
    fromEvent<CustomEvent<{ dark: boolean }>>(window, 'theme-switch').pipe(map(e => e.detail.dark)),
    {
      initialValue: targetDom.element.classList.contains('app--dark')
    }
  );
};