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