@slimr/css 2.1.12 → 2.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,19 @@
1
- # @slimr/css
1
+ # 🪶 @slimr/css [![npm package](https://img.shields.io/npm/v/@slimr/css.svg?style=flat-square)](https://npmjs.org/package/@slimr/css)
2
2
 
3
3
  tiny css-in-js features, inspired by the popular emotion library
4
4
 
5
+ Demo: [CodeSandbox](https://codesandbox.io/s/64r9px?file=/src/App.tsx)
6
+
7
+ Features:
8
+
9
+ - Much smaller (less bundle size)
10
+ - Less is more: faster, less bugs, no breaking changes
11
+ - Is progressive -- lazy loads styles
12
+ - Css shorthand props like [chakra-ui](https://chakra-ui.com/docs/styled-system/style-props)
13
+ - CSS responsive shorthand props like [chakra-ui](https://chakra-ui.com/docs/styled-system/responsive-styles)
14
+
15
+ ## Context
16
+
5
17
  `@slimr` is a set of slim React (hence '@slimr') libs:
6
18
 
7
19
  - [@slimr/css](https://www.npmjs.com/package/@slimr/css) - Framework agnostic css-in-js features inspired by the popular Emotion lib
@@ -14,85 +26,61 @@ tiny css-in-js features, inspired by the popular emotion library
14
26
  - [@slimr/swr](https://www.npmjs.com/package/@slimr/swr) - A React hook for fetching data that supports stale-while-refresh eager rendering
15
27
  - [@slimr/util](https://www.npmjs.com/package/@slimr/util) - Framework agnostic Javascript polyfills
16
28
 
17
- Pros:
18
-
19
- - Much smaller (less bundle size)
20
- - Less is more: faster, less bugs, no breaking changes
21
- - Is progressive -- lazy loads styles
22
- - Css shorthand props like [chakra-ui](https://chakra-ui.com/docs/styled-system/style-props):
23
- - `m` --> `margin`
24
- - `mx` --> `margin-left` and bottom
25
- - `py` --> `padding-top` and bottom
26
- - More [here](https://github.com/bdombro/slimr/blob/65bf012086760b7e481a4064f3be8aea6a098b91/packages/css/src/index.ts#L73)!
27
- - CSS Breakpoints shorthand like [chakra-ui](https://chakra-ui.com/docs/styled-system/responsive-styles):
28
-
29
- ```css
30
- margin: [auto, null, inherit];
31
- /* Translates to */
32
- margin: auto;
33
- @media (min-width: 48em) {
34
- margin: inherit;
35
- }
36
- ```
37
-
38
- - Breakpoints are `[30em, 48em, 62em, 80em, 96em]` and can be overridden by setting `css.breakpoints`
39
-
40
29
  ## Setup/Install
41
30
 
42
31
  ```bash
43
32
  npm i @slimr/css
44
33
  ```
45
34
 
46
- ## addCss
35
+ ## API
36
+
37
+ ### addCss
47
38
 
48
39
  Injects css to the page head
49
40
 
50
- - Lazy: Will not be added until the component mounts, if called from within a component
51
- - Batches dom changes for better performance
41
+ - Queues and batches injections for better performance
52
42
  - Has local cache to recall prior adds, to reduce duplicates and dom changes
53
43
 
54
- @param css - css to be injected
55
- @returns void
44
+ ```typescript
45
+ import { addCSs } from '@slimr/css'
46
+ addCss(`.foo { color: green; }`) // queues css for injection
47
+ addCss(`.foo { color: green; }`) // ignored bc duplicate
48
+ addCss(`.foo { background: purplse`) // queues more css
49
+ // the queue will be executed next javascript tick
50
+ ```
51
+
52
+ ### `css` (alias for `createClass`)
56
53
 
57
- ## `css` (alias for `createClass`)
54
+ Upserts and returns a unique css class for a given css string
58
55
 
59
- ## `classJoin`
56
+ - Leverages addCss under the hood, so very performant
57
+ - Supports several css short-hands, inspired by [Chakra-UI's box](https://chakra-ui.com/docs/styled-system/responsive-styles)
58
+ - Supports array values for responsive styles, similar to Chakra-UI's box. More [here](https://github.com/bdombro/slimr/blob/65bf012086760b7e481a4064f3be8aea6a098b91/packages/css/src/index.ts#L73)
59
+
60
+ ```typescript
61
+ import {createClass, css} from '@slimr/css'
62
+ c1 = createClass('c: red;') // queues the create of new css class 's0'
63
+ c2 = createClass('c: red;') // is duplicate so will return 's0'
64
+ c3 = createClass`c: red;` // same
65
+ c4 = css`c: red;` // same
66
+ // c1 = c2 = c3 = c4
67
+ <div className={css`c: red;`} /> // will resolve to 's0' like above
68
+ c5 = css`c: blue;` // queues the create of new css class 's1'
69
+ c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
70
+ ```
71
+
72
+ ...and the queue will be executed next javascript tick
73
+
74
+ ### `classJoin`
60
75
 
61
76
  Joins class names and omits falsey props
62
77
 
63
78
  ```typescript
79
+ import { classJoin } from '@slimr/css'
64
80
  classJoin('a', 'b', 'c') // 'a b c'
65
81
  classJoin('a', 0, 'b', null, 'c') // 'a b c'
66
82
  ```
67
83
 
68
- ## Shorthang Props (`shorthandPropsMap` and `ShorthandProps`)
69
-
70
- ## Usage
71
-
72
- ```tsx
73
- // Add some global styles
74
- addCss`
75
- body {
76
- color: lightgray;
77
- }
78
- `
79
- return (
80
- <div
81
- className={css`
82
- background: white;
83
- color: ${on ? 'red' : 'initial'};
84
- &:hover {
85
- font-weight: bold;
86
- }
87
- font-size: [20px, null, 16px];
88
- `}
89
- >
90
- Helo css!
91
- </div>
92
- )
93
- }
94
- ```
95
-
96
84
  ## Comparisons
97
85
 
98
86
  ### [Emotion](https://emotion.sh/docs/introduction)
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Injects css to the page head
3
+ *
4
+ * - Lazy: Will not be added until the component mounts, if called from within a component
5
+ * - Batches dom changes for better performance
6
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
7
+ *
8
+ * @param css
9
+ * css to be injected
10
+ *
11
+ * @returns void
12
+ *
13
+ * @example
14
+ * addCss(`.myClass { color: red; }`) // queues css for injection
15
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
16
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
17
+ * // ...and the queue will be executed next javascript tick
18
+ */
19
+ export declare function addCss(css: string): void;
20
+ export declare namespace addCss {
21
+ var que: Set<string>;
22
+ var count: number;
23
+ }
package/cjs/addCss.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addCss = void 0;
4
+ /**
5
+ * Injects css to the page head
6
+ *
7
+ * - Lazy: Will not be added until the component mounts, if called from within a component
8
+ * - Batches dom changes for better performance
9
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
10
+ *
11
+ * @param css
12
+ * css to be injected
13
+ *
14
+ * @returns void
15
+ *
16
+ * @example
17
+ * addCss(`.myClass { color: red; }`) // queues css for injection
18
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
19
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
20
+ * // ...and the queue will be executed next javascript tick
21
+ */
22
+ function addCss(css) {
23
+ addCss.que.add(css);
24
+ setTimeout(() => {
25
+ const css = [...addCss.que].join('\n');
26
+ if (css) {
27
+ addCss.que.clear();
28
+ const s = document.createElement('style');
29
+ s.id = `u${addCss.count++}`;
30
+ s.innerHTML = css;
31
+ document.head.appendChild(s);
32
+ }
33
+ }, 0);
34
+ }
35
+ exports.addCss = addCss;
36
+ addCss.que = new Set();
37
+ addCss.count = 0;
38
+ //# sourceMappingURL=addCss.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addCss.js","sourceRoot":"","sources":["../src/addCss.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,MAAM,CAAC,GAAW;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnB,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACtC,IAAI,GAAG,EAAE;YACP,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;YAClB,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YACzC,CAAC,CAAC,EAAE,GAAG,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,CAAA;YAC3B,CAAC,CAAC,SAAS,GAAG,GAAG,CAAA;YACjB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;SAC7B;IACH,CAAC,EAAE,CAAC,CAAC,CAAA;AACP,CAAC;AAZD,wBAYC;AACD,MAAM,CAAC,GAAG,GAAG,IAAI,GAAG,EAAU,CAAA;AAC9B,MAAM,CAAC,KAAK,GAAG,CAAC,CAAA"}
package/cjs/addCss.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Injects css to the page head
3
+ *
4
+ * - Lazy: Will not be added until the component mounts, if called from within a component
5
+ * - Batches dom changes for better performance
6
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
7
+ *
8
+ * @param css
9
+ * css to be injected
10
+ *
11
+ * @returns void
12
+ *
13
+ * @example
14
+ * addCss(`.myClass { color: red; }`) // queues css for injection
15
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
16
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
17
+ * // ...and the queue will be executed next javascript tick
18
+ */
19
+ export function addCss(css: string) {
20
+ addCss.que.add(css)
21
+ setTimeout(() => {
22
+ const css = [...addCss.que].join('\n')
23
+ if (css) {
24
+ addCss.que.clear()
25
+ const s = document.createElement('style')
26
+ s.id = `u${addCss.count++}`
27
+ s.innerHTML = css
28
+ document.head.appendChild(s)
29
+ }
30
+ }, 0)
31
+ }
32
+ addCss.que = new Set<string>()
33
+ addCss.count = 0
@@ -1,23 +1,10 @@
1
1
  import { T2SProps } from '@slimr/util';
2
- /**
3
- * Injects css to the page head
4
- *
5
- * - Lazy: Will not be added until the component mounts, if called from within a component
6
- * - Batches dom changes for better performance
7
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
8
- *
9
- * @param css - css to be injected
10
- * @returns void
11
- */
12
- export declare function addCss(css: string): void;
13
- export declare namespace addCss {
14
- var que: Set<string>;
15
- var count: number;
16
- }
17
2
  /**
18
3
  * Joins class names and omits falsey props
19
4
  *
20
- * @param classes - class names to be joined
5
+ * @param classes
6
+ * class names to be joined
7
+ *
21
8
  * @returns a string of joined class names
22
9
  *
23
10
  * @example
@@ -28,12 +15,25 @@ export declare namespace addCss {
28
15
  */
29
16
  export declare function classJoin(...classes: (string | 0 | null | undefined)[]): string;
30
17
  /**
31
- * Injects css and creates unique class names
18
+ * Upserts and returns a unique css class for a given css string
32
19
  *
33
- * - Skips if already added
20
+ * @param cssString
21
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
34
22
  *
35
- * @param cssString - string or template string, to be injected
36
23
  * @returns a unique class name
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
28
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
29
+ * c3 = createClass`c: red;` // same
30
+ * c4 = css`c: red;` // same
31
+ * // c1 = c2 = c3 = c4
32
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
33
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
34
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
35
+ * ```
36
+ * ...and the queue will be executed next javascript tick
37
37
  */
38
38
  export declare function createClass(...p: T2SProps): string;
39
39
  export declare namespace createClass {
@@ -1,38 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createClass = exports.classJoin = exports.addCss = void 0;
3
+ exports.createClass = exports.classJoin = void 0;
4
4
  const util_1 = require("@slimr/util");
5
+ const addCss_js_1 = require("./addCss.js");
5
6
  const shorthandProps_js_1 = require("./shorthandProps.js");
6
- /**
7
- * Injects css to the page head
8
- *
9
- * - Lazy: Will not be added until the component mounts, if called from within a component
10
- * - Batches dom changes for better performance
11
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
12
- *
13
- * @param css - css to be injected
14
- * @returns void
15
- */
16
- function addCss(css) {
17
- addCss.que.add(css);
18
- setTimeout(() => {
19
- const css = [...addCss.que].join('\n');
20
- if (css) {
21
- addCss.que.clear();
22
- const s = document.createElement('style');
23
- s.id = `u${addCss.count++}`;
24
- s.innerHTML = css;
25
- document.head.appendChild(s);
26
- }
27
- }, 0);
28
- }
29
- exports.addCss = addCss;
30
- addCss.que = new Set();
31
- addCss.count = 0;
32
7
  /**
33
8
  * Joins class names and omits falsey props
34
9
  *
35
- * @param classes - class names to be joined
10
+ * @param classes
11
+ * class names to be joined
12
+ *
36
13
  * @returns a string of joined class names
37
14
  *
38
15
  * @example
@@ -46,12 +23,25 @@ function classJoin(...classes) {
46
23
  }
47
24
  exports.classJoin = classJoin;
48
25
  /**
49
- * Injects css and creates unique class names
26
+ * Upserts and returns a unique css class for a given css string
50
27
  *
51
- * - Skips if already added
28
+ * @param cssString
29
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
52
30
  *
53
- * @param cssString - string or template string, to be injected
54
31
  * @returns a unique class name
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
36
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
37
+ * c3 = createClass`c: red;` // same
38
+ * c4 = css`c: red;` // same
39
+ * // c1 = c2 = c3 = c4
40
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
41
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
42
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
43
+ * ```
44
+ * ...and the queue will be executed next javascript tick
55
45
  */
56
46
  function createClass(...p) {
57
47
  let css = (0, util_1.t2s)(...p);
@@ -81,7 +71,7 @@ function createClass(...p) {
81
71
  })
82
72
  .join('\n');
83
73
  css = trimByLine(css) + '\n\n';
84
- addCss(css);
74
+ (0, addCss_js_1.addCss)(css);
85
75
  }
86
76
  return className;
87
77
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createClass.js","sourceRoot":"","sources":["../src/createClass.ts"],"names":[],"mappings":";;;AAAA,sCAAyC;AAEzC,2DAAoD;AAEpD;;;;;;;;;GASG;AACH,SAAgB,MAAM,CAAC,GAAW;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnB,UAAU,CAAC,GAAG,EAAE;QAC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,CAAC,CAAA;AACf,CAAC"}
@@ -1,37 +1,14 @@
1
1
  import {T2SProps, t2s} from '@slimr/util'
2
2
 
3
+ import {addCss} from './addCss.js'
3
4
  import {expandShorthands} from './shorthandProps.js'
4
5
 
5
- /**
6
- * Injects css to the page head
7
- *
8
- * - Lazy: Will not be added until the component mounts, if called from within a component
9
- * - Batches dom changes for better performance
10
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
11
- *
12
- * @param css - css to be injected
13
- * @returns void
14
- */
15
- export function addCss(css: string) {
16
- addCss.que.add(css)
17
- setTimeout(() => {
18
- const css = [...addCss.que].join('\n')
19
- if (css) {
20
- addCss.que.clear()
21
- const s = document.createElement('style')
22
- s.id = `u${addCss.count++}`
23
- s.innerHTML = css
24
- document.head.appendChild(s)
25
- }
26
- }, 0)
27
- }
28
- addCss.que = new Set<string>()
29
- addCss.count = 0
30
-
31
6
  /**
32
7
  * Joins class names and omits falsey props
33
8
  *
34
- * @param classes - class names to be joined
9
+ * @param classes
10
+ * class names to be joined
11
+ *
35
12
  * @returns a string of joined class names
36
13
  *
37
14
  * @example
@@ -45,12 +22,25 @@ export function classJoin(...classes: (string | 0 | null | undefined)[]) {
45
22
  }
46
23
 
47
24
  /**
48
- * Injects css and creates unique class names
25
+ * Upserts and returns a unique css class for a given css string
49
26
  *
50
- * - Skips if already added
27
+ * @param cssString
28
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
51
29
  *
52
- * @param cssString - string or template string, to be injected
53
30
  * @returns a unique class name
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
35
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
36
+ * c3 = createClass`c: red;` // same
37
+ * c4 = css`c: red;` // same
38
+ * // c1 = c2 = c3 = c4
39
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
40
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
41
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
42
+ * ```
43
+ * ...and the queue will be executed next javascript tick
54
44
  */
55
45
  export function createClass(...p: T2SProps) {
56
46
  let css = t2s(...p)
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Injects css to the page head
3
+ *
4
+ * - Lazy: Will not be added until the component mounts, if called from within a component
5
+ * - Batches dom changes for better performance
6
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
7
+ *
8
+ * @param css
9
+ * css to be injected
10
+ *
11
+ * @returns void
12
+ *
13
+ * @example
14
+ * addCss(`.myClass { color: red; }`) // queues css for injection
15
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
16
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
17
+ * // ...and the queue will be executed next javascript tick
18
+ */
19
+ export declare function addCss(css: string): void;
20
+ export declare namespace addCss {
21
+ var que: Set<string>;
22
+ var count: number;
23
+ }
package/esm/addCss.js ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Injects css to the page head
3
+ *
4
+ * - Lazy: Will not be added until the component mounts, if called from within a component
5
+ * - Batches dom changes for better performance
6
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
7
+ *
8
+ * @param css
9
+ * css to be injected
10
+ *
11
+ * @returns void
12
+ *
13
+ * @example
14
+ * addCss(`.myClass { color: red; }`) // queues css for injection
15
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
16
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
17
+ * // ...and the queue will be executed next javascript tick
18
+ */
19
+ export function addCss(css) {
20
+ addCss.que.add(css);
21
+ setTimeout(() => {
22
+ const css = [...addCss.que].join('\n');
23
+ if (css) {
24
+ addCss.que.clear();
25
+ const s = document.createElement('style');
26
+ s.id = `u${addCss.count++}`;
27
+ s.innerHTML = css;
28
+ document.head.appendChild(s);
29
+ }
30
+ }, 0);
31
+ }
32
+ addCss.que = new Set();
33
+ addCss.count = 0;
34
+ //# sourceMappingURL=addCss.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addCss.js","sourceRoot":"","sources":["../src/addCss.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,MAAM,CAAC,GAAW;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnB,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACtC,IAAI,GAAG,EAAE;YACP,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;YAClB,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YACzC,CAAC,CAAC,EAAE,GAAG,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,CAAA;YAC3B,CAAC,CAAC,SAAS,GAAG,GAAG,CAAA;YACjB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;SAC7B;IACH,CAAC,EAAE,CAAC,CAAC,CAAA;AACP,CAAC;AACD,MAAM,CAAC,GAAG,GAAG,IAAI,GAAG,EAAU,CAAA;AAC9B,MAAM,CAAC,KAAK,GAAG,CAAC,CAAA"}
package/esm/addCss.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Injects css to the page head
3
+ *
4
+ * - Lazy: Will not be added until the component mounts, if called from within a component
5
+ * - Batches dom changes for better performance
6
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
7
+ *
8
+ * @param css
9
+ * css to be injected
10
+ *
11
+ * @returns void
12
+ *
13
+ * @example
14
+ * addCss(`.myClass { color: red; }`) // queues css for injection
15
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
16
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
17
+ * // ...and the queue will be executed next javascript tick
18
+ */
19
+ export function addCss(css: string) {
20
+ addCss.que.add(css)
21
+ setTimeout(() => {
22
+ const css = [...addCss.que].join('\n')
23
+ if (css) {
24
+ addCss.que.clear()
25
+ const s = document.createElement('style')
26
+ s.id = `u${addCss.count++}`
27
+ s.innerHTML = css
28
+ document.head.appendChild(s)
29
+ }
30
+ }, 0)
31
+ }
32
+ addCss.que = new Set<string>()
33
+ addCss.count = 0
@@ -1,23 +1,10 @@
1
1
  import { T2SProps } from '@slimr/util';
2
- /**
3
- * Injects css to the page head
4
- *
5
- * - Lazy: Will not be added until the component mounts, if called from within a component
6
- * - Batches dom changes for better performance
7
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
8
- *
9
- * @param css - css to be injected
10
- * @returns void
11
- */
12
- export declare function addCss(css: string): void;
13
- export declare namespace addCss {
14
- var que: Set<string>;
15
- var count: number;
16
- }
17
2
  /**
18
3
  * Joins class names and omits falsey props
19
4
  *
20
- * @param classes - class names to be joined
5
+ * @param classes
6
+ * class names to be joined
7
+ *
21
8
  * @returns a string of joined class names
22
9
  *
23
10
  * @example
@@ -28,12 +15,25 @@ export declare namespace addCss {
28
15
  */
29
16
  export declare function classJoin(...classes: (string | 0 | null | undefined)[]): string;
30
17
  /**
31
- * Injects css and creates unique class names
18
+ * Upserts and returns a unique css class for a given css string
32
19
  *
33
- * - Skips if already added
20
+ * @param cssString
21
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
34
22
  *
35
- * @param cssString - string or template string, to be injected
36
23
  * @returns a unique class name
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
28
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
29
+ * c3 = createClass`c: red;` // same
30
+ * c4 = css`c: red;` // same
31
+ * // c1 = c2 = c3 = c4
32
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
33
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
34
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
35
+ * ```
36
+ * ...and the queue will be executed next javascript tick
37
37
  */
38
38
  export declare function createClass(...p: T2SProps): string;
39
39
  export declare namespace createClass {
@@ -1,34 +1,12 @@
1
1
  import { t2s } from '@slimr/util';
2
+ import { addCss } from './addCss.js';
2
3
  import { expandShorthands } from './shorthandProps.js';
3
- /**
4
- * Injects css to the page head
5
- *
6
- * - Lazy: Will not be added until the component mounts, if called from within a component
7
- * - Batches dom changes for better performance
8
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
9
- *
10
- * @param css - css to be injected
11
- * @returns void
12
- */
13
- export function addCss(css) {
14
- addCss.que.add(css);
15
- setTimeout(() => {
16
- const css = [...addCss.que].join('\n');
17
- if (css) {
18
- addCss.que.clear();
19
- const s = document.createElement('style');
20
- s.id = `u${addCss.count++}`;
21
- s.innerHTML = css;
22
- document.head.appendChild(s);
23
- }
24
- }, 0);
25
- }
26
- addCss.que = new Set();
27
- addCss.count = 0;
28
4
  /**
29
5
  * Joins class names and omits falsey props
30
6
  *
31
- * @param classes - class names to be joined
7
+ * @param classes
8
+ * class names to be joined
9
+ *
32
10
  * @returns a string of joined class names
33
11
  *
34
12
  * @example
@@ -41,12 +19,25 @@ export function classJoin(...classes) {
41
19
  return classes.filter(c => c && typeof c === 'string').join(' ');
42
20
  }
43
21
  /**
44
- * Injects css and creates unique class names
22
+ * Upserts and returns a unique css class for a given css string
45
23
  *
46
- * - Skips if already added
24
+ * @param cssString
25
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
47
26
  *
48
- * @param cssString - string or template string, to be injected
49
27
  * @returns a unique class name
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
32
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
33
+ * c3 = createClass`c: red;` // same
34
+ * c4 = css`c: red;` // same
35
+ * // c1 = c2 = c3 = c4
36
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
37
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
38
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
39
+ * ```
40
+ * ...and the queue will be executed next javascript tick
50
41
  */
51
42
  export function createClass(...p) {
52
43
  let css = t2s(...p);
@@ -1 +1 @@
1
- {"version":3,"file":"createClass.js","sourceRoot":"","sources":["../src/createClass.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,GAAG,EAAC,MAAM,aAAa,CAAA;AAEzC,OAAO,EAAC,gBAAgB,EAAC,MAAM,qBAAqB,CAAA;AAEpD;;;;;;;;;GASG;AACH,MAAM,UAAU,MAAM,CAAC,GAAW;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnB,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACtC,IAAI,GAAG,EAAE;YACP,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;YAClB,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YACzC,CAAC,CAAC,EAAE,GAAG,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,CAAA;YAC3B,CAAC,CAAC,SAAS,GAAG,GAAG,CAAA;YACjB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;SAC7B;IACH,CAAC,EAAE,CAAC,CAAC,CAAA;AACP,CAAC;AACD,MAAM,CAAC,GAAG,GAAG,IAAI,GAAG,EAAU,CAAA;AAC9B,MAAM,CAAC,KAAK,GAAG,CAAC,CAAA;AAEhB;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,SAAS,CAAC,GAAG,OAA0C;IACrE,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAClE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,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qBACnC,CAAC,CAAA;oBACF,MAAK;iBACN;aACF;SACF;QACD,IAAI,SAAS,KAAK,CAAC;YAAE,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,iBAAiB,GAAG,GAAG,CAAC,CAAA;KACpE;IACD,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,sCAAsC;AACtC,SAAS,UAAU,CAAC,GAAW;IAC7B,OAAO,GAAG;SACP,KAAK,CAAC,IAAI,CAAC;SACX,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;SAClB,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SACd,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC"}
@@ -1,37 +1,14 @@
1
1
  import {T2SProps, t2s} from '@slimr/util'
2
2
 
3
+ import {addCss} from './addCss.js'
3
4
  import {expandShorthands} from './shorthandProps.js'
4
5
 
5
- /**
6
- * Injects css to the page head
7
- *
8
- * - Lazy: Will not be added until the component mounts, if called from within a component
9
- * - Batches dom changes for better performance
10
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
11
- *
12
- * @param css - css to be injected
13
- * @returns void
14
- */
15
- export function addCss(css: string) {
16
- addCss.que.add(css)
17
- setTimeout(() => {
18
- const css = [...addCss.que].join('\n')
19
- if (css) {
20
- addCss.que.clear()
21
- const s = document.createElement('style')
22
- s.id = `u${addCss.count++}`
23
- s.innerHTML = css
24
- document.head.appendChild(s)
25
- }
26
- }, 0)
27
- }
28
- addCss.que = new Set<string>()
29
- addCss.count = 0
30
-
31
6
  /**
32
7
  * Joins class names and omits falsey props
33
8
  *
34
- * @param classes - class names to be joined
9
+ * @param classes
10
+ * class names to be joined
11
+ *
35
12
  * @returns a string of joined class names
36
13
  *
37
14
  * @example
@@ -45,12 +22,25 @@ export function classJoin(...classes: (string | 0 | null | undefined)[]) {
45
22
  }
46
23
 
47
24
  /**
48
- * Injects css and creates unique class names
25
+ * Upserts and returns a unique css class for a given css string
49
26
  *
50
- * - Skips if already added
27
+ * @param cssString
28
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
51
29
  *
52
- * @param cssString - string or template string, to be injected
53
30
  * @returns a unique class name
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
35
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
36
+ * c3 = createClass`c: red;` // same
37
+ * c4 = css`c: red;` // same
38
+ * // c1 = c2 = c3 = c4
39
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
40
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
41
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
42
+ * ```
43
+ * ...and the queue will be executed next javascript tick
54
44
  */
55
45
  export function createClass(...p: T2SProps) {
56
46
  let css = t2s(...p)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slimr/css",
3
- "version": "2.1.12",
3
+ "version": "2.1.14",
4
4
  "author": "Brian Dombrowski",
5
5
  "license": "ISC",
6
6
  "private": false,
@@ -34,6 +34,6 @@
34
34
  "prepack": "run-s build"
35
35
  },
36
36
  "dependencies": {
37
- "@slimr/util": "2.2.8"
37
+ "@slimr/util": "3.2.10"
38
38
  }
39
39
  }
package/src/addCss.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Injects css to the page head
3
+ *
4
+ * - Lazy: Will not be added until the component mounts, if called from within a component
5
+ * - Batches dom changes for better performance
6
+ * - Has local cache to recall prior adds, to reduce duplicates and dom changes
7
+ *
8
+ * @param css
9
+ * css to be injected
10
+ *
11
+ * @returns void
12
+ *
13
+ * @example
14
+ * addCss(`.myClass { color: red; }`) // queues css for injection
15
+ * addCss(`.myClass { color: red; }`) // is duplicate so will not be added
16
+ * addCss(`.myClass { color: blue; }`) // queues css for injection
17
+ * // ...and the queue will be executed next javascript tick
18
+ */
19
+ export function addCss(css: string) {
20
+ addCss.que.add(css)
21
+ setTimeout(() => {
22
+ const css = [...addCss.que].join('\n')
23
+ if (css) {
24
+ addCss.que.clear()
25
+ const s = document.createElement('style')
26
+ s.id = `u${addCss.count++}`
27
+ s.innerHTML = css
28
+ document.head.appendChild(s)
29
+ }
30
+ }, 0)
31
+ }
32
+ addCss.que = new Set<string>()
33
+ addCss.count = 0
@@ -1,37 +1,14 @@
1
1
  import {T2SProps, t2s} from '@slimr/util'
2
2
 
3
+ import {addCss} from './addCss.js'
3
4
  import {expandShorthands} from './shorthandProps.js'
4
5
 
5
- /**
6
- * Injects css to the page head
7
- *
8
- * - Lazy: Will not be added until the component mounts, if called from within a component
9
- * - Batches dom changes for better performance
10
- * - Has local cache to recall prior adds, to reduce duplicates and dom changes
11
- *
12
- * @param css - css to be injected
13
- * @returns void
14
- */
15
- export function addCss(css: string) {
16
- addCss.que.add(css)
17
- setTimeout(() => {
18
- const css = [...addCss.que].join('\n')
19
- if (css) {
20
- addCss.que.clear()
21
- const s = document.createElement('style')
22
- s.id = `u${addCss.count++}`
23
- s.innerHTML = css
24
- document.head.appendChild(s)
25
- }
26
- }, 0)
27
- }
28
- addCss.que = new Set<string>()
29
- addCss.count = 0
30
-
31
6
  /**
32
7
  * Joins class names and omits falsey props
33
8
  *
34
- * @param classes - class names to be joined
9
+ * @param classes
10
+ * class names to be joined
11
+ *
35
12
  * @returns a string of joined class names
36
13
  *
37
14
  * @example
@@ -45,12 +22,25 @@ export function classJoin(...classes: (string | 0 | null | undefined)[]) {
45
22
  }
46
23
 
47
24
  /**
48
- * Injects css and creates unique class names
25
+ * Upserts and returns a unique css class for a given css string
49
26
  *
50
- * - Skips if already added
27
+ * @param cssString
28
+ * string or template string, to be injected. Shorthand props are supported (see Readme).
51
29
  *
52
- * @param cssString - string or template string, to be injected
53
30
  * @returns a unique class name
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * c1 = createClass('c: red;') // queues the create of new css class 's0'
35
+ * c2 = createClass('c: red;') // is duplicate so will return 's0'
36
+ * c3 = createClass`c: red;` // same
37
+ * c4 = css`c: red;` // same
38
+ * // c1 = c2 = c3 = c4
39
+ * <div className={css`c: red;`} /> // will resolve to 's0' like above
40
+ * c5 = css`c: blue;` // queues the create of new css class 's1'
41
+ * c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
42
+ * ```
43
+ * ...and the queue will be executed next javascript tick
54
44
  */
55
45
  export function createClass(...p: T2SProps) {
56
46
  let css = t2s(...p)