@acusti/styling 0.3.1 → 0.4.0
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/dist/Style.js +12 -13
- package/dist/Style.js.flow +4 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -2
- package/dist/index.js.flow +3 -3
- package/dist/style-registry.d.ts +5 -1
- package/dist/style-registry.js +16 -16
- package/dist/style-registry.js.flow +10 -10
- package/package.json +5 -4
- package/src/Style.tsx +46 -0
- package/src/index.ts +4 -0
- package/src/style-registry.ts +72 -0
package/dist/Style.js
CHANGED
|
@@ -4,19 +4,19 @@ const { useCallback, useEffect, useRef, useState } = React;
|
|
|
4
4
|
const Style = ({ children: styles }) => {
|
|
5
5
|
const [ownerDocument, setOwnerDocument] = useState(null);
|
|
6
6
|
const handleRef = useCallback((element) => {
|
|
7
|
-
if (!element)
|
|
8
|
-
return;
|
|
7
|
+
if (!element) return;
|
|
9
8
|
setOwnerDocument(element.ownerDocument);
|
|
10
9
|
}, []);
|
|
11
|
-
useEffect(
|
|
12
|
-
|
|
13
|
-
return;
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
useEffect(
|
|
11
|
+
() => () => {
|
|
12
|
+
if (!ownerDocument) return;
|
|
13
|
+
unregisterStyles({ ownerDocument, styles });
|
|
14
|
+
},
|
|
15
|
+
[ownerDocument],
|
|
16
|
+
);
|
|
16
17
|
const previousStylesRef = useRef('');
|
|
17
18
|
useEffect(() => {
|
|
18
|
-
if (!ownerDocument)
|
|
19
|
-
return;
|
|
19
|
+
if (!ownerDocument) return;
|
|
20
20
|
updateStyles({
|
|
21
21
|
ownerDocument,
|
|
22
22
|
previousStyles: previousStylesRef.current,
|
|
@@ -24,9 +24,8 @@ const Style = ({ children: styles }) => {
|
|
|
24
24
|
});
|
|
25
25
|
previousStylesRef.current = styles;
|
|
26
26
|
}, [ownerDocument, styles]);
|
|
27
|
-
if (ownerDocument)
|
|
28
|
-
|
|
29
|
-
return React.createElement("style", { ref: handleRef }, styles);
|
|
27
|
+
if (ownerDocument) return null;
|
|
28
|
+
return React.createElement('style', { ref: handleRef }, styles);
|
|
30
29
|
};
|
|
31
30
|
export default Style;
|
|
32
|
-
//# sourceMappingURL=Style.js.map
|
|
31
|
+
//# sourceMappingURL=Style.js.map
|
package/dist/Style.js.flow
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for Style
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.
|
|
4
|
+
* Flowgen v1.14.1
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
declare type Props = {
|
|
9
|
-
|
|
10
|
-
};
|
|
8
|
+
declare type Props = {|
|
|
9
|
+
children: string,
|
|
10
|
+
|};
|
|
11
11
|
declare var Style: (x: Props) => React$Node | null;
|
|
12
12
|
declare export default typeof Style;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { default as Style } from './Style.js';
|
|
2
|
-
export declare const SYSTEM_UI_FONT =
|
|
2
|
+
export declare const SYSTEM_UI_FONT =
|
|
3
|
+
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { default as Style } from './Style.js';
|
|
2
|
-
export const SYSTEM_UI_FONT =
|
|
3
|
-
|
|
2
|
+
export const SYSTEM_UI_FONT =
|
|
3
|
+
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.flow
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for index
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.
|
|
4
|
+
* Flowgen v1.14.1
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
declare export { default as Style } from
|
|
9
|
-
declare export var SYSTEM_UI_FONT:
|
|
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';
|
package/dist/style-registry.d.ts
CHANGED
|
@@ -9,5 +9,9 @@ declare type UpdatePayload = {
|
|
|
9
9
|
previousStyles: string;
|
|
10
10
|
styles: string;
|
|
11
11
|
};
|
|
12
|
-
export declare const updateStyles: ({
|
|
12
|
+
export declare const updateStyles: ({
|
|
13
|
+
ownerDocument,
|
|
14
|
+
previousStyles,
|
|
15
|
+
styles,
|
|
16
|
+
}: UpdatePayload) => void;
|
|
13
17
|
export {};
|
package/dist/style-registry.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
const styleRegistry = new Map();
|
|
2
2
|
export const registerStyles = ({ ownerDocument, styles }) => {
|
|
3
|
-
if (!styles)
|
|
4
|
-
return;
|
|
3
|
+
if (!styles) return;
|
|
5
4
|
const stylesMap = styleRegistry.get(styles);
|
|
6
|
-
const existingStylesItem =
|
|
5
|
+
const existingStylesItem =
|
|
6
|
+
stylesMap === null || stylesMap === void 0
|
|
7
|
+
? void 0
|
|
8
|
+
: stylesMap.get(ownerDocument);
|
|
7
9
|
if (existingStylesItem) {
|
|
8
10
|
existingStylesItem.referenceCount++;
|
|
9
11
|
return;
|
|
@@ -20,15 +22,15 @@ export const registerStyles = ({ ownerDocument, styles }) => {
|
|
|
20
22
|
styleRegistry.set(styles, new Map([[ownerDocument, stylesItem]]));
|
|
21
23
|
};
|
|
22
24
|
export const unregisterStyles = ({ ownerDocument, styles }) => {
|
|
23
|
-
if (!styles)
|
|
24
|
-
return;
|
|
25
|
+
if (!styles) return;
|
|
25
26
|
const stylesMap = styleRegistry.get(styles);
|
|
26
|
-
const stylesItem =
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
const stylesItem =
|
|
28
|
+
stylesMap === null || stylesMap === void 0
|
|
29
|
+
? void 0
|
|
30
|
+
: stylesMap.get(ownerDocument);
|
|
31
|
+
if (!stylesMap || !stylesItem) return;
|
|
29
32
|
stylesItem.referenceCount--;
|
|
30
|
-
if (stylesItem.referenceCount)
|
|
31
|
-
return;
|
|
33
|
+
if (stylesItem.referenceCount) return;
|
|
32
34
|
// If no more references to these styles in this document, remove <style> element from the DOM
|
|
33
35
|
const { parentElement } = stylesItem.element;
|
|
34
36
|
if (parentElement) {
|
|
@@ -36,17 +38,15 @@ export const unregisterStyles = ({ ownerDocument, styles }) => {
|
|
|
36
38
|
}
|
|
37
39
|
// Then remove the document Map
|
|
38
40
|
stylesMap.delete(ownerDocument);
|
|
39
|
-
if (stylesMap.size)
|
|
40
|
-
return;
|
|
41
|
+
if (stylesMap.size) return;
|
|
41
42
|
// If no more references to these styles in any document, remove it entirely
|
|
42
43
|
styleRegistry.delete(styles);
|
|
43
44
|
};
|
|
44
|
-
export const updateStyles = ({ ownerDocument, previousStyles, styles
|
|
45
|
-
if (previousStyles === styles)
|
|
46
|
-
return;
|
|
45
|
+
export const updateStyles = ({ ownerDocument, previousStyles, styles }) => {
|
|
46
|
+
if (previousStyles === styles) return;
|
|
47
47
|
if (previousStyles) {
|
|
48
48
|
unregisterStyles({ ownerDocument, styles: previousStyles });
|
|
49
49
|
}
|
|
50
50
|
registerStyles({ ownerDocument, styles });
|
|
51
51
|
};
|
|
52
|
-
//# sourceMappingURL=style-registry.js.map
|
|
52
|
+
//# sourceMappingURL=style-registry.js.map
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flowtype definitions for style-registry
|
|
3
3
|
* Generated by Flowgen from a Typescript Definition
|
|
4
|
-
* Flowgen v1.
|
|
4
|
+
* Flowgen v1.14.1
|
|
5
5
|
* @flow
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
declare type Payload = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
8
|
+
declare type Payload = {|
|
|
9
|
+
ownerDocument: Document,
|
|
10
|
+
styles: string,
|
|
11
|
+
|};
|
|
12
12
|
declare export var registerStyles: (x: Payload) => void;
|
|
13
13
|
declare export var unregisterStyles: (x: Payload) => void;
|
|
14
|
-
declare type UpdatePayload = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
14
|
+
declare type UpdatePayload = {|
|
|
15
|
+
ownerDocument: Document,
|
|
16
|
+
previousStyles: string,
|
|
17
|
+
styles: string,
|
|
18
|
+
|};
|
|
19
19
|
declare export var updateStyles: (x: UpdatePayload) => void;
|
|
20
20
|
declare export {};
|
package/package.json
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acusti/styling",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
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> (
|
|
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"
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
"homepage": "https://github.com/acusti/uikit/tree/main/packages/styling#readme",
|
|
22
23
|
"devDependencies": {
|
|
23
24
|
"@types/react": "^17.0.3",
|
|
24
|
-
"typescript": "^4.
|
|
25
|
+
"typescript": "^4.4.3"
|
|
25
26
|
},
|
|
26
27
|
"peerDependencies": {
|
|
27
28
|
"react": "^16.8 || ^17",
|
package/src/Style.tsx
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
const handleRef = useCallback((element: HTMLElement | null) => {
|
|
15
|
+
if (!element) return;
|
|
16
|
+
setOwnerDocument(element.ownerDocument);
|
|
17
|
+
}, []);
|
|
18
|
+
|
|
19
|
+
useEffect(
|
|
20
|
+
() => () => {
|
|
21
|
+
if (!ownerDocument) return;
|
|
22
|
+
unregisterStyles({ ownerDocument, styles });
|
|
23
|
+
},
|
|
24
|
+
[ownerDocument], // eslint-disable-line react-hooks/exhaustive-deps
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const previousStylesRef = useRef<string>('');
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!ownerDocument) return;
|
|
31
|
+
|
|
32
|
+
updateStyles({
|
|
33
|
+
ownerDocument,
|
|
34
|
+
previousStyles: previousStylesRef.current,
|
|
35
|
+
styles,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
previousStylesRef.current = styles;
|
|
39
|
+
}, [ownerDocument, styles]);
|
|
40
|
+
|
|
41
|
+
if (ownerDocument) return null;
|
|
42
|
+
|
|
43
|
+
return <style ref={handleRef}>{styles}</style>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default Style;
|
package/src/index.ts
ADDED
|
@@ -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
|
+
};
|