kk-web-components 1.0.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/README.md +41 -0
- package/package.json +13 -0
- package/src/index.js +3 -0
- package/src/ui-icon.js +111 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# KK Web Components
|
|
2
|
+
|
|
3
|
+
A collection of reusable web components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install kk-web-components
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Importing the Components
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import "kk-web-components";
|
|
17
|
+
|
|
18
|
+
// Optionally configure UiIcon globally
|
|
19
|
+
window.UiIcon = {
|
|
20
|
+
version: "1.0",
|
|
21
|
+
filePath: "/icons/iconset.svg",
|
|
22
|
+
defaultSize: 24,
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Components
|
|
27
|
+
|
|
28
|
+
#### `<ui-icon>`
|
|
29
|
+
|
|
30
|
+
- **Attributes**:
|
|
31
|
+
- `name`: The name of the icon in the sprite sheet.
|
|
32
|
+
- `size`: The size of the icon (default: `24`).
|
|
33
|
+
- `rotation`: Rotate the icon in degrees (default: `0`).
|
|
34
|
+
- `viewbox`: Optional custom `viewBox` for the SVG.
|
|
35
|
+
|
|
36
|
+
Example:
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<ui-icon name="home" size="32" rotation="45"></ui-icon>
|
|
40
|
+
<ui-icon name="settings"></ui-icon>
|
|
41
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kk-web-components",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A collection of reusable web components, including ui-icon and others.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"module": "src/index.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"keywords": ["web-components", "ui-icon", "custom-elements", "svg"],
|
|
9
|
+
"author": "Krankikom GmbH",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"No tests specified\" && exit 0"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/index.js
ADDED
package/src/ui-icon.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Define a shared stylesheet using Constructable Stylesheets
|
|
2
|
+
const sharedStyles = new CSSStyleSheet();
|
|
3
|
+
sharedStyles.replaceSync(`
|
|
4
|
+
:host {
|
|
5
|
+
flex-shrink: 0;
|
|
6
|
+
|
|
7
|
+
svg {
|
|
8
|
+
display: block;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
`);
|
|
12
|
+
|
|
13
|
+
class UiIcon extends HTMLElement {
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
// Attach Shadow DOM and apply shared styles
|
|
17
|
+
const shadow = this.attachShadow({ mode: "open" });
|
|
18
|
+
shadow.adoptedStyleSheets = [sharedStyles];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
connectedCallback() {
|
|
22
|
+
const config = window.UiIcon;
|
|
23
|
+
const urlParametersObj = {};
|
|
24
|
+
let filepath = "/assets/iconset.svg";
|
|
25
|
+
let defaultSize = 24;
|
|
26
|
+
|
|
27
|
+
if (config) {
|
|
28
|
+
if (config.version) {
|
|
29
|
+
urlParametersObj["v"] = config.version;
|
|
30
|
+
}
|
|
31
|
+
if (config.filePath) {
|
|
32
|
+
filepath = config.filePath;
|
|
33
|
+
}
|
|
34
|
+
if (config.defaultSize) {
|
|
35
|
+
defaultSize = config.defaultSize;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Get all attributes with defaults
|
|
40
|
+
const options = this.getOptions({
|
|
41
|
+
name: null,
|
|
42
|
+
size: defaultSize,
|
|
43
|
+
rotation: 0,
|
|
44
|
+
viewbox: null,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const { name, size, rotation, viewbox } = options;
|
|
48
|
+
|
|
49
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
50
|
+
|
|
51
|
+
// Hide from screen readers
|
|
52
|
+
svg.setAttribute("aria-hidden", "true");
|
|
53
|
+
|
|
54
|
+
if (viewbox) {
|
|
55
|
+
svg.setAttribute("viewBox", viewbox);
|
|
56
|
+
} else {
|
|
57
|
+
// Set size attributes
|
|
58
|
+
svg.setAttribute("width", size);
|
|
59
|
+
svg.setAttribute("height", size);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Apply rotation if provided
|
|
63
|
+
if (rotation) {
|
|
64
|
+
svg.style.transform = `rotate(${rotation}deg)`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
|
|
68
|
+
if (Object.keys(urlParametersObj).length > 0) {
|
|
69
|
+
const urlParameters = `?${Object.entries(urlParametersObj)
|
|
70
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
71
|
+
.join("&")}`;
|
|
72
|
+
use.setAttribute("href", `${filepath}${urlParameters}#${name}`);
|
|
73
|
+
} else {
|
|
74
|
+
use.setAttribute("href", `${filepath}#${name}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
svg.appendChild(use);
|
|
78
|
+
|
|
79
|
+
this.shadowRoot.innerHTML = ""; // Clear old content
|
|
80
|
+
this.shadowRoot.appendChild(svg);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Utility to get attributes with defaults
|
|
84
|
+
getOptions(defaults) {
|
|
85
|
+
return Object.keys(defaults).reduce((acc, key) => {
|
|
86
|
+
const value = this.getAttribute(key);
|
|
87
|
+
acc[key] =
|
|
88
|
+
value !== null
|
|
89
|
+
? typeof defaults[key] === "number"
|
|
90
|
+
? parseFloat(value)
|
|
91
|
+
: value
|
|
92
|
+
: defaults[key];
|
|
93
|
+
return acc;
|
|
94
|
+
}, {});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Observe relevant attributes
|
|
98
|
+
static get observedAttributes() {
|
|
99
|
+
return ["name", "size", "rotation", "viewbox"];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Handle attribute changes
|
|
103
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
104
|
+
if (oldValue !== newValue) {
|
|
105
|
+
this.connectedCallback();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Register the custom element
|
|
111
|
+
customElements.define("ui-icon", UiIcon);
|