{"version":3,"file":"ngwr-density.mjs","sources":["../../../projects/lib/density/density-config.ts","../../../projects/lib/density/density.ts","../../../projects/lib/density/density.directive.ts","../../../projects/lib/density/provide-wr-density.ts","../../../projects/lib/density/ngwr-density.ts"],"sourcesContent":["/**\n * @license\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE\n */\n\nimport { InjectionToken } from '@angular/core';\n\nimport type { WrDensityConfig } from './interfaces';\n\nexport const DEFAULT_WR_DENSITY_CONFIG: WrDensityConfig = {\n  defaultDensity: 'md',\n  storageKey: 'wr-density',\n  attribute: 'data-wr-density',\n};\n\nexport const WR_DENSITY_CONFIG = new InjectionToken<WrDensityConfig>('WR_DENSITY_CONFIG', {\n  factory: (): WrDensityConfig => DEFAULT_WR_DENSITY_CONFIG,\n});\n\nexport type { WrDensityValue, WrDensityConfig } from './interfaces';\n","/**\n * @license\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE\n */\n\nimport { DOCUMENT } from '@angular/common';\nimport { Service, type Signal, effect, inject, signal } from '@angular/core';\n\nimport { WrStorage } from 'ngwr/storage';\n\nimport { WR_DENSITY_CONFIG, type WrDensityValue } from './density-config';\n\nconst VALID: readonly WrDensityValue[] = ['sm', 'md', 'lg', 'touch'];\n\nfunction isDensity(v: unknown): v is WrDensityValue {\n  return typeof v === 'string' && (VALID as readonly string[]).includes(v);\n}\n\n/**\n * Global density scale. Writes `[data-wr-density]` on `<html>` so the\n * stylesheet's density-scoped CSS-variable rules apply, then components\n * pick up the new multipliers without re-rendering.\n *\n * - **Per-subtree override** — drop `[wrDensity]` on any element; its\n *   descendants read the override instead of the global value.\n * - **Persisted** via {@link WrStorage} (swap the engine globally if you\n *   need a different store).\n *\n * @example\n * ```ts\n * const density = inject(WrDensity);\n * density.set('sm');\n * density.current();   // 'sm'\n * ```\n *\n * @see https://ngwr.dev/services/density\n */\n@Service()\nexport class WrDensity {\n  private readonly doc = inject(DOCUMENT);\n  private readonly storage = inject(WrStorage);\n  private readonly config = inject(WR_DENSITY_CONFIG);\n\n  /** Active density. */\n  readonly current: Signal<WrDensityValue>;\n\n  private readonly _current = signal<WrDensityValue>(this.readPersisted() ?? this.config.defaultDensity);\n\n  constructor() {\n    this.current = this._current.asReadonly();\n    effect(() => {\n      const value = this._current();\n      const html = this.doc.documentElement;\n      if (html) html.setAttribute(this.config.attribute, value);\n      this.persist(value);\n    });\n  }\n\n  /** Switch the active density. Ignores unknown values. */\n  set(density: WrDensityValue): void {\n    if (!isDensity(density)) return;\n    this._current.set(density);\n  }\n\n  /** Cycle sm → md → lg → touch → sm … */\n  cycle(): void {\n    const order: readonly WrDensityValue[] = ['sm', 'md', 'lg', 'touch'];\n    const i = order.indexOf(this._current());\n    this._current.set(order[(i + 1) % order.length]);\n  }\n\n  // Persistence\n\n  private readPersisted(): WrDensityValue | null {\n    if (!this.config.storageKey) return null;\n    const raw = this.storage.get<WrDensityValue>(this.config.storageKey);\n    return isDensity(raw) ? raw : null;\n  }\n\n  private persist(density: WrDensityValue): void {\n    if (!this.config.storageKey) return;\n    this.storage.set(this.config.storageKey, density);\n  }\n}\n","/**\n * @license\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE\n */\n\nimport { Directive, ElementRef, effect, inject, input } from '@angular/core';\n\nimport { WR_DENSITY_CONFIG, type WrDensityValue } from './density-config';\n\n/**\n * Scope a density override to a subtree. Writes the same `data-*`\n * attribute the global service writes — but on the host instead of\n * `<html>` — so the CSS-variable scope rules cascade only to descendants.\n *\n * @example\n * ```html\n * <!-- The whole sidebar is sm, the rest of the app keeps its global density. -->\n * <aside wrDensity=\"sm\">\n *   <wr-list ...></wr-list>\n * </aside>\n * ```\n */\n@Directive({\n  selector: '[wrDensity]',\n})\nexport class WrDensityDirective {\n  /** Density to apply to this subtree. */\n  readonly wrDensity = input.required<WrDensityValue>();\n\n  private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);\n  private readonly config = inject(WR_DENSITY_CONFIG);\n\n  constructor() {\n    effect(() => {\n      this.host.nativeElement.setAttribute(this.config.attribute, this.wrDensity());\n    });\n  }\n}\n","/**\n * @license\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE\n */\n\nimport { type EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';\n\nimport { DEFAULT_WR_DENSITY_CONFIG, WR_DENSITY_CONFIG, type WrDensityConfig } from './density-config';\n\n/** Configure {@link WrDensity}. All fields optional — merged with defaults. */\nexport function provideWrDensity(config: Partial<WrDensityConfig> = {}): EnvironmentProviders {\n  return makeEnvironmentProviders([\n    { provide: WR_DENSITY_CONFIG, useValue: { ...DEFAULT_WR_DENSITY_CONFIG, ...config } },\n  ]);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;AAAA;;;;;AAKG;AAMI,MAAM,yBAAyB,GAAoB;AACxD,IAAA,cAAc,EAAE,IAAI;AACpB,IAAA,UAAU,EAAE,YAAY;AACxB,IAAA,SAAS,EAAE,iBAAiB;;MAGjB,iBAAiB,GAAG,IAAI,cAAc,CAAkB,mBAAmB,EAAE;AACxF,IAAA,OAAO,EAAE,MAAuB,yBAAyB;AAC1D,CAAA;;ACnBD;;;;;AAKG;AASH,MAAM,KAAK,GAA8B,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC;AAEpE,SAAS,SAAS,CAAC,CAAU,EAAA;IAC3B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAK,KAA2B,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC1E;AAEA;;;;;;;;;;;;;;;;;;AAkBG;MAEU,SAAS,CAAA;AACH,IAAA,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC;AACtB,IAAA,OAAO,GAAG,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