Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💩 Removes the old way to mount styles #82

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 0 additions & 2 deletions examples/Misc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ storiesOf('Miscellaneous', module)
) : (
<Logo primary onClick={() => setActive(false)} />
)}
<Logo onClick={() => setActive(true)} />
<Logo primary onClick={() => setActive(false)} />
</Fragment>
);
};
Expand Down
127 changes: 0 additions & 127 deletions packages/registry/src/registry-prod.spec.ts

This file was deleted.

43 changes: 12 additions & 31 deletions packages/registry/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ const createStyleElement = (attributeId: string) => {
const getStyleElement = (targetElement: HTMLElement, attributeId: string) =>
targetElement.querySelector<HTMLStyleElement>(`style[${attributeId}]`);

const getSheet = (tag: HTMLStyleElement): CSSStyleSheet => {
if (tag.sheet) {
// @ts-ignore
return tag.sheet;
}
const getSheet = (element: HTMLStyleElement): CSSStyleSheet => {
if (element.sheet) return element.sheet;

for (let i = 0; i < document.styleSheets.length; i++) {
if (document.styleSheets[i].ownerNode === tag) {
// @ts-ignore
if (document.styleSheets[i].ownerNode === element) {
return document.styleSheets[i];
}
}
Expand Down Expand Up @@ -71,36 +67,21 @@ const registry = (
const register = (id: string, styles: string, isGlobal?: boolean) => {
if (has(id)) return;

const selector = !isGlobal ? id : ``;
const processedStyles = stylis(selector, styles);

if (process.env.NODE_ENV === 'production') {
try {
splitRules(processedStyles).forEach(styles => {
const sheet = getSheet(styleElement!);
sheet.insertRule(
styles,
isGlobal ? 0 : sheet.cssRules.length,
);
});

return;
} catch (error) {
const processedStyles = stylis(!isGlobal ? id : ``, styles);

try {
splitRules(processedStyles).forEach(styles => {
const sheet = getSheet(styleElement!);
sheet.insertRule(styles, isGlobal ? 0 : sheet.cssRules.length);
});
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
console.warn(
`Trousers: unable to insert rule: ${styles}`,
error,
);
}
}

const styleNode = document.createTextNode(`${processedStyles}\n`);
const mountedStyles = styleElement!.getAttribute(attributeId);

styleElement!.appendChild(styleNode);
styleElement!.setAttribute(
attributeId,
`${mountedStyles} ${id}`.trim(),
);
};

return {
Expand Down