@acusti/styling 0.3.1 → 0.5.1

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,7 @@
1
1
  # @acusti/styling
2
2
 
3
3
  [![latest version](https://img.shields.io/npm/v/@acusti/styling?style=for-the-badge)](https://www.npmjs.com/package/@acusti/styling)
4
- [![dependencies status](https://img.shields.io/david/acusti/uikit?path=packages%2Fstyling&style=for-the-badge)](https://david-dm.org/acusti/uikit?path=packages%2Fstyling)
4
+ [![dependencies status](https://img.shields.io/librariesio/release/npm/@acusti/styling?style=for-the-badge)](https://libraries.io/npm/@acusti%2Fstyling/sourcerank)
5
5
  [![bundle size](https://img.shields.io/bundlephobia/minzip/@acusti/styling?style=for-the-badge)](https://bundlephobia.com/package/@acusti/styling)
6
6
  [![downloads per month](https://img.shields.io/npm/dm/@acusti/styling?style=for-the-badge)](https://www.npmjs.com/package/@acusti/styling)
7
7
 
package/dist/Style.js CHANGED
@@ -3,11 +3,6 @@ import { unregisterStyles, updateStyles } from './style-registry.js';
3
3
  const { useCallback, useEffect, useRef, useState } = React;
4
4
  const Style = ({ children: styles }) => {
5
5
  const [ownerDocument, setOwnerDocument] = useState(null);
6
- const handleRef = useCallback((element) => {
7
- if (!element)
8
- return;
9
- setOwnerDocument(element.ownerDocument);
10
- }, []);
11
6
  useEffect(() => () => {
12
7
  if (!ownerDocument)
13
8
  return;
@@ -24,9 +19,16 @@ const Style = ({ children: styles }) => {
24
19
  });
25
20
  previousStylesRef.current = styles;
26
21
  }, [ownerDocument, styles]);
22
+ const handleRef = useCallback((element) => {
23
+ if (!element)
24
+ return;
25
+ setOwnerDocument(element.ownerDocument);
26
+ }, []);
27
27
  if (ownerDocument)
28
28
  return null;
29
- return React.createElement("style", { ref: handleRef }, styles);
29
+ return (React.createElement("style", { dangerouslySetInnerHTML: {
30
+ __html: styles,
31
+ }, ref: handleRef }));
30
32
  };
31
33
  export default Style;
32
34
  //# sourceMappingURL=Style.js.map
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * Flowtype definitions for Style
3
3
  * Generated by Flowgen from a Typescript Definition
4
- * Flowgen v1.13.0
4
+ * Flowgen v1.20.0
5
5
  * @flow
6
6
  */
7
7
 
8
- declare type Props = {
8
+ declare type Props = {|
9
9
  children: string,
10
- };
10
+ |};
11
11
  declare var Style: (x: Props) => React$Node | null;
12
12
  declare export default typeof Style;
package/dist/Style.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Style.js","sourceRoot":"","sources":["../src/Style.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAErE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;AAM3D,MAAM,KAAK,GAAG,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAS,EAAE,EAAE;IAC1C,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAAkB,IAAI,CAAC,CAAC;IAE1E,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,OAA2B,EAAE,EAAE;QAC1D,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,gBAAgB,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,CACL,GAAG,EAAE,CAAC,GAAG,EAAE;QACP,IAAI,CAAC,aAAa;YAAE,OAAO;QAC3B,gBAAgB,CAAC,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,CAAC;IAChD,CAAC,EACD,CAAC,aAAa,CAAC,CAClB,CAAC;IAEF,MAAM,iBAAiB,GAAG,MAAM,CAAS,EAAE,CAAC,CAAC;IAE7C,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,CAAC,aAAa;YAAE,OAA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C,CAAC,EAAE,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,CAAC;IAE5B,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,OAA2B,EAAE,EAAE;QAC1D,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,gBAAgB,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAI,aAAa;QAAE,OAAO,IAAI,CAAC;IAE/B,OAAO,CACH,+BACI,uBAAuB,EAAE;YACrB,MAAM,EAAE,MAAM;SACjB,EACD,GAAG,EAAE,SAAS,GAChB,CACL,CAAC;AACN,CAAC,CAAC;AAEF,eAAe,KAAK,CAAC"}
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Flowtype definitions for index
3
3
  * Generated by Flowgen from a Typescript Definition
4
- * Flowgen v1.13.0
4
+ * Flowgen v1.20.0
5
5
  * @flow
6
6
  */
7
7
 
8
- declare export { default as Style } from "./Style";
9
- declare export var SYSTEM_UI_FONT: any; // "-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif"
8
+ declare export { default as Style } from "./Style.js";
9
+ declare export var SYSTEM_UI_FONT: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
@@ -1,20 +1,19 @@
1
1
  /**
2
2
  * Flowtype definitions for style-registry
3
3
  * Generated by Flowgen from a Typescript Definition
4
- * Flowgen v1.13.0
4
+ * Flowgen v1.20.0
5
5
  * @flow
6
6
  */
7
7
 
8
- declare type Payload = {
8
+ declare type Payload = {|
9
9
  ownerDocument: Document,
10
10
  styles: string,
11
- };
11
+ |};
12
12
  declare export var registerStyles: (x: Payload) => void;
13
13
  declare export var unregisterStyles: (x: Payload) => void;
14
- declare type UpdatePayload = {
14
+ declare type UpdatePayload = {|
15
15
  ownerDocument: Document,
16
16
  previousStyles: string,
17
17
  styles: string,
18
- };
18
+ |};
19
19
  declare export var updateStyles: (x: UpdatePayload) => void;
20
- declare export {};
package/package.json CHANGED
@@ -1,30 +1,31 @@
1
1
  {
2
2
  "name": "@acusti/styling",
3
- "version": "0.3.1",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "exports": "./dist/index.js",
7
7
  "main": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
9
9
  "files": [
10
- "dist"
10
+ "dist",
11
+ "src"
11
12
  ],
12
13
  "repository": {
13
14
  "type": "git",
14
15
  "url": "git+https://github.com/acusti/uikit.git"
15
16
  },
16
- "author": "andrew patton <andrew@acusti.ca> (http://acusti.ca)",
17
+ "author": "andrew patton <andrew@acusti.ca> (https://www.acusti.ca)",
17
18
  "license": "Unlicense",
18
19
  "bugs": {
19
20
  "url": "https://github.com/acusti/uikit/issues"
20
21
  },
21
22
  "homepage": "https://github.com/acusti/uikit/tree/main/packages/styling#readme",
22
23
  "devDependencies": {
23
- "@types/react": "^17.0.3",
24
- "typescript": "^4.2.3"
24
+ "@types/react": "^18.0.15",
25
+ "typescript": "~4.6.4"
25
26
  },
26
27
  "peerDependencies": {
27
- "react": "^16.8 || ^17",
28
- "react-dom": "^16.8 || ^17"
28
+ "react": "^16.8 || ^17 || ^18",
29
+ "react-dom": "^16.8 || ^17 || ^18"
29
30
  }
30
31
  }
package/src/Style.tsx ADDED
@@ -0,0 +1,53 @@
1
+ import * as React from 'react';
2
+
3
+ import { unregisterStyles, updateStyles } from './style-registry.js';
4
+
5
+ const { useCallback, useEffect, useRef, useState } = React;
6
+
7
+ type Props = {
8
+ children: string;
9
+ };
10
+
11
+ const Style = ({ children: styles }: Props) => {
12
+ const [ownerDocument, setOwnerDocument] = useState<Document | null>(null);
13
+
14
+ useEffect(
15
+ () => () => {
16
+ if (!ownerDocument) return;
17
+ unregisterStyles({ ownerDocument, styles });
18
+ },
19
+ [ownerDocument], // eslint-disable-line react-hooks/exhaustive-deps
20
+ );
21
+
22
+ const previousStylesRef = useRef<string>('');
23
+
24
+ useEffect(() => {
25
+ if (!ownerDocument) return;
26
+
27
+ updateStyles({
28
+ ownerDocument,
29
+ previousStyles: previousStylesRef.current,
30
+ styles,
31
+ });
32
+
33
+ previousStylesRef.current = styles;
34
+ }, [ownerDocument, styles]);
35
+
36
+ const handleRef = useCallback((element: HTMLElement | null) => {
37
+ if (!element) return;
38
+ setOwnerDocument(element.ownerDocument);
39
+ }, []);
40
+
41
+ if (ownerDocument) return null;
42
+
43
+ return (
44
+ <style
45
+ dangerouslySetInnerHTML={{
46
+ __html: styles,
47
+ }}
48
+ ref={handleRef}
49
+ />
50
+ );
51
+ };
52
+
53
+ export default Style;
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { default as Style } from './Style.js';
2
+
3
+ export const SYSTEM_UI_FONT =
4
+ '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
@@ -0,0 +1,72 @@
1
+ type StyleRegistry = Map<
2
+ string,
3
+ Map<Document, { element: HTMLStyleElement; referenceCount: number }>
4
+ >;
5
+
6
+ const styleRegistry: StyleRegistry = new Map();
7
+
8
+ type Payload = { ownerDocument: Document; styles: string };
9
+
10
+ export const registerStyles = ({ ownerDocument, styles }: Payload) => {
11
+ if (!styles) return;
12
+
13
+ const stylesMap = styleRegistry.get(styles);
14
+ const existingStylesItem = stylesMap?.get(ownerDocument);
15
+
16
+ if (existingStylesItem) {
17
+ existingStylesItem.referenceCount++;
18
+ return;
19
+ }
20
+
21
+ const element = ownerDocument.createElement('style');
22
+ element.setAttribute('data-ukt-styling', '');
23
+ element.innerHTML = styles;
24
+ ownerDocument.head.appendChild(element);
25
+ const stylesItem = { element, referenceCount: 1 };
26
+
27
+ if (stylesMap) {
28
+ stylesMap.set(ownerDocument, stylesItem);
29
+ return;
30
+ }
31
+
32
+ styleRegistry.set(styles, new Map([[ownerDocument, stylesItem]]));
33
+ };
34
+
35
+ export const unregisterStyles = ({ ownerDocument, styles }: Payload) => {
36
+ if (!styles) return;
37
+
38
+ const stylesMap = styleRegistry.get(styles);
39
+ const stylesItem = stylesMap?.get(ownerDocument);
40
+ if (!stylesMap || !stylesItem) return;
41
+
42
+ stylesItem.referenceCount--;
43
+ if (stylesItem.referenceCount) return;
44
+
45
+ // If no more references to these styles in this document, remove <style> element from the DOM
46
+ const { parentElement } = stylesItem.element;
47
+ if (parentElement) {
48
+ parentElement.removeChild(stylesItem.element);
49
+ }
50
+ // Then remove the document Map
51
+ stylesMap.delete(ownerDocument);
52
+
53
+ if (stylesMap.size) return;
54
+ // If no more references to these styles in any document, remove it entirely
55
+ styleRegistry.delete(styles);
56
+ };
57
+
58
+ type UpdatePayload = { ownerDocument: Document; previousStyles: string; styles: string };
59
+
60
+ export const updateStyles = ({
61
+ ownerDocument,
62
+ previousStyles,
63
+ styles,
64
+ }: UpdatePayload) => {
65
+ if (previousStyles === styles) return;
66
+
67
+ if (previousStyles) {
68
+ unregisterStyles({ ownerDocument, styles: previousStyles });
69
+ }
70
+
71
+ registerStyles({ ownerDocument, styles });
72
+ };