element-vir 0.1.1 → 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.
@@ -1,5 +1,4 @@
1
1
  import { EventsInitMap } from './element-events';
2
2
  import { PropertyInitMapBase } from './element-properties';
3
- import { FunctionalElementConstructor } from './functional-element';
4
- import { FunctionalElementInit } from './functional-element-init';
5
- export declare function defineFunctionalElement<EventsInitGeneric extends EventsInitMap = never, PropertyInitGeneric extends PropertyInitMapBase = never>(functionalElementInit: FunctionalElementInit<PropertyInitGeneric, EventsInitGeneric>): FunctionalElementConstructor<PropertyInitGeneric, EventsInitGeneric>;
3
+ import { FunctionalElement, FunctionalElementInit } from './functional-element';
4
+ export declare function defineFunctionalElement<EventsInitGeneric extends EventsInitMap = {}, PropertyInitGeneric extends PropertyInitMapBase = {}>(functionalElementInit: FunctionalElementInit<PropertyInitGeneric, EventsInitGeneric>): FunctionalElement<PropertyInitGeneric, EventsInitGeneric>;
@@ -0,0 +1,19 @@
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 {};
@@ -0,0 +1,36 @@
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,5 +1,4 @@
1
1
  import { PartInfo } from 'lit/directive.js';
2
- import { ElementEvent } from '../element-events';
3
2
  import { PropertyInitMapBase, StaticElementPropertyDescriptor } from '../element-properties';
4
3
  import { FunctionalElementInstance } from '../functional-element';
5
4
  /**
@@ -9,15 +8,8 @@ import { FunctionalElementInstance } from '../functional-element';
9
8
  export declare function assign<PropName extends string, PropValue>(propertyDescriptor: StaticElementPropertyDescriptor<PropName, PropValue>, value: typeof propertyDescriptor['initValue']): import("lit-html/directive").DirectiveResult<{
10
9
  new (partInfo: PartInfo): {
11
10
  readonly element: FunctionalElementInstance<PropertyInitMapBase>;
12
- lastListenerMetaData: ListenerMetaData<any> | undefined;
13
11
  render(propName: string, value: unknown): symbol;
14
12
  readonly _$isConnected: boolean;
15
13
  update(_part: import("lit-html").Part, props: unknown[]): unknown;
16
14
  };
17
15
  }>;
18
- declare type ListenerMetaData<EventDetail> = {
19
- eventType: string;
20
- callback: (event: ElementEvent<string, EventDetail>) => void;
21
- listener: (event: any) => void;
22
- };
23
- export {};
@@ -0,0 +1,3 @@
1
+ import { CSSResult } from 'lit';
2
+ import { FunctionalElement } from '../functional-element';
3
+ export declare function cssSelector(element: FunctionalElement): CSSResult;
@@ -0,0 +1,6 @@
1
+ import { unsafeCSS } from 'lit';
2
+ export function cssSelector(element) {
3
+ // This is actually safe because it's not user input.
4
+ // If the dev using this package allows user input for tag names... all hope is lost anyway.
5
+ return unsafeCSS(element.tagName);
6
+ }
@@ -1,6 +1,14 @@
1
1
  import { ElementPartInfo, PartInfo } from 'lit/directive.js';
2
- import { ExtraPartInfoProperties } from '../../vir-html/directive';
3
2
  import { PropertyInitMapBase } from '../element-properties';
4
3
  import { FunctionalElementInstance } from '../functional-element';
4
+ /** For some reason these aren't defined in lit's types already. */
5
+ export declare type ExtraPartInfoProperties = {
6
+ element: Element;
7
+ options: {
8
+ host: Element;
9
+ renderBefore: Element;
10
+ isConnected: boolean;
11
+ };
12
+ };
5
13
  export declare function extractFunctionalElement<PropertyInitGeneric extends PropertyInitMapBase>(partInfo: PartInfo, directiveName: string): FunctionalElementInstance<PropertyInitGeneric>;
6
14
  export declare function assertsIsElementPartInfo(partInfo: PartInfo, directiveName: string): asserts partInfo is ElementPartInfo & ExtraPartInfoProperties;
@@ -2,6 +2,20 @@ import { CSSResult, LitElement, TemplateResult } from 'lit';
2
2
  import { EventDescriptorMap, EventsInitMap } from './element-events';
3
3
  import { ElementPropertyDescriptorMap, PropertyInitMapBase } from './element-properties';
4
4
  import { RenderCallback } from './render-callback';
5
+ export declare type FunctionalElementInit<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = {
6
+ /**
7
+ * HTML tag name. This should not be used directly, as interpolating it with the html tagged
8
+ * template from this package is preferred.
9
+ */
10
+ tagName: string;
11
+ /** Static styles. These should not and cannot change. */
12
+ styles?: CSSResult | undefined;
13
+ /** Initializer for element properties. (These can be thought of as "inputs".) */
14
+ props?: PropertyInitGeneric | undefined;
15
+ /** Initializer for events that the element can dispatch. (These can be thought of as "outputs".) */
16
+ events?: EventsInitGeneric | undefined;
17
+ renderCallback: RenderCallback<PropertyInitGeneric, EventsInitGeneric>;
18
+ };
5
19
  export declare abstract class FunctionalElementBaseClass<PropertyInitGeneric extends PropertyInitMapBase> extends LitElement {
6
20
  static readonly tagName: string;
7
21
  static readonly styles: CSSResult;
@@ -9,8 +23,8 @@ export declare abstract class FunctionalElementBaseClass<PropertyInitGeneric ext
9
23
  abstract render(): TemplateResult | Promise<TemplateResult>;
10
24
  abstract readonly instanceProps: PropertyInitGeneric;
11
25
  }
