primeng
Version:
PrimeNG is a premium UI library for Angular featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeBlock,
112 lines (108 loc) • 4.71 kB
JavaScript
import { DOCUMENT } from '@angular/common';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { setAttribute, setAttributes } from '@primeuix/utils';
let _id = 0;
class UseStyle {
document = inject(DOCUMENT);
styleSheets = new Map();
shadowRoots = new Set();
/**
* Applies the injected styles inside the given shadow root.
*/
addShadowRoot(shadowRoot) {
if (!this.isAdoptionSupported() || !(shadowRoot instanceof ShadowRoot))
return () => { };
if (!this.shadowRoots.has(shadowRoot)) {
this.shadowRoots.add(shadowRoot);
shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, ...[...this.styleSheets.values()].filter((styleSheet) => !shadowRoot.adoptedStyleSheets.includes(styleSheet))];
}
return () => this.removeShadowRoot(shadowRoot);
}
/**
* Stops applying the injected styles inside the given shadow root.
*/
removeShadowRoot(shadowRoot) {
if (!this.shadowRoots.delete(shadowRoot))
return;
const styleSheets = new Set(this.styleSheets.values());
shadowRoot.adoptedStyleSheets = shadowRoot.adoptedStyleSheets.filter((styleSheet) => !styleSheets.has(styleSheet));
}
/**
* Removes an injected style from the document and from the shadow roots that adopted it.
*/
remove(name) {
const styleSheet = this.styleSheets.get(name);
this.styleSheets.delete(name);
this.document?.querySelector(`style[data-primeng-style-id="${name}"]`)?.remove();
if (!styleSheet)
return;
this.shadowRoots.forEach((shadowRoot) => {
shadowRoot.adoptedStyleSheets = shadowRoot.adoptedStyleSheets.filter((adopted) => adopted !== styleSheet);
});
}
use(css, options = {}) {
let isLoaded = false;
let cssRef = css;
let styleRef = null;
const { immediate = true, manual = false, name = `style_${++_id}`, id = undefined, media = undefined, nonce = undefined, first = false, variables = false, props = {} } = options;
if (!this.document)
return;
styleRef = (this.document.querySelector(`style[data-primeng-style-id="${name}"]`) || (id && this.document.getElementById(id)) || this.document.createElement('style'));
if (styleRef) {
if (!styleRef.isConnected) {
cssRef = css;
const HEAD = this.document.head;
setAttribute(styleRef, 'nonce', nonce);
first && HEAD.firstChild ? HEAD.insertBefore(styleRef, HEAD.firstChild) : HEAD.appendChild(styleRef);
setAttributes(styleRef, {
type: 'text/css',
media,
nonce,
'data-primeng-style-id': name
});
}
if (styleRef.textContent !== cssRef) {
styleRef.textContent = cssRef;
}
}
if (!variables) {
this.adoptStyleSheet(name, cssRef ?? '', first);
}
return {
id,
name,
el: styleRef,
css: cssRef
};
}
adoptStyleSheet(name, css, first) {
if (!this.isAdoptionSupported())
return;
const styleSheet = this.styleSheets.get(name);
if (styleSheet) {
styleSheet.replaceSync(css);
return;
}
const added = new CSSStyleSheet();
added.replaceSync(css);
this.styleSheets = first ? new Map([[name, added], ...this.styleSheets]) : this.styleSheets.set(name, added);
this.shadowRoots.forEach((shadowRoot) => {
shadowRoot.adoptedStyleSheets = first ? [added, ...shadowRoot.adoptedStyleSheets] : [...shadowRoot.adoptedStyleSheets, added];
});
}
isAdoptionSupported() {
return typeof ShadowRoot !== 'undefined' && typeof CSSStyleSheet !== 'undefined' && typeof CSSStyleSheet.prototype.replaceSync === 'function';
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.2", ngImport: i0, type: UseStyle, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.2", ngImport: i0, type: UseStyle, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.2", ngImport: i0, type: UseStyle, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { UseStyle };
//# sourceMappingURL=primeng-usestyle.mjs.map