element-vir 5.6.0 → 6.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.
Files changed (74) hide show
  1. package/README.md +97 -93
  2. package/dist/augments/testing.d.ts +15 -0
  3. package/dist/augments/testing.js +76 -0
  4. package/dist/augments/type.d.ts +7 -0
  5. package/dist/augments/type.js +13 -1
  6. package/dist/{functional-element → declarative-element}/css-vars.d.ts +0 -0
  7. package/dist/{functional-element → declarative-element}/css-vars.js +0 -0
  8. package/dist/declarative-element/declarative-element-init.d.ts +33 -0
  9. package/dist/declarative-element/declarative-element-init.js +1 -0
  10. package/dist/declarative-element/declarative-element.d.ts +44 -0
  11. package/dist/declarative-element/declarative-element.js +18 -0
  12. package/dist/declarative-element/define-element-no-inputs.d.ts +5 -0
  13. package/dist/declarative-element/define-element-no-inputs.js +118 -0
  14. package/dist/declarative-element/define-element.d.ts +5 -0
  15. package/dist/declarative-element/define-element.js +13 -0
  16. package/dist/declarative-element/definition-options.d.ts +5 -0
  17. package/dist/declarative-element/definition-options.js +4 -0
  18. package/dist/declarative-element/directives/assign-with-clean-up.directive.d.ts +23 -0
  19. package/dist/declarative-element/directives/assign-with-clean-up.directive.js +43 -0
  20. package/dist/declarative-element/directives/assign.directive.d.ts +12 -0
  21. package/dist/declarative-element/directives/assign.directive.js +35 -0
  22. package/dist/{functional-element/directives/directive-util.d.ts → declarative-element/directives/directive-helpers.d.ts} +2 -3
  23. package/dist/{functional-element/directives/directive-util.js → declarative-element/directives/directive-helpers.js} +4 -4
  24. package/dist/{functional-element → declarative-element}/directives/listen.directive.d.ts +2 -2
  25. package/dist/{functional-element → declarative-element}/directives/listen.directive.js +1 -1
  26. package/dist/{functional-element → declarative-element}/directives/on-dom-created.directive.d.ts +0 -0
  27. package/dist/{functional-element → declarative-element}/directives/on-dom-created.directive.js +1 -1
  28. package/dist/{functional-element → declarative-element}/directives/on-resize.directive.d.ts +0 -0
  29. package/dist/{functional-element → declarative-element}/directives/on-resize.directive.js +1 -1
  30. package/dist/{functional-element → declarative-element}/element-events.d.ts +0 -0
  31. package/dist/{functional-element → declarative-element}/element-events.js +1 -1
  32. package/dist/{functional-element → declarative-element}/element-properties.d.ts +3 -3
  33. package/dist/declarative-element/element-properties.js +53 -0
  34. package/dist/declarative-element/has-declarative-element-parent.d.ts +1 -0
  35. package/dist/declarative-element/has-declarative-element-parent.js +16 -0
  36. package/dist/declarative-element/host-classes.d.ts +21 -0
  37. package/dist/{functional-element → declarative-element}/host-classes.js +0 -0
  38. package/dist/declarative-element/render-callback.d.ts +22 -0
  39. package/dist/{functional-element → declarative-element}/render-callback.js +4 -3
  40. package/dist/{functional-element → declarative-element}/styles.d.ts +7 -1
  41. package/dist/{functional-element → declarative-element}/styles.js +2 -2
  42. package/dist/{functional-element → declarative-element}/tag-name.d.ts +0 -0
  43. package/dist/{functional-element → declarative-element}/tag-name.js +0 -0
  44. package/dist/declarative-element-marker-symbol.d.ts +1 -0
  45. package/dist/declarative-element-marker-symbol.js +1 -0
  46. package/dist/index.d.ts +14 -12
  47. package/dist/index.js +13 -12
  48. package/dist/require-declarative-element.d.ts +2 -0
  49. package/dist/require-declarative-element.js +4 -0
  50. package/dist/template-transforms/nested-mapped-templates.d.ts +6 -0
  51. package/dist/template-transforms/nested-mapped-templates.js +96 -0
  52. package/dist/template-transforms/transform-template.js +21 -2
  53. package/dist/template-transforms/vir-css/css-transform.d.ts +2 -2
  54. package/dist/template-transforms/vir-css/vir-css.d.ts +2 -2
  55. package/dist/template-transforms/vir-css/vir-css.js +1 -1
  56. package/dist/template-transforms/vir-html/html-transform.js +3 -3
  57. package/dist/template-transforms/vir-html/vir-html.d.ts +1 -1
  58. package/dist/template-transforms/vir-html/vir-html.js +1 -1
  59. package/index.html +14 -0
  60. package/package.json +14 -14
  61. package/public/index.css +7 -0
  62. package/dist/functional-element/define-functional-element.d.ts +0 -4
  63. package/dist/functional-element/define-functional-element.js +0 -70
  64. package/dist/functional-element/directives/assign-with-clean-up.directive.d.ts +0 -19
  65. package/dist/functional-element/directives/assign-with-clean-up.directive.js +0 -36
  66. package/dist/functional-element/directives/assign.directive.d.ts +0 -15
  67. package/dist/functional-element/directives/assign.directive.js +0 -23
  68. package/dist/functional-element/element-properties.js +0 -50
  69. package/dist/functional-element/functional-element.d.ts +0 -57
  70. package/dist/functional-element/functional-element.js +0 -3
  71. package/dist/functional-element/host-classes.d.ts +0 -19
  72. package/dist/functional-element/render-callback.d.ts +0 -21
  73. package/dist/require-functional-element.d.ts +0 -2
  74. package/dist/require-functional-element.js +0 -4
