Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/component/src/define-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Template, TemplateCompiler } from '@semantic-ui/templating';
import { camelToKebab, each, isClient, isServer, kebabToCamel, noop } from '@semantic-ui/utils';
import { camelToKebab, each, isClient, isPlainObject, isServer, kebabToCamel, noop } from '@semantic-ui/utils';
import { unsafeCSS } from 'lit';

import { adjustPropertyFromAttribute } from './helpers/adjust-property-from-attribute.js';
Expand All @@ -11,6 +11,7 @@ export const defineComponent = ({
ast,
css = '',
pageCSS = '',
frameworkCSS = {},
tagName,
delegatesFocus = false,
templateName = kebabToCamel(tagName),
Expand Down Expand Up @@ -57,6 +58,21 @@ export const defineComponent = ({
adoptStylesheet(pageCSS);
}

if (frameworkCSS) {
if (isPlainObject(frameworkCSS)) {
each(frameworkCSS, (css, id) => {
adoptStylesheet(css, {
hash: id,
cacheStylesheet: true,
});
});
}
else if (isString(frameworkCSS)) {
adoptStylesheet(frameworkCSS, {
cacheStylesheet: true,
});
}
}

/*
Create Component Returns Either a Template or WebComponent
Expand Down
41 changes: 31 additions & 10 deletions packages/component/src/helpers/adopt-stylesheet.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
import { hashCode, isServer } from '@semantic-ui/utils';
import { scopeStyles } from './scope-styles.js';

export const adoptStylesheet = (css, adoptedElement, { scopeSelector } = {}) => {
export const adoptStylesheet = (css, adoptedElement, {
scopeSelector,
hash = hashCode(css),
cacheStylesheet = false,
} = {}) => {
if (isServer) {
return;
}
if (!adoptedElement) {
adoptedElement = document;
}

const hash = hashCode(css);
if (!adoptedElement.cssHashes) {
adoptedElement.cssHashes = [];
}
// already added
if (adoptedElement.cssHashes.includes(hash)) {
// already added
return;
}

adoptedElement.cssHashes.push(hash);

const stylesheet = new CSSStyleSheet();
let stylesheet;

// allow selectors to be scoped if passed in
// i.e .foo => .scope .foo
if (scopeSelector) {
css = scopeStyles(css, scopeSelector);
if (cacheStylesheet && document.cachedStylesheets[hash]) {
// reuse stylesheet if cached
stylesheet = documnet.cachedStylesheets[hash];
}
stylesheet.id = hash;
stylesheet.replaceSync(css);
else {
// otherwise create from scratch
stylesheet = new CSSStyleSheet();

// allow selectors to be scoped if passed in
// i.e .foo => .scope .foo
if (scopeSelector) {
css = scopeStyles(css, scopeSelector);
}
stylesheet.id = hash;
stylesheet.replaceSync(css);
}

// store stylesheet globally in cache for reuse if specified
if (cacheStylesheet) {
if (!document.cachedStylesheets) {
document.cachedStylesheets = {};
}
document.cachedStylesheets[hash] = stylesheet;
}

// adopt this stylesheet after others
adoptedElement.adoptedStyleSheets = [
...adoptedElement.adoptedStyleSheets,
Expand Down