12
- export declare type FunctionalElementInstance<PropertyInitGeneric extends PropertyInitMapBase> = FunctionalElementBaseClass<PropertyInitGeneric> & PropertyInitGeneric;
13
- export declare type FunctionalElementConstructor<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = (new () => FunctionalElementInstance<PropertyInitGeneric>) & ExtraStaticFunctionalElementProperties<PropertyInitGeneric, EventsInitGeneric>;
26
+ export declare type FunctionalElementInstance<PropertyInitGeneric extends PropertyInitMapBase = {}> = FunctionalElementBaseClass<PropertyInitGeneric> & PropertyInitGeneric;
27
+ export declare type FunctionalElement<PropertyInitGeneric extends PropertyInitMapBase = any, EventsInitGeneric extends EventsInitMap = any> = (new () => FunctionalElementInstance<PropertyInitGeneric>) & ExtraStaticFunctionalElementProperties<PropertyInitGeneric, EventsInitGeneric>;
14
28
  export declare type ExtraStaticFunctionalElementProperties<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = Readonly<{
15
29
  /** Pass through the render callback for direct unit testability */
16
30
  renderCallback: RenderCallback<PropertyInitGeneric, EventsInitGeneric>;
package/dist/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  export * from './functional-element/define-functional-element';
2
+ export * from './functional-element/directives/assign-with-clean-up.directive';
2
3
  export * from './functional-element/directives/assign.directive';
4
+ export * from './functional-element/directives/cssSelector';
3
5
  export * from './functional-element/directives/listen.directive';
4
6
  export * from './functional-element/directives/on-dom-created.directive';
5
7
  export * from './functional-element/directives/on-resize.directive';
6
8
  export * from './functional-element/element-events';
7
9
  export * from './functional-element/element-properties';
8
10
  export * from './functional-element/functional-element';
9
- export * from './functional-element/functional-element-init';
10
11
  export * from './functional-element/render-callback';
11
- export * from './vir-html/directive';
12
12
  export * from './vir-html/vir-html';
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  export * from './functional-element/define-functional-element';
2
+ export * from './functional-element/directives/assign-with-clean-up.directive';
2
3
  export * from './functional-element/directives/assign.directive';
4
+ export * from './functional-element/directives/cssSelector';
3
5
  export * from './functional-element/directives/listen.directive';
4
6
  export * from './functional-element/directives/on-dom-created.directive';
5
7
  export * from './functional-element/directives/on-resize.directive';
6
8
  export * from './functional-element/element-events';
7
9
  export * from './functional-element/element-properties';
8
10
  export * from './functional-element/functional-element';
9
- export * from './functional-element/functional-element-init';
10
11
  export * from './functional-element/render-callback';
11
- export * from './vir-html/directive';
12
12
  export * from './vir-html/vir-html';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "element-vir",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "keywords": [
5
5
  "custom",
6
6
  "web",
@@ -38,8 +38,9 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "@snowpack/plugin-typescript": "^1.2.1",
41
+ "augment-vir": "^1.0.0",
41
42
  "snowpack": "^3.8.8",
42
43
  "typescript": "^4.4.3",
43
- "virmator": "^1.1.15"
44
+ "virmator": "^1.2.0"
44
45
  }
45
46
  }
@@ -1,18 +0,0 @@
1
- import { CSSResult } from 'lit';
2
- import { EventsInitMap } from './element-events';
3
- import { PropertyInitMapBase } from './element-properties';
4
- import { RenderCallback } from './render-callback';
5
- export declare type FunctionalElementInit<PropertyInitGeneric extends PropertyInitMapBase, EventsInitGeneric extends EventsInitMap> = {
6
- /**
7
- * HTML tag name. This should not be used directly, as interpolating it with the html tagged
8
- * template from this package is preferred.
9
- */
10
- tagName: string;
11
- /** Static styles. These should not and cannot change. */
12
- styles?: CSSResult | undefined;
13
- /** Initializer for element properties. (These can be thought of as "inputs".) */
14
- props?: PropertyInitGeneric | undefined;
15
- /** Initializer for events that the element can dispatch. (These can be thought of as "outputs".) */
16
- events?: EventsInitGeneric | undefined;
17
- renderCallback: RenderCallback<PropertyInitGeneric, EventsInitGeneric>;
18
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,9 +0,0 @@
1
- /** For some reason these aren't defined in lit's types already. */
2
- export declare type ExtraPartInfoProperties = {
3
- element: Element;
4
- options: {
5
- host: Element;
6
- renderBefore: Element;
7
- isConnected: boolean;
8
- };
9
- };
@@ -1 +0,0 @@
1
- export {};