p-elements-core 1.2.15 → 1.2.16

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,133 +1,115 @@
1
- import {
2
- createDom,
3
- createProjection,
4
- extend,
5
- initPropertiesAndChildren,
6
- } from "./projection";
7
-
8
- const missingTransition = () => {
9
- throw new Error(
10
- "Provide a transitions object to the projectionOptions to do animations"
11
- );
12
- };
13
-
14
- const DEFAULT_PROJECTION_OPTIONS: ProjectionOptions = {
15
- namespace: undefined,
16
- eventHandlerInterceptor: undefined,
17
- styleApplyer: (domNode: HTMLElement, styleName: string, value: string) => {
18
- // Provides a hook to add vendor prefixes for browsers that still need it.
19
- (domNode.style as any)[styleName] = value;
20
- },
21
- transitions: {
22
- enter: missingTransition,
23
- exit: missingTransition,
24
- },
25
- };
26
-
27
- export const applyDefaultProjectionOptions = (
28
- projectorOptions?: ProjectionOptions
29
- ) => {
30
- return extend(DEFAULT_PROJECTION_OPTIONS, projectorOptions);
31
- };
32
-
33
- export const dom = {
34
- /**
35
- * Creates a real DOM tree from `vnode`. The [[Projection]] object returned will contain the resulting DOM Node in
36
- * its [[Projection.domNode|domNode]] property.
37
- * This is a low-level method. Users will typically use a [[Projector]] instead.
38
- * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]]
39
- * objects may only be rendered once.
40
- * @param projectionOptions - Options to be used to create and update the projection.
41
- * @returns The [[Projection]] which also contains the DOM Node that was created.
42
- */
43
- create: (vnode: VNode, projectionOptions?: ProjectionOptions): Projection => {
44
- projectionOptions = applyDefaultProjectionOptions(projectionOptions);
45
- createDom(
46
- vnode,
47
- document.createElement("div"),
48
- undefined,
49
- projectionOptions
50
- );
51
- return createProjection(vnode, projectionOptions);
52
- },
53
-
54
- /**
55
- * Appends a new child node to the DOM which is generated from a [[VNode]].
56
- * This is a low-level method. Users will typically use a [[Projector]] instead.
57
- * @param parentNode - The parent node for the new child node.
58
- * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]]
59
- * objects may only be rendered once.
60
- * @param projectionOptions - Options to be used to create and update the [[Projection]].
61
- * @returns The [[Projection]] that was created.
62
- */
63
- append: (
64
- parentNode: Element,
65
- vnode: VNode,
66
- projectionOptions?: ProjectionOptions
67
- ): Projection => {
68
- projectionOptions = applyDefaultProjectionOptions(projectionOptions);
69
- createDom(vnode, parentNode, undefined, projectionOptions);
70
- return createProjection(vnode, projectionOptions);
71
- },
72
-
73
- /**
74
- * Inserts a new DOM node which is generated from a [[VNode]].
75
- * This is a low-level method. Users wil typically use a [[Projector]] instead.
76
- * @param beforeNode - The node that the DOM Node is inserted before.
77
- * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function.
78
- * NOTE: [[VNode]] objects may only be rendered once.
79
- * @param projectionOptions - Options to be used to create and update the projection, see [[createProjector]].
80
- * @returns The [[Projection]] that was created.
81
- */
82
- insertBefore: (
83
- beforeNode: Element,
84
- vnode: VNode,
85
- projectionOptions?: ProjectionOptions
86
- ): Projection => {
87
- projectionOptions = applyDefaultProjectionOptions(projectionOptions);
88
- createDom(vnode, beforeNode.parentNode!, beforeNode, projectionOptions);
89
- return createProjection(vnode, projectionOptions);
90
- },
91
-
92
- /**
93
- * Merges a new DOM node which is generated from a [[VNode]] with an existing DOM Node.
94
- * This means that the virtual DOM and the real DOM will have one overlapping element.
95
- * Therefore the selector for the root [[VNode]] will be ignored, but its properties and children will be applied to the Element provided.
96
- * This is a low-level method. Users wil typically use a [[Projector]] instead.
97
- * @param element - The existing element to adopt as the root of the new virtual DOM. Existing attributes and child nodes are preserved.
98
- * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]] objects
99
- * may only be rendered once.
100
- * @param projectionOptions - Options to be used to create and update the projection, see [[createProjector]].
101
- * @returns The [[Projection]] that was created.
102
- */
103
- merge: (
104
- element: Element,
105
- vnode: VNode,
106
- projectionOptions?: ProjectionOptions
107
- ): Projection => {
108
- projectionOptions = applyDefaultProjectionOptions(projectionOptions);
109
- vnode.domNode = element;
110
- initPropertiesAndChildren(element, vnode, projectionOptions);
111
- return createProjection(vnode, projectionOptions);
112
- },
113
-
114
- /**
115
- * Replaces an existing DOM node with a node generated from a [[VNode]].
116
- * This is a low-level method. Users will typically use a [[Projector]] instead.
117
- * @param element - The node for the [[VNode]] to replace.
118
- * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]]
119
- * objects may only be rendered once.
120
- * @param projectionOptions - Options to be used to create and update the [[Projection]].
121
- * @returns The [[Projection]] that was created.
122
- */
123
- replace: (
124
- element: Element,
125
- vnode: VNode,
126
- projectionOptions?: ProjectionOptions
127
- ): Projection => {
128
- projectionOptions = applyDefaultProjectionOptions(projectionOptions);
129
- createDom(vnode, element.parentNode!, element, projectionOptions);
130
- element.parentNode!.removeChild(element);
131
- return createProjection(vnode, projectionOptions);
132
- },
133
- };
1
+ /**
2
+ * Contains simple low-level utility functions to manipulate the real DOM.
3
+ */
4
+ import { Projection, ProjectionOptions, VNode } from "./interfaces";
5
+ import { createDom, createProjection, extend, initPropertiesAndChildren } from "./projection";
6
+
7
+ const DEFAULT_PROJECTION_OPTIONS: ProjectionOptions = {
8
+ namespace: undefined,
9
+ performanceLogger: () => undefined,
10
+ eventHandlerInterceptor: undefined,
11
+ styleApplyer: (domNode: HTMLElement, styleName: string, value: string) => {
12
+ if (styleName.charAt(0) === "-") {
13
+ // CSS variables must be set using setProperty
14
+ domNode.style.setProperty(styleName, value);
15
+ } else {
16
+ // properties like 'backgroundColor' must be set as a js-property
17
+ (domNode.style as any)[styleName] = value;
18
+ }
19
+ },
20
+ };
21
+
22
+ export let applyDefaultProjectionOptions = (
23
+ projectorOptions?: ProjectionOptions
24
+ ): ProjectionOptions => {
25
+ return extend(DEFAULT_PROJECTION_OPTIONS, projectorOptions);
26
+ };
27
+
28
+ export let dom = {
29
+ /**
30
+ * Creates a real DOM tree from `vnode`. The [[Projection]] object returned will contain the resulting DOM Node in
31
+ * its [[Projection.domNode|domNode]] property.
32
+ * This is a low-level method. Users will typically use a [[Projector]] instead.
33
+ * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]]
34
+ * objects may only be rendered once.
35
+ * @param projectionOptions - Options to be used to create and update the projection.
36
+ * @returns The [[Projection]] which also contains the DOM Node that was created.
37
+ */
38
+ create: (vnode: VNode, projectionOptions?: ProjectionOptions): Projection => {
39
+ projectionOptions = applyDefaultProjectionOptions(projectionOptions);
40
+ createDom(vnode, document.createElement("div"), undefined, projectionOptions);
41
+ return createProjection(vnode, projectionOptions);
42
+ },
43
+
44
+ /**
45
+ * Appends a new child node to the DOM which is generated from a [[VNode]].
46
+ * This is a low-level method. Users will typically use a [[Projector]] instead.
47
+ * @param parentNode - The parent node for the new child node.
48
+ * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]]
49
+ * objects may only be rendered once.
50
+ * @param projectionOptions - Options to be used to create and update the [[Projection]].
51
+ * @returns The [[Projection]] that was created.
52
+ */
53
+ append: (
54
+ parentNode: Element,
55
+ vnode: VNode,
56
+ projectionOptions?: ProjectionOptions
57
+ ): Projection => {
58
+ projectionOptions = applyDefaultProjectionOptions(projectionOptions);
59
+ createDom(vnode, parentNode, undefined, projectionOptions);
60
+ return createProjection(vnode, projectionOptions);
61
+ },
62
+
63
+ /**
64
+ * Inserts a new DOM node which is generated from a [[VNode]].
65
+ * This is a low-level method. Users wil typically use a [[Projector]] instead.
66
+ * @param beforeNode - The node that the DOM Node is inserted before.
67
+ * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function.
68
+ * NOTE: [[VNode]] objects may only be rendered once.
69
+ * @param projectionOptions - Options to be used to create and update the projection, see [[createProjector]].
70
+ * @returns The [[Projection]] that was created.
71
+ */
72
+ insertBefore: (
73
+ beforeNode: Element,
74
+ vnode: VNode,
75
+ projectionOptions?: ProjectionOptions
76
+ ): Projection => {
77
+ projectionOptions = applyDefaultProjectionOptions(projectionOptions);
78
+ createDom(vnode, beforeNode.parentNode!, beforeNode, projectionOptions);
79
+ return createProjection(vnode, projectionOptions);
80
+ },
81
+
82
+ /**
83
+ * Merges a new DOM node which is generated from a [[VNode]] with an existing DOM Node.
84
+ * This means that the virtual DOM and the real DOM will have one overlapping element.
85
+ * Therefore the selector for the root [[VNode]] will be ignored, but its properties and children will be applied to the Element provided.
86
+ * This is a low-level method. Users wil typically use a [[Projector]] instead.
87
+ * @param element - The existing element to adopt as the root of the new virtual DOM. Existing attributes and child nodes are preserved.
88
+ * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]] objects
89
+ * may only be rendered once.
90
+ * @param projectionOptions - Options to be used to create and update the projection, see [[createProjector]].
91
+ * @returns The [[Projection]] that was created.
92
+ */
93
+ merge: (element: Element, vnode: VNode, projectionOptions?: ProjectionOptions): Projection => {
94
+ projectionOptions = applyDefaultProjectionOptions(projectionOptions);
95
+ vnode.domNode = element;
96
+ initPropertiesAndChildren(element, vnode, projectionOptions);
97
+ return createProjection(vnode, projectionOptions);
98
+ },
99
+
100
+ /**
101
+ * Replaces an existing DOM node with a node generated from a [[VNode]].
102
+ * This is a low-level method. Users will typically use a [[Projector]] instead.
103
+ * @param element - The node for the [[VNode]] to replace.
104
+ * @param vnode - The root of the virtual DOM tree that was created using the [[h]] function. NOTE: [[VNode]]
105
+ * objects may only be rendered once.
106
+ * @param projectionOptions - Options to be used to create and update the [[Projection]].
107
+ * @returns The [[Projection]] that was created.
108
+ */
109
+ replace: (element: Element, vnode: VNode, projectionOptions?: ProjectionOptions): Projection => {
110
+ projectionOptions = applyDefaultProjectionOptions(projectionOptions);
111
+ createDom(vnode, element.parentNode!, element, projectionOptions);
112
+ element.parentNode!.removeChild(element);
113
+ return createProjection(vnode, projectionOptions);
114
+ },
115
+ };
@@ -0,0 +1,100 @@
1
+ import { VNode, VNodeChild, VNodeProperties } from "./interfaces";
2
+
3
+ let toTextVNode = (data: string): VNode => {
4
+ return {
5
+ vnodeSelector: "",
6
+ properties: undefined,
7
+ children: undefined,
8
+ text: data.toString(),
9
+ domNode: null,
10
+ };
11
+ };
12
+
13
+ let appendChildren = (parentSelector: string, insertions: VNodeChild[], main: VNode[]) => {
14
+ for (let i = 0, length = insertions.length; i < length; i++) {
15
+ let item = insertions[i];
16
+ if (Array.isArray(item)) {
17
+ appendChildren(parentSelector, item, main);
18
+ } else {
19
+ if (item !== null && item !== undefined && item !== false) {
20
+ if (typeof item === "string") {
21
+ item = toTextVNode(item);
22
+ }
23
+ main.push(item);
24
+ }
25
+ }
26
+ }
27
+ };
28
+
29
+ /**
30
+ * The `h` function is used to create a virtual DOM node.
31
+ * This function is largely inspired by the mercuryjs and mithril frameworks.
32
+ * The `h` stands for (virtual) hyperscript.
33
+ *
34
+ * @param selector Contains the tagName, id and fixed css classnames in CSS selector format.
35
+ * It is formatted as follows: `tagname.cssclass1.cssclass2#id`.
36
+ * @param properties An object literal containing properties that will be placed on the DOM node.
37
+ * @param children Virtual DOM nodes and strings to add as child nodes.
38
+ * `children` may contain [[VNode]]s, `string`s, nested arrays, `null` and `undefined`.
39
+ * Nested arrays are flattened, `null` and `undefined` are removed.
40
+ *
41
+ * @returns A VNode object, used to render a real DOM later.
42
+ *
43
+ * NOTE: There are {@link http://maquettejs.org/docs/rules.html two basic rules} you should be aware of when updating the virtual DOM.
44
+ */
45
+ export function h(
46
+ selector: string,
47
+ properties?: VNodeProperties,
48
+ children?: VNodeChild[] | null
49
+ ): VNode;
50
+ /**
51
+ * The `h` function is used to create a virtual DOM node.
52
+ * This function is largely inspired by the mercuryjs and mithril frameworks.
53
+ * The `h` stands for (virtual) hyperscript.
54
+ *
55
+ * @param selector Contains the tagName, id and fixed css classnames in CSS selector format.
56
+ * It is formatted as follows: `tagname.cssclass1.cssclass2#id`.
57
+ * @param children Virtual DOM nodes and strings to add as child nodes.
58
+ * `children` may contain [[VNode]]s, `string`s, nested arrays, `null` and `undefined`.
59
+ * Nested arrays are flattened, `null` and `undefined` are removed.
60
+ *
61
+ * @returns A VNode object, used to render a real DOM later.
62
+ *
63
+ * NOTE: There are {@link http://maquettejs.org/docs/rules.html two basic rules} you should be aware of when updating the virtual DOM.
64
+ */
65
+ export function h(selector: string, children: VNodeChild[]): VNode;
66
+
67
+ export function h(
68
+ selector: string,
69
+ properties?: VNodeProperties,
70
+ children?: VNodeChild[] | null
71
+ ): VNode {
72
+ if (Array.isArray(properties)) {
73
+ children = properties;
74
+ properties = undefined;
75
+ } else if (
76
+ (properties && (typeof (properties as any) === "string" || properties.vnodeSelector)) ||
77
+ (children && (typeof (children as any) === "string" || (children as any).vnodeSelector))
78
+ ) {
79
+ throw new Error("h called with invalid arguments");
80
+ }
81
+ let text: string | undefined;
82
+ let flattenedChildren: VNode[] | undefined;
83
+ // Recognize a common special case where there is only a single text node
84
+ if (children && children.length === 1 && typeof children[0] === "string") {
85
+ text = children[0];
86
+ } else if (children) {
87
+ flattenedChildren = [];
88
+ appendChildren(selector, children, flattenedChildren);
89
+ if (flattenedChildren.length === 0) {
90
+ flattenedChildren = undefined;
91
+ }
92
+ }
93
+ return {
94
+ vnodeSelector: selector,
95
+ properties: properties,
96
+ children: flattenedChildren,
97
+ text: text === "" ? undefined : text,
98
+ domNode: null,
99
+ };
100
+ }
@@ -0,0 +1,12 @@
1
+ // Comment that is displayed in the API documentation for the maquette module:
2
+ /**
3
+ * Welcome to the API documentation of the **maquette** library.
4
+ *
5
+ * [[https://maquettejs.org/|To the maquette homepage]]
6
+ */
7
+ export * from "./interfaces";
8
+ export { dom } from "./dom";
9
+ export { h } from "./h";
10
+ export { createProjector } from "./projector";
11
+ export { createCache } from "./cache";
12
+ export { createMapping } from "./mapping";