@barocss/browser 0.8.2 → 0.9.0

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.
package/dist/index.cjs CHANGED
@@ -4,6 +4,12 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require
4
4
  `}catch(n){return(0,e.isDebug)()&&console.warn(`[StylePartitionManager] Failed to insert rule in category: root ${t.join(`
5
5
  `)}`,n),{success:0,failed:t.length}}return{success:t.length,failed:0}}addRules(e){let t=0,n=0;for(let r of e){let e=this.getCategory(r.cls);if(e)for(let t of r.cssList)this.addCategoryRule(t,e);else for(let e of r.cssList)this.addRule(e)?t++:n++}return{success:t,failed:n}}removeRule(e,t){let n;if(t){if(this.classToCategoryPartitionMap.get(e)!==t)return!1;n=this.categoryPartitions.get(t)}else{let t=this.classToPartitionMap.get(e);n=t===void 0?void 0:this.partitions[t]}if(!n)return!1;let r=n.styles.indexOf(e);if(r===-1)return!1;let i=n.styleElement.sheet,a=!!i&&i.cssRules.length===n.styles.length;return n.styles.splice(r,1),n.keys?.splice(r,1),a&&i?i.deleteRule(r):n.styleElement.textContent=n.styles.length?n.styles.join(`
6
6
  `)+`