package/dist/index.js CHANGED
@@ -1,15 +1,16 @@
1
- export * from './functional-element/define-functional-element';
2
- export * from './functional-element/directives/assign-with-clean-up.directive';
3
- export * from './functional-element/directives/assign.directive';
4
- export * from './functional-element/directives/directive-util';
5
- export * from './functional-element/directives/listen.directive';
6
- export * from './functional-element/directives/on-dom-created.directive';
7
- export * from './functional-element/directives/on-resize.directive';
8
- export * from './functional-element/element-events';
9
- export * from './functional-element/element-properties';
10
- export * from './functional-element/functional-element';
11
- export * from './functional-element/render-callback';
12
- export { requireAllCustomElementsToBeFunctionalElement } from './require-functional-element';
1
+ export * from './declarative-element/declarative-element';
2
+ export * from './declarative-element/define-element';
3
+ export * from './declarative-element/define-element-no-inputs';
4
+ export * from './declarative-element/directives/assign-with-clean-up.directive';
5
+ export * from './declarative-element/directives/assign.directive';
6
+ export * from './declarative-element/directives/directive-helpers';
7
+ export * from './declarative-element/directives/listen.directive';
8
+ export * from './declarative-element/directives/on-dom-created.directive';
9
+ export * from './declarative-element/directives/on-resize.directive';
10
+ export * from './declarative-element/element-events';
11
+ export * from './declarative-element/element-properties';
12
+ export * from './declarative-element/render-callback';
13
+ export { requireAllCustomElementsToBeDeclarativeElements } from './require-declarative-element';
13
14
  export * from './template-transforms/vir-css/vir-css';
14
15
  export * from './template-transforms/vir-html/vir-html';
15
16
  export * from './typed-event/typed-event';
