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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kk-web-components",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "A collection of reusable web components, including ui-icon and others.",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",
package/src/index.js CHANGED
@@ -1,8 +1,2 @@
1
- import UiIcon from "./ui-icon.js";
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
- // Define shared styles as a string
2
- const sharedStylesText = `
3
- :host {
4
- flex-shrink: 0;
5
- display: block;
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
- // Create a constructable stylesheet if supported
13
- const sharedStyles = new CSSStyleSheet();
14
- sharedStyles.replaceSync(sharedStylesText);
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
- // Attach Shadow DOM
20
- const shadow = this.attachShadow({ mode: "open" });
18
+ this.attachShadow({ mode: "open" });
21
19
 
22
- // Apply styles
23
- if (shadow.adoptedStyleSheets) {
24
- shadow.adoptedStyleSheets = [sharedStyles];
20
+ if (this.shadowRoot.adoptedStyleSheets) {
21
+ this.shadowRoot.adoptedStyleSheets = [sharedStyles];
25
22
  } else {
26
- // Fallback for browsers without adoptedStyleSheets (e.g., Safari)
27
- const style = document.createElement('style');
28
- style.textContent = sharedStylesText; // Use the same shared styles text
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
- const config = window.UiIcon;
35
- const urlParametersObj = {};
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
- // Get all attributes with defaults
33
+ render() {
52
34
  const options = this.getOptions({
53
35
  name: null,
54
- size: defaultSize,
36
+ icon: null,
37
+ size: config.defaultSize,
55
38
  rotation: 0,
56
39
  viewbox: null,
57
40
  });
58
41
 
59
- const { name, size, rotation, viewbox } = options;
42
+ const iconName = options.icon || options.name;
60
43
 
61
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
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
- // Hide from screen readers
50
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
64
51
  svg.setAttribute("aria-hidden", "true");
65
- svg.setAttribute("part", "icon"); // Expose svg for styling
52
+ svg.setAttribute("part", "icon");
66
53
 
67
- // Validate values
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
- if (Object.keys(urlParametersObj).length > 0) {
84
- const urlParameters = `?${Object.entries(urlParametersObj)
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 = ""; // Clear old content
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.connectedCallback();
101
+ this.render();
121
102
  }
122
103
  }
123
104
  }
124
105
 
125
- // Register the custom element
126
- customElements.define("ui-icon", UiIcon);
106
+ const sharedStylesText = `
107
+ :host {
108
+ flex-shrink: 0;
109
+ display: block;
110
+ }
111
+ svg {
112
+ display: block;
113
+ }
114
+ `;
127
115
 
128
- export default UiIcon;
116
+ const sharedStyles = new CSSStyleSheet();
117
+ sharedStyles.replaceSync(sharedStylesText);