7
- `:``,t?this.classToCategoryPartitionMap.delete(e):this.classToPartitionMap.delete(e),!0}get ruleCount(){return this.classToPartitionMap.size+this.classToCategoryPartitionMap.size}findRulePartition(e){let t=this.classToPartitionMap.get(e);if(t!==void 0&&this.partitions[t])return this.partitions[t];let n=this.classToCategoryPartitionMap.get(e);return n===void 0?null:this.categoryPartitions.get(n)||null}updateRuleContent(t,n,r=!1){let i=this.getCategoryPartition(t);if(i)i.styleElement.textContent=n;else{let i=this.createNewCategoryPartition(t,r);(0,e.isDebug)()&&console.log(`[StylePartitionManager] Created new partition for category: ${t}`),i.styleElement.textContent=n}}cleanup(){this.partitions.forEach(e=>{e.styleElement.parentNode&&e.styleElement.parentNode.removeChild(e.styleElement)}),this.categoryPartitions.forEach(e=>{e.styleElement.parentNode&&e.styleElement.parentNode.removeChild(e.styleElement)}),this.partitions=[],this.categoryPartitions.clear(),this.partitionCounter=0,this.classToPartitionMap.clear(),this.classToCategoryPartitionMap.clear()}};function n(e){return e?typeof e==`object`&&typeof e.baseVal==`string`?e.baseVal:e.toString():``}function r(e){return e?n(e).split(/\s+/).filter(Boolean):[]}var i=class{constructor(t,n,r=t=>(0,e.parseClassName)(t).utility?.category){this.observer=null,this.gc=null,this.incrementalParser=t,this.BrowserRuntime=n,this.getCategory=r}setGc(e){this.gc=e}setParser(e){this.incrementalParser=e}observe(e=document.body,t){return typeof window>`u`?new MutationObserver(()=>{}):(this.observer&&this.observer.disconnect(),this.gc?.setRoot(e),this.observer=new MutationObserver(t=>{let n=new Set,i=this.gc;if(t.forEach(t=>{if(i&&(t.type===`attributes`?i.reconcile(t.target):t.type===`childList`&&(t.removedNodes.forEach(e=>i.reconcileTree(e)),t.addedNodes.forEach(e=>i.reconcileTree(e)))),t.type===`attributes`&&t.attributeName===`class`&&e.contains(t.target)){let e=t.target;e.className&&r(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||n.add(e)})}t.type===`childList`&&t.addedNodes.forEach(t=>{if(t.nodeType===Node.ELEMENT_NODE&&e.contains(t)){let e=t;this.processElement(e,n),e.querySelectorAll(`[class]`).forEach(e=>{this.processElement(e,n)})}})}),n.size>0){let e=Array.from(n),t=this.incrementalParser.processClasses(e);this.BrowserRuntime?.applyParseResults(t)}else this.BrowserRuntime?.applyParseResults([]);i?.afterBatch()}),this.observer.observe(e,{attributes:!0,subtree:!0,attributeFilter:[`class`],childList:!0}),t?.scan&&this.scanExistingClasses(e,t),this.observer)}scanExistingClasses(e,t){let n=new Set;e.className&&r(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||n.add(e)});let i=e.querySelectorAll(`[class]`);for(let e of i)if(e.className){let t=r(e.className);for(let e of t)this.incrementalParser.isProcessed(e)||n.add(e)}if(n.size>0){let e=Array.from(n),r=this.incrementalParser.processClasses(e),i=r.filter(e=>this.getCategory(e.cls)===`layout`),a=r.filter(e=>this.getCategory(e.cls)!==`layout`);this.BrowserRuntime?.applyParseResults(i),t?.onReady?.(),this.BrowserRuntime?.applyParseResults(a)}else t?.onReady?.()}processElement(e,t){e.className&&r(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||t.add(e)})}disconnect(){this.observer&&=(this.observer.disconnect(),null)}};function a(e){return e.replace(/\\([0-9a-fA-F]{1,6})\s?|\\(.)/g,(e,t,n)=>t?String.fromCodePoint(parseInt(t,16)):n)}var o=/^\s*(?::(?:where|is)\(\s*)?\.((?:\\[0-9a-fA-F]{1,6}\s?|\\.|[\w-]|[^\x00-\x7F])+)/;function s(e){let t=[],n=0,r=0;for(let i=0;i<e.length;i++){let a=e[i];a===`\\`?i++:a===`(`||a===`[`?n++:a===`)`||a===`]`?n--:a===`,`&&n===0&&(t.push(e.slice(r,i)),r=i+1)}return t.push(e.slice(r)),t}function c(e,t=new Set){for(let n of Array.from(e)){if(n.type===7&&typeof n.name==`string`){t.add(n.name);continue}let e=n.cssRules;e&&e.length&&c(e,t)}return t}function l(e,t=new Set){for(let n of Array.from(e)){let e=n.selectorText;if(typeof e==`string`)for(let n of s(e)){let e=o.exec(n);e&&t.add(a(e[1]))}let r=n.cssRules;r&&r.length&&l(r,t)}return t}var u=class{constructor(e,t,n,r=()=>Date.now()){this.host=e,this.graceMs=t,this.maxRules=n,this.now=r,this.counts=new Map,this.counted=new WeakMap,this.candidates=new Map,this.timer=null,this.root=null}setRoot(e){this.counts.clear(),this.counted=new WeakMap,this.candidates.clear(),this.cancel(),this.root=e,this.reconcileTree(e)}count(e){return this.counts.get(e)??0}reconcileTree(e){if(e.nodeType!==1)return;let t=e;this.reconcile(t),t.querySelectorAll(`[class]`).forEach(e=>this.reconcile(e))}reconcile(e){let t=this.root,n=t&&t.contains(e)?Array.from(new Set(r(e.getAttribute(`class`)))):[],i=this.counted.get(e);if(!i&&n.length===0)return;let a=new Set(i??[]),o=new Set(n);for(let e of o){if(a.has(e))continue;let t=(this.counts.get(e)??0)+1;this.counts.set(e,t),this.candidates.delete(e)}for(let e of a){if(o.has(e))continue;let t=(this.counts.get(e)??0)-1;t>0?this.counts.set(e,t):(this.counts.delete(e),this.candidates.delete(e),this.candidates.set(e,this.now()))}n.length?this.counted.set(e,n):this.counted.delete(e)}afterBatch(){this.candidates.size!==0&&(this.host.cachedCount()>this.maxRules?this.schedule(0):this.schedule(this.graceMs))}schedule(e){if(this.timer!==null){if(e>0)return;clearTimeout(this.timer)}this.timer=setTimeout(()=>{this.timer=null,this.sweep()},e)}sweep(){let e=this.now(),t=Math.max(0,this.host.cachedCount()-this.maxRules),n=[],r=0;for(let[i,a]of this.candidates){let o=e-a>=this.graceMs;!o&&r>=t||(this.candidates.delete(i),!(this.count(i)>0||this.inDom(i)||this.host.isPermanent(i))&&(n.push(i),o||r++))}n.length&&this.host.reclaim(n),this.candidates.size&&this.schedule(this.graceMs)}inDom(e){let t=this.root;if(!t)return!1;let n=t.ownerDocument??document;return t.classList.contains(e)||n.documentElement.classList.contains(e)||n.getElementsByClassName(e).length>0}cancel(){this.timer!==null&&clearTimeout(this.timer),this.timer=null}stats(){return{trackedClasses:this.counts.size,candidates:this.candidates.size}}},d=`style[data-barocss-ssr]`,f=`@layer theme, base, components, utilities;`,p=class{constructor(n={}){this.cache=new Map,this.rootCache=new Set,this.isDestroyed=!1,this.existing=null,this.existingKeyframes=new Set,this.existingSheetCount=-1,this.pinned=new Set,this.gc=null,this.reclaimedCount=0,this.ssrRules=[],this.ssrClasses=new Set,this.observedOnce=!1,this.getCategory=t=>(0,e.parseClassName)(t,this.context).utility?.category;let r={};this.options={config:n.config||r,styleId:n.styleId||`barocss-runtime`,insertionPoint:n.insertionPoint||`head`,maxRulesPerPartition:n.maxRulesPerPartition||50,skipExisting:n.skipExisting??!1,gc:n.gc??!0,gcGraceMs:n.gcGraceMs??3e3,maxRules:n.maxRules??1/0},this.context=(0,e.createContext)(this.options.config),this.incrementalParser=new e.IncrementalParser(this.context),this.changeDetector=new i(this.incrementalParser,this,this.getCategory),this.stylePartitionManager=new t(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.options.gc&&(this.gc=new u({reclaim:e=>this.reclaim(e),isPermanent:e=>this.isPermanent(e),cachedCount:()=>this.cache.size},this.options.gcGraceMs,this.options.maxRules),this.changeDetector.setGc(this.gc)),this.init()}debugLog(t,n,r){(0,e.isDebug)()&&(console[t]||console.log)(`[BrowserRuntime:${t.toUpperCase()}] ${n}`,r||``)}init(){this.debugLog(`debug`,`init`),this.injectPreflightCSS(),this.ensureCssVars(),this.adoptSsrSheets()}adoptSsrSheets(){if(typeof document>`u`)return;let e=[];if(document.head){for(let t of Array.from(document.head.querySelectorAll(`${d}:not([data-barocss-adopted])`))){let n=t.sheet;if(!n)continue;t.setAttribute(`data-barocss-adopted`,``);let r=[];for(let e=n.cssRules.length-1;e>=0;e--){let t=n.cssRules[e],i=l([t]);i.size!==0&&(i.forEach(e=>this.ssrClasses.add(e)),r.unshift({css:t.cssText,cls:i.values().next().value}),n.deleteRule(e))}e.push(...r)}e.length!==0&&(this.ssrRules.push(...e),this.insertSsrRules(e))}}insertSsrRules(e){for(let{css:t,cls:n}of e){let e=this.getCategory(n);e?this.stylePartitionManager.addCategoryRule(t,e):this.stylePartitionManager.addRule(t)}}injectPreflightCSS(){let e=this.options.config.preflight??!0;if(e){let t=this.context.getPreflightCSS(e);this.stylePartitionManager.updateRuleContent(`preflight`,`${f}\n@layer base {\n${t}\n}`,!0)}}ensureCssVars(){if(this.isDestroyed)return;let e=this.context.themeToCssVars?this.context.themeToCssVars():`:root { /* CSS Variables will be generated here */ }`;this.stylePartitionManager.updateRuleContent(`css-vars`,e)}getInsertionPoint(){if(typeof this.options.insertionPoint!=`string`)return this.options.insertionPoint;switch(this.options.insertionPoint){case`body`:return document.body||document.head;default:return document.head}}addClass(e){if(this.isDestroyed)return;let t=this.normalizeClasses(e).filter(Boolean);t.forEach(e=>this.pinned.add(e)),this.processClasses(t)}processClasses(e){this.processClassesIncremental(e)}processClassesIncremental(e){let t=typeof window<`u`,n=this.incrementalParser.processClasses(e);this.debugLog(`debug`,`results`,{results:n,classList:e}),this.applyParseResults(n,{isBrowser:t})}applyParseResults(e,t){if(this.isDestroyed)return;if(this.getInsertionPoint().isConnected&&this.stylePartitionManager.hasDetachedPartitions()){let t=Array.from(this.cache.values());this.reset(),e=[...t,...e],e.forEach(e=>this.incrementalParser.markProcessed(e.cls))}if(this.ssrClasses.size>0&&(e=e.filter(e=>!this.ssrClasses.has(e.cls))),this.options.skipExisting&&e.length>0&&typeof document<`u`){let t=this.getExistingClasses();e=e.filter(e=>!t.has(e.cls))}if(e.length===0)return;let n=[],r=[],i=this.options.skipExisting&&typeof document<`u`?(this.getExistingClasses(),this.existingKeyframes):null;for(let t of e)if(t.css&&Array.isArray(t.cssList)&&(n.push(t),this.cache.set(t.cls,t)),t.rootCss&&Array.isArray(t.rootCssList))for(let e of t.rootCssList){if(i?.size){let t=/^\s*@keyframes\s+([^\s{]+)/.exec(e)?.[1];if(t&&i.has(t))continue}this.rootCache.has(e)||(this.rootCache.add(e),r.push(e))}r.length>0&&this.stylePartitionManager.addRootRules(r.filter(Boolean)),n.length>0&&this.stylePartitionManager.addRules(n),this.debugLog(`info`,`Applied ${e.length} parser results`,{cssRuleCount:n.length,rootCssCount:r.length})}isPermanent(e){return this.pinned.has(e)||this.ssrClasses.has(e)||typeof document>`u`?!0:this.getExistingClasses().has(e)}reclaim(e){if(this.isDestroyed)return;let t=e.filter(e=>this.cache.has(e));if(t.length===0)return;let n=t.map(e=>this.cache.get(e));t.forEach(e=>{this.cache.delete(e),this.incrementalParser.unmarkProcessed(e)});let r=new Set;for(let e of this.cache.values())e.cssList.forEach(e=>r.add(e));for(let e of n){let t=this.getCategory(e.cls);for(let n of e.cssList)r.has(n)||this.stylePartitionManager.removeRule(n,t)}this.reclaimedCount+=t.length}getExistingClasses(){let e=new Set(Array.from(document.querySelectorAll(`style[data-barocss]`),e=>e.sheet)),t=Array.from(document.styleSheets).filter(t=>{if(e.has(t))return!1;let n=t.ownerNode;return!(n&&typeof n.hasAttribute==`function`&&(n.hasAttribute(`data-barocss`)||(n.id||``).startsWith(this.options.styleId)))});if(this.existing&&t.length===this.existingSheetCount)return this.existing;let n=new Set,r=new Set;for(let e of t){let t;try{t=e.cssRules}catch{continue}l(t,n),c(t,r)}return this.existingKeyframes=r,this.existing=n,this.existingSheetCount=t.length,n}observe(e=document.body,t){return this.observedOnce||(this.observedOnce=!0,this.adoptSsrSheets()),this.changeDetector.observe(e,t)}normalizeClasses(e){return Array.isArray(e)?e.flatMap(e=>e.split(/\s+/)):e.split(/\s+/)}has(e){return this.cache.has(e)}getCss(e){return this.cache.get(e)?.cssList.join(`
7
+ `:``,t?this.classToCategoryPartitionMap.delete(e):this.classToPartitionMap.delete(e),!0}get ruleCount(){return this.classToPartitionMap.size+this.classToCategoryPartitionMap.size}findRulePartition(e){let t=this.classToPartitionMap.get(e);if(t!==void 0&&this.partitions[t])return this.partitions[t];let n=this.classToCategoryPartitionMap.get(e);return n===void 0?null:this.categoryPartitions.get(n)||null}updateRuleContent(t,n,r=!1){let i=this.getCategoryPartition(t);if(i)i.styleElement.textContent=n;else{let i=this.createNewCategoryPartition(t,r);(0,e.isDebug)()&&console.log(`[StylePartitionManager] Created new partition for category: ${t}`),i.styleElement.textContent=n}}cleanup(){this.partitions.forEach(e=>{e.styleElement.parentNode&&e.styleElement.parentNode.removeChild(e.styleElement)}),this.categoryPartitions.forEach(e=>{e.styleElement.parentNode&&e.styleElement.parentNode.removeChild(e.styleElement)}),this.partitions=[],this.categoryPartitions.clear(),this.partitionCounter=0,this.classToPartitionMap.clear(),this.classToCategoryPartitionMap.clear()}};function n(e){return e?typeof e==`object`&&typeof e.baseVal==`string`?e.baseVal:e.toString():``}function r(e){return e?n(e).split(/\s+/).filter(Boolean):[]}var i=class{constructor(t,n,r=t=>(0,e.parseClassName)(t).utility?.category){this.observer=null,this.gc=null,this.incrementalParser=t,this.BrowserRuntime=n,this.getCategory=r}setGc(e){this.gc=e}setParser(e){this.incrementalParser=e}observe(e=document.body,t){return typeof window>`u`?new MutationObserver(()=>{}):(this.observer&&this.observer.disconnect(),this.gc?.setRoot(e),this.observer=new MutationObserver(t=>{let n=new Set,i=this.gc;if(t.forEach(t=>{if(i&&(t.type===`attributes`?i.reconcile(t.target):t.type===`childList`&&(t.removedNodes.forEach(e=>i.reconcileTree(e)),t.addedNodes.forEach(e=>i.reconcileTree(e)))),t.type===`attributes`&&t.attributeName===`class`&&e.contains(t.target)){let e=t.target;e.className&&r(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||n.add(e)})}t.type===`childList`&&t.addedNodes.forEach(t=>{if(t.nodeType===Node.ELEMENT_NODE&&e.contains(t)){let e=t;this.processElement(e,n),e.querySelectorAll(`[class]`).forEach(e=>{this.processElement(e,n)})}})}),n.size>0){let e=Array.from(n),t=this.incrementalParser.processClasses(e);this.BrowserRuntime?.applyParseResults(t)}else this.BrowserRuntime?.applyParseResults([]);i?.afterBatch()}),this.observer.observe(e,{attributes:!0,subtree:!0,attributeFilter:[`class`],childList:!0}),t?.scan&&this.scanExistingClasses(e,t),this.observer)}scanExistingClasses(e,t){let n=new Set,i=e.className;i&&r(i).forEach(e=>{this.incrementalParser.isProcessed(e)||n.add(e)});let a=e.querySelectorAll(`[class]`);for(let e of a)if(e.className){let t=r(e.className);for(let e of t)this.incrementalParser.isProcessed(e)||n.add(e)}if(n.size>0){let e=Array.from(n),r=this.incrementalParser.processClasses(e),i=r.filter(e=>this.getCategory(e.cls)===`layout`),a=r.filter(e=>this.getCategory(e.cls)!==`layout`);this.BrowserRuntime?.applyParseResults(i),t?.onReady?.(),this.BrowserRuntime?.applyParseResults(a)}else t?.onReady?.()}processElement(e,t){e.className&&r(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||t.add(e)})}disconnect(){this.observer&&=(this.observer.disconnect(),null)}};function a(e){return e.replace(/\\([0-9a-fA-F]{1,6})\s?|\\(.)/g,(e,t,n)=>t?String.fromCodePoint(parseInt(t,16)):n)}var o=/^\s*(?::(?:where|is)\(\s*)?\.((?:\\[0-9a-fA-F]{1,6}\s?|\\.|[\w-]|[^\x00-\x7F])+)/;function s(e){let t=[],n=0,r=0;for(let i=0;i<e.length;i++){let a=e[i];a===`\\`?i++:a===`(`||a===`[`?n++:a===`)`||a===`]`?n--:a===`,`&&n===0&&(t.push(e.slice(r,i)),r=i+1)}return t.push(e.slice(r)),t}function c(e,t=new Set){for(let n of Array.from(e)){if(n.type===7&&typeof n.name==`string`){t.add(n.name);continue}let e=n.cssRules;e&&e.length&&c(e,t)}return t}function l(e,t=new Set){for(let n of Array.from(e)){let e=n.selectorText;if(typeof e==`string`)for(let n of s(e)){let e=o.exec(n);e&&t.add(a(e[1]))}let r=n.cssRules;r&&r.length&&l(r,t)}return t}var u=class{constructor(e,t,n,r=()=>Date.now()){this.host=e,this.graceMs=t,this.maxRules=n,this.now=r,this.counts=new Map,this.counted=new WeakMap,this.candidates=new Map,this.timer=null,this.root=null}setRoot(e){this.counts.clear(),this.counted=new WeakMap,this.candidates.clear(),this.cancel(),this.root=e,e.nodeType===1?this.reconcileTree(e):e.querySelectorAll(`[class]`).forEach(e=>this.reconcile(e))}count(e){return this.counts.get(e)??0}reconcileTree(e){if(e.nodeType!==1)return;let t=e;this.reconcile(t),t.querySelectorAll(`[class]`).forEach(e=>this.reconcile(e))}reconcile(e){let t=this.root,n=t&&t.contains(e)?Array.from(new Set(r(e.getAttribute(`class`)))):[],i=this.counted.get(e);if(!i&&n.length===0)return;let a=new Set(i??[]),o=new Set(n);for(let e of o){if(a.has(e))continue;let t=(this.counts.get(e)??0)+1;this.counts.set(e,t),this.candidates.delete(e)}for(let e of a){if(o.has(e))continue;let t=(this.counts.get(e)??0)-1;t>0?this.counts.set(e,t):(this.counts.delete(e),this.candidates.delete(e),this.candidates.set(e,this.now()))}n.length?this.counted.set(e,n):this.counted.delete(e)}afterBatch(){this.candidates.size!==0&&(this.host.cachedCount()>this.maxRules?this.schedule(0):this.schedule(this.graceMs))}schedule(e){if(this.timer!==null){if(e>0)return;clearTimeout(this.timer)}this.timer=setTimeout(()=>{this.timer=null,this.sweep()},e)}sweep(){let e=this.now(),t=Math.max(0,this.host.cachedCount()-this.maxRules),n=[],r=0;for(let[i,a]of this.candidates){let o=e-a>=this.graceMs;!o&&r>=t||(this.candidates.delete(i),!(this.count(i)>0||this.inDom(i)||this.host.isPermanent(i))&&(n.push(i),o||r++))}n.length&&this.host.reclaim(n),this.candidates.size&&this.schedule(this.graceMs)}inDom(e){let t=this.root;if(!t)return!1;if(t.nodeType!==1)return Array.from(t.querySelectorAll(`[class]`)).some(t=>t.classList.contains(e));let n=t.ownerDocument??document;return t.classList.contains(e)||n.documentElement.classList.contains(e)||n.getElementsByClassName(e).length>0}cancel(){this.timer!==null&&clearTimeout(this.timer),this.timer=null}stats(){return{trackedClasses:this.counts.size,candidates:this.candidates.size}}};function d(e){let t=[],n=e.replace(/\/\*[\s\S]*?\*\//g,``);return n=n.replace(/(?<=^|[{};])(\s*)([^{};@\s][^{};]*?)\{([^{}]*)\}/g,(e,n,r,i)=>{let a=r.split(`,`).map(e=>e.trim()).filter(Boolean);if(a.includes(`body`))for(let e of i.split(`;`).map(e=>e.trim()).filter(Boolean))/^(min-height|scroll-behavior)\s*:/.test(e)||t.push(e);let o=Array.from(new Set(a.filter(e=>e!==`body`).map(e=>e===`html`||e===`:root`?`:host`:e)));return o.length===0?``:`${n}${o.join(`, `)} {${i}}`}),t.length&&(n+=`\n@layer base {\n:host {\n ${t.join(`;
8
+ `)};\n}\n}\n`),n}function f(e){return JSON.stringify(e,(e,t)=>typeof t==`function`?`fn:${String(t)}`:t instanceof RegExp?`re:${t}`:t)??``}function p(e){let t=2166136261;for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t=Math.imul(t,16777619);return(t>>>0).toString(36)}function m(){try{return typeof ShadowRoot<`u`&&`adoptedStyleSheets`in ShadowRoot.prototype&&typeof CSSStyleSheet==`function`&&typeof new CSSStyleSheet().replaceSync==`function`}catch{return!1}}var h=e=>e.replace(/\\\//g,`\\/`),g=class{constructor(t,n,r=t){this.results=new Map,this.generations=0,this.prologueSheet=null,this.rulesSheet=null,this.prologue={preflight:``,vars:``},this.segments=new Map([[`root`,{rules:[],keys:[]}],[``,{rules:[],keys:[]}]]),this.refs=new Map,this.attached=[],this.key=t,this.fullKey=r,this.context=(0,e.createContext)(n),this.constructable=m(),this.constructable&&(this.prologueSheet=new CSSStyleSheet,this.rulesSheet=new CSSStyleSheet)}get rootCount(){return this.attached.length}get ruleCount(){return this.refs.size}attach(e){if(this.attached.some(t=>t.root===e))return;if(this.prologueSheet&&this.rulesSheet){let t=[this.prologueSheet,this.rulesSheet];e.adoptedStyleSheets=[...e.adoptedStyleSheets.filter(e=>!t.includes(e)),...t],this.attached.push({root:e});return}let t=e.ownerDocument??document,n=t.createElement(`style`),r=t.createElement(`style`);n.setAttribute(`data-barocss`,`root-prologue`),r.setAttribute(`data-barocss`,`root`),n.textContent=this.prologueText(),r.textContent=this.allRules().join(`
9
+ `),e.insertBefore(r,e.firstChild),e.insertBefore(n,r),this.attached.push({root:e,styles:[n,r]})}detach(e){let t=this.attached.findIndex(t=>t.root===e);if(t===-1)return;let[n]=this.attached.splice(t,1);n.styles?n.styles.forEach(e=>e.remove()):e.adoptedStyleSheets&&=e.adoptedStyleSheets.filter(e=>e!==this.prologueSheet&&e!==this.rulesSheet)}setPrologue(e,t){if(this.prologue[e]===t)return;this.prologue[e]=t;let n=this.prologueText();this.prologueSheet&&this.prologueSheet.replaceSync(n);for(let e of this.attached)e.styles&&(e.styles[0].textContent=n)}prologueText(){return[this.prologue.preflight,this.prologue.vars].filter(Boolean).join(`
10
+ `)}allRules(){return Array.from(this.segments.values()).flatMap(e=>e.rules)}offset(e){let t=0;for(let n of this.segments.values()){if(n===e)return t;t+=n.rules.length}return t}retain(t,n){let r=this.refs.get(t);if(r!==void 0)return this.refs.set(t,r+1),!0;let i=this.segments.get(n);i||(i={rules:[],keys:[]},this.segments.set(n,i));let a=n===`root`,o=(0,e.ruleSortKey)(t),s=a?i.rules.length:(0,e.upperBound)(i.keys,o),c=this.offset(i)+s;try{this.insertAt(t,c)}catch(n){return(0,e.isDebug)()&&console.warn(`[BrowserRuntime] Failed to insert rule into shadow root sheet: ${t}`,n),!1}return i.rules.splice(s,0,t),i.keys.splice(s,0,o),this.refs.set(t,1),!0}release(e){let t=this.refs.get(e);if(t!==void 0){if(t>1){this.refs.set(e,t-1);return}this.refs.delete(e);for(let t of this.segments.values()){let n=t.rules.indexOf(e);if(n===-1)continue;let r=this.offset(t)+n;t.rules.splice(n,1),t.keys.splice(n,1),this.deleteAt(r);return}}}insertAt(e,t){let n=h(e);if(this.rulesSheet){this.rulesSheet.insertRule(n,t);return}let r=this.allRules().length;for(let i of this.attached){let a=i.styles[1],o=a.sheet;if(o&&o.cssRules.length===r)o.insertRule(n,t);else{let n=this.allRules();n.splice(t,0,e),a.textContent=n.join(`
11
+ `)}}}deleteAt(e){if(this.rulesSheet){this.rulesSheet.deleteRule(e);return}let t=this.allRules().length;for(let n of this.attached){let r=n.styles[1],i=r.sheet;i&&i.cssRules.length===t+1?i.deleteRule(e):r.textContent=this.allRules().join(`
12
+ `)}}cssText(){return[this.prologueText(),...this.allRules()].join(`
13
+ `)}},_=new Map;function v(e,t=p){let n=f(e),r=_.get(n);return r||(r=new g(t(n),e,n),_.set(n,r)),r}function y(e){e.rootCount===0&&_.get(e.fullKey)===e&&_.delete(e.fullKey)}var b=class extends e.IncrementalParser{constructor(e){super(e.context),this.shared=e}processClass(e){if(this.isProcessed(e))return null;let t=this.shared.results.get(e);if(t)return this.markProcessed(e),t;let n=super.processClass(e);return n&&(this.shared.results.set(e,n),this.shared.generations++),n}},x=class{constructor(t,n,r=t=>(0,e.parseClassName)(t).utility?.category){this.shared=t,this.root=n,this.getCategory=r,this.owned=new Set,t.attach(n)}hasDetachedPartitions(){return!1}updateRuleContent(e,t){e===`preflight`?this.shared.setPrologue(`preflight`,d(t)):e===`css-vars`&&this.shared.setPrologue(`vars`,t)}take(e,t){return this.owned.has(e)||!this.shared.retain(e,t)?!1:(this.owned.add(e),!0)}addRule(e){return this.take(e,``)}addCategoryRule(e,t){return this.take(e,`c:${t}`)}addRootRules(e){let t=0;for(let n of e)this.take(n,`root`)&&t++;return{success:t,failed:e.length-t}}addRules(e){let t=0,n=0;for(let r of e){let e=this.getCategory(r.cls);for(let i of r.cssList)(e?this.addCategoryRule(i,e):this.addRule(i))?t++:n++}return{success:t,failed:n}}removeRule(e,t){return this.owned.delete(e)?(this.shared.release(e),!0):!1}get ruleCount(){return this.owned.size}cleanup(){this.owned.forEach(e=>this.shared.release(e)),this.owned.clear(),this.shared.detach(this.root),y(this.shared)}},S=`style[data-barocss-ssr]`,C=`@layer theme, base, components, utilities;`,w=class{constructor(t={}){this.cache=new Map,this.rootCache=new Set,this.shadowRoot=null,this.isDestroyed=!1,this.existing=null,this.existingKeyframes=new Set,this.existingSheetCount=-1,this.pinned=new Set,this.gc=null,this.reclaimedCount=0,this.ssrRules=[],this.ssrClasses=new Set,this.observedOnce=!1,this.getCategory=t=>(0,e.parseClassName)(t,this.context).utility?.category;let n={};this.options={config:t.config||n,styleId:t.styleId||`barocss-runtime`,insertionPoint:t.insertionPoint||`head`,maxRulesPerPartition:t.maxRulesPerPartition||50,skipExisting:t.skipExisting??!1,gc:t.gc??!0,gcGraceMs:t.gcGraceMs??3e3,maxRules:t.maxRules??1/0};let r=t.root;if(r&&r.nodeType===11&&(this.shadowRoot=r),this.shadowRoot){let e=v(this.options.config);this.context=e.context,this.incrementalParser=new b(e)}else this.context=(0,e.createContext)(this.options.config),this.incrementalParser=new e.IncrementalParser(this.context);this.changeDetector=new i(this.incrementalParser,this,this.getCategory),this.stylePartitionManager=this.createStyles(),this.options.gc&&(this.gc=new u({reclaim:e=>this.reclaim(e),isPermanent:e=>this.isPermanent(e),cachedCount:()=>this.cache.size},this.options.gcGraceMs,this.options.maxRules),this.changeDetector.setGc(this.gc)),this.init(),this.shadowRoot&&this.observe(this.shadowRoot,{scan:!0})}createStyles(){if(this.shadowRoot){let e=this.incrementalParser.shared,t=v(this.options.config);return t!==e&&(e=t,this.context=e.context,this.incrementalParser=new b(e),this.changeDetector?.setParser(this.incrementalParser)),new x(e,this.shadowRoot,this.getCategory)}return new t(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory)}debugLog(t,n,r){(0,e.isDebug)()&&(console[t]||console.log)(`[BrowserRuntime:${t.toUpperCase()}] ${n}`,r||``)}init(){this.debugLog(`debug`,`init`),this.injectPreflightCSS(),this.ensureCssVars(),this.adoptSsrSheets()}adoptSsrSheets(){if(typeof document>`u`||this.shadowRoot)return;let e=[];if(document.head){for(let t of Array.from(document.head.querySelectorAll(`${S}:not([data-barocss-adopted])`))){let n=t.sheet;if(!n)continue;t.setAttribute(`data-barocss-adopted`,``);let r=[];for(let e=n.cssRules.length-1;e>=0;e--){let t=n.cssRules[e],i=l([t]);i.size!==0&&(i.forEach(e=>this.ssrClasses.add(e)),r.unshift({css:t.cssText,cls:i.values().next().value}),n.deleteRule(e))}e.push(...r)}e.length!==0&&(this.ssrRules.push(...e),this.insertSsrRules(e))}}insertSsrRules(e){for(let{css:t,cls:n}of e){let e=this.getCategory(n);e?this.stylePartitionManager.addCategoryRule(t,e):this.stylePartitionManager.addRule(t)}}injectPreflightCSS(){let e=this.options.config.preflight??!0;if(e){let t=this.context.getPreflightCSS(e);this.stylePartitionManager.updateRuleContent(`preflight`,`${C}\n@layer base {\n${t}\n}`,!0)}}ensureCssVars(){if(this.isDestroyed)return;let e=this.context.themeToCssVars?this.context.themeToCssVars():`:root { /* CSS Variables will be generated here */ }`;this.stylePartitionManager.updateRuleContent(`css-vars`,e)}getInsertionPoint(){if(typeof this.options.insertionPoint!=`string`)return this.options.insertionPoint;switch(this.options.insertionPoint){case`body`:return document.body||document.head;default:return document.head}}addClass(e){if(this.isDestroyed)return;let t=this.normalizeClasses(e).filter(Boolean);t.forEach(e=>this.pinned.add(e)),this.processClasses(t)}processClasses(e){this.processClassesIncremental(e)}processClassesIncremental(e){let t=typeof window<`u`,n=this.incrementalParser.processClasses(e);this.debugLog(`debug`,`results`,{results:n,classList:e}),this.applyParseResults(n,{isBrowser:t})}applyParseResults(e,t){if(this.isDestroyed)return;if(!this.shadowRoot&&this.getInsertionPoint().isConnected&&this.stylePartitionManager.hasDetachedPartitions()){let t=Array.from(this.cache.values());this.reset(),e=[...t,...e],e.forEach(e=>this.incrementalParser.markProcessed(e.cls))}if(this.ssrClasses.size>0&&(e=e.filter(e=>!this.ssrClasses.has(e.cls))),this.options.skipExisting&&e.length>0&&typeof document<`u`){let t=this.getExistingClasses();e=e.filter(e=>!t.has(e.cls))}if(e.length===0)return;let n=[],r=[],i=this.options.skipExisting&&typeof document<`u`?(this.getExistingClasses(),this.existingKeyframes):null;for(let t of e)if(t.css&&Array.isArray(t.cssList)&&(n.push(t),this.cache.set(t.cls,t)),t.rootCss&&Array.isArray(t.rootCssList))for(let e of t.rootCssList){if(i?.size){let t=/^\s*@keyframes\s+([^\s{]+)/.exec(e)?.[1];if(t&&i.has(t))continue}this.rootCache.has(e)||(this.rootCache.add(e),r.push(e))}r.length>0&&this.stylePartitionManager.addRootRules(r.filter(Boolean)),n.length>0&&this.stylePartitionManager.addRules(n),this.debugLog(`info`,`Applied ${e.length} parser results`,{cssRuleCount:n.length,rootCssCount:r.length})}isPermanent(e){return this.pinned.has(e)||this.ssrClasses.has(e)?!0:this.shadowRoot?!1:typeof document>`u`||this.getExistingClasses().has(e)}reclaim(e){if(this.isDestroyed)return;let t=e.filter(e=>this.cache.has(e));if(t.length===0)return;let n=t.map(e=>this.cache.get(e));t.forEach(e=>{this.cache.delete(e),this.incrementalParser.unmarkProcessed(e)});let r=new Set;for(let e of this.cache.values())e.cssList.forEach(e=>r.add(e));for(let e of n){let t=this.getCategory(e.cls);for(let n of e.cssList)r.has(n)||this.stylePartitionManager.removeRule(n,t)}this.reclaimedCount+=t.length}getExistingClasses(){let e=new Set(Array.from(document.querySelectorAll(`style[data-barocss]`),e=>e.sheet)),t=Array.from(document.styleSheets).filter(t=>{if(e.has(t))return!1;let n=t.ownerNode;return!(n&&typeof n.hasAttribute==`function`&&(n.hasAttribute(`data-barocss`)||(n.id||``).startsWith(this.options.styleId)))});if(this.existing&&t.length===this.existingSheetCount)return this.existing;let n=new Set,r=new Set;for(let e of t){let t;try{t=e.cssRules}catch{continue}l(t,n),c(t,r)}return this.existingKeyframes=r,this.existing=n,this.existingSheetCount=t.length,n}observe(e=this.shadowRoot??document.body,t){return this.observedOnce||(this.observedOnce=!0,this.adoptSsrSheets()),this.changeDetector.observe(e,t)}normalizeClasses(e){return Array.isArray(e)?e.flatMap(e=>e.split(/\s+/)):e.split(/\s+/)}has(e){return this.cache.has(e)}getCss(e){return this.cache.get(e)?.cssList.join(`
8
14
  `)}getAllCss(){return[...this.rootCache,...Array.from(this.cache.values()).flatMap(e=>e.cssList)].join(`