@@ -0,0 +1,2 @@
1
+ export declare let declarativeElementRequired: boolean;
2
+ export declare function requireAllCustomElementsToBeDeclarativeElements(): void;
@@ -0,0 +1,4 @@
1
+ export let declarativeElementRequired = false;
2
+ export function requireAllCustomElementsToBeDeclarativeElements() {
3
+ declarativeElementRequired = true;
4
+ }
@@ -0,0 +1,6 @@
1
+ import { TemplateTransform } from './transform-template';
2
+ export declare function getAlreadyMappedTemplate<PossibleValues>(templateStringsKey: TemplateStringsArray, values: PossibleValues[]): TemplateTransform | undefined;
3
+ export declare function setMappedTemplate<PossibleValues>(templateStringsKey: TemplateStringsArray, values: PossibleValues[], valueToSet: TemplateTransform): {
4
+ result: boolean;
5
+ reason: string;
6
+ };
@@ -0,0 +1,96 @@
1
+ import { typedHasOwnProperties } from 'augment-vir';
2
+ import { DeclarativeElementMarkerSymbol } from '../declarative-element-marker-symbol';
3
+ function extractElementKeys(values) {
4
+ return values.filter((value) => {
5
+ return (typedHasOwnProperties(value, [
6
+ 'tagName',
7
+ DeclarativeElementMarkerSymbol,
8
+ ]) &&
9
+ !!value.tagName &&
10
+ !!value[DeclarativeElementMarkerSymbol]);
11
+ });
12
+ }
13
+ /**
14
+ * The transformed templates are written to a map so that we can preserve reference equality between
15
+ * calls. Without maintaining reference equality between html`` calls, lit-element reconstructs all
16
+ * of its children on every render.
17
+ *
18
+ * This is a WeakMap because we only care about the transformed array value as long as the original
19
+ * template array key exists.
20
+ */
21
+ const transformedTemplateStrings = new WeakMap();
22
+ export function getAlreadyMappedTemplate(templateStringsKey, values) {
23
+ var _a;
24
+ const elementKeys = extractElementKeys(values);
25
+ const nestedValue = getNestedValues(transformedTemplateStrings, [
26
+ templateStringsKey,
27
+ ...elementKeys,
28
+ ]);
29
+ return (_a = nestedValue.value) === null || _a === void 0 ? void 0 : _a.template;
30
+ }
31
+ export function setMappedTemplate(templateStringsKey, values, valueToSet) {
32
+ const elementKeys = extractElementKeys(values);
33
+ return setNestedValues(transformedTemplateStrings, [
34
+ templateStringsKey,
35
+ ...elementKeys,
36
+ ], valueToSet);
37
+ }
38
+ function getNestedValues(map, keys, index = 0) {
39
+ const { currentTemplateAndNested, reason } = getCurrentKeyAndValue(map, keys, index);
40
+ if (!currentTemplateAndNested) {
41
+ return { value: currentTemplateAndNested, reason };
42
+ }
43
+ if (index === keys.length - 1) {
44
+ return { value: currentTemplateAndNested, reason: `reached end of keys array` };
45
+ }
46
+ if (!currentTemplateAndNested.nested) {
47
+ return { value: undefined, reason: `map at key index ${index} did not have nested maps` };
48
+ }
49
+ return getNestedValues(currentTemplateAndNested.nested, keys, index + 1);
50
+ }
51
+ function getCurrentKeyAndValue(map, keys, index) {
52
+ const currentKey = keys[index];
53
+ if (currentKey == undefined) {
54
+ return {
55
+ currentKey: undefined,
56
+ currentTemplateAndNested: undefined,
57
+ reason: `key at index ${index} not found`,
58
+ };
59
+ }
60
+ if (!map.has(currentKey)) {
61
+ return {
62
+ currentKey,
63
+ currentTemplateAndNested: undefined,
64
+ reason: `key at index ${index} was not in the map`,
65
+ };
66
+ }
67
+ const currentTemplateAndNested = map.get(currentKey);
68
+ if (currentTemplateAndNested == undefined) {
69
+ return {
70
+ currentKey,
71
+ currentTemplateAndNested: undefined,
72
+ reason: `value at key at index ${index} was undefined`,
73
+ };
74
+ }
75
+ return { currentKey, currentTemplateAndNested, reason: `key and value exists` };
76
+ }
77
+ function setNestedValues(map, keys, valueToSet, index = 0) {
78
+ var _a;
79
+ const { currentTemplateAndNested, currentKey, reason } = getCurrentKeyAndValue(map, keys, index);
80
+ if (!currentKey) {
81
+ return { result: false, reason };
82
+ }
83
+ const nestedAndTemplate = currentTemplateAndNested !== null && currentTemplateAndNested !== void 0 ? currentTemplateAndNested : { nested: undefined, template: undefined };
84
+ if (!currentTemplateAndNested) {
85
+ map.set(currentKey, nestedAndTemplate);
86
+ }
87
+ if (index === keys.length - 1) {
88
+ nestedAndTemplate.template = valueToSet;
89
+ return { result: true, reason: `set value at end of keys array` };
90
+ }
91
+ const nestedWeakMap = (_a = nestedAndTemplate.nested) !== null && _a !== void 0 ? _a : new WeakMap();
92
+ if (!nestedAndTemplate.nested) {
93
+ nestedAndTemplate.nested = nestedWeakMap;
94
+ }
95
+ return setNestedValues(nestedWeakMap, keys, valueToSet, index + 1);
96
+ }
@@ -1,4 +1,7 @@
1
+ import { typedHasOwnProperties } from 'augment-vir';
1
2
  import { filterOutArrayIndexes } from '../augments/array';
