@vnejs/module.layer 0.1.1

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/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./modules/controller";
2
+ export * from "./modules/view";
@@ -0,0 +1,46 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+
3
+ export class ModuleLayerController extends ModuleController {
4
+ name = "layer.controller";
5
+
6
+ LAYER = null;
7
+ ZINDEX = 0;
8
+ WITH_POINTER_EVENTS_NONE = false;
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.LAYER.CHILD_RM, this.onChildRm);
12
+ this.on(this.EVENTS.LAYER.CHILDS_RM, this.onChildsRm);
13
+
14
+ this.on(this.EVENTS.LAYER.CHILD_ADD, this.onChildAdd);
15
+
16
+ this.on(this.EVENTS.LAYER.CHILD_CHANGE, this.onChildChange);
17
+ this.on(this.EVENTS.LAYER.CHILDS_CHANGE, this.onChildsChange);
18
+
19
+ this.on(this.EVENTS.LAYER.CHILD_GET, this.onChildGet);
20
+
21
+ this.on(this.EVENTS.LAYER.FORCE_UPDATE, this.onForceUpdate);
22
+ this.on(this.EVENTS.LAYER.STATE_GET, this.onStateGet);
23
+ };
24
+
25
+ init = () => void this.updateStateAndViewFast({ withPointerEventsNone: this.WITH_POINTER_EVENTS_NONE });
26
+
27
+ onChildAdd = ({ layer = "", child } = {}) => this.LAYER === layer && this.updateStateAndViewForce({ children: [...this.state.children, child] });
28
+ onChildRm = ({ layer = "", childId } = {}) =>
29
+ this.LAYER === layer && this.updateStateAndViewForce({ children: [...this.state.children].filter((e) => e.id !== childId) });
30
+ onChildsRm = ({ layer = "", childsIds = [] } = {}) =>
31
+ this.LAYER === layer && this.updateStateAndViewForce({ children: [...this.state.children].filter((e) => !childsIds.includes(e.id)) });
32
+ onChildChange = ({ layer = "", childId, duration = 0, props = {} } = {}) => this.LAYER === layer && this.updateChildren([childId], props, duration);
33
+ onChildsChange = ({ layer = "", childsIds = [], duration = 0, props = {} } = {}) => this.LAYER === layer && this.updateChildren(childsIds, props, duration);
34
+ onChildGet = ({ layer = "", childId }) => this.LAYER === layer && this.state.children.find((e) => e.id === childId);
35
+ onForceUpdate = ({ layer } = {}) => this.LAYER === layer && this.updateViewForce();
36
+
37
+ onStateGet = ({ layer } = {}) => this.LAYER === layer && this.state;
38
+
39
+ updateChildren = (childsIds, props, duration) => {
40
+ this.updateStateAndViewFast({ children: this.state.children.map((child) => (childsIds.includes(child.id) ? { ...child, ...props } : child)) });
41
+
42
+ return this.waitTimeout(duration);
43
+ };
44
+
45
+ getDefaultState = () => ({ children: [], zIndex: this.ZINDEX, withPointerEventsNone: this.WITH_POINTER_EVENTS_NONE });
46
+ }
@@ -0,0 +1,8 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+
3
+ import { render } from "../view";
4
+
5
+ export class ModuleLayerView extends ModuleView {
6
+ renderFunc = render;
7
+ updateHandler = this.onUpdateStoreComponent;
8
+ }
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@vnejs/module.layer",
3
+ "version": "0.1.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "publish:major": "npm version major && npm publish --access public",
9
+ "publish:minor": "npm version minor && npm publish --access public",
10
+ "publish:patch": "npm version patch && npm publish --access public"
11
+ },
12
+ "author": "",
13
+ "license": "ISC"
14
+ }
package/view/index.jsx ADDED
@@ -0,0 +1,63 @@
1
+ import { useEffect, useMemo, useState } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+
4
+ import { Image, Screen, Sprite } from "@vnejs/uis.react";
5
+ import { cn } from "@vnejs/uis.utils";
6
+
7
+ const b = cn("Layer");
8
+
9
+ const renderChild = (child, isForce) => {
10
+ if (child.type === "image")
11
+ return (
12
+ <Image
13
+ key={child.id}
14
+ {...child}
15
+ transition={isForce ? 0 : child.transition}
16
+ />
17
+ );
18
+ if (child.type === "sprite")
19
+ return (
20
+ <Sprite
21
+ key={child.id}
22
+ {...child}
23
+ transition={isForce ? 0 : child.transition}
24
+ transitionBox={isForce ? 0 : null}
25
+ />
26
+ );
27
+ };
28
+
29
+ const filtersToObj = (acc, filter) => {
30
+ acc[filter] = true;
31
+ return acc;
32
+ };
33
+
34
+ const EMPTY_ARR = [];
35
+
36
+ const Layer = ({ store, onMount, ...props } = {}) => {
37
+ const [state, setState] = useState({});
38
+
39
+ const { zIndex = 0, children = EMPTY_ARR, filters = EMPTY_ARR, transition = 300, isForce = false, withPointerEventsNone = false } = state;
40
+
41
+ useEffect(() => store.subscribe(setState, EMPTY_ARR));
42
+ useEffect(() => void onMount(), EMPTY_ARR);
43
+
44
+ const filtersObj = useMemo(() => filters.reduce(filtersToObj, []), [filters]);
45
+ const renderedChildren = useMemo(() => children.map((child) => renderChild(child, isForce)), [children, isForce]);
46
+
47
+ const isShow = Boolean(renderedChildren.length);
48
+
49
+ return (
50
+ <Screen
51
+ zIndex={zIndex}
52
+ isShow={isShow}
53
+ isForce={isForce}
54
+ withPointerEventsNone={withPointerEventsNone}
55
+ className={b(filtersObj)}
56
+ transition={isForce ? 0 : transition}
57
+ >
58
+ {renderedChildren}
59
+ </Screen>
60
+ );
61
+ };
62
+
63
+ export const render = (props, root) => createRoot(root).render(<Layer {...props} />);
@@ -0,0 +1,2 @@
1
+ .Layer
2
+ display: block