@sonny-ui/core 0.1.0-alpha.11 → 0.1.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import { twMerge } from 'tailwind-merge';
3
3
  import { cva } from 'class-variance-authority';
4
4
  export { cva } from 'class-variance-authority';
5
5
  import * as i0 from '@angular/core';
6
- import { InjectionToken, makeEnvironmentProviders, inject, PLATFORM_ID, signal, computed, Injectable, input, Directive, ChangeDetectionStrategy, Component, ElementRef, model, viewChild, effect, forwardRef, HostListener, Injector, afterNextRender, Renderer2, output, contentChildren } from '@angular/core';
6
+ import { inject, PLATFORM_ID, signal, computed, Injectable, InjectionToken, makeEnvironmentProviders, provideEnvironmentInitializer, input, Directive, ChangeDetectionStrategy, Component, ElementRef, model, viewChild, effect, forwardRef, HostListener, Injector, afterNextRender, Renderer2, output, contentChildren } from '@angular/core';
7
7
  import { DOCUMENT, isPlatformBrowser } from '@angular/common';
8
8
  import { Dialog, DialogRef } from '@angular/cdk/dialog';
9
9
  import { NG_VALUE_ACCESSOR } from '@angular/forms';
@@ -13,27 +13,22 @@ function cn(...inputs) {
13
13
  return twMerge(clsx(inputs));
14
14
  }
15
15
 
16
- const DEFAULT_CONFIG = { prefix: 'sny', defaultTheme: 'light' };
17
- const SNY_CONFIG = new InjectionToken('SNY_CONFIG', {
18
- providedIn: 'root',
19
- factory: () => DEFAULT_CONFIG,
20
- });
21
- function provideSonnyUI(config = {}) {
22
- return makeEnvironmentProviders([
23
- { provide: SNY_CONFIG, useValue: { ...DEFAULT_CONFIG, ...config } },
24
- ]);
25
- }
26
-
16
+ const STORAGE_KEY = 'sny-theme';
27
17
  class ThemeService {
28
18
  document = inject(DOCUMENT);
29
19
  isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
20
+ config = inject(SNY_CONFIG);
30
21
  _theme = signal('light', ...(ngDevMode ? [{ debugName: "_theme" }] : /* istanbul ignore next */ []));
31
22
  theme = this._theme.asReadonly();
32
23
  isDark = computed(() => this._theme() === 'dark', ...(ngDevMode ? [{ debugName: "isDark" }] : /* istanbul ignore next */ []));
24
+ constructor() {
25
+ this.setTheme(this.resolveInitialTheme());
26
+ }
33
27
  setTheme(theme) {
34
28
  this._theme.set(theme);
35
29
  if (!this.isBrowser)
36
30
  return;
31
+ localStorage.setItem(STORAGE_KEY, theme);
37
32
  const root = this.document.documentElement;
38
33
  root.classList.remove('dark');
39
34
  root.removeAttribute('data-theme');
@@ -48,13 +43,40 @@ class ThemeService {
48
43
  toggleDark() {
49
44
  this.setTheme(this._theme() === 'dark' ? 'light' : 'dark');
50
45
  }
46
+ resolveInitialTheme() {
47
+ if (this.isBrowser) {
48
+ const stored = localStorage.getItem(STORAGE_KEY);
49
+ if (stored)
50
+ return stored;
51
+ }
52
+ const defaultTheme = this.config.defaultTheme ?? 'light';
53
+ if (defaultTheme === 'system') {
54
+ if (this.isBrowser && window.matchMedia('(prefers-color-scheme: dark)').matches) {
55
+ return 'dark';
56
+ }
57
+ return 'light';
58
+ }
59
+ return defaultTheme;
60
+ }
51
61
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.5", ngImport: i0, type: ThemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
52
62
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.5", ngImport: i0, type: ThemeService, providedIn: 'root' });
53
63
  }
54
64
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.5", ngImport: i0, type: ThemeService, decorators: [{
55
65
  type: Injectable,
56
66
  args: [{ providedIn: 'root' }]
57
- }] });
67
+ }], ctorParameters: () => [] });
68
+
69
+ const DEFAULT_CONFIG = { prefix: 'sny', defaultTheme: 'light' };
70
+ const SNY_CONFIG = new InjectionToken('SNY_CONFIG', {
71
+ providedIn: 'root',
72
+ factory: () => DEFAULT_CONFIG,
73
+ });
74
+ function provideSonnyUI(config = {}) {
75
+ return makeEnvironmentProviders([
76
+ { provide: SNY_CONFIG, useValue: { ...DEFAULT_CONFIG, ...config } },
77
+ provideEnvironmentInitializer(() => inject(ThemeService)),
78
+ ]);
79
+ }
58
80
 
59
81
  const buttonVariants = cva('inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-sm text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0', {
60
82
  variants: {