@blockquote-web-components/blockquote-controller-xstate 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 blockquote-controller-xstate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,194 @@
1
+ # BlockquoteControllerXstate
2
+
3
+ ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
4
+
5
+ ### Connect XState machines with Lit's reactive property
6
+ The `BlockquoteControllerXstate` is a Lit Reactive Controller specifically designed for straightforward integration with XState.
7
+ This controller allows you to subscribe to an XState actor, updating a specified reactive property whenever the state machine transitions.
8
+
9
+ - [xstate v5](https://stately.ai/docs/installation)
10
+ - [xstate v5 - examples](https://stately.ai/docs/examples)
11
+ - [Original idea](https://codesandbox.io/s/z3o0s?file=/src/toggleMachine.ts)
12
+
13
+ <hr>
14
+
15
+ ### Demo
16
+
17
+ [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/oscarmarina/XstateController)
18
+
19
+ [![Stately.ai](https://img.shields.io/badge/Stately.ai-black.svg)](https://stately.ai/registry/editor/154a7a42-9338-4cc0-8c0c-131c859d8349)
20
+
21
+ ### Usage
22
+
23
+ ***counterMachine.js***
24
+
25
+ ```javascript
26
+ import { createMachine, assign } from 'xstate';
27
+
28
+ const states = {
29
+ enabled: 'enabled',
30
+ disabled: 'disabled',
31
+ };
32
+
33
+ const increment = {
34
+ counter: ({ context }) => context.counter + 1,
35
+ event: ({ event }) => event,
36
+ };
37
+ const decrement = {
38
+ counter: ({ context }) => context.counter - 1,
39
+ event: ({ event }) => event,
40
+ };
41
+
42
+ const isNotMax = ({ context }) => context.counter < 10;
43
+ const isNotMin = ({ context }) => context.counter > 0;
44
+
45
+ export const counterMachine = createMachine(
46
+ {
47
+ id: 'counter',
48
+ context: { counter: 0, event: undefined },
49
+ initial: 'enabled',
50
+ states: {
51
+ enabled: {
52
+ on: {
53
+ INC: {
54
+ actions: {
55
+ type: 'increment',
56
+ },
57
+ guard: {
58
+ type: 'isNotMax',
59
+ },
60
+ },
61
+ DEC: {
62
+ actions: {
63
+ type: 'decrement',
64
+ },
65
+ guard: {
66
+ type: 'isNotMin',
67
+ },
68
+ },
69
+ TOGGLE: {
70
+ target: states.disabled,
71
+ },
72
+ },
73
+ },
74
+ disabled: {
75
+ on: {
76
+ TOGGLE: {
77
+ target: states.enabled,
78
+ },
79
+ },
80
+ },
81
+ },
82
+ },
83
+ {
84
+ actions: {
85
+ increment: assign(increment),
86
+ decrement: assign(decrement),
87
+ },
88
+ guards: {
89
+ isNotMax,
90
+ isNotMin,
91
+ },
92
+ },
93
+ );
94
+ ```
95
+
96
+ ***xstate-counter.js***
97
+
98
+ ```javascript
99
+ import { html, LitElement } from 'lit';
100
+ import { BlockquoteControllerXstate } from './BlockquoteControllerXstate.js';
101
+ import { counterMachine } from './counterMachine.js';
102
+
103
+ export class XstateCounter extends LitElement {
104
+ static properties = {
105
+ _xstate: {
106
+ type: Object,
107
+ state: true,
108
+ },
109
+ };
110
+
111
+ constructor() {
112
+ super();
113
+ this._xstate = {};
114
+ this.counterController = new BlockquoteControllerXstate(this, counterMachine, '_xstate');
115
+ }
116
+
117
+ updated(props) {
118
+ super.updated && super.updated(props);
119
+ if (props.has('_xstate')) {
120
+ const { context, value } = this._xstate;
121
+ const counterEvent = new CustomEvent('counterchange', {
122
+ bubbles: true,
123
+ detail: { ...context, value },
124
+ });
125
+ this.dispatchEvent(counterEvent);
126
+ }
127
+ }
128
+
129
+ // ...
130
+
131
+ get #disabled() {
132
+ return this.counterController.state.matches('disabled');
133
+ }
134
+
135
+ render() {
136
+ return html`
137
+ <button
138
+ ?disabled="${this.#disabled}"
139
+ data-counter="increment"
140
+ \@click=${() => this.counterController.send({ type: 'INC' })}
141
+ >
142
+ Increment
143
+ </button>
144
+ <button
145
+ ?disabled="${this.#disabled}"
146
+ data-counter="decrement"
147
+ \@click=${() => this.counterController.send({ type: 'DEC' })}
148
+ >
149
+ Decrement
150
+ </button>
151
+ `;
152
+ }
153
+
154
+ // ...
155
+ }
156
+ ```
157
+ <hr>
158
+
159
+
160
+ ### `src/BlockquoteControllerXstate.js`:
161
+
162
+ #### class: `BlockquoteControllerXstate`
163
+
164
+ ##### Fields
165
+
166
+ | Name | Privacy | Type | Default | Description | Inherited From |
167
+ | --------- | ------- | ---- | --------- | ----------- | -------------- |
168
+ | `state` | | | | | |
169
+ | `service` | | | | | |
170
+ | `propKey` | | | `propKey` | | |
171
+
172
+ ##### Methods
173
+
174
+ | Name | Privacy | Description | Parameters | Return | Inherited From |
175
+ | ------------------ | ------- | ----------- | ----------------- | ------ | -------------- |
176
+ | `send` | | | `ev: EventObject` | | |
177
+ | `hostConnected` | | | | | |
178
+ | `hostDisconnected` | | | | | |
179
+
180
+ <hr/>
181
+
182
+ #### Exports
183
+
184
+ | Kind | Name | Declaration | Module | Package |
185
+ | ---- | ---------------------------- | -------------------------- | --------------------------------- | ------- |
186
+ | `js` | `BlockquoteControllerXstate` | BlockquoteControllerXstate | src/BlockquoteControllerXstate.js | |
187
+
188
+ ### `index.js`:
189
+
190
+ #### Exports
191
+
192
+ | Kind | Name | Declaration | Module | Package |
193
+ | ---- | ---------------------------- | -------------------------- | ----------------------------------- | ------- |
194
+ | `js` | `BlockquoteControllerXstate` | BlockquoteControllerXstate | ./src/BlockquoteControllerXstate.js | |
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export { BlockquoteControllerXstate } from './src/BlockquoteControllerXstate.js';
package/package.json ADDED
@@ -0,0 +1,147 @@
1
+ {
2
+ "name": "@blockquote-web-components/blockquote-controller-xstate",
3
+ "version": "1.0.0",
4
+ "description": "This controller allows you to subscribe to an XState actor, updating a specified reactive property whenever the state machine transitions.",
5
+ "keywords": [
6
+ "lit",
7
+ "web-component",
8
+ "XState",
9
+ "stately",
10
+ "state-machine",
11
+ "statechart",
12
+ "actor"
13
+ ],
14
+ "license": "MIT",
15
+ "author": "blockquote-controller-xstate",
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "default": "./index.js"
20
+ },
21
+ "./package.json": {
22
+ "default": "./package.json"
23
+ },
24
+ "./src/BlockquoteControllerXstate.js": {
25
+ "default": "./src/BlockquoteControllerXstate.js"
26
+ },
27
+ "./index.js": {
28
+ "default": "./index.js"
29
+ }
30
+ },
31
+ "files": [
32
+ "/define/",
33
+ "/src/",
34
+ "index.js",
35
+ "!/**/*.scss"
36
+ ],
37
+ "scripts": {
38
+ "analyze": "cem analyze --litelement --globs \"{src,define}/**/*.{js,ts}\" \"index.js\"",
39
+ "build": "echo \"This is not a TypeScript project, so no need to build.\"",
40
+ "dev:vite": "vite build",
41
+ "format": "npm run format:eslint && npm run format:prettier && npm run format:stylelint",
42
+ "format:eslint": "eslint \"**/*.{js,ts,html}\" --fix --ignore-path .eslintignore",
43
+ "format:prettier": "prettier \"**/*.{js,ts,json,html}\" --write --ignore-path .eslintignore",
44
+ "format:stylelint": "stylelint \"**/*.{scss,css}\" --fix --allow-empty-input --ignore-path .eslintignore",
45
+ "postinstall": "npm run sort:package",
46
+ "lint": "npm run lint:eslint && npm run lint:prettier && npm run lint:stylelint",
47
+ "lint:eslint": "eslint \"**/*.{js,ts,html}\" --ignore-path .eslintignore",
48
+ "lint:prettier": "prettier \"**/*.{js,ts,json,html}\" --check --ignore-path .eslintignore",
49
+ "lint:stylelint": "stylelint \"**/*.{scss,css}\" --allow-empty-input --ignore-path .eslintignore",
50
+ "preview:vite": "vite preview",
51
+ "sass:watch": "sass-style-template",
52
+ "sort:package": "npx sort-package-json",
53
+ "start": "concurrently -k -r \"npm:sass:watch\" \"npm:vite\"",
54
+ "test": "wtr --coverage",
55
+ "test:watch": "wtr --watch",
56
+ "vite": "vite"
57
+ },
58
+ "lint-staged": {
59
+ "**/*.{js,ts,html}": [
60
+ "npm run format:eslint"
61
+ ],
62
+ "**/*.{js,ts,json,html}": [
63
+ "npm run format:prettier"
64
+ ],
65
+ "**/*.{scss,css}": [
66
+ "npm run format:stylelint"
67
+ ]
68
+ },
69
+ "prettier": {
70
+ "arrowParens": "avoid",
71
+ "printWidth": 100,
72
+ "singleQuote": true,
73
+ "trailingComma": "all",
74
+ "overrides": [
75
+ {
76
+ "files": "*.{scss,css}",
77
+ "options": {
78
+ "printWidth": 280,
79
+ "singleQuote": false
80
+ }
81
+ }
82
+ ]
83
+ },
84
+ "eslintConfig": {
85
+ "parserOptions": {
86
+ "ecmaVersion": "latest"
87
+ },
88
+ "extends": [
89
+ "@open-wc",
90
+ "prettier"
91
+ ],
92
+ "rules": {
93
+ "class-methods-use-this": "off",
94
+ "no-unused-expressions": [
95
+ "error",
96
+ {
97
+ "allowShortCircuit": true,
98
+ "allowTernary": true
99
+ }
100
+ ],
101
+ "object-curly-newline": "off",
102
+ "import/extensions": [
103
+ "error",
104
+ "always",
105
+ {
106
+ "ignorePackages": true
107
+ }
108
+ ],
109
+ "import/no-extraneous-dependencies": [
110
+ "error",
111
+ {
112
+ "devDependencies": [
113
+ "**/test/**/*.{js,ts}",
114
+ "**/*.config.{js,ts,mjs,cjs}",
115
+ "**/*.conf.{js,ts,mjs,cjs}"
116
+ ]
117
+ }
118
+ ],
119
+ "import/no-unresolved": "off",
120
+ "import/prefer-default-export": "off",
121
+ "lit/no-classfield-shadowing": "off",
122
+ "lit/no-native-attributes": "off"
123
+ }
124
+ },
125
+ "stylelint": {
126
+ "extends": "stylelint-config-standard-scss",
127
+ "rules": {
128
+ "custom-property-pattern": null,
129
+ "no-duplicate-selectors": null,
130
+ "color-function-notation": null,
131
+ "alpha-value-notation": null
132
+ }
133
+ },
134
+ "dependencies": {
135
+ "lit": "^3.1.0",
136
+ "xstate": "^5.0.0-beta.45"
137
+ },
138
+ "devDependencies": {
139
+ "@blockquote-web-components/blockquote-base-common-dev-dependencies": "^1.7.0",
140
+ "@blockquote-web-components/blockquote-base-embedded-webview": "^1.6.1"
141
+ },
142
+ "publishConfig": {
143
+ "access": "public"
144
+ },
145
+ "customElements": "custom-elements.json",
146
+ "gitHead": "e2417c85a1b9179b093333c0dd89f90fdbb4e91b"
147
+ }
@@ -0,0 +1,198 @@
1
+ import { createActor } from 'xstate';
2
+
3
+ /**
4
+ * # BlockquoteControllerXstate
5
+ *
6
+ * ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
7
+ *
8
+ * ### Connect XState machines with Lit's reactive property
9
+ * The `BlockquoteControllerXstate` is a Lit Reactive Controller specifically designed for straightforward integration with XState.
10
+ * This controller allows you to subscribe to an XState actor, updating a specified reactive property whenever the state machine transitions.
11
+ *
12
+ * - [xstate v5](https://stately.ai/docs/installation)
13
+ * - [xstate v5 - examples](https://stately.ai/docs/examples)
14
+ * - [Original idea](https://codesandbox.io/s/z3o0s?file=/src/toggleMachine.ts)
15
+ *
16
+ * <hr>
17
+ *
18
+ * ### Demo
19
+ *
20
+ * [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/oscarmarina/XstateController)
21
+ *
22
+ * [![Stately.ai](https://img.shields.io/badge/Stately.ai-black.svg)](https://stately.ai/registry/editor/154a7a42-9338-4cc0-8c0c-131c859d8349)
23
+ *
24
+ * ### Usage
25
+ *
26
+ * ***counterMachine.js***
27
+ *
28
+ * ```javascript
29
+ * import { createMachine, assign } from 'xstate';
30
+ *
31
+ * const states = {
32
+ * enabled: 'enabled',
33
+ * disabled: 'disabled',
34
+ * };
35
+ *
36
+ * const increment = {
37
+ * counter: ({ context }) => context.counter + 1,
38
+ * event: ({ event }) => event,
39
+ * };
40
+ * const decrement = {
41
+ * counter: ({ context }) => context.counter - 1,
42
+ * event: ({ event }) => event,
43
+ * };
44
+ *
45
+ * const isNotMax = ({ context }) => context.counter < 10;
46
+ * const isNotMin = ({ context }) => context.counter > 0;
47
+ *
48
+ * export const counterMachine = createMachine(
49
+ * {
50
+ * id: 'counter',
51
+ * context: { counter: 0, event: undefined },
52
+ * initial: 'enabled',
53
+ * states: {
54
+ * enabled: {
55
+ * on: {
56
+ * INC: {
57
+ * actions: {
58
+ * type: 'increment',
59
+ * },
60
+ * guard: {
61
+ * type: 'isNotMax',
62
+ * },
63
+ * },
64
+ * DEC: {
65
+ * actions: {
66
+ * type: 'decrement',
67
+ * },
68
+ * guard: {
69
+ * type: 'isNotMin',
70
+ * },
71
+ * },
72
+ * TOGGLE: {
73
+ * target: states.disabled,
74
+ * },
75
+ * },
76
+ * },
77
+ * disabled: {
78
+ * on: {
79
+ * TOGGLE: {
80
+ * target: states.enabled,
81
+ * },
82
+ * },
83
+ * },
84
+ * },
85
+ * },
86
+ * {
87
+ * actions: {
88
+ * increment: assign(increment),
89
+ * decrement: assign(decrement),
90
+ * },
91
+ * guards: {
92
+ * isNotMax,
93
+ * isNotMin,
94
+ * },
95
+ * },
96
+ * );
97
+ * ```
98
+ *
99
+ * ***xstate-counter.js***
100
+ *
101
+ * ```javascript
102
+ * import { html, LitElement } from 'lit';
103
+ * import { BlockquoteControllerXstate } from './BlockquoteControllerXstate.js';
104
+ * import { counterMachine } from './counterMachine.js';
105
+ *
106
+ * export class XstateCounter extends LitElement {
107
+ * static properties = {
108
+ * _xstate: {
109
+ * type: Object,
110
+ * state: true,
111
+ * },
112
+ * };
113
+ *
114
+ * constructor() {
115
+ * super();
116
+ * this._xstate = {};
117
+ * this.counterController = new BlockquoteControllerXstate(this, counterMachine, '_xstate');
118
+ * }
119
+ *
120
+ * updated(props) {
121
+ * super.updated && super.updated(props);
122
+ * if (props.has('_xstate')) {
123
+ * const { context, value } = this._xstate;
124
+ * const counterEvent = new CustomEvent('counterchange', {
125
+ * bubbles: true,
126
+ * detail: { ...context, value },
127
+ * });
128
+ * this.dispatchEvent(counterEvent);
129
+ * }
130
+ * }
131
+ *
132
+ * // ...
133
+ *
134
+ * get #disabled() {
135
+ * return this.counterController.state.matches('disabled');
136
+ * }
137
+ *
138
+ * render() {
139
+ * return html`
140
+ * <button
141
+ * ?disabled="${this.#disabled}"
142
+ * data-counter="increment"
143
+ * \@click=${() => this.counterController.send({ type: 'INC' })}
144
+ * >
145
+ * Increment
146
+ * </button>
147
+ * <button
148
+ * ?disabled="${this.#disabled}"
149
+ * data-counter="decrement"
150
+ * \@click=${() => this.counterController.send({ type: 'DEC' })}
151
+ * >
152
+ * Decrement
153
+ * </button>
154
+ * `;
155
+ * }
156
+ *
157
+ * // ...
158
+ * }
159
+ * ```
160
+ * <hr>
161
+ */
162
+ export class BlockquoteControllerXstate {
163
+ /**
164
+ * @param {import('lit').ReactiveElement} host
165
+ * @param {import('xstate').StateMachine} machine
166
+ * @param {string} propKey
167
+ */
168
+ constructor(host, machine, propKey) {
169
+ (this.host = host).addController(this);
170
+ this.service = createActor(machine).start();
171
+ this.propKey = propKey;
172
+ }
173
+
174
+ get state() {
175
+ return this.service.getSnapshot();
176
+ }
177
+
178
+ /**
179
+ * @param {import('xstate').EventObject} ev
180
+ */
181
+ send(ev) {
182
+ this.service.send(ev);
183
+ }
184
+
185
+ hostConnected() {
186
+ /* Do not mutate the context object. Instead, you should use the assign(...) action to update context immutably.
187
+ * https://stately.ai/docs/context#updating-context-with-assign
188
+ * https://lit.dev/docs/components/properties/#mutating-properties
189
+ */
190
+ this.service.subscribe(state => {
191
+ this.propKey in this.host && (this.host[this.propKey] = state);
192
+ });
193
+ }
194
+
195
+ hostDisconnected() {
196
+ this.service.stop();
197
+ }
198
+ }