UNPKG

@sandlada/mdc

Version:

@sandlada/mdc is an open source component library that follows the Material Design 3 design specifications.

27 lines 1.91 kB
/** * @license * Copyright 2026 Kai-Orion & Sandlada * SPDX-License-Identifier: MIT * * @fileoverview * Scope-based navigation state store used to synchronize navigation tabs * across containers such as bar, rail, and drawer. * * @example * ```ts * import { GlobalNavigationStateStore } from '@sandlada/mdc/utils' * * const unsubscribe = GlobalNavigationStateStore.subscribe('main-nav', (mutation) => { * console.log(mutation.activeValue, mutation.source, mutation.trigger) * }) * * GlobalNavigationStateStore.setActive('main-nav', '/home', { * source: 'user', * trigger: 'pointer', * }) * * unsubscribe() * ``` */ var e=class e{constructor(){this.mutationCounter=0,this.scopes=new Map}static{this._Instance=null}static get Instance(){return this._Instance||=new e,this._Instance}getActive(e){return this.getOrCreateScope(this.normalizeScope(e)).activeValue}subscribe(e,t){let n=this.normalizeScope(e);return this.getOrCreateScope(n).listeners.add(t),()=>{let e=this.scopes.get(n);e&&(e.listeners.delete(t),e.listeners.size===0&&e.activeValue===null&&e.previousValue===null&&this.scopes.delete(n))}}setActive(e,t,n={}){let r=this.normalizeScope(e),i=this.getOrCreateScope(r),a=i.activeValue,o=a!==t;o&&(i.previousValue=a,i.activeValue=t),this.mutationCounter+=1;let s={scope:r,activeValue:i.activeValue,previousValue:a,changed:o,source:n.source??`external`,trigger:n.trigger??`programmatic`,originId:n.originId??null,mutationId:this.mutationCounter},c=Array.from(i.listeners);for(let e of c)e(s);return s}clearScope(e){this.scopes.delete(this.normalizeScope(e))}normalizeScope(e){let t=e.trim();return t.length>0?t:`global`}getOrCreateScope(e){let t=this.scopes.get(e);return t||(t={activeValue:null,previousValue:null,listeners:new Set},this.scopes.set(e,t)),t}};const t=e.Instance;export{t as GlobalNavigationStateStore,e as NavigationStateStore}; //# sourceMappingURL=navigation-state-store.js.map