@blockquote-web-components/blockquote-controller-context-meta 1.4.10 → 1.5.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 CHANGED
@@ -145,35 +145,41 @@ customElements.define('consumer-el', ConsumerEl);
145
145
  | `js` | `BaseContextMetaElement` | BaseContextMetaElement | ./BaseContextMetaElement.js | |
146
146
  | `js` | `contextMetaProvider` | contextMetaProvider | ./directives/context-meta-provider.js | |
147
147
  | `js` | `cacheContextMetaProvider` | cacheContextMetaProvider | ./directives/cache-context-meta-provider.js | |
148
- | `js` | `contextMetaKeyProvider` | contextMetaKeyProvider | ./directives/cache-context-meta-provider.js | |
149
148
 
150
- ### `src/directives/cache-context-meta-provider.js`:
151
-
152
- #### Variables
153
149
 
154
- | Name | Description | Type |
155
- | ------------------------ | ----------- | ---- |
156
- | `contextMetaKeyProvider` | | |
150
+ ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
157
151
 
158
- <hr/>
152
+ `contextMetaProviderDirective` is a Lit directive that enables normal DOM elements to act as context providers.
153
+ You can use this directive in both attribute and element bindings in Lit templates.
159
154
 
160
- #### Functions
155
+ > https://github.com/lit/lit/discussions/4690
161
156
 
