Skip to content

Commit ac836b3

Browse files
committed
enable ssr
1 parent af7a09c commit ac836b3

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

src/css-utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import CSS_COLOR_NAMES from "./external/bobspace:html-colors";
2-
import toPx from "./external/heygrady:units:length";
1+
import CSS_COLOR_NAMES from './external/bobspace:html-colors';
2+
3+
const toPx = typeof document !== 'undefined' && require('./external/heygrady:units:length');
34

45
/** @returns {string} */
56
function convertPlainColor(val) {
@@ -42,7 +43,12 @@ function toNumber(length, element, err) {
4243
if (err) throw err
4344
else return false
4445
else if (length?.match(/(\d+(\.\d+)?(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)|0)/))
45-
return toPx(element, length)
46+
if (typeof toPx === 'function')
47+
return toPx(element, length)
48+
else if (length.endsWith('px'))
49+
return Number(length.substring(0, length.length - 2))
50+
else
51+
return false
4652
}
4753

4854
/** @returns {number} */

src/external/apearce:eact-shadow-dom.js renamed to src/external/apearce:react-shadow-dom.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ import React from 'react';
77
import ReactDOM from 'react-dom';
88
import PropTypes from 'prop-types';
99

10-
const constructableStylesheetsSupported = window
10+
const constructableStylesheetsSupported = typeof window !== 'undefined' && window
1111
&& window.ShadowRoot
1212
&& window.ShadowRoot.prototype.hasOwnProperty('adoptedStyleSheets')
1313
&& window.CSSStyleSheet
1414
&& window.CSSStyleSheet.prototype.hasOwnProperty('replace');
1515

16-
const shadowRootSupported = window
16+
const shadowRootSupported = typeof window !== 'undefined' && window
1717
&& window.Element
1818
&& window.Element.prototype.hasOwnProperty('attachShadow');
1919

2020
export default class ShadowRoot extends React.PureComponent {
2121
static constructableStylesheetsSupported = constructableStylesheetsSupported;
22-
static constructibleStylesheetsSupported = constructableStylesheetsSupported;
2322
static defaultProps = {
2423
delegatesFocus: false,
2524
mode: 'open'

src/external/getMatchedCSSRules-polyfill.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable */
22
// polyfill element.matches
3-
if (!Element.prototype.matches) {
3+
if (typeof Element !== 'undefined' && !Element.prototype.matches) {
44
Element.prototype.matches =
55
Element.prototype.matchesSelector ||
66
Element.prototype.mozMatchesSelector ||
77
Element.prototype.msMatchesSelector ||
88
Element.prototype.oMatchesSelector ||
99
Element.prototype.webkitMatchesSelector ||
10-
function(s) {
10+
function (s) {
1111
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
1212
i = matches.length;
1313
while (--i >= 0 && matches.item(i) !== this) {}
@@ -16,42 +16,43 @@ if (!Element.prototype.matches) {
1616
}
1717

1818
// polyfill window.getMatchedCSSRules() in FireFox 6+
19-
if ( typeof window.getMatchedCSSRules !== 'function' ) {
19+
if (typeof window !== 'undefined' && typeof window.getMatchedCSSRules !== 'function') {
2020
var ELEMENT_RE = /[\w-]+/g,
2121
ID_RE = /#[\w-]+/g,
2222
CLASS_RE = /\.[\w-]+/g,
2323
ATTR_RE = /\[[^\]]+\]/g,
2424
// :not() pseudo-class does not add to specificity, but its content does as if it was outside it
2525
PSEUDO_CLASSES_RE = /\:(?!not)[\w-]+(\(.*\))?/g,
2626
PSEUDO_ELEMENTS_RE = /\:\:?(after|before|first-letter|first-line|selection)/g;
27+
2728
// convert an array-like object to array
28-
function toArray (list) {
29+
function toArray(list) {
2930
return [].slice.call(list);
3031
}
3132

3233
// handles extraction of `cssRules` as an `Array` from a stylesheet or something that behaves the same
33-
function getSheetRules (stylesheet) {
34+
function getSheetRules(stylesheet) {
3435
var sheet_media = stylesheet.media && stylesheet.media.mediaText;
3536
// if this sheet is disabled skip it
36-
if ( stylesheet.disabled ) return [];
37+
if (stylesheet.disabled) return [];
3738
// if this sheet's media is specified and doesn't match the viewport then skip it
38-
if ( sheet_media && sheet_media.length && ! window.matchMedia(sheet_media).matches ) return [];
39+
if (sheet_media && sheet_media.length && !window.matchMedia(sheet_media).matches) return [];
3940
// get the style rules of this sheet
4041
return toArray(stylesheet.cssRules);
4142
}
4243

43-
function _find (string, re) {
44+
function _find(string, re) {
4445
var matches = string.match(re);
4546
return re ? re.length : 0;
4647
}
4748

4849
// calculates the specificity of a given `selector`
49-
function calculateScore (selector) {
50-
var score = [0,0,0],
50+
function calculateScore(selector) {
51+
var score = [0, 0, 0],
5152
parts = selector.split(' '),
5253
part, match;
5354
//TODO: clean the ':not' part since the last ELEMENT_RE will pick it up
54-
while ( part = parts.shift(), typeof part == 'string' ) {
55+
while (part = parts.shift(), typeof part == 'string') {
5556
// find all pseudo-elements
5657
match = _find(part, PSEUDO_ELEMENTS_RE);
5758
score[2] = match;
@@ -84,21 +85,21 @@ if ( typeof window.getMatchedCSSRules !== 'function' ) {
8485
}
8586

8687
// returns the heights possible specificity score an element can get from a give rule's selectorText
87-
function getSpecificityScore (element, selector_text) {
88+
function getSpecificityScore(element, selector_text) {
8889
var selectors = selector_text.split(','),
8990
selector, score, result = 0;
90-
while ( selector = selectors.shift() ) {
91-
if ( element.matches(selector) ) {
91+
while (selector = selectors.shift()) {
92+
if (element.matches(selector)) {
9293
score = calculateScore(selector);
9394
result = score > result ? score : result;
9495
}
9596
}
9697
return result;
9798
}
9899

99-
function sortBySpecificity (element, rules) {
100+
function sortBySpecificity(element, rules) {
100101
// comparing function that sorts CSSStyleRules according to specificity of their `selectorText`
101-
function compareSpecificity (a, b) {
102+
function compareSpecificity(a, b) {
102103
return getSpecificityScore(element, b.selectorText) - getSpecificityScore(element, a.selectorText);
103104
}
104105

@@ -116,28 +117,28 @@ if ( typeof window.getMatchedCSSRules !== 'function' ) {
116117

117118
// assuming the browser hands us stylesheets in order of appearance
118119
// we iterate them from the beginning to follow proper cascade order
119-
while ( sheet = style_sheets.shift() ) {
120+
while (sheet = style_sheets.shift()) {
120121
// get the style rules of this sheet
121122
rules = getSheetRules(sheet);
122123
// loop the rules in order of appearance
123-
while ( rule = rules.shift() ) {
124+
while (rule = rules.shift()) {
124125
// if this is an @import rule
125-
if ( rule.styleSheet ) {
126+
if (rule.styleSheet) {
126127
// insert the imported stylesheet's rules at the beginning of this stylesheet's rules
127128
rules = getSheetRules(rule.styleSheet).concat(rules);
128129
// and skip this rule
129130
continue;
130131
}
131132
// if there's no stylesheet attribute BUT there IS a media attribute it's a media rule
132-
else if ( rule.media ) {
133+
else if (rule.media) {
133134
// insert the contained rules of this media rule to the beginning of this stylesheet's rules
134135
rules = getSheetRules(rule).concat(rules);
135136
// and skip it
136137
continue
137138
}
138139
//TODO: for now only polyfilling Gecko
139140
// check if this element matches this rule's selector
140-
if ( element.matches(rule.selectorText) ) {
141+
if (element.matches(rule.selectorText)) {
141142
// push the rule to the results set
142143
result.push(rule);
143144
}

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, {useRef, useEffect, useState, useCallback} from 'react'
22
import generateSvgSquircle from './generator'
33
import './external/getMatchedCSSRules-polyfill'
44
import updateStates from "./updateStates"
5-
import ShadowRoot from "./external/apearce:eact-shadow-dom"
5+
import ShadowRoot from "./external/apearce:react-shadow-dom"
66
import attachCSSWatcher from './styleSheetWatcher'
77
import getMaskPaths from './mask-generator'
88

src/styleSheetWatcher.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const CSSChangeEvent = new CustomEvent('css-change');
1+
console.log()
2+
3+
const CSSChangeEvent = typeof CustomEvent !== 'undefined' ? new CustomEvent('css-change') : 'css-change';
24

35
export default function attachCSSWatcher(callback) {
46
CSSWatcher.addEventListener('css-change', () => callback())
@@ -14,13 +16,16 @@ const CSSWatcher = new EventTarget()
1416
CSS = newCSS
1517
CSSWatcher.dispatchEvent(CSSChangeEvent)
1618
}, 30)
19+
20+
if (typeof window === 'undefined') return
1721
window.addEventListener('resize', () => {
1822
CSS = getCSSText()
1923
CSSWatcher.dispatchEvent(CSSChangeEvent)
2024
})
2125
})()
2226

2327
function getCSSText() {
28+
if (typeof document === 'undefined') return ''
2429
let CSS = ''
2530
for (let i = 0; i < document.styleSheets.length; i++) {
2631
const sheet = document.styleSheets[i]

0 commit comments

Comments
 (0)