Skip to content

Commit 144fd61

Browse files
committed
Add cjs and browser versions
1 parent efb53ae commit 144fd61

5 files changed

Lines changed: 135 additions & 11 deletions

File tree

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@ This plugin is a JavaScript module that loads [JS-in-CSS stylesheets](https://re
88

99
## Downloading
1010

11-
You can download `index.js` and add it to your codebase, or download it with npm:
11+
You can download jsincss and add it to your codebase manually, or download it with npm:
1212

1313
```bash
1414
npm install jsincss
1515
```
1616

17-
Another option that works for building or testing, that isn't ideal for production use, is linking to the module directly from a CDN like unpkg:
17+
> Another option that works for building or testing, that isn't ideal for production use, is linking to the module directly from a CDN like unpkg:
1818
1919
```html
2020
<script type=module>
21-
import jsincss from 'https://unpkg.com/jsincss/index.js'
21+
import jsincss from 'https://unpkg.com/jsincss/index.vanilla.js'
2222
</script>
2323
```
2424

2525
## Importing
2626

2727
You can import the plugin into your own JavaScript modules in a couple of ways.
2828

29-
The first way is using the native [`import` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) in JavaScript. Here you can assign any name you want to the function you are importing, and you only need to provide a path to the plugin's `index.js` file:
29+
The first way is using the native [`import` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) in JavaScript. Here you can assign any name you want to the function you are importing, and you only need to provide a path to the plugin's `index.vanilla.js` file:
3030

3131
```js
32-
import jsincss from './node_modules/jsincss/index.js'
32+
import jsincss from './node_modules/jsincss/index.vanilla.js'
3333
```
3434

35-
If you want to use `require` to load this plugin instead, and use a bundler like Webpack or Parcel, make sure to add `.default` as you require it:
35+
You can also use `require` to load this plugin instead with a bundler like Webpack or Parcel:
3636

3737
```js
38-
const jsincss = require('jsincss').default
38+
const jsincss = require('jsincss')
3939
```
4040

4141
Once you have imported this plugin into your module, you can use the plugin as `jsincss()`
4242

4343
## Using JS-in-CSS Stylesheets
4444

45-
The main goal of this plugin is to let people using a JS-in-CSS workflow load JIC stylesheets inside of a JavaScript module.
45+
The main goal of this plugin is to let people using a JS-in-CSS workflow load [JIC stylesheets](https://responsive.style/theory/what-is-a-jic-stylesheet.html) inside of a JavaScript module.
4646

4747
The plugin has the following format:
4848

@@ -56,13 +56,15 @@ jsincss(stylesheet, selector, events)
5656

5757
The default `selector` is `window`, and the default list of `events` is `['load', 'resize', 'input', 'click']`.
5858

59+
You can also create and listen for custom events with JavaScript using `new Event()` and `dispatchEvent()` for total control over when jsincss reprocesses styles.
60+
5961
## Example
6062

6163
This example uses the default `selector` and `events` list, and provides the stylesheet inline.
6264

6365
```js
6466
<script type=module>
65-
import jsincss from 'https://unpkg.com/jsincss/index.js'
67+
import jsincss from 'https://unpkg.com/jsincss/index.vanilla.js'
6668

6769
jsincss(() => `
6870
@@ -89,7 +91,7 @@ export default () => `
8991
And then import both the `jsincss` plugin and the stylesheet into your module and run them like this:
9092

9193
```js
92-
import jsincss from 'https://unpkg.com/jsincss/index.js'
94+
import jsincss from 'https://unpkg.com/jsincss/index.vanilla.js'
9395
import stylesheet from './path/to/stylesheet.js'
9496

9597
jsincss(stylesheet)

index.browser.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function registerEvent(target, event, id, stylesheet) {
2+
3+
return target.addEventListener(event, e => populateStylesheet(id, stylesheet))
4+
5+
}
6+
7+
function populateStylesheet(id, stylesheet) {
8+
9+
let tag = document.querySelector(`#jsincss-${id}`)
10+
11+
if (!tag) {
12+
13+
tag = document.createElement('style')
14+
tag.id = `jsincss-${id}`
15+
document.head.appendChild(tag)
16+
17+
}
18+
19+
const generatedStyles = stylesheet()
20+
const currentStyles = tag.innerHTML
21+
22+
if (!currentStyles
23+
|| (currentStyles && generatedStyles !== currentStyles)) {
24+
25+
return tag.innerHTML = generatedStyles
26+
27+
}
28+
29+
}
30+
31+
function jsincss(stylesheet, selector, events) {
32+
33+
let id = Date.now() + Math.floor(Math.random() * 100)
34+
35+
selector = selector || window
36+
events = events || ['load', 'resize', 'input', 'click']
37+
38+
if (selector === window) {
39+
40+
events.forEach(event => {
41+
42+
registerEvent(window, event, id, stylesheet)
43+
44+
})
45+
46+
} else {
47+
48+
document.querySelectorAll(selector).forEach(tag => {
49+
50+
events.forEach(event => {
51+
52+
registerEvent(tag, event, id, stylesheet)
53+
54+
})
55+
56+
})
57+
58+
}
59+
60+
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function populateStylesheet(id, stylesheet) {
2828

2929
}
3030

31-
export default (stylesheet, selector, events) => {
31+
module.exports = (stylesheet, selector, events) => {
3232

3333
let id = Date.now() + Math.floor(Math.random() * 100)
3434

index.vanilla.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function registerEvent(target, event, id, stylesheet) {
2+
3+
return target.addEventListener(event, e => populateStylesheet(id, stylesheet))
4+
5+
}
6+
7+
function populateStylesheet(id, stylesheet) {
8+
9+
let tag = document.querySelector(`#jsincss-${id}`)
10+
11+
if (!tag) {
12+
13+
tag = document.createElement('style')
14+
tag.id = `jsincss-${id}`
15+
document.head.appendChild(tag)
16+
17+
}
18+
19+
const generatedStyles = stylesheet()
20+
const currentStyles = tag.innerHTML
21+
22+
if (!currentStyles
23+
|| (currentStyles && generatedStyles !== currentStyles)) {
24+
25+
return tag.innerHTML = generatedStyles
26+
27+
}
28+
29+
}
30+
31+
export default (stylesheet, selector, events) => {
32+
33+
let id = Date.now() + Math.floor(Math.random() * 100)
34+
35+
selector = selector || window
36+
events = events || ['load', 'resize', 'input', 'click']
37+
38+
if (selector === window) {
39+
40+
events.forEach(event => {
41+
42+
registerEvent(window, event, id, stylesheet)
43+
44+
})
45+
46+
} else {
47+
48+
document.querySelectorAll(selector).forEach(tag => {
49+
50+
events.forEach(event => {
51+
52+
registerEvent(tag, event, id, stylesheet)
53+
54+
})
55+
56+
})
57+
58+
}
59+
60+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"version": "1.0.0",
44
"description": "A JS-in-CSS stylesheet loader",
55
"main": "index.js",
6+
"module": "index.vanilla.js",
7+
"browser": "index.browser.js",
68
"scripts": {
79
"test": "echo \"Error: no test specified\" && exit 1"
810
},

0 commit comments

Comments
 (0)