mi-element 0.6.0 → 0.6.1

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/docs/store.md ADDED
@@ -0,0 +1,65 @@
1
+ # Store
2
+
3
+ Store implements [Flux](https://www.npmjs.com/package/flux) pattern, which is a
4
+ signal triggered by an action (dispatchers).
5
+
6
+ 1. Define actions which allow changing the store value.
7
+ The actions must be a function with the shape:
8
+
9
+ ```js
10
+ const actions = {
11
+ [actionName]: (change) => (state) => {
12
+ // change state based on change e.g.
13
+ const stateChange = { ...state, change }
14
+ return stateChange
15
+ }
16
+ }
17
+ ```
18
+
19
+ 2. Create store and add export to component.
20
+ Component imports store and connects to it by subscribing with a callback
21
+ function to receive any state changes:
22
+ ```js
23
+ const callback = (state) => {
24
+ // update component with new state
25
+ }
26
+ ```
27
+
28
+ Sample:
29
+
30
+ ```js
31
+ import { Store, Signal } from 'mi-element'
32
+
33
+ const actions = {
34
+ increment:
35
+ (by = 1) =>
36
+ (state) =>
37
+ state + by
38
+ }
39
+ const initialValue = 1
40
+ const store = new Store(actions, initialValue)
41
+
42
+ // create effect, which is executed immediately
43
+ const unsubscribe = Signal.effect(() => console.log(`count is ${store.get()}`))
44
+ //> count is 1
45
+
46
+ // change the store
47
+ store.increment(2) // increment by 2
48
+ //> count is 3
49
+
50
+ unsubscribe()
51
+ ```
52
+
53
+ If `initialValue` is an object, the object's reference must be changed
54
+ with the spread operator to notify on state changes, e.g.
55
+
56
+ **Example**
57
+
58
+ ```js
59
+ const initialValue = { count: 0, other: 'foo' }
60
+ const actions = {
61
+ increment:
62
+ (by = 1) =>
63
+ (state) => ({ ...state, count: state.count + by })
64
+ }
65
+ ```
@@ -0,0 +1,77 @@
1
+ **Table of contents**
2
+
3
+ <!-- !toc (minlevel=2) -->
4
+
5
+ - [classMap](#classmap)
6
+ - [styleMap](#stylemap)
7
+ - [addGlobalStyles](#addglobalstyles)
8
+
9
+ <!-- toc! -->
10
+
11
+ # Styling
12
+
13
+ ## classMap
14
+
15
+ Obtain a class string from an object. Only class-names with trueish values are
16
+ returned.
17
+
18
+ ```js
19
+ import { classMap } from 'mi-element'
20
+
21
+ const className = classMap({ enabled: true, hidden: '', number: 1 })
22
+ //> className == 'enabled number'
23
+ ```
24
+
25
+ ## styleMap
26
+
27
+ Convert a map of styles into a styles string. CamelCased properties are
28
+ converted to kebab-case. Numbers are converted to css `px` unit. This can be
29
+ changed through its options.
30
+
31
+ ```js
32
+ import { styleMap } from 'mi-element'
33
+
34
+ const styles = styleMap(
35
+ { backgroundColor: 'blue', color: '#fff', width: 200 },
36
+ { unit: 'px' } // optional
37
+ )
38
+ //> styles == 'background-color:blue;color:#fff;width:200px'
39
+ ```
40
+
41
+ Signature
42
+
43
+ ```ts
44
+ function styleMap(
45
+ map: { [name: string]: string | number | undefined | null },
46
+ options?: { unit?: string | undefined } | undefined
47
+ ): string
48
+ ```
49
+
50
+ ## addGlobalStyles
51
+
52
+ Apply global style sheets to shadowRoot of a web-component.
53
+
54
+ E.g. you'd like to apply your global style sheet to
55
+
56
+ ```css
57
+ h1 { color: rebeccapurple; }
58
+ ```
59
+
60
+ ```js
61
+ import { addGlobalStyles } from 'mi-element'
62
+
63
+ const template = document.createElement('template')
64
+ template.innerHTML = `<h1>Hello World!</h1>`
65
+
66
+ customElements.define(
67
+ 'x-hello-world',
68
+ class extends HTMLElement {
69
+ connectedCallback() {
70
+ this.renderRoot = this.shadowRoot ?? this.attachShadow({ mode: 'open' })
71
+ // apply global styles
72
+ addGlobalStyles(this.renderRoot)
73
+ this.renderRoot.append(template.content.cloneNode(true))
74
+ }
75
+ }
76
+ )
77
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mi-element",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Build lightweight reactive micro web-components",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/commenthol/mi-element/tree/main/packages/mi-element#readme",
@@ -70,10 +70,11 @@
70
70
  "files": [
71
71
  "src",
72
72
  "dist",
73
+ "docs",
73
74
  "types"
74
75
  ],
75
76
  "dependencies": {
76
- "mi-signal": "0.6.0"
77
+ "mi-signal": "0.6.1"
77
78
  },
78
79
  "devDependencies": {
79
80
  "@eslint/js": "^9.33.0",
@@ -89,11 +90,11 @@
89
90
  "playwright": "^1.54.2",
90
91
  "prettier": "^3.6.2",
91
92
  "rimraf": "^6.0.1",
92
- "rollup": "^4.46.2",
93
+ "rollup": "^4.46.3",
93
94
  "typescript": "^5.9.2",
94
95
  "vite": "^7.1.2",
95
96
  "vitest": "^3.2.4",
96
- "mi-html": "0.6.0"
97
+ "mi-html": "0.6.1"
97
98
  },
98
99
  "scripts": {
99
100
  "all": "npm-run-all pretty lint test build types",
package/src/element.js CHANGED
@@ -66,6 +66,7 @@ export class MiElement extends HTMLElement {
66
66
  * Default options used when calling `attachShadow`. Used in
67
67
  * `connectedCallback()`.
68
68
  * If override is `null`, no shadow-root will be attached.
69
+ * @type {{mode: string}|null}
69
70
  */
70
71
  static shadowRootOptions = { mode: 'open' }
71
72
 
@@ -75,20 +76,20 @@ export class MiElement extends HTMLElement {
75
76
  */
76
77
  static template
77
78
 
78
- // /**
79
- // * observable attributes
80
- // * @returns {Record<string, any>|{}}
81
- // */
82
- // static get attributes() {
83
- // return {}
84
- // }
85
- // /**
86
- // * observable properties
87
- // * @returns {Record<string, any>|{}}
88
- // */
89
- // static get properties() {
90
- // return {}
91
- // }
79
+ /**
80
+ * observable attributes
81
+ * @returns {Record<string, any>|{}}
82
+ */
83
+ static get attributes() {
84
+ return {}
85
+ }
86
+ /**
87
+ * observable properties
88
+ * @returns {Record<string, any>|{}}
89
+ */
90
+ static get properties() {
91
+ return {}
92
+ }
92
93
 
93
94
  constructor() {
94
95
  super()
@@ -44,15 +44,26 @@ export class MiElement extends HTMLElement {
44
44
  * Default options used when calling `attachShadow`. Used in
45
45
  * `connectedCallback()`.
46
46
  * If override is `null`, no shadow-root will be attached.
47
+ * @type {{mode: string}|null}
47
48
  */
48
49
  static shadowRootOptions: {
49
50
  mode: string;
50
- };
51
+ } | null;
51
52
  /**
52
53
  * defines template for render().
53
54
  * @type {String|HTMLTemplateElement}
54
55
  */
55
56
  static template: string | HTMLTemplateElement;
57
+ /**
58
+ * observable attributes
59
+ * @returns {Record<string, any>|{}}
60
+ */
61
+ static get attributes(): Record<string, any> | {};
62
+ /**
63
+ * observable properties
64
+ * @returns {Record<string, any>|{}}
65
+ */
66
+ static get properties(): Record<string, any> | {};
56
67
  /**
57
68
  * creates the element's renderRoot, sets up styling
58
69
  * @category lifecycle