3
+ import { DeclarativeElementMarkerSymbol } from '../declarative-element-marker-symbol';
4
+ import { getAlreadyMappedTemplate, setMappedTemplate } from './nested-mapped-templates';
2
5
  export function makeCheckTransform(name, check, transform) {
3
6
  return {
4
7
  name,
@@ -15,11 +18,27 @@ export function makeCheckTransform(name, check, transform) {
15
18
  * template array key exists.
16
19
  */
17
20
  const transformedTemplateStrings = new WeakMap();
21
+ function extractElementValues(values) {
22
+ return values.filter((value) => {
23
+ return (typedHasOwnProperties(value, [
24
+ 'tagName',
25
+ DeclarativeElementMarkerSymbol,
26
+ ]) &&
27
+ !!value.tagName &&
28
+ !!value[DeclarativeElementMarkerSymbol]);
29
+ });
30
+ }
18
31
  export function getTransformedTemplate(templateStringsKey, values, fallbackTransform) {
19
- const alreadyTransformedTemplateStrings = transformedTemplateStrings.get(templateStringsKey);
32
+ const alreadyTransformedTemplateStrings = getAlreadyMappedTemplate(templateStringsKey, values);
20
33
  const templateTransform = alreadyTransformedTemplateStrings !== null && alreadyTransformedTemplateStrings !== void 0 ? alreadyTransformedTemplateStrings : fallbackTransform();
21
34
  if (!alreadyTransformedTemplateStrings) {
22
- transformedTemplateStrings.set(templateStringsKey, templateTransform);
35
+ const result = setMappedTemplate(templateStringsKey, values, templateTransform);
36
+ if (!result.result) {
37
+ throw new Error(`Failed to set template transform: ${result.reason}`);
38
+ }
39
+ else {
40
+ transformedTemplateStrings.set(templateStringsKey, templateTransform);
41
+ }
23
42
  }
24
43
  const transformedValuesArray = filterOutArrayIndexes(values, templateTransform.valueIndexDeletions);
25
44
  return { strings: templateTransform.templateStrings, values: transformedValuesArray };
@@ -1,7 +1,7 @@
1
1
  import { CSSResultGroup } from 'lit';
2
- import { FunctionalElement } from '../../functional-element/functional-element';
2
+ import { DeclarativeElementDefinition } from '../../declarative-element/declarative-element';
3
3
  export declare type CssTemplateTransform = {
4
4
  templateStrings: TemplateStringsArray;
5
5
  valueIndexDeletions: number[];
6
6
  };
7
- export declare function transformCssTemplate(inputTemplateStrings: TemplateStringsArray, inputValues: (number | CSSResultGroup | FunctionalElement)[]): CssTemplateTransform;
7
+ export declare function transformCssTemplate(inputTemplateStrings: TemplateStringsArray, inputValues: (number | CSSResultGroup | DeclarativeElementDefinition)[]): CssTemplateTransform;
@@ -1,3 +1,3 @@
1
1
  import { CSSResult, CSSResultGroup } from 'lit';
2
- import { FunctionalElement } from '../../functional-element/functional-element';
3
- export declare function css(inputTemplateStrings: TemplateStringsArray, ...inputValues: (number | CSSResultGroup | FunctionalElement)[]): CSSResult;
2
+ import { DeclarativeElementDefinition } from '../../declarative-element/declarative-element';
3
+ export declare function css(inputTemplateStrings: TemplateStringsArray, ...inputValues: (number | CSSResultGroup | DeclarativeElementDefinition)[]): CSSResult;
@@ -6,7 +6,7 @@ export function css(inputTemplateStrings, ...inputValues) {
6
6
  return transformCssTemplate(inputTemplateStrings, inputValues);
7
7
  });
8
8
  const cssResult = litCss(transformedTemplate.strings,
9
- /** The filter will remove the FunctionalElement elements */
9
+ /** The filter will remove the DeclarativeElement elements */
10
10
  ...transformedTemplate.values);
11
11
  return cssResult;
12
12
  }
@@ -1,5 +1,5 @@
1
1
  import { collapseWhiteSpace, safeMatch } from 'augment-vir';
2
- import { functionalElementRequired } from '../../require-functional-element';
2
+ import { declarativeElementRequired } from '../../require-declarative-element';
3
3
  import { hasStaticTagName } from '../has-static-tag-name';
4
4
  import { makeCheckTransform, transformTemplate, } from '../transform-template';
5
5
  const htmlChecksAndTransforms = [
@@ -32,10 +32,10 @@ function extractCustomElementTags(input) {
32
32
  }, []);
33
33
  }
34
34
  function stringValidator(input) {
35
- if (functionalElementRequired) {
35
+ if (declarativeElementRequired) {
36
36
  const customElementTagNames = extractCustomElementTags(input);
37
37
  if (customElementTagNames.length) {
38
- console.error(`Custom element tags must be interpolated from functional elements: ${customElementTagNames.join(', ')}`);
38
+ console.error(`Custom element tags must be interpolated from declarative elements: ${customElementTagNames.join(', ')}`);
39
39
  }
40
40
  }
41
41
  }
@@ -1,3 +1,3 @@
1
1
  import { HTMLTemplateResult } from 'lit';
2
- /** Enables interpolation of FunctionalElement tag names */
2
+ /** Enables interpolation of DeclarativeElement tag names */
3
3
  export declare function html(inputTemplateStrings: TemplateStringsArray, ...inputValues: unknown[]): HTMLTemplateResult;
@@ -1,7 +1,7 @@
1
1
  import { html as litHtml } from 'lit';
2
2
  import { getTransformedTemplate } from '../transform-template';
3
3
  import { transformHtmlTemplate } from './html-transform';
4
- /** Enables interpolation of FunctionalElement tag names */
4
+ /** Enables interpolation of DeclarativeElement tag names */
5
5
  export function html(inputTemplateStrings, ...inputValues) {
6
6
  const litTemplate = litHtml(inputTemplateStrings, ...inputValues);
7
7
  const transformedTemplate = getTransformedTemplate(inputTemplateStrings, inputValues, () => {
package/index.html ADDED
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Element Vir Test</title>
5
+ <script type="module" src="/src/test/elements/app.element.ts"></script>
6
+ <link rel="stylesheet" type="text/css" href="/index.css" />
7
+ <script>
8
+ console.info('yo');
9
+ </script>
10
+ </head>
11
+ <body>
12
+ <element-vir-test-app></element-vir-test-app>
13
+ </body>
14
+ </html>
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "element-vir",
3
- "version": "5.6.0",
3
+ "version": "6.0.0",
4
4
  "keywords": [
5
5
  "custom",
6
6
  "web",
7
7
  "components",
8
8
  "elements",
9
- "functional",
9
+ "declarative",
10
10
  "lit"
11
11
  ],
12
- "homepage": "https://github.com/electrovir/element-vir#readme",
12
+ "homepage": "https://github.com/electrovir/element-vir",
13
13
  "bugs": {
14
14
  "url": "https://github.com/electrovir/element-vir/issues"
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "git+https://github.com/electrovir/element-vir.git"
18
+ "url": "https://github.com/electrovir/element-vir"
19
19
  },
20
20
  "license": "MIT",
21
21
  "author": {
@@ -26,23 +26,23 @@
26
26
  "types": "dist/index.d.ts",
27
27
  "scripts": {
28
28
  "compile": "rm -rf dist && tsc --project tsconfig.json",
29
- "format": "virmator format write",
30
- "jest": "jest --config ./src/jest/jest.config.ts",
29
+ "format": "virmator format",
31
30
  "prepublishOnly": "npm run compile && npm run test:full",
32
31
  "spellcheck": "virmator spellcheck",
33
- "start": "npm install && vite --force --config ./src/vite/vite.config.ts",
34
- "test": "npm run type-check && virmator test --no-write-config",
32
+ "start": "npm install && virmator frontend",
33
+ "test": "npm run test:types && virmator test-web",
35
34
  "test:full": "npm test && npm run spellcheck && virmator format check && npm run update-docs -- --check",
36
- "type-check": "tsc --noEmit",
35
+ "test:types": "tsc --noEmit",
37
36
  "update-docs": "virmator code-in-markdown README.md --index src/index.ts"
38
37
  },
39
38
  "dependencies": {
40
- "augment-vir": "2.2.1",
41
- "lit": "2.2.7"
39
+ "augment-vir": "2.5.0",
40
+ "lit": "2.3.1"
42
41
  },
43
42
  "devDependencies": {
44
- "jest-environment-jsdom": "28.1.2",
45
- "virmator": "2.0.7",
46
- "vite": "2.9.13"
43
+ "@open-wc/testing": "3.1.6",
44
+ "@web/dev-server-esbuild": "0.3.2",
45
+ "@web/test-runner": "0.14.0",
46
+ "virmator": "3.0.6"
47
47
  }
48
48
  }
@@ -0,0 +1,7 @@
1
+ html,
2
+ body {
3
+ margin: 0;
4
+ padding: 0;
5
+ height: 100%;
6
+ width: 100%;
7
+ }
@@ -1,4 +0,0 @@
1
- import { EventsInitMap } from './element-events';
2
- import { PropertyInitMapBase } from './element-properties';
3
- import { FunctionalElement, FunctionalElementInit } from './functional-element';
4
- export declare function defineFunctionalElement<HostClassKeys extends string = '', CssVarKeys extends string = '', EventsInitGeneric extends EventsInitMap = {}, PropertyInitGeneric extends PropertyInitMapBase = {}>(functionalElementInit: FunctionalElementInit<PropertyInitGeneric, EventsInitGeneric, HostClassKeys, CssVarKeys>): FunctionalElement<PropertyInitGeneric, EventsInitGeneric, HostClassKeys, CssVarKeys>;
@@ -1,70 +0,0 @@
1
- import { css } from 'lit';
2
- import { property } from 'lit/decorators.js';
3
- import { createCssVarNamesMap, createCssVarValuesMap } from './css-vars';
4
- import { createEventDescriptorMap } from './element-events';
5
- import { createPropertyDescriptorMap, createPropertyProxy, } from './element-properties';
6
- import { FunctionalElementBaseClass, } from './functional-element';
7
- import { createHostClassNamesMap } from './host-classes';
8
- import { createRenderParams } from './render-callback';
9
- import { applyHostClasses, hostClassNamesToStylesInput } from './styles';
10
- const defaultInit = {
11
- events: {},
12
- props: {},
13
- };
14
- export function defineFunctionalElement(functionalElementInit) {
15
- var _a;
16
- const eventsMap = createEventDescriptorMap(functionalElementInit.events);
17
- const hostClassNames = createHostClassNamesMap(functionalElementInit.tagName, functionalElementInit.hostClasses);
18
- const cssVarNames = createCssVarNamesMap(functionalElementInit.tagName, functionalElementInit.cssVars);
19
- const cssVarValues = createCssVarValuesMap(functionalElementInit.cssVars, cssVarNames);
20
- console.log({ hostClassNames });
21
- console.log(hostClassNamesToStylesInput({
22
- hostClassNames,
23
- cssVarNames,
24
- cssVarValues,
25
- }));
26
- const calculatedStyles = typeof functionalElementInit.styles === 'function'
27
- ? functionalElementInit.styles(hostClassNamesToStylesInput({ hostClassNames, cssVarNames, cssVarValues }))
28
- : functionalElementInit.styles || css ``;
29
- if (typeof functionalElementInit.styles === 'function') {
30
- console.log({ calculatedStyles: String(calculatedStyles) });
31
- }
32
- const anonymousClass = (_a = class extends FunctionalElementBaseClass {
33
- constructor() {
34
- super();
35
- this.initCalled = false;
36
- this.instanceProps = createPropertyProxy(functionalElementInit.props, this);
37
- const initProps = functionalElementInit.props || {};
38
- Object.keys(initProps).forEach((propName) => {
39
- const functionalElementInstance = this;
40
- property()(functionalElementInstance, propName);
41
- functionalElementInstance[propName] = initProps[propName];
42
- });
43
- }
44
- createRenderParams() {
45
- return createRenderParams(this, eventsMap);
46
- }
47
- render() {
48
- const renderParams = this.createRenderParams();
49
- if (!this.initCalled && functionalElementInit.initCallback) {
50
- this.initCalled = true;
51
- functionalElementInit.initCallback(renderParams);
52
- }
53
- const renderResult = functionalElementInit.renderCallback(renderParams);
54
- applyHostClasses(renderParams.host, functionalElementInit.hostClasses, hostClassNames, renderParams.props);
55
- return renderResult;
56
- }
57
- },
58
- _a.tagName = functionalElementInit.tagName,
59
- _a.styles = calculatedStyles,
60
- _a.init = { ...defaultInit, ...functionalElementInit },
61
- _a.events = eventsMap,
62
- _a.renderCallback = functionalElementInit.renderCallback,
63
- _a.props = createPropertyDescriptorMap(functionalElementInit.props),
64
- _a.hostClasses = hostClassNames,
65
- _a.cssVarNames = cssVarNames,
66
- _a.cssVarValues = cssVarNames,
67
- _a);
68
- window.customElements.define(functionalElementInit.tagName, anonymousClass);
69
- return anonymousClass;
70
- }
@@ -1,19 +0,0 @@
1
- import { AsyncDirective } from 'lit/async-directive.js';
2
- import { PartInfo } from 'lit/directive.js';
3
- import { StaticElementPropertyDescriptor } from '../element-properties';
4
- /**
5
- * The directive generics (in listenDirective) are not strong enough to maintain their values. Thus,
6
- * the directive call is wrapped in this function.
7
- */
8
- export declare function assignWithCleanup<PropName extends string, PropValue>(propertyDescriptor: StaticElementPropertyDescriptor<PropName, PropValue>, value: typeof propertyDescriptor['initValue'], cleanupCallback: CleanupCallback<typeof propertyDescriptor['initValue']>): import("lit-html/directive").DirectiveResult<typeof AssignWithCleanupDirectiveClass>;
9
- export declare type CleanupCallback<T> = (oldValue: T) => void;
10
- export declare type EqualityCheckCallback<T> = (oldValue: T, newValue: T) => boolean;
11
- declare class AssignWithCleanupDirectiveClass extends AsyncDirective {
12
- private readonly element;
13
- private lastValue;
14
- private lastCallback;
15
- constructor(partInfo: PartInfo);
16
- disconnected(): void;
17
- render(propName: string, value: unknown, cleanupCallback: CleanupCallback<any>, equalityCheck?: EqualityCheckCallback<any>): symbol;
18
- }
19
- export {};
@@ -1,36 +0,0 @@
1
- import { noChange } from 'lit';
2
- import { AsyncDirective } from 'lit/async-directive.js';
3
- import { directive } from 'lit/directive.js';
4
- import { extractFunctionalElement } from './directive-util';
5
- /**
6
- * The directive generics (in listenDirective) are not strong enough to maintain their values. Thus,
7
- * the directive call is wrapped in this function.
8
- */
9
- export function assignWithCleanup(propertyDescriptor, value, cleanupCallback) {
10
- return assignWithCleanupDirective(propertyDescriptor.propName, value, cleanupCallback);
11
- }
12
- class AssignWithCleanupDirectiveClass extends AsyncDirective {
13
- constructor(partInfo) {
14
- super(partInfo);
15
- this.element = extractFunctionalElement(partInfo, 'assign');
16
- }
17
- disconnected() {
18
- if (this.lastValue != undefined && this.lastCallback != undefined) {
19
- this.lastCallback(this.lastValue);
20
- }
21
- }
22
- render(propName, value, cleanupCallback, equalityCheck = (a, b) => a === b) {
23
- if (!(propName in this.element.instanceProps)) {
24
- throw new Error(`${this.element.tagName} element has no property of name "${propName}"`);
25
- }
26
- // reference equality check!
27
- if (!equalityCheck(this.lastValue, value)) {
28
- cleanupCallback(this.lastValue);
29
- }
30
- this.element.instanceProps[propName] = value;
31
- this.lastValue = value;
32
- this.lastCallback = cleanupCallback;
33
- return noChange;
34
- }
35
- }
36
- const assignWithCleanupDirective = directive(AssignWithCleanupDirectiveClass);
@@ -1,15 +0,0 @@
1
- import { PartInfo } from 'lit/directive.js';
2
- import { PropertyInitMapBase, StaticElementPropertyDescriptor } from '../element-properties';
3
- import { FunctionalElementInstanceFromInit } from '../functional-element';
4
- /**
5
- * The directive generics (in listenDirective) are not strong enough to maintain their values. Thus,
6
- * the directive call is wrapped in this function.
7
- */
8
- export declare function assign<PropName extends string, PropValue>(propertyDescriptor: StaticElementPropertyDescriptor<PropName, PropValue>, value: typeof propertyDescriptor['initValue']): import("lit-html/directive").DirectiveResult<{
9
- new (partInfo: PartInfo): {
10
- readonly element: FunctionalElementInstanceFromInit<PropertyInitMapBase>;
11
- render(propName: string, value: unknown): symbol;
12
- readonly _$isConnected: boolean;
13
- update(_part: import("lit-html").Part, props: unknown[]): unknown;
14
- };
15
- }>;
@@ -1,23 +0,0 @@
1
- import { noChange } from 'lit';
2
- import { directive, Directive } from 'lit/directive.js';
3
- import { extractFunctionalElement } from './directive-util';
4
- /**
5
- * The directive generics (in listenDirective) are not strong enough to maintain their values. Thus,
6
- * the directive call is wrapped in this function.
7
- */
8
- export function assign(propertyDescriptor, value) {
9
- return assignDirective(propertyDescriptor.propName, value);
10
- }
11
- const assignDirective = directive(class extends Directive {
12
- constructor(partInfo) {
13
- super(partInfo);
14
- this.element = extractFunctionalElement(partInfo, 'assign');
15
- }
16
- render(propName, value) {
17
- if (!(propName in this.element.instanceProps)) {
18
- throw new Error(`${this.element.tagName} element has no property of name "${propName}"`);
19
- }
20
- this.element.instanceProps[propName] = value;
21
- return noChange;
22
- }
23
- });
@@ -1,50 +0,0 @@
1
- function assertValidPropertyName(propertyName, propsInitMap, elementTagName) {
2
- if (typeof propertyName !== 'string') {
3
- throw new Error(`Property name must be a string, got type "${typeof propertyName}" from: "${String(propertyName)}" for ${elementTagName}`);
4
- }
5
- if (!(propertyName in propsInitMap)) {
6
- throw new Error(`Property name "${propertyName}" does not exist on ${elementTagName}.`);
7
- }
8
- }
9
- export function createPropertyProxy(propsInitMap, element) {
10
- if (!propsInitMap) {
11
- return {};
12
- }
13
- const props = Object.keys(propsInitMap).reduce((accum, propertyName) => {
14
- accum[propertyName] = element[propertyName];
15
- return accum;
16
- }, {});
17
- const propsProxy = new Proxy(props, {
18
- get: (_target, propertyName) => {
19
- assertValidPropertyName(propertyName, propsInitMap, element.tagName);
20
- return element[propertyName];
21
- },
22
- set: (_target, propertyName, value) => {
23
- assertValidPropertyName(propertyName, propsInitMap, element.tagName);
24
- element[propertyName] = value;
25
- return true;
26
- },
27
- });
28
- return propsProxy;
29
- }
30
- export function createPropertyDescriptorMap(propertyInit) {
31
- if (!propertyInit) {
32
- return {};
33
- }
34
- return Object.keys(propertyInit)
35
- .filter((key) => {
36
- if (typeof key === 'string') {
37
- return true;
38
- }
39
- else {
40
- throw new Error(`Property init cannot have non string keys: "${key}"`);
41
- }
42
- })
43
- .reduce((accum, currentKey) => {
44
- accum[currentKey] = {
45
- propName: currentKey,
46
- initValue: propertyInit[currentKey],
47
- };
48
- return accum;
49
- }, {});
50
- }
@@ -1,57 +0,0 @@
1
- import { RequiredBy } from 'augment-vir';
2
- import { CSSResult, LitElement, TemplateResult } from 'lit';
3
- import { CssVarNameOrValueMap, CssVarsInitMap } from './css-vars';
4
- import { EventDescriptorMap, EventsInitMap } from './element-events';
5
- import { ElementPropertyDescriptorMap, PropertyInitMapBase } from './element-properties';
6
- import { HostClassesInitMap, HostClassNamesMap } from './host-classes';
7
- import { InitCallback, RenderCallback } from './render-callback';
8
- import { StylesCallback } from './styles';
9
- export declare type CustomElementTagName = `${string}-${string}`;
10
- export declare type FunctionalElementInit<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap, HostClassKeys extends string, CssVarKeys extends string> = {
11
- /**
12
- * HTML tag name. This should not be used directly, as interpolating it with the html tagged
13
- * template from this package is preferred.
14
- */
15
- tagName: CustomElementTagName;
16
- /** Static styles. These should not and cannot change. */
17
- styles?: CSSResult | StylesCallback<HostClassKeys, CssVarKeys>;
18
- /** Element properties. (These can be thought of as "inputs".) */
19
- props?: PropertyInitGeneric;
20
- /** Events that the element can dispatch. (These can be thought of as "outputs".) */
21
- events?: EventsInitGeneric;
22
- /**
23
- * CSS host classes. Values can be callbacks to determine when a host class should be defined,
24
- * based on current instance props, or just undefined to indicate that the host class will only
25
- * be manually set.
26
- */
27
- hostClasses?: HostClassesInitMap<HostClassKeys, PropertyInitGeneric>;
28
- cssVars?: CssVarsInitMap<CssVarKeys>;
29
- /** Called as part of the first renderCallback call, before the first renderCallback call. */
30
- initCallback?: InitCallback<PropertyInitGeneric, EventsInitGeneric>;
31
- renderCallback: RenderCallback<PropertyInitGeneric, EventsInitGeneric>;
32
- };
33
- export declare abstract class FunctionalElementBaseClass<PropertyInitGeneric extends PropertyInitMapBase> extends LitElement {
34
- static readonly tagName: string;
35
- static readonly styles: CSSResult;
36
- abstract render(): TemplateResult | Promise<TemplateResult>;
37
- abstract readonly instanceProps: PropertyInitGeneric;
38
- }
39
- export declare type FunctionalElementInstanceFromInit<PropertyInitGeneric extends PropertyInitMapBase = {}> = FunctionalElementBaseClass<NonNullable<Required<PropertyInitGeneric>>> & PropertyInitGeneric;
40
- export declare type FunctionalElementInstance<FunctionalElementGeneric extends FunctionalElement> = FunctionalElementInstanceFromInit<FunctionalElementGeneric['init']['props']>;
41
- export declare type FunctionalElement<PropertyInitGeneric extends PropertyInitMapBase = any, EventsInitGeneric extends EventsInitMap = any, HostClassKeys extends string = string, CssVarKeys extends string = string> = (new () => FunctionalElementInstanceFromInit<PropertyInitGeneric>) & ExtraStaticFunctionalElementProperties<PropertyInitGeneric, EventsInitGeneric, HostClassKeys, CssVarKeys>;
42
- export declare type ExtraStaticFunctionalElementProperties<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap, HostClassKeys extends string, CssVarKeys extends string> = Readonly<{
43
- /** Pass through the render callback for direct unit testability */
44
- renderCallback: RenderCallback<PropertyInitGeneric, EventsInitGeneric>;
45
- events: EventDescriptorMap<EventsInitGeneric>;
46
- props: ElementPropertyDescriptorMap<PropertyInitGeneric>;
47
- init: RequiredBy<FunctionalElementInit<PropertyInitGeneric, EventsInitGeneric, HostClassKeys, CssVarKeys>, 'props' | 'events'>;
48
- hostClasses: HostClassNamesMap<string, HostClassKeys>;
49
- cssVarNames: CssVarNameOrValueMap<CssVarKeys>;
50
- cssVarValues: CssVarNameOrValueMap<CssVarKeys>;
51
- /**
52
- * Static properties have to be copied here cause they get nuked in the "new () =>
53
- * FunctionalElementInstance<PropertyInitGeneric>" type.
54
- */
55
- tagName: string;
56
- styles: CSSResult;
57
- }>;
@@ -1,3 +0,0 @@
1
- import { LitElement } from 'lit';
2
- export class FunctionalElementBaseClass extends LitElement {
3
- }