mi-element 0.0.1 → 0.2.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/LICENSE +20 -0
- package/README.md +92 -0
- package/dist/case.js +3 -0
- package/dist/context.js +61 -0
- package/dist/element.js +116 -0
- package/dist/escape.js +9 -0
- package/dist/index.js +13 -0
- package/dist/refs.js +15 -0
- package/dist/signal.js +62 -0
- package/dist/store.js +10 -0
- package/dist/styling.js +17 -0
- package/package.json +97 -4
- package/src/case.js +15 -0
- package/src/context.js +164 -0
- package/src/element.js +350 -0
- package/src/escape.js +38 -0
- package/src/index.js +25 -0
- package/src/refs.js +41 -0
- package/src/signal.js +111 -0
- package/src/store.js +78 -0
- package/src/styling.js +33 -0
- package/types/case.d.ts +2 -0
- package/types/context.d.ts +68 -0
- package/types/element.d.ts +147 -0
- package/types/escape.d.ts +3 -0
- package/types/index.d.ts +15 -0
- package/types/refs.d.ts +22 -0
- package/types/signal.d.ts +54 -0
- package/types/store.d.ts +64 -0
- package/types/styling.d.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present, commenthol and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,2 +1,94 @@
|
|
|
1
1
|
# mi-element
|
|
2
2
|
|
|
3
|
+
> a lightweight alternative to write web components
|
|
4
|
+
|
|
5
|
+
Only weights 2.3kB minified and gzipped.
|
|
6
|
+
|
|
7
|
+
mi-element provides further features to build web applications through
|
|
8
|
+
[Web Components][] like:
|
|
9
|
+
|
|
10
|
+
- controllers to hook into the components lifecycle
|
|
11
|
+
- ContextProvider, ContextConsumer for data provisioning from outside of a
|
|
12
|
+
component
|
|
13
|
+
- Store for managing shared state across components
|
|
14
|
+
|
|
15
|
+
# Usage
|
|
16
|
+
|
|
17
|
+
In your project:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
npm i mi-element
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
/** @file ./mi-counter.js */
|
|
25
|
+
|
|
26
|
+
import { MiElement, define, refsById } from 'mi-element'
|
|
27
|
+
|
|
28
|
+
// define your Component
|
|
29
|
+
class MiCounter extends MiElement {
|
|
30
|
+
static template = `
|
|
31
|
+
<style>
|
|
32
|
+
:host { font-size: 1.25rem; }
|
|
33
|
+
</style>
|
|
34
|
+
<button id="decrement" aria-label="Decrement counter"> - </button>
|
|
35
|
+
<span id aria-label="Counter value">0</span>
|
|
36
|
+
<button id="increment" aria-label="Increment counter"> + </button>
|
|
37
|
+
`
|
|
38
|
+
|
|
39
|
+
// declare reactive attributes
|
|
40
|
+
static get attributes() {
|
|
41
|
+
return { count: 0 }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// called by connectedCallback()
|
|
45
|
+
render() {
|
|
46
|
+
// gather refs from template (here by id)
|
|
47
|
+
this.refs = refsById(this.renderRoot)
|
|
48
|
+
// apply event listeners
|
|
49
|
+
this.refs.button.decrement.addEventListener('click', () => this.count--)
|
|
50
|
+
this.refs.button.increment.addEventListener('click', () => this.count++)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// called on every change of an observed attributes via `this.requestUpdate()`
|
|
54
|
+
update() {
|
|
55
|
+
this.refs.span.textContent = this.count
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// create the custom element
|
|
60
|
+
define('mi-counter', MiCounter)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Now use in your HTML
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<body>
|
|
67
|
+
<mi-counter></mi-counter>
|
|
68
|
+
<mi-counter count="-3"></mi-counter>
|
|
69
|
+
<script type="module" src="./mi-counter.js"></script>
|
|
70
|
+
</body>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
In `./example` you'll find a working sample of a Todo App. Check it out with
|
|
74
|
+
`npm run example`
|
|
75
|
+
|
|
76
|
+
# Documentation
|
|
77
|
+
|
|
78
|
+
- [lifecycle][docs-lifecycle] mi-element's lifecycle
|
|
79
|
+
- [controller][docs-controller] adding controllers to mi-element to hook into the lifecycle
|
|
80
|
+
- [context][docs-context] Implementation of the [Context Protocol][].
|
|
81
|
+
- [store][docs-store] Manage shared state in an application
|
|
82
|
+
- [styling][docs-styling] Styling directives for "class" and "style"
|
|
83
|
+
|
|
84
|
+
# License
|
|
85
|
+
|
|
86
|
+
MIT licensed
|
|
87
|
+
|
|
88
|
+
[Context Protocol]: https://github.com/webcomponents-cg/community-protocols/blob/main/proposals/context.md
|
|
89
|
+
[Web Components]: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements#custom_element_lifecycle_callbacks
|
|
90
|
+
[docs-lifecycle]: https://github.com/commenthol/mi-element/tree/main/packages/mi-element/docs/lifecycle.md
|
|
91
|
+
[docs-controller]: https://github.com/commenthol/mi-element/tree/main/packages/mi-element/docs/controller.md
|
|
92
|
+
[docs-context]: https://github.com/commenthol/mi-element/tree/main/packages/mi-element/docs/context.md
|
|
93
|
+
[docs-store]: https://github.com/commenthol/mi-element/tree/main/packages/mi-element/docs/store.md
|
|
94
|
+
[docs-styling]: https://github.com/commenthol/mi-element/tree/main/packages/mi-element/docs/styling.md
|
package/dist/case.js
ADDED
package/dist/context.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createSignal, effect } from './signal.js';
|
|
2
|
+
|
|
3
|
+
class ContextProvider {
|
|
4
|
+
constructor(host, context, initialValue) {
|
|
5
|
+
this.host = host, this.context = context, this.state = createSignal(initialValue),
|
|
6
|
+
this.host.addController?.(this);
|
|
7
|
+
}
|
|
8
|
+
hostConnected() {
|
|
9
|
+
this.host.addEventListener("context-request", this.onContextRequest);
|
|
10
|
+
}
|
|
11
|
+
hostDisconnected() {
|
|
12
|
+
this.host.removeEventListener("context-request", this.onContextRequest);
|
|
13
|
+
}
|
|
14
|
+
set(newValue) {
|
|
15
|
+
this.state.set(newValue);
|
|
16
|
+
}
|
|
17
|
+
get() {
|
|
18
|
+
return this.state.get();
|
|
19
|
+
}
|
|
20
|
+
onContextRequest=ev => {
|
|
21
|
+
if (ev.context !== this.context) return;
|
|
22
|
+
let unsubscribe;
|
|
23
|
+
ev.stopPropagation(), ev.subscribe && (unsubscribe = effect((() => {
|
|
24
|
+
const value = this.get();
|
|
25
|
+
unsubscribe && ev.callback(value, unsubscribe);
|
|
26
|
+
}))), ev.callback(this.get(), unsubscribe);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class ContextRequestEvent extends Event {
|
|
31
|
+
constructor(context, callback, subscribe) {
|
|
32
|
+
super("context-request", {
|
|
33
|
+
bubbles: !0,
|
|
34
|
+
composed: !0
|
|
35
|
+
}), this.context = context, this.callback = callback, this.subscribe = subscribe;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class ContextConsumer {
|
|
40
|
+
constructor(host, context, options) {
|
|
41
|
+
const {subscribe: subscribe = !1, validate: validate = () => !0} = options || {};
|
|
42
|
+
this.host = host, this.context = context, this.subscribe = !!subscribe, this.validate = validate,
|
|
43
|
+
this.value = void 0, this.unsubscribe = void 0, this.host.addController?.(this);
|
|
44
|
+
}
|
|
45
|
+
hostConnected() {
|
|
46
|
+
this.dispatchRequest();
|
|
47
|
+
}
|
|
48
|
+
hostDisconnected() {
|
|
49
|
+
this.unsubscribe && (this.unsubscribe(), this.unsubscribe = void 0);
|
|
50
|
+
}
|
|
51
|
+
dispatchRequest() {
|
|
52
|
+
this.host.dispatchEvent(new ContextRequestEvent(this.context, this._callback.bind(this), this.subscribe));
|
|
53
|
+
}
|
|
54
|
+
_callback(value, unsubscribe) {
|
|
55
|
+
unsubscribe && (this.subscribe ? this.unsubscribe && (this.unsubscribe !== unsubscribe && this.unsubscribe(),
|
|
56
|
+
this.unsubscribe = unsubscribe) : unsubscribe()), this.validate(value) && (this.value = value,
|
|
57
|
+
this.host.requestUpdate());
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { ContextConsumer, ContextProvider, ContextRequestEvent };
|
package/dist/element.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { camelToKebabCase } from './case.js';
|
|
2
|
+
|
|
3
|
+
import { createSignal } from './signal.js';
|
|
4
|
+
|
|
5
|
+
class MiElement extends HTMLElement {
|
|
6
|
+
#attr={};
|
|
7
|
+
#attrLc=new Map;
|
|
8
|
+
#types=new Map;
|
|
9
|
+
#disposers=new Set;
|
|
10
|
+
#controllers=new Set;
|
|
11
|
+
#changedAttr={};
|
|
12
|
+
static shadowRootOptions={
|
|
13
|
+
mode: 'open'
|
|
14
|
+
};
|
|
15
|
+
constructor() {
|
|
16
|
+
super(), this.#observedAttributes(this.constructor.attributes);
|
|
17
|
+
}
|
|
18
|
+
#observedAttributes(attributes = {}) {
|
|
19
|
+
for (const [name, value] of Object.entries(attributes)) this.#types.set(name, initialType(value)),
|
|
20
|
+
this.#attrLc.set(name.toLowerCase(), name), this.#attrLc.set(camelToKebabCase(name), name),
|
|
21
|
+
this.#attr[name] = createSignal(value), Object.defineProperty(this, name, {
|
|
22
|
+
enumerable: !0,
|
|
23
|
+
get() {
|
|
24
|
+
return this.#attr[name].get();
|
|
25
|
+
},
|
|
26
|
+
set(newValue) {
|
|
27
|
+
const oldValue = this.#attr[name].get();
|
|
28
|
+
oldValue !== newValue && (this.#attr[name].set(newValue), this.#changedAttr[name] = oldValue,
|
|
29
|
+
this.requestUpdate());
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
#getName(name) {
|
|
34
|
+
return this.#attrLc.get(name) || name;
|
|
35
|
+
}
|
|
36
|
+
#getType(name) {
|
|
37
|
+
return this.#types.get(name);
|
|
38
|
+
}
|
|
39
|
+
connectedCallback() {
|
|
40
|
+
this.#controllers.forEach((controller => controller.hostConnected?.()));
|
|
41
|
+
const {shadowRootOptions: shadowRootOptions, template: template} = this.constructor;
|
|
42
|
+
this.renderRoot = shadowRootOptions ? this.shadowRoot ?? this.attachShadow(shadowRootOptions) : this,
|
|
43
|
+
this.addTemplate(template), this.render(), this.requestUpdate();
|
|
44
|
+
}
|
|
45
|
+
disconnectedCallback() {
|
|
46
|
+
this.#disposers.forEach((remover => remover())), this.#controllers.forEach((controller => controller.hostDisconnected?.()));
|
|
47
|
+
}
|
|
48
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
49
|
+
const attr = this.#getName(name), type = this.#getType(attr);
|
|
50
|
+
this.#changedAttr[attr] = this[attr], this[attr] = convertType(newValue, type),
|
|
51
|
+
'Boolean' === type && 'false' === newValue && this.removeAttribute(name), this.requestUpdate();
|
|
52
|
+
}
|
|
53
|
+
setAttribute(name, newValue) {
|
|
54
|
+
const attr = this.#getName(name);
|
|
55
|
+
if (!(attr in this.#attr)) return;
|
|
56
|
+
const type = this.#getType(attr);
|
|
57
|
+
'Boolean' === type ? !0 === newValue || '' === newValue ? super.setAttribute(name, '') : super.removeAttribute(name) : [ 'String', 'Number' ].includes(type) || !0 === newValue ? super.setAttribute(name, newValue) : (this.#changedAttr[attr] = this[attr],
|
|
58
|
+
this[attr] = newValue, this.requestUpdate());
|
|
59
|
+
}
|
|
60
|
+
shouldUpdate(_changedAttributes) {
|
|
61
|
+
return !0;
|
|
62
|
+
}
|
|
63
|
+
requestUpdate() {
|
|
64
|
+
this.isConnected && requestAnimationFrame((() => {
|
|
65
|
+
this.shouldUpdate(this.#changedAttr) && this.update(this.#changedAttr), this.#changedAttr = {};
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
addTemplate(template) {
|
|
69
|
+
template instanceof HTMLTemplateElement && this.renderRoot.appendChild(template.content.cloneNode(!0));
|
|
70
|
+
}
|
|
71
|
+
render() {}
|
|
72
|
+
update(_changedAttributes) {}
|
|
73
|
+
on(eventName, listener, node = this) {
|
|
74
|
+
node.addEventListener(eventName, listener), this.#disposers.add((() => node.removeEventListener(eventName, listener)));
|
|
75
|
+
}
|
|
76
|
+
once(eventName, listener, node = this) {
|
|
77
|
+
node.addEventListener(eventName, listener, {
|
|
78
|
+
once: !0
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
dispose(...listeners) {
|
|
82
|
+
for (const listener of listeners) {
|
|
83
|
+
if ('function' != typeof listener) throw new TypeError('listener must be a function');
|
|
84
|
+
this.#disposers.add(listener);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
addController(controller) {
|
|
88
|
+
this.#controllers.add(controller), this.isConnected && controller.hostConnected?.();
|
|
89
|
+
}
|
|
90
|
+
removeController(controller) {
|
|
91
|
+
this.#controllers.delete(controller);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const define = (name, element, options) => {
|
|
96
|
+
element.observedAttributes = (element.observedAttributes || Object.keys(element.attributes || [])).map((attr => attr.toLowerCase())),
|
|
97
|
+
renderTemplate(element), window.customElements.define(name, element, options);
|
|
98
|
+
}, renderTemplate = element => {
|
|
99
|
+
if ('string' != typeof element.template) return;
|
|
100
|
+
const el = document.createElement('template');
|
|
101
|
+
el.innerHTML = element.template, element.template = el;
|
|
102
|
+
}, initialType = value => toString.call(value).slice(8, -1), convertType = (any, type) => {
|
|
103
|
+
switch (type) {
|
|
104
|
+
case 'Number':
|
|
105
|
+
return (any => {
|
|
106
|
+
const n = Number(any);
|
|
107
|
+
return isNaN(n) ? any : n;
|
|
108
|
+
})(any);
|
|
109
|
+
|
|
110
|
+
case 'Boolean':
|
|
111
|
+
return 'false' !== any && ('' === any || !!any);
|
|
112
|
+
}
|
|
113
|
+
return any;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export { MiElement, convertType, define };
|
package/dist/escape.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const escMap = {
|
|
2
|
+
'&': '&',
|
|
3
|
+
'<': '<',
|
|
4
|
+
'>': '>',
|
|
5
|
+
"'": ''',
|
|
6
|
+
'"': '"'
|
|
7
|
+
}, escHtml = string => ('' + string).replace(/&/g, '&').replace(/[&<>'"]/g, (tag => escMap[tag])), escAttr = string => ('' + string).replace(/['"]/g, (tag => escMap[tag])), esc = (strings, ...vars) => strings.map(((string, i) => string + escHtml(vars[i] ?? ''))).join('');
|
|
8
|
+
|
|
9
|
+
export { esc, escAttr, escHtml };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { ContextConsumer, ContextProvider, ContextRequestEvent } from './context.js';
|
|
2
|
+
|
|
3
|
+
export { MiElement, convertType, define } from './element.js';
|
|
4
|
+
|
|
5
|
+
export { esc, escAttr, escHtml } from './escape.js';
|
|
6
|
+
|
|
7
|
+
export { refsById, refsBySelector } from './refs.js';
|
|
8
|
+
|
|
9
|
+
export { default as Signal } from './signal.js';
|
|
10
|
+
|
|
11
|
+
export { Store } from './store.js';
|
|
12
|
+
|
|
13
|
+
export { classMap, styleMap } from './styling.js';
|
package/dist/refs.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { kebabToCamelCase } from './case.js';
|
|
2
|
+
|
|
3
|
+
function refsById(container) {
|
|
4
|
+
const nodes = container.querySelectorAll?.('[id]') || [], found = {};
|
|
5
|
+
for (const node of nodes) found[kebabToCamelCase(node.getAttribute('id') || node.nodeName.toLowerCase())] = node;
|
|
6
|
+
return found;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function refsBySelector(container, selectors) {
|
|
10
|
+
const found = {};
|
|
11
|
+
for (const [name, selector] of Object.entries(selectors)) found[name] = container.querySelector?.(selector);
|
|
12
|
+
return found;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { refsById, refsBySelector };
|
package/dist/signal.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const context = [];
|
|
2
|
+
|
|
3
|
+
class State {
|
|
4
|
+
subscribers=new Set;
|
|
5
|
+
constructor(value, options) {
|
|
6
|
+
const {equals: equals} = options || {};
|
|
7
|
+
this.value = value, this.equals = equals ?? ((value, nextValue) => value === nextValue);
|
|
8
|
+
}
|
|
9
|
+
get() {
|
|
10
|
+
const running = context[context.length - 1];
|
|
11
|
+
return running && (this.subscribers.add(running), running.dependencies.add(this.subscribers)),
|
|
12
|
+
this.value;
|
|
13
|
+
}
|
|
14
|
+
set(nextValue) {
|
|
15
|
+
if (!this.equals(this.value, nextValue)) {
|
|
16
|
+
this.value = nextValue;
|
|
17
|
+
for (const running of [ ...this.subscribers ]) running.execute();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const createSignal = value => value instanceof State ? value : new State(value);
|
|
23
|
+
|
|
24
|
+
function cleanup(running) {
|
|
25
|
+
for (const dep of running.dependencies) dep.delete(running);
|
|
26
|
+
running.dependencies.clear();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function effect(cb) {
|
|
30
|
+
const execute = () => {
|
|
31
|
+
cleanup(running), context.push(running);
|
|
32
|
+
try {
|
|
33
|
+
cb();
|
|
34
|
+
} finally {
|
|
35
|
+
context.pop();
|
|
36
|
+
}
|
|
37
|
+
}, running = {
|
|
38
|
+
execute: execute,
|
|
39
|
+
dependencies: new Set
|
|
40
|
+
};
|
|
41
|
+
return execute(), () => {
|
|
42
|
+
cleanup(running);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
class Computed {
|
|
47
|
+
constructor(cb) {
|
|
48
|
+
this.state = new State, effect((() => this.state.set(cb)));
|
|
49
|
+
}
|
|
50
|
+
get() {
|
|
51
|
+
return this.state.get();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var signal = {
|
|
56
|
+
State: State,
|
|
57
|
+
createSignal: createSignal,
|
|
58
|
+
effect: effect,
|
|
59
|
+
Computed: Computed
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { Computed, State, createSignal, signal as default, effect };
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { State } from './signal.js';
|
|
2
|
+
|
|
3
|
+
class Store extends State {
|
|
4
|
+
constructor(actions, initialValue, options) {
|
|
5
|
+
super(initialValue, options);
|
|
6
|
+
for (const [action, dispatcher] of Object.entries(actions)) this[action] = data => this.set(dispatcher(data)(this.get()));
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { Store };
|
package/dist/styling.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { camelToKebabCase } from './case.js';
|
|
2
|
+
|
|
3
|
+
const classMap = map => {
|
|
4
|
+
const acc = [];
|
|
5
|
+
for (const [name, value] of Object.entries(map ?? {})) value && acc.push(name);
|
|
6
|
+
return acc.join(' ');
|
|
7
|
+
}, styleMap = (map, options) => {
|
|
8
|
+
const {unit: unit = "px"} = options || {}, acc = [];
|
|
9
|
+
for (const [name, value] of Object.entries(map ?? {})) {
|
|
10
|
+
if (null == value) continue;
|
|
11
|
+
const _unit = Number.isFinite(value) ? unit : '';
|
|
12
|
+
acc.push(`${camelToKebabCase(name)}:${value}${_unit}`);
|
|
13
|
+
}
|
|
14
|
+
return acc.join(';');
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { classMap, styleMap };
|
package/package.json
CHANGED
|
@@ -1,13 +1,106 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mi-element",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Build lightweight reactive micro web-components",
|
|
5
5
|
"keywords": [],
|
|
6
|
+
"homepage": "https://github.com/commenthol/mi-element/tree/main/packages/mi-element#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/commenthol/mi-element/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/commenthol/mi-element.git",
|
|
13
|
+
"directory": "packages/mi-element"
|
|
14
|
+
},
|
|
6
15
|
"license": "MIT",
|
|
7
16
|
"author": "commenthol <commenthol@gmail.com>",
|
|
17
|
+
"maintainers": [
|
|
18
|
+
"commenthol <commenthol@gmail.com>"
|
|
19
|
+
],
|
|
8
20
|
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"default": "./dist/index.js",
|
|
24
|
+
"development": "./src/index.js",
|
|
25
|
+
"types": "./types/index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./element": {
|
|
28
|
+
"default": "./dist/element.js",
|
|
29
|
+
"development": "./src/element.js",
|
|
30
|
+
"types": "./types/element.d.ts"
|
|
31
|
+
},
|
|
32
|
+
"./case": {
|
|
33
|
+
"default": "./dist/case.js",
|
|
34
|
+
"development": "./src/case.js",
|
|
35
|
+
"types": "./types/case.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./context": {
|
|
38
|
+
"default": "./dist/context.js",
|
|
39
|
+
"development": "./src/context.js",
|
|
40
|
+
"types": "./types/context.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./escape": {
|
|
43
|
+
"default": "./dist/escape.js",
|
|
44
|
+
"development": "./src/escape.js",
|
|
45
|
+
"types": "./types/escape.d.ts"
|
|
46
|
+
},
|
|
47
|
+
"./refs": {
|
|
48
|
+
"default": "./dist/refs.js",
|
|
49
|
+
"development": "./src/refs.js",
|
|
50
|
+
"types": "./types/refs.d.ts"
|
|
51
|
+
},
|
|
52
|
+
"./signal": {
|
|
53
|
+
"default": "./dist/signal.js",
|
|
54
|
+
"development": "./src/signal.js",
|
|
55
|
+
"types": "./types/signal.d.ts"
|
|
56
|
+
},
|
|
57
|
+
"./store": {
|
|
58
|
+
"default": "./dist/store.js",
|
|
59
|
+
"development": "./src/store.js",
|
|
60
|
+
"types": "./types/store.d.ts"
|
|
61
|
+
},
|
|
62
|
+
"./styling": {
|
|
63
|
+
"default": "./dist/styling.js",
|
|
64
|
+
"development": "./src/styling.js",
|
|
65
|
+
"types": "./types/styling.d.ts"
|
|
66
|
+
},
|
|
67
|
+
"./package.json": {
|
|
68
|
+
"default": "./package.json"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
9
71
|
"main": "src/index.js",
|
|
72
|
+
"files": [
|
|
73
|
+
"src",
|
|
74
|
+
"dist",
|
|
75
|
+
"types"
|
|
76
|
+
],
|
|
77
|
+
"devDependencies": {
|
|
78
|
+
"@eslint/js": "^9.9.1",
|
|
79
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
80
|
+
"@testing-library/dom": "^10.4.0",
|
|
81
|
+
"@types/node": "^22.5.0",
|
|
82
|
+
"@vitest/browser": "^2.0.5",
|
|
83
|
+
"@vitest/coverage-istanbul": "^2.0.5",
|
|
84
|
+
"eslint": "^9.9.1",
|
|
85
|
+
"globals": "^15.9.0",
|
|
86
|
+
"npm-run-all2": "^6.2.2",
|
|
87
|
+
"playwright": "^1.46.1",
|
|
88
|
+
"prettier": "^3.3.3",
|
|
89
|
+
"rimraf": "^6.0.1",
|
|
90
|
+
"rollup": "^4.21.1",
|
|
91
|
+
"typescript": "^5.5.4",
|
|
92
|
+
"vite": "^5.4.2",
|
|
93
|
+
"vitest": "^2.0.5"
|
|
94
|
+
},
|
|
10
95
|
"scripts": {
|
|
11
|
-
"
|
|
96
|
+
"all": "npm-run-all pretty lint test build types",
|
|
97
|
+
"build": "rimraf dist && rollup -c && gzip -k dist/index.min.js && ls -al dist && rimraf dist/index.min.*",
|
|
98
|
+
"example": "vite --open /example/",
|
|
99
|
+
"lint": "eslint",
|
|
100
|
+
"pretty": "prettier -w **/*.js",
|
|
101
|
+
"test": "vitest run --coverage",
|
|
102
|
+
"test:browser": "vitest --coverage",
|
|
103
|
+
"dev": "npm run test:browser",
|
|
104
|
+
"types": "tsc"
|
|
12
105
|
}
|
|
13
|
-
}
|
|
106
|
+
}
|
package/src/case.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* convert lowerCamelCase to kebab-case
|
|
3
|
+
* @param {string} str
|
|
4
|
+
* @returns {string}
|
|
5
|
+
*/
|
|
6
|
+
export const camelToKebabCase = (str = '') =>
|
|
7
|
+
str.replace(/([A-Z])/g, (_, m) => `-${m.toLowerCase()}`)
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* convert kebab-case to lowerCamelCase
|
|
11
|
+
* @param {string} str
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
export const kebabToCamelCase = (str = '') =>
|
|
15
|
+
str.toLowerCase().replace(/[-_]\w/g, (m) => m[1].toUpperCase())
|