162
- | Name | Description | Parameters | Return |
163
- | -------------------------- | ----------- | --------------------------- | ------ |
164
- | `cacheContextMetaProvider` | | `element, contextOrOptions` | |
157
+ Usage:
158
+ This directive transforms a DOM element into a Lit context provider using the BlockquoteControllerContextMeta class, a
159
+ Lit Reactive Controller that encapsulates controllers provided by [@lit/context](https://lit.dev/docs/data/context/).
165
160
 
166
- <hr/>
161
+ ## Features
162
+ - Enables non-Lit elements to provide context.
163
+ - Works seamlessly with [`@lit/context`](https://lit.dev/docs/data/context/).
164
+ - Utilizes `BlockquoteControllerContextMeta`, a Lit Reactive Controller for managing context.
167
165
 
168
- #### Exports
166
+ ```js
167
+ <div ${contextMetaProviderDirective(someValue, myContext)}>
168
+ <!-- Children can consume the provided context -->
169
+ </div>
170
+ //
171
+ <div data-info="${contextMetaProviderDirective(someValue, myContext)}">
172
+ <!-- Children can consume the provided context -->
173
+ </div>
174
+ ```
169
175
 
170
- | Kind | Name | Declaration | Module | Package |
171
- | ---- | -------------------------- | ------------------------ | --------------------------------------------- | ------- |
172
- | `js` | `contextMetaKeyProvider` | contextMetaKeyProvider | src/directives/cache-context-meta-provider.js | |
173
- | `js` | `cacheContextMetaProvider` | cacheContextMetaProvider | src/directives/cache-context-meta-provider.js | |
174
176
 
175
177
  ### `src/directives/context-meta-provider.js`:
176
178
 
179
+ #### class: `README`
180
+
181
+ <hr/>
182
+
177
183
  #### Variables
178
184
 
179
185
  | Name | Description | Type |
@@ -186,9 +192,24 @@ customElements.define('consumer-el', ConsumerEl);
186
192
 
187
193
  | Kind | Name | Declaration | Module | Package |
188
194
  | ---- | --------------------- | ------------------- | --------------------------------------- | ------- |
195
+ | `js` | `README` | README | src/directives/context-meta-provider.js | |
189
196
  | `js` | `contextMetaProvider` | contextMetaProvider | src/directives/context-meta-provider.js | |
190
197
 
191
- ---
198
+ ### `src/directives/cache-context-meta-provider.js`:
199
+
200
+ #### Functions
201
+
202
+ | Name | Description | Parameters | Return |
203
+ | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------ |
204
+ | `cacheContextMetaProvider` | Return or create a cached BlockquoteControllerContextMeta for (element, context).&#xA;&#xA;This function memoizes BlockquoteControllerContextMeta instances per (element, contextKey).&#xA;If a BlockquoteControllerContextMeta already exists for the given element and context, it is&#xA;returned; otherwise a new BlockquoteControllerContextMeta is created, cached and returned. | `element: HTMLElement\|*, {context = contextMetaSymbol, initialValue}, arg: { * context?: *, * initialValue?: ContextType<*>, * }` | |
205
+
206
+ <hr/>
207
+
208
+ #### Exports
209
+
210
+ | Kind | Name | Declaration | Module | Package |
211
+ | ---- | -------------------------- | ------------------------ | --------------------------------------------- | ------- |
212
+ | `js` | `cacheContextMetaProvider` | cacheContextMetaProvider | src/directives/cache-context-meta-provider.js | |
192
213
 
193
214
  ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
194
215
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockquote-web-components/blockquote-controller-context-meta",
3
- "version": "1.4.10",
3
+ "version": "1.5.0",
4
4
  "description": "Webcomponent blockquote-controller-context-meta following open-wc recommendations",
5
5
  "keywords": [
6
6
  "lit",
@@ -122,11 +122,11 @@
122
122
  },
123
123
  "devDependencies": {
124
124
  "@blockquote-web-components/blockquote-base-common-dev-dependencies": "^1.12.4",
125
- "@blockquote-web-components/blockquote-base-embedded-webview": "^1.13.9"
125
+ "@blockquote-web-components/blockquote-base-embedded-webview": "^1.13.10"
126
126
  },
127
127
  "publishConfig": {
128
128
  "access": "public"
129
129
  },
130
130
  "customElements": "custom-elements.json",
131
- "gitHead": "3b3617da1d3da881b015dc350d1c9498991b97c7"
131
+ "gitHead": "e97796a020703685a2f3c51b580cdf3a13198680"
132
132
  }
@@ -1,15 +1,50 @@
1
- import {BlockquoteControllerContextMeta} from '../BlockquoteControllerContextMeta.js';
1
+ import {
2
+ BlockquoteControllerContextMeta,
3
+ contextMetaSymbol,
4
+ } from '../BlockquoteControllerContextMeta.js';
2
5
 
3
- export const contextMetaKeyProvider = Symbol();
6
+ const metaByElement = new WeakMap();
4
7
 
5
- export const cacheContextMetaProvider = (element, contextOrOptions) => {
6
- if (element[contextMetaKeyProvider]) {
7
- return element[contextMetaKeyProvider];
8
+ /**
9
+ * Get or create the per-element Map that holds ContextMeta instances for
10
+ * different context keys.
11
+ *
12
+ * The per-element map is stored in the private WeakMap `metaByElement`.
13
+ *
14
+ * @param {HTMLElement|*} element - target used as WeakMap key
15
+ */
16
+ const getPerElementMap = (element) => {
17
+ let per = metaByElement.get(element);
18
+ if (!per) {
19
+ per = new Map();
20
+ metaByElement.set(element, per);
8
21
  }
22
+ return per;
23
+ };
24
+
25
+ /**
26
+ * Return or create a cached BlockquoteControllerContextMeta for (element, context).
27
+ *
28
+ * This function memoizes BlockquoteControllerContextMeta instances per (element, contextKey).
29
+ * If a BlockquoteControllerContextMeta already exists for the given element and context, it is
30
+ * returned; otherwise a new BlockquoteControllerContextMeta is created, cached and returned.
31
+ *
32
+ * @param {HTMLElement|*} element - element
33
+ * @param {{
34
+ * context?: *,
35
+ * initialValue?: import('@lit/context').ContextType<*>,
36
+ * }} arg - options for the context provider.
37
+ */
38
+ export const cacheContextMetaProvider = (element, {context = contextMetaSymbol, initialValue}) => {
39
+ const options = {context, initialValue};
40
+ const contextKey = options.context;
41
+ const perElement = getPerElementMap(element);
42
+ let meta = perElement.get(contextKey);
9
43
 
10
- const ctx =
11
- contextOrOptions?.context !== undefined ? {...contextOrOptions} : {context: contextOrOptions};
44
+ if (!meta) {
45
+ meta = new BlockquoteControllerContextMeta(element, options);
46
+ perElement.set(contextKey, meta);
47
+ }
12
48
 
13
- element[contextMetaKeyProvider] = new BlockquoteControllerContextMeta(element, ctx);
14
- return element[contextMetaKeyProvider];
49
+ return meta;
15
50
  };
@@ -1,8 +1,10 @@
1
1
  import {Directive, directive, PartType} from 'lit/directive.js';
2
2
  import {noChange} from 'lit';
3
- import {cacheContextMetaProvider, contextMetaKeyProvider} from './cache-context-meta-provider.js';
3
+ import {cacheContextMetaProvider} from './cache-context-meta-provider.js';
4
4
 
5
5
  /**
6
+ * ![Lit](https://img.shields.io/badge/lit-3.0.0-blue.svg)
7
+ *
6
8
  * `contextMetaProviderDirective` is a Lit directive that enables normal DOM elements to act as context providers.
7
9
  * You can use this directive in both attribute and element bindings in Lit templates.
8
10
  *
@@ -17,16 +19,18 @@ import {cacheContextMetaProvider, contextMetaKeyProvider} from './cache-context-
17
19
  * - Works seamlessly with [`@lit/context`](https://lit.dev/docs/data/context/).
18
20
  * - Utilizes `BlockquoteControllerContextMeta`, a Lit Reactive Controller for managing context.
19
21
  *
20
- * js```
21
- * <div ${contextMetaProviderDirective(myContext, someValue)}>
22
+ * ```js
23
+ * <div ${contextMetaProviderDirective(someValue, myContext)}>
22
24
  * <!-- Children can consume the provided context -->
23
25
  * </div>
24
26
  * //
25
- * <div data-info="${contextMetaProviderDirective(myContext, someValue)}">
27
+ * <div data-info="${contextMetaProviderDirective(someValue, myContext)}">
26
28
  * <!-- Children can consume the provided context -->
27
29
  * </div>
28
30
  * ```
29
31
  */
32
+ export class README extends Text {}
33
+
30
34
  class ContextMetaProviderDirective extends Directive {
31
35
  /** @type {*} */
32
36
  #partInfo = undefined;
@@ -50,16 +54,15 @@ class ContextMetaProviderDirective extends Directive {
50
54
  * Main render method called by Lit.
51
55
  * @param {*} value - The context value to provide.
52
56
  * @param {{
53
- * context?: *,
54
- * initialValue?: import('@lit/context').ContextType<*>,
55
- * }} context - The context object.
56
- * @returns {unknown} - The serialized context value or noChange.
57
+ * context?: *,
58
+ * initialValue?: import('@lit/context').ContextType<*>,
59
+ * }} arg - options for the context provider.
57
60
  */
58
- render(value, context) {
61
+ render(value, arg) {
59
62
  if (value !== this.#currentValue) {
60
63
  this.#currentValue = value;
61
- this.updateValue(value, context);
62
- return this.resolveAttrValue();
64
+ this.updateValue(value, arg);
65
+ return this.resolveAttrValue(value);
63
66
  }
64
67
  return noChange;
65
68
  }
@@ -68,25 +71,25 @@ class ContextMetaProviderDirective extends Directive {
68
71
  * Updates the context value for the element.
69
72
  * @param {*} value - The new context value.
70
73
  * @param {{
71
- * context?: *,
72
- * initialValue?: import('@lit/context').ContextType<*>,
73
- * }} context - The context object.
74
+ * context?: *,
75
+ * initialValue?: import('@lit/context').ContextType<*>,
76
+ * }} options - options for the context provider.
74
77
  */
75
- updateValue(value, context) {
78
+ updateValue(value, options) {
76
79
  const element = this.#partInfo.element;
77
- cacheContextMetaProvider(element, context);
78
- element[contextMetaKeyProvider].setValue(value);
80
+ const metaProvider = cacheContextMetaProvider(element, options);
81
+ metaProvider.setValue(value);
79
82
  }
80
83
 
81
84
  /**
82
- * Decides whether to return the currentValue if the directive is used as an attribute.
83
- * @returns {unknown} - The serialized context value or noChange.
85
+ * Decides whether to return the value if the directive is used as an attribute.
86
+ * @param {*} value - The context value to provide.
84
87
  */
85
- resolveAttrValue() {
88
+ resolveAttrValue(value) {
86
89
  if (this.#partInfo.type !== PartType.ATTRIBUTE) {
87
90
  return noChange;
88
91
  }
89
- return this.#currentValue;
92
+ return value;
90
93
  }
91
94
  }
92
95
 
package/src/index.js CHANGED
@@ -1,7 +1,4 @@
1
1
  export {BlockquoteControllerContextMeta} from './BlockquoteControllerContextMeta.js';
2
2
  export {BaseContextMetaElement} from './BaseContextMetaElement.js';
3
3
  export {contextMetaProvider} from './directives/context-meta-provider.js';
4
- export {
5
- cacheContextMetaProvider,
6
- contextMetaKeyProvider,
7
- } from './directives/cache-context-meta-provider.js';
4
+ export {cacheContextMetaProvider} from './directives/cache-context-meta-provider.js';