9
- `)}getClasses(){return Array.from(this.cache.keys())}getCacheStats(){let e=this.incrementalParser.getStats();return{runtime:{cachedClasses:this.cache.size,rootCacheSize:this.rootCache.size,ruleCount:this.stylePartitionManager.ruleCount,reclaimedClasses:this.reclaimedCount,gc:this.gc?.stats()??null},ast:e.cacheStats.ast,incremental:e}}clearCaches(){this.isDestroyed||(this.cache.clear(),this.rootCache.clear(),(0,e.clearAstCache)(this.context),this.incrementalParser.clearProcessed(),this.stylePartitionManager.cleanup(),this.stylePartitionManager=new t(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.injectPreflightCSS(),this.ensureCssVars(),this.insertSsrRules(this.ssrRules))}reset(){this.isDestroyed||(this.cache.clear(),this.rootCache.clear(),this.incrementalParser.clearProcessed(),this.stylePartitionManager.cleanup(),this.stylePartitionManager=new t(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.injectPreflightCSS(),this.ensureCssVars(),this.insertSsrRules(this.ssrRules))}updateConfig(t){if(this.isDestroyed)return;let n=Array.from(this.cache.keys());this.options.config=t,this.context=(0,e.createContext)(t),this.incrementalParser=new e.IncrementalParser(this.context),this.changeDetector.setParser(this.incrementalParser),this.reset(),n.length>0&&this.addClass(n)}removeClass(e){if(this.isDestroyed)return;let t=new Set(this.normalizeClasses(e)),n=Array.from(this.cache.values()).filter(e=>!t.has(e.cls));n.length!==this.cache.size&&(this.reset(),n.forEach(e=>this.incrementalParser.markProcessed(e.cls)),this.applyParseResults(n))}destroy(){this.isDestroyed||=(this.changeDetector.disconnect(),this.gc?.cancel(),this.stylePartitionManager.cleanup(),this.cache.clear(),this.rootCache.clear(),!0)}getStats(){return{cachedClasses:this.cache.size,styleElementId:this.options.styleId,isDestroyed:this.isDestroyed,config:this.options.config,cacheStats:this.getCacheStats()}}},m=null,h;function g(e={}){return!m||m.getStats().isDestroyed?(m=new p(e),h=e.config):e.config&&e.config!==h&&(m.updateConfig(e.config),h=e.config),m}function _({loadingClassName:e=`baro-boot`,...t}={}){if(!document.body){document.addEventListener(`DOMContentLoaded`,()=>_({loadingClassName:e,...t}),{once:!0});return}let n=`${e}-doing`,r=`${e}-done`;try{document.body.classList.add(n),g(t).observe(document.body,{scan:!0,onReady:()=>{document.body.classList.remove(n),document.body.classList.add(r)}})}catch(e){document.body?.classList.remove(n),console.error(`BaroCSS boot failed:`,e)}}var v=_;function y(e){if(!e||typeof e!=`object`||Array.isArray(e))return[];let t=e.elements;if(!t||typeof t!=`object`||Array.isArray(t))return[];let n=new Set;for(let e of Object.keys(t)){let r=t[e];if(!r||typeof r!=`object`||Array.isArray(r))continue;let i=r.props;if(!i||typeof i!=`object`||Array.isArray(i))continue;let a=i.className;if(typeof a==`string`)for(let e of a.split(/\s+/))e&&n.add(e)}return Array.from(n)}function b(e,t){let n=y(e);n.length>0&&t.addClass(n)}var x={colors:Object.fromEntries(`background.foreground.card.card-foreground.popover.popover-foreground.primary.primary-foreground.secondary.secondary-foreground.muted.muted-foreground.accent.accent-foreground.destructive.border.input.ring.chart-1.chart-2.chart-3.chart-4.chart-5.sidebar.sidebar-foreground.sidebar-primary.sidebar-primary-foreground.sidebar-accent.sidebar-accent-foreground.sidebar-border.sidebar-ring`.split(`.`).map(e=>[e,`var(--${e})`])),borderRadius:{sm:`calc(var(--radius) - 4px)`,md:`calc(var(--radius) - 2px)`,lg:`var(--radius)`,xl:`calc(var(--radius) + 4px)`}};exports.BrowserRuntime=p,exports.ChangeDetector=i,exports.LAYER_ORDER=f,exports.SSR_STYLE_SELECTOR=d,exports.StylePartitionManager=t,exports.baroBoot=_,exports.baroStart=v,exports.collectJsonRenderClassNames=y,exports.getRuntime=g,exports.normalizeClassName=n,exports.normalizeClassNameList=r,exports.preloadJsonRenderClasses=b,exports.shadcnTheme=x;
15
+ `)}getClasses(){return Array.from(this.cache.keys())}getCacheStats(){let e=this.incrementalParser.getStats();return{runtime:{cachedClasses:this.cache.size,rootCacheSize:this.rootCache.size,ruleCount:this.stylePartitionManager.ruleCount,reclaimedClasses:this.reclaimedCount,gc:this.gc?.stats()??null},ast:e.cacheStats.ast,incremental:e}}clearCaches(){this.isDestroyed||(this.cache.clear(),this.rootCache.clear(),(0,e.clearAstCache)(this.context),this.incrementalParser.clearProcessed(),this.stylePartitionManager.cleanup(),this.stylePartitionManager=this.createStyles(),this.injectPreflightCSS(),this.ensureCssVars(),this.insertSsrRules(this.ssrRules))}reset(){this.isDestroyed||(this.cache.clear(),this.rootCache.clear(),this.incrementalParser.clearProcessed(),this.stylePartitionManager.cleanup(),this.stylePartitionManager=this.createStyles(),this.injectPreflightCSS(),this.ensureCssVars(),this.insertSsrRules(this.ssrRules))}updateConfig(t){if(this.isDestroyed)return;let n=Array.from(this.cache.keys());if(this.options.config=t,this.shadowRoot){this.stylePartitionManager.cleanup();let e=v(t);this.context=e.context,this.incrementalParser=new b(e)}else this.context=(0,e.createContext)(t),this.incrementalParser=new e.IncrementalParser(this.context);this.changeDetector.setParser(this.incrementalParser),this.reset(),n.length>0&&this.addClass(n)}removeClass(e){if(this.isDestroyed)return;let t=new Set(this.normalizeClasses(e)),n=Array.from(this.cache.values()).filter(e=>!t.has(e.cls));n.length!==this.cache.size&&(this.reset(),n.forEach(e=>this.incrementalParser.markProcessed(e.cls)),this.applyParseResults(n))}destroy(){this.isDestroyed||=(this.changeDetector.disconnect(),this.gc?.cancel(),this.stylePartitionManager.cleanup(),this.cache.clear(),this.rootCache.clear(),!0)}getStats(){return{cachedClasses:this.cache.size,styleElementId:this.options.styleId,isDestroyed:this.isDestroyed,config:this.options.config,cacheStats:this.getCacheStats(),sharedSheet:this.shadowRoot&&this.stylePartitionManager instanceof x?{roots:this.stylePartitionManager.shared.rootCount,rules:this.stylePartitionManager.shared.ruleCount,generations:this.stylePartitionManager.shared.generations,constructable:this.stylePartitionManager.shared.constructable}:null}}},T=null,E;function D(e={}){return!T||T.getStats().isDestroyed?(T=new w(e),E=e.config):e.config&&e.config!==E&&(T.updateConfig(e.config),E=e.config),T}function O({loadingClassName:e=`baro-boot`,...t}={}){if(t.root&&t.root.nodeType===11)return new w(t);if(!document.body){document.addEventListener(`DOMContentLoaded`,()=>O({loadingClassName:e,...t}),{once:!0});return}let n=`${e}-doing`,r=`${e}-done`;try{document.body.classList.add(n),D(t).observe(document.body,{scan:!0,onReady:()=>{document.body.classList.remove(n),document.body.classList.add(r)}})}catch(e){document.body?.classList.remove(n),console.error(`BaroCSS boot failed:`,e)}}var k=O;function A(e){if(!e||typeof e!=`object`||Array.isArray(e))return[];let t=e.elements;if(!t||typeof t!=`object`||Array.isArray(t))return[];let n=new Set;for(let e of Object.keys(t)){let r=t[e];if(!r||typeof r!=`object`||Array.isArray(r))continue;let i=r.props;if(!i||typeof i!=`object`||Array.isArray(i))continue;let a=i.className;if(typeof a==`string`)for(let e of a.split(/\s+/))e&&n.add(e)}return Array.from(n)}function j(e,t){let n=A(e);n.length>0&&t.addClass(n)}var M={colors:Object.fromEntries(`background.foreground.card.card-foreground.popover.popover-foreground.primary.primary-foreground.secondary.secondary-foreground.muted.muted-foreground.accent.accent-foreground.destructive.border.input.ring.chart-1.chart-2.chart-3.chart-4.chart-5.sidebar.sidebar-foreground.sidebar-primary.sidebar-primary-foreground.sidebar-accent.sidebar-accent-foreground.sidebar-border.sidebar-ring`.split(`.`).map(e=>[e,`var(--${e})`])),borderRadius:{sm:`calc(var(--radius) - 4px)`,md:`calc(var(--radius) - 2px)`,lg:`var(--radius)`,xl:`calc(var(--radius) + 4px)`}};exports.BrowserRuntime=w,exports.ChangeDetector=i,exports.LAYER_ORDER=C,exports.SSR_STYLE_SELECTOR=S,exports.StylePartitionManager=t,exports.baroBoot=O,exports.baroStart=k,exports.collectJsonRenderClassNames=A,exports.getRuntime=D,exports.normalizeClassName=n,exports.normalizeClassNameList=r,exports.preloadJsonRenderClasses=j,exports.shadcnTheme=M;
package/dist/index.d.cts CHANGED
@@ -3,7 +3,15 @@ import { GenerateCssRulesResult } from '@barocss/kit';
3
3
  import { IncrementalParser } from '@barocss/kit';
