brew-js-react 0.2.3 → 0.2.6
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/dist/brew-js-react.js +130 -106
- package/dist/brew-js-react.js.map +1 -1
- package/dist/brew-js-react.min.js +1 -1
- package/dist/brew-js-react.min.js.map +1 -1
- package/hooks.js +119 -113
- package/include/brew-js/util/path.js +1 -0
- package/mixins/FlyoutMixin.d.ts +1 -1
- package/mixins/FlyoutMixin.js +6 -6
- package/mixins/StatefulMixin.js +6 -6
- package/package.json +2 -2
- package/view.d.ts +27 -0
- package/view.js +223 -211
package/hooks.js
CHANGED
|
@@ -1,113 +1,119 @@
|
|
|
1
|
-
import { createElement, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { ViewStateProvider } from "zeta-dom-react";
|
|
3
|
-
import { definePrototype, extend, kv, setImmediateOnce, throwNotFunction } from "./include/zeta-dom/util.js";
|
|
4
|
-
import { ZetaEventContainer } from "./include/zeta-dom/events.js";
|
|
5
|
-
import { app } from "./app.js";
|
|
6
|
-
import { useViewContainerState } from "./view.js";
|
|
7
|
-
|
|
8
|
-
const emitter = new ZetaEventContainer();
|
|
9
|
-
const states = {};
|
|
10
|
-
|
|
11
|
-
function getCurrentStates() {
|
|
12
|
-
return states[history.state] || (states[history.state] = {});
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function ViewState(value) {
|
|
16
|
-
this.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
var
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
1
|
+
import { createElement, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { ViewStateProvider } from "zeta-dom-react";
|
|
3
|
+
import { definePrototype, extend, kv, setImmediateOnce, throwNotFunction, watch } from "./include/zeta-dom/util.js";
|
|
4
|
+
import { ZetaEventContainer } from "./include/zeta-dom/events.js";
|
|
5
|
+
import { app } from "./app.js";
|
|
6
|
+
import { useViewContainerState } from "./view.js";
|
|
7
|
+
|
|
8
|
+
const emitter = new ZetaEventContainer();
|
|
9
|
+
const states = {};
|
|
10
|
+
|
|
11
|
+
function getCurrentStates() {
|
|
12
|
+
return states[history.state] || (states[history.state] = {});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function ViewState(key, value) {
|
|
16
|
+
this.key = key;
|
|
17
|
+
this.value = value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
definePrototype(ViewState, {
|
|
21
|
+
get: function () {
|
|
22
|
+
return this.value;
|
|
23
|
+
},
|
|
24
|
+
set: function (value) {
|
|
25
|
+
this.value = value;
|
|
26
|
+
},
|
|
27
|
+
onPopState: function (callback) {
|
|
28
|
+
throwNotFunction(callback);
|
|
29
|
+
return emitter.add(this, 'popstate', function (e) {
|
|
30
|
+
callback.call(this, e.newValue);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export function useAppReady() {
|
|
36
|
+
const sReady = useState(false);
|
|
37
|
+
const ready = sReady[0], setReady = sReady[1];
|
|
38
|
+
useEffect(function () {
|
|
39
|
+
app.ready.then(function () {
|
|
40
|
+
setReady(true);
|
|
41
|
+
});
|
|
42
|
+
}, []);
|
|
43
|
+
return ready;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useRouteParam(name, defaultValue) {
|
|
47
|
+
const container = useViewContainerState();
|
|
48
|
+
const route = app.route;
|
|
49
|
+
const value = route[name] || '';
|
|
50
|
+
const ref = useRef(value);
|
|
51
|
+
const forceUpdate = useState()[1];
|
|
52
|
+
useEffect(function () {
|
|
53
|
+
var setValue = function () {
|
|
54
|
+
var current = route[name] || '';
|
|
55
|
+
if (current !== ref.current) {
|
|
56
|
+
if (container.active) {
|
|
57
|
+
ref.current = current;
|
|
58
|
+
forceUpdate({});
|
|
59
|
+
} else {
|
|
60
|
+
watch(container, 'active', setValue);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
// route parameter might be changed after state initialization and before useEffect hook is called
|
|
65
|
+
setValue();
|
|
66
|
+
if (name in route) {
|
|
67
|
+
return route.watch(name, function () {
|
|
68
|
+
setImmediateOnce(setValue);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
console.error('Route parameter ' + name + ' does not exist');
|
|
72
|
+
}, [name, defaultValue]);
|
|
73
|
+
ref.current = value;
|
|
74
|
+
if (!value && defaultValue !== undefined) {
|
|
75
|
+
app.navigate(route.getPath(extend({}, route, kv(name, defaultValue))), true);
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function useRouteState(key, defaultValue) {
|
|
81
|
+
const container = useViewContainerState();
|
|
82
|
+
const cur = getCurrentStates();
|
|
83
|
+
const state = useState(key in cur ? cur[key] : defaultValue);
|
|
84
|
+
if (container.active) {
|
|
85
|
+
cur[key] = state[0];
|
|
86
|
+
}
|
|
87
|
+
return state;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function ViewStateContainer(props) {
|
|
91
|
+
const container = useViewContainerState();
|
|
92
|
+
const provider = useState(function () {
|
|
93
|
+
const cache = {};
|
|
94
|
+
return {
|
|
95
|
+
getState: function (uniqueId, key) {
|
|
96
|
+
var cur = getCurrentStates();
|
|
97
|
+
var state = cache[uniqueId] || (cache[uniqueId] = new ViewState(key, cur[key] && cur[key].value));
|
|
98
|
+
if (container.active) {
|
|
99
|
+
var stateId = state.stateId;
|
|
100
|
+
if (stateId && (stateId !== history.state || key !== state.key)) {
|
|
101
|
+
var newValue = cur[key] && cur[key].value;
|
|
102
|
+
emitter.emit('popstate', state, {
|
|
103
|
+
newValue: newValue
|
|
104
|
+
});
|
|
105
|
+
// detach value in previous history state from current one
|
|
106
|
+
var previous = new ViewState(state.key, state.value);
|
|
107
|
+
states[stateId][previous.key] = previous;
|
|
108
|
+
state.value = newValue;
|
|
109
|
+
state.key = key;
|
|
110
|
+
}
|
|
111
|
+
state.stateId = history.state;
|
|
112
|
+
cur[key] = state;
|
|
113
|
+
}
|
|
114
|
+
return state;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
})[0];
|
|
118
|
+
return createElement(ViewStateProvider, { value: provider }, props.children);
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "brew-js/util/path";
|
package/mixins/FlyoutMixin.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { AnimationEffect } from "./AnimateMixin";
|
|
|
5
5
|
export interface FlyoutMixinOptions {
|
|
6
6
|
modal?: boolean;
|
|
7
7
|
tabThrough?: boolean;
|
|
8
|
-
swipeToDismiss?:
|
|
8
|
+
swipeToDismiss?: 'up' | 'down' | 'left' | 'right';
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export default class FlyoutMixin extends ClassNameMixin {
|
package/mixins/FlyoutMixin.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { defineAliasProperty, definePrototype, each, extend,
|
|
1
|
+
import { defineAliasProperty, definePrototype, each, extend, makeArray } from "../include/zeta-dom/util.js";
|
|
2
2
|
import { closeFlyout, openFlyout } from "../include/brew-js/domAction.js";
|
|
3
|
-
import { declareVar, getVar } from "../include/brew-js/var.js";
|
|
4
3
|
import { app } from "../app.js";
|
|
5
4
|
import ClassNameMixin from "./ClassNameMixin.js";
|
|
6
5
|
import FlyoutToggleMixin from "./FlyoutToggleMixin.js";
|
|
7
6
|
|
|
8
7
|
const FlyoutMixinSuper = ClassNameMixin.prototype;
|
|
9
|
-
const
|
|
8
|
+
const valueMap = new WeakMap();
|
|
10
9
|
var flyoutMixinCounter = 0;
|
|
11
10
|
|
|
12
11
|
export default function FlyoutMixin() {
|
|
@@ -49,7 +48,9 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
|
|
|
49
48
|
});
|
|
50
49
|
},
|
|
51
50
|
open: function (value) {
|
|
52
|
-
|
|
51
|
+
var element = this.elements()[0];
|
|
52
|
+
valueMap.set(element, value);
|
|
53
|
+
return openFlyout(element);
|
|
53
54
|
},
|
|
54
55
|
close: function (value) {
|
|
55
56
|
return closeFlyout(this.elements()[0], value);
|
|
@@ -58,7 +59,7 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
|
|
|
58
59
|
var element = this.elements()[0];
|
|
59
60
|
return this.onToggleState(function (opened) {
|
|
60
61
|
if (opened) {
|
|
61
|
-
return callback(
|
|
62
|
+
return callback(valueMap.get(element));
|
|
62
63
|
}
|
|
63
64
|
});
|
|
64
65
|
},
|
|
@@ -73,7 +74,6 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
|
|
|
73
74
|
FlyoutMixinSuper.initElement.call(self, element, state);
|
|
74
75
|
if (!element.id) {
|
|
75
76
|
element.id = 'flyout-' + (++flyoutMixinCounter);
|
|
76
|
-
declareVar(element, varname, undefined);
|
|
77
77
|
}
|
|
78
78
|
app.on(element, {
|
|
79
79
|
animationstart: function () {
|
package/mixins/StatefulMixin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createPrivateStore, definePrototype, each, inherit, randomId, values } from "../include/zeta-dom/util.js";
|
|
1
|
+
import { createPrivateStore, definePrototype, each, extend, inherit, randomId, setAdd, values } from "../include/zeta-dom/util.js";
|
|
2
2
|
import Mixin from "./Mixin.js";
|
|
3
3
|
|
|
4
4
|
const _ = createPrivateStore();
|
|
@@ -16,6 +16,7 @@ definePrototype(MixinRefImpl, {
|
|
|
16
16
|
export default function StatefulMixin() {
|
|
17
17
|
Mixin.call(this);
|
|
18
18
|
_(this, {
|
|
19
|
+
elements: new WeakSet(),
|
|
19
20
|
states: {},
|
|
20
21
|
prefix: '',
|
|
21
22
|
counter: 0
|
|
@@ -46,8 +47,8 @@ definePrototype(StatefulMixin, Mixin, {
|
|
|
46
47
|
const self = this;
|
|
47
48
|
const state = self.state;
|
|
48
49
|
return function (current) {
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
state.element = current;
|
|
51
|
+
if (current && setAdd(_(self).elements, current)) {
|
|
51
52
|
self.initElement(current, state);
|
|
52
53
|
}
|
|
53
54
|
};
|
|
@@ -67,11 +68,10 @@ definePrototype(StatefulMixin, Mixin, {
|
|
|
67
68
|
clone: function () {
|
|
68
69
|
const self = this;
|
|
69
70
|
const clone = inherit(Object.getPrototypeOf(self), self);
|
|
70
|
-
_(clone, {
|
|
71
|
-
states: _(self).states,
|
|
71
|
+
_(clone, extend({}, _(self), {
|
|
72
72
|
prefix: randomId() + '.',
|
|
73
73
|
counter: 0
|
|
74
|
-
});
|
|
74
|
+
}));
|
|
75
75
|
return clone;
|
|
76
76
|
},
|
|
77
77
|
dispose: function () {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brew-js-react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"homepage": "https://hackmd.io/@misonou/brew-js-react",
|
|
18
18
|
"repository": "github:misonou/brew-js-react",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"brew-js": ">=0.3.
|
|
20
|
+
"brew-js": ">=0.3.11",
|
|
21
21
|
"waterpipe": "^2.5.0",
|
|
22
22
|
"zeta-dom": ">=0.2.3",
|
|
23
23
|
"zeta-dom-react": ">=0.2.1"
|
package/view.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export type ViewComponentRootProps = React.DetailedHTMLProps<React.HTMLAttribute
|
|
|
2
2
|
export type ViewComponent<P> = React.FC<P>;
|
|
3
3
|
|
|
4
4
|
export interface ViewContainerState {
|
|
5
|
+
readonly view: ViewComponent<any>;
|
|
5
6
|
readonly active: boolean;
|
|
6
7
|
}
|
|
7
8
|
|
|
@@ -22,6 +23,32 @@ export function registerView<P>(factory: () => Promise<{ default: React.Componen
|
|
|
22
23
|
*/
|
|
23
24
|
export function isViewMatched(view: ViewComponent<any>): boolean;
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Gets the view component that matches the current path within all registered views.
|
|
28
|
+
* However it does not imply if the view is in fact being rendered.
|
|
29
|
+
*/
|
|
30
|
+
export function matchView(): ViewComponent<any> | undefined;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Gets the view component that matches the current path within the given set of views.
|
|
34
|
+
* However it does not imply if the view is in fact being rendered.
|
|
35
|
+
* @param views A list of view components.
|
|
36
|
+
*/
|
|
37
|
+
export function matchView(views: ViewComponent<any>[]): ViewComponent<any> | undefined;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Gets the view component that matches the specified path.
|
|
41
|
+
* @param path A valid path that could navigate to.
|
|
42
|
+
*/
|
|
43
|
+
export function matchView(path: string): ViewComponent<any> | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Gets the view component that matches the specified path, within the given set of views.
|
|
47
|
+
* @param path A valid path that could navigate to.
|
|
48
|
+
* @param views A list of view components.
|
|
49
|
+
*/
|
|
50
|
+
export function matchView(path: string, views: ViewComponent<any>[]): ViewComponent<any> | undefined;
|
|
51
|
+
|
|
25
52
|
/**
|
|
26
53
|
* Renders view by matching current route state against registered route parameters of each supplied views.
|
|
27
54
|
* @param args A list of view components created by {@link registerView}.
|