kk-web-components 1.0.7 → 1.1.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/package.json +1 -1
- package/src/index.js +2 -8
- package/src/ui-icon.js +53 -64
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export { UiIcon };
|
|
4
|
-
|
|
5
|
-
export function registerComponents() {
|
|
6
|
-
customElements.define("ui-icon", UiIcon);
|
|
7
|
-
customElements.define("other-component");
|
|
8
|
-
}
|
|
1
|
+
import { defineUiIcon } from "./ui-icon.js";
|
|
2
|
+
export { defineUiIcon };
|
package/src/ui-icon.js
CHANGED
|
@@ -1,101 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
svg {
|
|
8
|
-
display: block;
|
|
9
|
-
}
|
|
10
|
-
`;
|
|
1
|
+
let config = {
|
|
2
|
+
version: Date.now(),
|
|
3
|
+
filePath: "/assets/iconset.svg",
|
|
4
|
+
defaultSize: 24,
|
|
5
|
+
};
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
export function defineUiIcon(userConfig = {}) {
|
|
8
|
+
config = { ...config, ...userConfig };
|
|
9
|
+
|
|
10
|
+
if (!customElements.get("ui-icon")) {
|
|
11
|
+
customElements.define("ui-icon", UiIcon);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
15
14
|
|
|
16
15
|
class UiIcon extends HTMLElement {
|
|
17
16
|
constructor() {
|
|
18
17
|
super();
|
|
19
|
-
|
|
20
|
-
const shadow = this.attachShadow({ mode: "open" });
|
|
18
|
+
this.attachShadow({ mode: "open" });
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
shadow.adoptedStyleSheets = [sharedStyles];
|
|
20
|
+
if (this.shadowRoot.adoptedStyleSheets) {
|
|
21
|
+
this.shadowRoot.adoptedStyleSheets = [sharedStyles];
|
|
25
22
|
} else {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
style
|
|
29
|
-
shadow.appendChild(style);
|
|
23
|
+
const style = document.createElement("style");
|
|
24
|
+
style.textContent = sharedStylesText;
|
|
25
|
+
this.shadowRoot.appendChild(style);
|
|
30
26
|
}
|
|
31
27
|
}
|
|
32
28
|
|
|
33
29
|
connectedCallback() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
let filepath = "/assets/iconset.svg";
|
|
37
|
-
let defaultSize = 24;
|
|
38
|
-
|
|
39
|
-
if (config) {
|
|
40
|
-
if (config.version) {
|
|
41
|
-
urlParametersObj["v"] = config.version;
|
|
42
|
-
}
|
|
43
|
-
if (config.filePath) {
|
|
44
|
-
filepath = config.filePath;
|
|
45
|
-
}
|
|
46
|
-
if (config.defaultSize) {
|
|
47
|
-
defaultSize = config.defaultSize;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
30
|
+
this.render();
|
|
31
|
+
}
|
|
50
32
|
|
|
51
|
-
|
|
33
|
+
render() {
|
|
52
34
|
const options = this.getOptions({
|
|
53
35
|
name: null,
|
|
54
|
-
|
|
36
|
+
icon: null,
|
|
37
|
+
size: config.defaultSize,
|
|
55
38
|
rotation: 0,
|
|
56
39
|
viewbox: null,
|
|
57
40
|
});
|
|
58
41
|
|
|
59
|
-
const
|
|
42
|
+
const iconName = options.icon || options.name;
|
|
60
43
|
|
|
61
|
-
|
|
44
|
+
if (options.name && !options.icon) {
|
|
45
|
+
console.warn(`[ui-icon] Attribute "name" is deprecated. Use "icon" instead.`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { size, rotation, viewbox } = options;
|
|
62
49
|
|
|
63
|
-
|
|
50
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
64
51
|
svg.setAttribute("aria-hidden", "true");
|
|
65
|
-
svg.setAttribute("part", "icon");
|
|
52
|
+
svg.setAttribute("part", "icon");
|
|
66
53
|
|
|
67
|
-
|
|
68
|
-
const isValidSize = Number.isInteger(size) && size > 0;
|
|
54
|
+
const isValidSize = Number.isFinite(size) && size > 0;
|
|
69
55
|
const isValidViewbox = viewbox && /^(\d+(\.\d+)?\s){3}\d+(\.\d+)?$/.test(viewbox);
|
|
70
56
|
const isValidRotation = !isNaN(parseFloat(rotation));
|
|
71
|
-
|
|
57
|
+
|
|
72
58
|
if (isValidViewbox) {
|
|
73
59
|
svg.setAttribute("viewBox", viewbox);
|
|
60
|
+
svg.setAttribute("width", size);
|
|
61
|
+
svg.setAttribute("height", size);
|
|
74
62
|
} else if (isValidSize) {
|
|
75
63
|
svg.setAttribute("width", size);
|
|
76
64
|
svg.setAttribute("height", size);
|
|
77
65
|
}
|
|
66
|
+
|
|
78
67
|
if (isValidRotation) {
|
|
79
68
|
svg.style.transform = `rotate(${rotation}deg)`;
|
|
80
69
|
}
|
|
81
70
|
|
|
82
71
|
const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
.map(([key, value]) => `${key}=${value}`)
|
|
86
|
-
.join("&")}`;
|
|
87
|
-
use.setAttribute("href", `${filepath}${urlParameters}#${name}`);
|
|
88
|
-
} else {
|
|
89
|
-
use.setAttribute("href", `${filepath}#${name}`);
|
|
90
|
-
}
|
|
72
|
+
const encodedIconName = encodeURIComponent(iconName || "");
|
|
73
|
+
const urlParams = `?v=${encodeURIComponent(config.version)}`;
|
|
91
74
|
|
|
75
|
+
use.setAttribute("href", `${config.filePath}${urlParams}#${encodedIconName}`);
|
|
92
76
|
svg.appendChild(use);
|
|
93
77
|
|
|
94
|
-
this.shadowRoot.innerHTML = "";
|
|
78
|
+
this.shadowRoot.innerHTML = "";
|
|
95
79
|
this.shadowRoot.appendChild(svg);
|
|
96
80
|
}
|
|
97
81
|
|
|
98
|
-
// Utility to get attributes with defaults
|
|
99
82
|
getOptions(defaults) {
|
|
100
83
|
return Object.keys(defaults).reduce((acc, key) => {
|
|
101
84
|
const value = this.getAttribute(key);
|
|
@@ -109,20 +92,26 @@ class UiIcon extends HTMLElement {
|
|
|
109
92
|
}, {});
|
|
110
93
|
}
|
|
111
94
|
|
|
112
|
-
// Observe relevant attributes
|
|
113
95
|
static get observedAttributes() {
|
|
114
|
-
return ["name", "size", "rotation", "viewbox"];
|
|
96
|
+
return ["name", "icon", "size", "rotation", "viewbox"];
|
|
115
97
|
}
|
|
116
98
|
|
|
117
|
-
// Handle attribute changes
|
|
118
99
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
119
100
|
if (oldValue !== newValue) {
|
|
120
|
-
this.
|
|
101
|
+
this.render();
|
|
121
102
|
}
|
|
122
103
|
}
|
|
123
104
|
}
|
|
124
105
|
|
|
125
|
-
|
|
126
|
-
|
|
106
|
+
const sharedStylesText = `
|
|
107
|
+
:host {
|
|
108
|
+
flex-shrink: 0;
|
|
109
|
+
display: block;
|
|
110
|
+
}
|
|
111
|
+
svg {
|
|
112
|
+
display: block;
|
|
113
|
+
}
|
|
114
|
+
`;
|
|
127
115
|
|
|
128
|
-
|
|
116
|
+
const sharedStyles = new CSSStyleSheet();
|
|
117
|
+
sharedStyles.replaceSync(sharedStylesText);
|