4
4
  import { RuleKey } from '@barocss/kit';
5
5
 
6
- export declare function baroBoot({ loadingClassName, ...options }?: BaroBootOptions): void;
6
+ /**
7
+ * Starts BaroCSS on the document (the shared runtime). #327: with `root: shadowRoot` it instead returns a new
8
+ * runtime that observes and styles only that shadow root (runtimes with the same config share one sheet).
9
+ */
10
+ export declare function baroBoot(options: BaroBootOptions & {
11
+ root: ShadowRoot;
12
+ }): BrowserRuntime;
13
+
14
+ export declare function baroBoot(options?: BaroBootOptions): void;
7
15
 
8
16
  declare type BaroBootOptions = BrowserRuntimeOptions & {
9
17
  loadingClassName?: string;
@@ -16,6 +24,8 @@ export declare class BrowserRuntime {
16
24
  private rootCache;
17
25
  private context;
18
26
  private options;
27
+ /** #327: the shadow root this runtime styles, or null in document mode. */
28
+ private shadowRoot;
19
29
  private isDestroyed;
20
30
  private existing;
21
31
  /** #274: @keyframes names the page's own sheets define (filled with `existing`). */
@@ -34,6 +44,8 @@ export declare class BrowserRuntime {
34
44
  private stylePartitionManager;
35
45
  private getCategory;
36
46
  constructor(options?: BrowserRuntimeOptions);
47
+ /** Document partitions, or (#327) this root's view of the shared shadow-root sheet. */
48
+ private createStyles;
37
49
  /**
38
50
  * Add debug logs (by level)
39
51
  */
@@ -81,7 +93,7 @@ export declare class BrowserRuntime {
81
93
  /**
82
94
  * MutationObserver instance method to automatically call addClass when class attributes change in DOM
83
95
  */
84
- observe(root?: HTMLElement, options?: {
96
+ observe(root?: HTMLElement | ShadowRoot, options?: {
85
97
  scan?: boolean;
86
98
  onReady?: () => void;
87
99
  }): MutationObserver;
@@ -164,6 +176,13 @@ export declare class BrowserRuntime {
164
176
  };
165
177
  };
166
178
  };
179
+ /** #327: the shared shadow-root sheet this runtime uses (null in document mode). */
180
+ sharedSheet: {
181
+ roots: number;
182
+ rules: number;
183
+ generations: number;
184
+ constructable: boolean;
185
+ } | null;
167
186
  };
168
187
  }
169
188
 
@@ -196,6 +215,16 @@ export declare interface BrowserRuntimeOptions {
196
215
  * oldest-first without waiting for the grace period; classes in use are never evicted. Default: no cap.
197
216
  */
198
217
  maxRules?: number;
218
+ /**
219
+ * #327: a ShadowRoot to style instead of the document. The runtime observes that root (with an initial scan)
220
+ * and places all of its CSS in it: utilities, theme variables, @property, @keyframes and a preflight rewritten
221
+ * for the root (`html`/`:root` -> `:host`, `body` declarations re-emitted on `:host`). Nothing goes to
222
+ * `document.head`. Runtimes with the same config share one constructable sheet adopted by every root
223
+ * (`adoptedStyleSheets`); without constructable sheets each root gets `<style>` elements instead.
224
+ * `insertionPoint`, `styleId` and `maxRulesPerPartition` are ignored in this mode, and server sheets (#268)
225
+ * are not adopted. `document` (or omitting it) keeps the document mode.
226
+ */
227
+ root?: ShadowRoot | Document;
199
228
  }
200
229
 
201
230
  /**
@@ -247,7 +276,7 @@ export declare class ChangeDetector {
247
276
  * @param options - Configuration options including initial scan and onReady callback
248
277
  * @returns The MutationObserver instance for external control
249
278
  */
250
- observe(root?: HTMLElement, options?: {
279
+ observe(root?: HTMLElement | ShadowRoot, options?: {
251
280
  scan?: boolean;
252
281
  onReady?: () => void;
253
282
  }): MutationObserver;
@@ -291,7 +320,7 @@ declare class ClassGc {
291
320
  private root;
292
321
  constructor(host: ClassGcHost, graceMs: number, maxRules: number, now?: () => number);
293
322
  /** Start counting for a new root: count every element currently inside it. */
294
- setRoot(root: Element): void;
323
+ setRoot(root: Element | ShadowRoot): void;
295
324
  count(cls: string): number;
296
325
  /** Re-count `el` and (optionally) all its descendants from their current state. */
297
326
  reconcileTree(node: Node): void;
package/dist/index.d.ts CHANGED
@@ -3,7 +3,15 @@ import { GenerateCssRulesResult } from '@barocss/kit';
3
3
  import { IncrementalParser } from '@barocss/kit';
4
4
  import { RuleKey } from '@barocss/kit';
5
5
 
6
- export declare function baroBoot({ loadingClassName, ...options }?: BaroBootOptions): void;
6
+ /**
7
+ * Starts BaroCSS on the document (the shared runtime). #327: with `root: shadowRoot` it instead returns a new
8
+ * runtime that observes and styles only that shadow root (runtimes with the same config share one sheet).
9
+ */
10
+ export declare function baroBoot(options: BaroBootOptions & {
11
+ root: ShadowRoot;
12
+ }): BrowserRuntime;
13
+
14
+ export declare function baroBoot(options?: BaroBootOptions): void;
7
15
 
8
16
  declare type BaroBootOptions = BrowserRuntimeOptions & {
9
17
  loadingClassName?: string;
@@ -16,6 +24,8 @@ export declare class BrowserRuntime {
16
24
  private rootCache;
17
25
  private context;
18
26
  private options;
27
+ /** #327: the shadow root this runtime styles, or null in document mode. */
28
+ private shadowRoot;
19
29
  private isDestroyed;
20
30
  private existing;
21
31
  /** #274: @keyframes names the page's own sheets define (filled with `existing`). */
@@ -34,6 +44,8 @@ export declare class BrowserRuntime {
34
44
  private stylePartitionManager;
35
45
  private getCategory;
36
46
  constructor(options?: BrowserRuntimeOptions);
47
+ /** Document partitions, or (#327) this root's view of the shared shadow-root sheet. */
48
+ private createStyles;
37
49
  /**
38
50
  * Add debug logs (by level)
39
51
  */
@@ -81,7 +93,7 @@ export declare class BrowserRuntime {
81
93
  /**
82
94
  * MutationObserver instance method to automatically call addClass when class attributes change in DOM
83
95
  */
84
- observe(root?: HTMLElement, options?: {
96
+ observe(root?: HTMLElement | ShadowRoot, options?: {
85
97
  scan?: boolean;
86
98
  onReady?: () => void;
87
99
  }): MutationObserver;
@@ -164,6 +176,13 @@ export declare class BrowserRuntime {
164
176
  };
165
177
  };
166
178
  };
179
+ /** #327: the shared shadow-root sheet this runtime uses (null in document mode). */
180
+ sharedSheet: {
181
+ roots: number;
182
+ rules: number;
183
+ generations: number;
184
+ constructable: boolean;
185
+ } | null;
167
186
  };
168
187
  }
169
188
 
@@ -196,6 +215,16 @@ export declare interface BrowserRuntimeOptions {
196
215
  * oldest-first without waiting for the grace period; classes in use are never evicted. Default: no cap.
197
216
  */
198
217
  maxRules?: number;
218
+ /**
219
+ * #327: a ShadowRoot to style instead of the document. The runtime observes that root (with an initial scan)
220
+ * and places all of its CSS in it: utilities, theme variables, @property, @keyframes and a preflight rewritten
221
+ * for the root (`html`/`:root` -> `:host`, `body` declarations re-emitted on `:host`). Nothing goes to
222
+ * `document.head`. Runtimes with the same config share one constructable sheet adopted by every root
223
+ * (`adoptedStyleSheets`); without constructable sheets each root gets `<style>` elements instead.
224
+ * `insertionPoint`, `styleId` and `maxRulesPerPartition` are ignored in this mode, and server sheets (#268)
225
+ * are not adopted. `document` (or omitting it) keeps the document mode.
226
+ */
227
+ root?: ShadowRoot | Document;
199
228
  }
200
229
 
201
230
  /**
@@ -247,7 +276,7 @@ export declare class ChangeDetector {
247
276
  * @param options - Configuration options including initial scan and onReady callback
248
277
  * @returns The MutationObserver instance for external control
249
278
  */
250
- observe(root?: HTMLElement, options?: {
279
+ observe(root?: HTMLElement | ShadowRoot, options?: {
251
280
  scan?: boolean;
252
281
  onReady?: () => void;
253
282
  }): MutationObserver;
@@ -291,7 +320,7 @@ declare class ClassGc {
291
320
  private root;
292
321
  constructor(host: ClassGcHost, graceMs: number, maxRules: number, now?: () => number);
293
322
  /** Start counting for a new root: count every element currently inside it. */
294
- setRoot(root: Element): void;
323
+ setRoot(root: Element | ShadowRoot): void;
295
324
  count(cls: string): number;
296
325
  /** Re-count `el` and (optionally) all its descendants from their current state. */
297
326
  reconcileTree(node: Node): void;