Skip to content

Commit

Permalink
Adds support for external stylesheets with absolute urls (#9)
Browse files Browse the repository at this point in the history
* Update index.js

* Update test.js

* Update package.json

* Update test.js

* Update index.js

* Update test.js

* Update README.md
  • Loading branch information
bratberg authored and lukejacksonn committed Jan 20, 2020
1 parent fc39490 commit 7ed0eae
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -24,13 +24,17 @@ The package is designed to be used as an ES module. You can import it directly f
```js
import css from 'https://unpkg.com/csz';

const static = css`background: blue;`; // generate class name for ruleset
const dynamic = css`/index.css`; // generate class name for file contents
// static
const inlined = css`background: blue;`; // generate class name for ruleset

// dynamic (from stylesheet)
const relative = css`/index.css`; // generate class name for file contents
const absolute = css`https://example.com/index.css`; // generate class name for file contents
```

Both variations (static and dynamic) are sync and return a string in a format similar to `csz-b60d61b8`. If a ruleset is provided as a string then it is processed immediately but if a filepath is provided then processing is deferred until the contents of the file has been fetched and parsed.

> **NOTE:** All file paths must start with a `/` and be absolute (relative to the current hostname) so if you are running your app on `example.com` and require `/styles/index.css` then csz will try fetch it from `example.com/styles/index.css`.
> **NOTE:** File paths starting with `/` must be relative to the current hostname, so if you are running your app on `example.com` and require `/styles/index.css` then csz will try fetch it from `example.com/styles/index.css`.
Styles imported from a file are inevitably going to take some amount of time to download. Whilst the stylesheet is being downloaded a temporary ruleset is applied to the element which hides it (using `display: none`) until the fetched files have been processed. This was implemented to prevent flashes of unstyled content.

Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -14,12 +14,14 @@ const hide = hash => (sheet.innerHTML = none(hash) + sheet.innerHTML);
const show = hash =>
(sheet.innerHTML = sheet.innerHTML.replace(none(hash), ''));

const isExternalStyleSheet = key => /^(\/|https?:\/\/)/.test(key.trim());

const process = key => hash => rules => {
sheet.innerHTML += (cache[key] = {
hash,
rules: stylis()(`.${hash}`, rules)
}).rules;
if (key.startsWith('/')) show(hash);
if (isExternalStyleSheet(key)) show(hash);
};

export default (strings, ...values) => {
Expand All @@ -36,7 +38,7 @@ export default (strings, ...values) => {
const className = 'csz-' + hash();
const append = process(key)(className);

if (key.startsWith('/')) {
if (isExternalStyleSheet(key)) {
hide(className);
fetch(key)
.then(res => res.text())
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "csz",
"version": "1.1.1",
"version": "1.2.0",
"description": "Runtime CSS modules with SASS like preprocessing",
"main": "index.js",
"module": "index.js",
Expand Down
3 changes: 3 additions & 0 deletions test/test.js
Expand Up @@ -18,6 +18,9 @@ const App = () => {
>
<h1>Hello World!</h1>
<button className="btn" onClick=${e => setToggle(!toggle)}>Toggle</button>
<div className=${css`https://unpkg.com/tailwindcss@1.1.4/dist/tailwind.min.css`}>
<h1 class="bg-blue-500">Hello from tailwind</h1>
</div>
</div>
`
}
Expand Down

0 comments on commit 7ed0eae

Please sign in to comment.