@blockquote-web-components/blockquote-controller-context-meta 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) 2024 blockquote-controller-context-meta
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,138 @@
1
+ ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
2
+
3
+ `BlockquoteControllerContextMeta` is a Lit Reactive Controller that encapsulates the controllers provided by @lit/context.
4
+
5
+ **Features:**
6
+ - It enables a component to serve as both a provider and a consumer.
7
+ - It places the consumer after the first update to reduce the chance of a consumer in LightDOM requesting a context that currently lacks a provider.
8
+ - Create a context object using a global symbol (Symbol.for('my-context')).
9
+
10
+ <hr>
11
+
12
+ ### Demo
13
+
14
+ [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/oscarmarina/blockquote-web-components/tree/main/packages/controllers/blockquote-controller-context-meta)
15
+
16
+ ### Usage
17
+ - [Lit examples context-basics](https://lit.dev/playground/#sample=examples/context-basics)
18
+ ```js
19
+
20
+ import { html, LitElement, css } from 'lit';
21
+ import { BlockquoteControllerContextMeta } from '@blockquote-web-components/blockquote-controller-context-meta';
22
+
23
+ export class ProviderEl extends LitElement {
24
+ static styles = css`
25
+ slot {
26
+ display: block;
27
+ border: dashed 4px grey;
28
+ padding: 0px 10px;
29
+ }
30
+
31
+ :host {
32
+ display: block;
33
+ border: solid 4px gainsboro;
34
+ padding: 2px;
35
+ }
36
+
37
+ h3 {
38
+ margin-top: 0;
39
+ }
40
+ `;
41
+
42
+ static properties = {
43
+ data: {},
44
+ };
45
+
46
+ constructor() {
47
+ super();
48
+ this._provider = new BlockquoteControllerContextMeta(this, {
49
+ context: 'contextKey', // String used as key in Symbol.for when creating context with createContext(Symbol.for(context))
50
+ });
51
+
52
+ this.data = 'Initial';
53
+ }
54
+
55
+ // `data` will be provided to any consumer that is in the DOM tree below it.
56
+ set data(value) {
57
+ this._data = value;
58
+ this._provider.setValue(value);
59
+ }
60
+
61
+ get data() {
62
+ return this._data;
63
+ }
64
+
65
+ render() {
66
+ return html`
67
+ <h3>Provider's data: <code>${this.data}</code></h3>
68
+ <slot></slot>
69
+ `;
70
+ }
71
+ }
72
+ customElements.define('provider-el', ProviderEl);
73
+
74
+ export class ConsumerEl extends LitElement {
75
+ _consumer = new BlockquoteControllerContextMeta(this, {
76
+ context: 'contextKey', // String used as key in Symbol.for when creating context with createContext(Symbol.for(context))
77
+ callback: (v) => {
78
+ this.setAttribute('data-callback', v);
79
+ },
80
+ });
81
+
82
+
83
+ // `providedData` will be populated by the first ancestor element which
84
+ // provides a value for `context`.
85
+
86
+ get providedData() {
87
+ return this._consumer.value;
88
+ }
89
+
90
+ render() {
91
+ return html`<h3>Consumer data: <code>${this.providedData}</code></h3>
92
+ <hr />
93
+ <slot></slot>`;
94
+ }
95
+ }
96
+ customElements.define('consumer-el', ConsumerEl);
97
+ ```
98
+
99
+ <hr>
100
+
101
+
102
+ ### `src/BlockquoteControllerContextMeta.js`:
103
+
104
+ #### class: `ContextMeta`
105
+
106
+ ##### Fields
107
+
108
+ | Name | Privacy | Type | Default | Description | Inherited From |
109
+ | ---------------------- | ------- | ---- | ---------------------------------------------------------------------------------------------------------- | ----------- | -------------- |
110
+ | `value` | | | | | |
111
+ | `context` | | | | | |
112
+ | `initialValue` | | | `initialValue` | | |
113
+ | `callback` | | | `callback` | | |
114
+ | `host` | | | `host` | | |
115
+ | `_contextMetaProvider` | | | `new ContextProvider(this.host, { context: this.context, initialValue: this.initialValue, })` | | |
116
+
117
+ ##### Methods
118
+
119
+ | Name | Privacy | Description | Parameters | Return | Inherited From |
120
+ | --------------- | ------- | ----------- | ---------- | ------ | -------------- |
121
+ | `setValue` | | | `v, force` | | |
122
+ | `hostConnected` | | | | | |
123
+
124
+ <hr/>
125
+
126
+ #### Exports
127
+
128
+ | Kind | Name | Declaration | Module | Package |
129
+ | ---- | --------------------------------- | ----------- | -------------------------------------- | ------- |
130
+ | `js` | `BlockquoteControllerContextMeta` | ContextMeta | src/BlockquoteControllerContextMeta.js | |
131
+
132
+ ### `src/index.js`:
133
+
134
+ #### Exports
135
+
136
+ | Kind | Name | Declaration | Module | Package |
137
+ | ---- | --------------------------------- | ------------------------------- | ------------------------------------ | ------- |
138
+ | `js` | `BlockquoteControllerContextMeta` | BlockquoteControllerContextMeta | ./BlockquoteControllerContextMeta.js | |
package/package.json ADDED
@@ -0,0 +1,157 @@
1
+ {
2
+ "name": "@blockquote-web-components/blockquote-controller-context-meta",
3
+ "version": "1.0.0",
4
+ "description": "Webcomponent blockquote-controller-context-meta following open-wc recommendations",
5
+ "keywords": [
6
+ "lit",
7
+ "web-component",
8
+ "lit-element"
9
+ ],
10
+ "license": "MIT",
11
+ "author": "blockquote-controller-context-meta",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "default": "./src/index.js"
16
+ },
17
+ "./package.json": {
18
+ "default": "./package.json"
19
+ },
20
+ "./src/BlockquoteControllerContextMeta.js": {
21
+ "default": "./src/BlockquoteControllerContextMeta.js"
22
+ },
23
+ "./index.js": {
24
+ "default": "./src/index.js"
25
+ }
26
+ },
27
+ "main": "src/index.js",
28
+ "module": "src/index.js",
29
+ "files": [
30
+ "/define/",
31
+ "/src/",
32
+ "index.js",
33
+ "!/**/*.scss"
34
+ ],
35
+ "scripts": {
36
+ "analyze": "cem analyze --litelement --globs \"{src,define}/**/*.{js,ts}\" \"index.js\"",
37
+ "build": "echo \"This is not a TypeScript project, so no need to build.\"",
38
+ "dev:vite": "vite build",
39
+ "format": "npm run format:eslint && npm run format:prettier && npm run format:stylelint",
40
+ "format:eslint": "eslint \"**/*.{js,ts,html}\" --fix --ignore-path .eslintignore",
41
+ "format:prettier": "prettier \"**/*.{js,ts,json,html}\" --write --ignore-path .eslintignore",
42
+ "format:stylelint": "stylelint \"**/*.{scss,css}\" --fix --allow-empty-input --ignore-path .eslintignore",
43
+ "postinstall": "npm run sort:package",
44
+ "lint": "npm run lint:eslint && npm run lint:prettier && npm run lint:stylelint",
45
+ "lint:eslint": "eslint \"**/*.{js,ts,html}\" --ignore-path .eslintignore",
46
+ "lint:prettier": "prettier \"**/*.{js,ts,json,html}\" --check --ignore-path .eslintignore",
47
+ "lint:stylelint": "stylelint \"**/*.{scss,css}\" --allow-empty-input --ignore-path .eslintignore",
48
+ "prepare": "husky",
49
+ "preview:vite": "vite preview",
50
+ "sass:watch": "sass-style-template",
51
+ "sort:package": "npx sort-package-json",
52
+ "start": "concurrently -k -r \"npm:sass:watch\" \"npm:vite\"",
53
+ "test": "wtr",
54
+ "test:watch": "wtr --watch",
55
+ "vite": "vite"
56
+ },
57
+ "lint-staged": {
58
+ "**/*.{js,ts,html}": [
59
+ "npm run format:eslint"
60
+ ],
61
+ "**/*.{js,ts,json,html}": [
62
+ "npm run format:prettier"
63
+ ],
64
+ "**/*.{scss,css}": [
65
+ "npm run format:stylelint"
66
+ ]
67
+ },
68
+ "prettier": {
69
+ "arrowParens": "avoid",
70
+ "printWidth": 100,
71
+ "singleQuote": true,
72
+ "trailingComma": "all",
73
+ "overrides": [
74
+ {
75
+ "files": "*.{scss,css}",
76
+ "options": {
77
+ "printWidth": 280,
78
+ "singleQuote": false
79
+ }
80
+ }
81
+ ]
82
+ },
83
+ "eslintConfig": {
84
+ "parserOptions": {
85
+ "ecmaVersion": "latest"
86
+ },
87
+ "extends": [
88
+ "@open-wc",
89
+ "prettier"
90
+ ],
91
+ "rules": {
92
+ "class-methods-use-this": "off",
93
+ "indent": [
94
+ "error",
95
+ 2,
96
+ {
97
+ "SwitchCase": 1,
98
+ "ignoredNodes": [
99
+ "PropertyDefinition",
100
+ "TemplateLiteral > CallExpression"
101
+ ]
102
+ }
103
+ ],
104
+ "no-unused-expressions": [
105
+ "error",
106
+ {
107
+ "allowShortCircuit": true,
108
+ "allowTernary": true
109
+ }
110
+ ],
111
+ "object-curly-newline": "off",
112
+ "import/extensions": [
113
+ "error",
114
+ "always",
115
+ {
116
+ "ignorePackages": true
117
+ }
118
+ ],
119
+ "import/no-extraneous-dependencies": [
120
+ "error",
121
+ {
122
+ "devDependencies": [
123
+ "**/test/**/*.{js,ts}",
124
+ "**/*.config.{js,ts,mjs,cjs}",
125
+ "**/*.conf.{js,ts,mjs,cjs}"
126
+ ]
127
+ }
128
+ ],
129
+ "import/no-unresolved": "off",
130
+ "import/prefer-default-export": "off",
131
+ "lit/no-classfield-shadowing": "off",
132
+ "lit/no-native-attributes": "off"
133
+ }
134
+ },
135
+ "stylelint": {
136
+ "extends": "stylelint-config-standard-scss",
137
+ "rules": {
138
+ "custom-property-pattern": null,
139
+ "no-duplicate-selectors": null,
140
+ "color-function-notation": null,
141
+ "alpha-value-notation": null
142
+ }
143
+ },
144
+ "dependencies": {
145
+ "@lit/context": "^1.1.0",
146
+ "lit": "^3.1.2"
147
+ },
148
+ "devDependencies": {
149
+ "@blockquote-web-components/blockquote-base-common-dev-dependencies": "^1.8.0",
150
+ "@blockquote-web-components/blockquote-base-embedded-webview": "^1.10.0"
151
+ },
152
+ "publishConfig": {
153
+ "access": "public"
154
+ },
155
+ "customElements": "custom-elements.json",
156
+ "gitHead": "51af18a7ff99ffb40cc02b5363ba04f11d058c60"
157
+ }
@@ -0,0 +1,32 @@
1
+ import { ReactiveController, ReactiveControllerHost } from 'lit';
2
+ import { Context, ContextType } from '@lit/context';
3
+
4
+ declare class ContextMeta<
5
+ TMeta extends Context<unknown, unknown>,
6
+ HostElement extends ReactiveControllerHost & HTMLElement,
7
+ > implements ReactiveController
8
+ {
9
+ private host;
10
+ private context;
11
+ private initialValue?;
12
+ private callback?;
13
+ private _contextMetaProvider;
14
+ private _contextMetaConsumer;
15
+ constructor(
16
+ host: HostElement,
17
+ {
18
+ context,
19
+ initialValue,
20
+ callback,
21
+ }: {
22
+ context: string;
23
+ initialValue?: ContextType<TMeta>;
24
+ callback?: (v: ContextType<TMeta>, dispose?: () => void) => void;
25
+ },
26
+ );
27
+ get value(): ContextType<TMeta> | undefined;
28
+ setValue(v: ContextType<TMeta>, force?: boolean): void;
29
+ hostConnected(): Promise<void>;
30
+ }
31
+
32
+ export { ContextMeta as BlockquoteControllerContextMeta };
@@ -0,0 +1,144 @@
1
+ import { createContext, ContextProvider, ContextConsumer } from '@lit/context';
2
+
3
+ /**
4
+ * ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
5
+ *
6
+ * `BlockquoteControllerContextMeta` is a Lit Reactive Controller that encapsulates the controllers provided by @lit/context.
7
+ *
8
+ * **Features:**
9
+ * - It enables a component to serve as both a provider and a consumer.
10
+ * - It places the consumer after the first update to reduce the chance of a consumer in LightDOM requesting a context that currently lacks a provider.
11
+ * - Create a context object using a global symbol (Symbol.for('my-context')).
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/blockquote-web-components/tree/main/packages/controllers/blockquote-controller-context-meta)
18
+ *
19
+ * ### Usage
20
+ * - [Lit examples context-basics](https://lit.dev/playground/#sample=examples/context-basics)
21
+ * ```js
22
+ *
23
+ * import { html, LitElement, css } from 'lit';
24
+ * import { BlockquoteControllerContextMeta } from '@blockquote-web-components/blockquote-controller-context-meta';
25
+ *
26
+ * export class ProviderEl extends LitElement {
27
+ * static styles = css`
28
+ * slot {
29
+ * display: block;
30
+ * border: dashed 4px grey;
31
+ * padding: 0px 10px;
32
+ * }
33
+ *
34
+ * :host {
35
+ * display: block;
36
+ * border: solid 4px gainsboro;
37
+ * padding: 2px;
38
+ * }
39
+ *
40
+ * h3 {
41
+ * margin-top: 0;
42
+ * }
43
+ * `;
44
+ *
45
+ * static properties = {
46
+ * data: {},
47
+ * };
48
+ *
49
+ * constructor() {
50
+ * super();
51
+ * this._provider = new BlockquoteControllerContextMeta(this, {
52
+ * context: 'contextKey', // String used as key in Symbol.for when creating context with createContext(Symbol.for(context))
53
+ * });
54
+ *
55
+ * this.data = 'Initial';
56
+ * }
57
+ *
58
+ * // `data` will be provided to any consumer that is in the DOM tree below it.
59
+ * set data(value) {
60
+ * this._data = value;
61
+ * this._provider.setValue(value);
62
+ * }
63
+ *
64
+ * get data() {
65
+ * return this._data;
66
+ * }
67
+ *
68
+ * render() {
69
+ * return html`
70
+ * <h3>Provider's data: <code>${this.data}</code></h3>
71
+ * <slot></slot>
72
+ * `;
73
+ * }
74
+ * }
75
+ * customElements.define('provider-el', ProviderEl);
76
+ *
77
+ * export class ConsumerEl extends LitElement {
78
+ * _consumer = new BlockquoteControllerContextMeta(this, {
79
+ * context: 'contextKey', // String used as key in Symbol.for when creating context with createContext(Symbol.for(context))
80
+ * callback: (v) => {
81
+ * this.setAttribute('data-callback', v);
82
+ * },
83
+ * });
84
+ *
85
+ *
86
+ * // `providedData` will be populated by the first ancestor element which
87
+ * // provides a value for `context`.
88
+ *
89
+ * get providedData() {
90
+ * return this._consumer.value;
91
+ * }
92
+ *
93
+ * render() {
94
+ * return html`<h3>Consumer data: <code>${this.providedData}</code></h3>
95
+ * <hr />
96
+ * <slot></slot>`;
97
+ * }
98
+ * }
99
+ * customElements.define('consumer-el', ConsumerEl);
100
+ * ```
101
+ *
102
+ * <hr>
103
+ */
104
+ class ContextMeta {
105
+ /**
106
+ * @param {import('lit').ReactiveElement} host - The host object.
107
+ * @param {{
108
+ * context: string,
109
+ * initialValue?: import('@lit/context').ContextType<*>,
110
+ * callback?: (value: import('@lit/context').ContextType<*>, dispose?: () => void) => void
111
+ * }} arg - The arguments for the constructor.
112
+ */
113
+ constructor(host, { context, initialValue, callback }) {
114
+ this.context = createContext(Symbol.for(context));
115
+ this.initialValue = initialValue;
116
+ this.callback = callback;
117
+ this.host = host;
118
+
119
+ this._contextMetaProvider = new ContextProvider(this.host, {
120
+ context: this.context,
121
+ initialValue: this.initialValue,
122
+ });
123
+
124
+ this.host.addController?.(this);
125
+ }
126
+
127
+ get value() {
128
+ return this._contextMetaConsumer?.value;
129
+ }
130
+
131
+ setValue(v, force = false) {
132
+ this._contextMetaProvider?.setValue?.(v, force);
133
+ }
134
+
135
+ async hostConnected() {
136
+ await this.host.updateComplete;
137
+ this._contextMetaConsumer = new ContextConsumer(this.host, {
138
+ context: this.context,
139
+ subscribe: true,
140
+ callback: this.callback,
141
+ });
142
+ }
143
+ }
144
+ export { ContextMeta as BlockquoteControllerContextMeta };
package/src/index.js ADDED
@@ -0,0 +1 @@
1
+ export { BlockquoteControllerContextMeta } from './BlockquoteControllerContextMeta.js';