brew-js-react 0.4.2 → 0.4.3
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 +61 -35
- package/dist/brew-js-react.js.map +1 -1
- package/dist/brew-js-react.min.js +2 -2
- package/dist/brew-js-react.min.js.map +1 -1
- package/hooks.js +7 -6
- package/mixins/ScrollableMixin.d.ts +3 -2
- package/mixins/ScrollableMixin.js +2 -0
- package/package.json +1 -1
- package/view.js +36 -17
package/dist/brew-js-react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! brew-js-react v0.4.
|
|
1
|
+
/*! brew-js-react v0.4.3 | (c) misonou | https://hackmd.io/@misonou/brew-js-react */
|
|
2
2
|
(function webpackUniversalModuleDefinition(root, factory) {
|
|
3
3
|
if(typeof exports === 'object' && typeof module === 'object')
|
|
4
4
|
module.exports = factory(require("brew-js"), require("react"), require("react-dom"), (function webpackLoadOptionalExternalModule() { try { return require("react-dom/client"); } catch(e) {} }()), require("zeta-dom"), require("zeta-dom-react"), require("waterpipe"), require("jQuery"));
|
|
@@ -590,15 +590,17 @@ var setBaseUrl = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_br
|
|
|
590
590
|
|
|
591
591
|
|
|
592
592
|
|
|
593
|
+
var _ = createPrivateStore();
|
|
594
|
+
|
|
593
595
|
var root = zeta_dom_dom.root;
|
|
594
596
|
var routeMap = new Map();
|
|
595
597
|
var usedParams = {};
|
|
596
598
|
var sortedViews = [];
|
|
597
599
|
var emitter = new ZetaEventContainer();
|
|
598
|
-
var
|
|
599
|
-
container: root
|
|
600
|
-
active: true
|
|
600
|
+
var rootContext = freeze(extend(new ViewContext(null, null), {
|
|
601
|
+
container: root
|
|
601
602
|
}));
|
|
603
|
+
var StateContext = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createContext(rootContext);
|
|
602
604
|
var errorView;
|
|
603
605
|
/** @type {Partial<Zeta.ZetaEventType<"beforepageload", Brew.RouterEventMap, Element>>} */
|
|
604
606
|
|
|
@@ -606,11 +608,25 @@ var view_event = {};
|
|
|
606
608
|
onAppInit(function () {
|
|
607
609
|
app_app.on('beforepageload', function (e) {
|
|
608
610
|
view_event = e;
|
|
611
|
+
|
|
612
|
+
_(rootContext).setPage(app_app.page);
|
|
609
613
|
});
|
|
610
614
|
});
|
|
611
615
|
|
|
612
|
-
function ViewContext(view) {
|
|
613
|
-
|
|
616
|
+
function ViewContext(view, page) {
|
|
617
|
+
var self = this;
|
|
618
|
+
defineOwnProperty(self, 'view', view, true);
|
|
619
|
+
|
|
620
|
+
_(self, {
|
|
621
|
+
setPage: defineObservableProperty(self, 'page', page, true),
|
|
622
|
+
setActive: defineObservableProperty(self, 'active', true, true)
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
watch(self, 'page', function (page, previousPage) {
|
|
626
|
+
emitter.emit('pagechange', self, {
|
|
627
|
+
previousPage: previousPage
|
|
628
|
+
});
|
|
629
|
+
});
|
|
614
630
|
}
|
|
615
631
|
|
|
616
632
|
definePrototype(ViewContext, {
|
|
@@ -676,31 +692,40 @@ function ViewContainer() {
|
|
|
676
692
|
this.stateId = history.state;
|
|
677
693
|
}
|
|
678
694
|
|
|
695
|
+
ViewContainer.contextType = StateContext;
|
|
679
696
|
definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react_root_React_.Component, {
|
|
680
697
|
componentDidMount: function componentDidMount() {
|
|
681
698
|
/** @type {any} */
|
|
682
699
|
var self = this;
|
|
683
700
|
self.componentWillUnmount = combineFn(watch(app_app.route, function () {
|
|
684
|
-
self.setActive(self.getViewComponent() === self.currentViewComponent);
|
|
701
|
+
(self.setActive || noop)(self.getViewComponent() === self.currentViewComponent);
|
|
685
702
|
}), app_app.on('beforepageload', function () {
|
|
686
703
|
self.stateId = history.state;
|
|
687
|
-
|
|
688
|
-
self.
|
|
704
|
+
|
|
705
|
+
if (self.context === rootContext || self.updateOnNext) {
|
|
706
|
+
view_event.waitFor(new Promise(function (resolve) {
|
|
707
|
+
self.onRender = resolve;
|
|
708
|
+
}));
|
|
709
|
+
self.updateView();
|
|
710
|
+
self.forceUpdate();
|
|
711
|
+
}
|
|
689
712
|
}));
|
|
690
713
|
},
|
|
691
714
|
render: function render() {
|
|
692
715
|
/** @type {any} */
|
|
693
716
|
var self = this;
|
|
694
717
|
|
|
695
|
-
if (history.state === self.stateId) {
|
|
718
|
+
if (history.state === self.stateId && self.context.active) {
|
|
696
719
|
self.updateView();
|
|
697
720
|
}
|
|
698
721
|
|
|
722
|
+
(self.onRender || noop)();
|
|
699
723
|
return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(external_commonjs_react_commonjs2_react_amd_react_root_React_.Fragment, null, self.prevView, self.currentView);
|
|
700
724
|
},
|
|
701
725
|
updateView: function updateView() {
|
|
702
726
|
var self = this;
|
|
703
727
|
var V = self.getViewComponent();
|
|
728
|
+
self.updateOnNext = false;
|
|
704
729
|
|
|
705
730
|
if (V) {
|
|
706
731
|
// ensure the current path actually corresponds to the matched view
|
|
@@ -709,6 +734,8 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
|
|
|
709
734
|
|
|
710
735
|
if (targetPath !== removeQueryAndHash(app_app.path)) {
|
|
711
736
|
app_app.navigate(targetPath, true);
|
|
737
|
+
self.updateOnNext = true;
|
|
738
|
+
return;
|
|
712
739
|
}
|
|
713
740
|
}
|
|
714
741
|
|
|
@@ -728,6 +755,7 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
|
|
|
728
755
|
});
|
|
729
756
|
}
|
|
730
757
|
|
|
758
|
+
(self.cancelPrevious || noop)();
|
|
731
759
|
var onComponentLoaded;
|
|
732
760
|
var promise = new Promise(function (resolve) {
|
|
733
761
|
onComponentLoaded = resolve;
|
|
@@ -743,7 +771,7 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
|
|
|
743
771
|
});
|
|
744
772
|
notifyAsync(element, promise);
|
|
745
773
|
});
|
|
746
|
-
var state = new ViewContext(V);
|
|
774
|
+
var state = new ViewContext(V, app_app.page);
|
|
747
775
|
var viewProps = freeze({
|
|
748
776
|
navigationType: view_event.navigationType,
|
|
749
777
|
viewContext: state,
|
|
@@ -758,17 +786,11 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
|
|
|
758
786
|
onComponentLoaded: onComponentLoaded,
|
|
759
787
|
viewProps: viewProps
|
|
760
788
|
}))));
|
|
761
|
-
extend(self, {
|
|
789
|
+
extend(self, _(state), {
|
|
790
|
+
cancelPrevious: onComponentLoaded,
|
|
762
791
|
currentPath: app_app.path,
|
|
763
792
|
currentView: view,
|
|
764
|
-
currentViewComponent: V
|
|
765
|
-
setPage: defineObservableProperty(state, 'page', app_app.page, true),
|
|
766
|
-
setActive: defineObservableProperty(state, 'active', true, true)
|
|
767
|
-
});
|
|
768
|
-
watch(state, 'page', function (page, previousPage) {
|
|
769
|
-
emitter.emit('pagechange', state, {
|
|
770
|
-
previousPage: previousPage
|
|
771
|
-
});
|
|
793
|
+
currentViewComponent: V
|
|
772
794
|
});
|
|
773
795
|
(view_event.waitFor || noop)(promise);
|
|
774
796
|
}
|
|
@@ -846,6 +868,7 @@ function createViewComponent(factory) {
|
|
|
846
868
|
if (isThenable(children)) {
|
|
847
869
|
promise = children;
|
|
848
870
|
children = null;
|
|
871
|
+
catchAsync(promise);
|
|
849
872
|
}
|
|
850
873
|
|
|
851
874
|
var state = (0,external_zeta_dom_react_.useAsync)(function () {
|
|
@@ -994,10 +1017,11 @@ function useAppReadyState() {
|
|
|
994
1017
|
}
|
|
995
1018
|
function useRouteParam(name, defaultValue) {
|
|
996
1019
|
var container = useViewContext();
|
|
1020
|
+
var params = container.page.params;
|
|
997
1021
|
var route = app_app.route;
|
|
998
|
-
var value =
|
|
1022
|
+
var value = params[name] || '';
|
|
999
1023
|
var ref = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useRef)(value);
|
|
1000
|
-
var forceUpdate = (0,
|
|
1024
|
+
var forceUpdate = (0,external_zeta_dom_react_.useUpdateTrigger)();
|
|
1001
1025
|
(0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useEffect)(function () {
|
|
1002
1026
|
var setValue = function setValue() {
|
|
1003
1027
|
var current = route[name] || '';
|
|
@@ -1005,7 +1029,7 @@ function useRouteParam(name, defaultValue) {
|
|
|
1005
1029
|
if (current !== ref.current) {
|
|
1006
1030
|
if (container.active) {
|
|
1007
1031
|
ref.current = current;
|
|
1008
|
-
forceUpdate(
|
|
1032
|
+
forceUpdate();
|
|
1009
1033
|
} else {
|
|
1010
1034
|
watch(container, 'active', setValue);
|
|
1011
1035
|
}
|
|
@@ -1025,8 +1049,8 @@ function useRouteParam(name, defaultValue) {
|
|
|
1025
1049
|
}, [name, defaultValue]);
|
|
1026
1050
|
ref.current = value;
|
|
1027
1051
|
|
|
1028
|
-
if (defaultValue
|
|
1029
|
-
app_app.navigate(route.getPath(extend({},
|
|
1052
|
+
if (defaultValue && container.active && (!value || name === 'remainingSegments' && value === '/')) {
|
|
1053
|
+
app_app.navigate(route.getPath(extend({}, params, kv(name, defaultValue))), true);
|
|
1030
1054
|
}
|
|
1031
1055
|
|
|
1032
1056
|
return value;
|
|
@@ -1284,7 +1308,7 @@ var observe_lib$dom = external_commonjs_zeta_dom_commonjs2_zeta_dom_amd_zeta_dom
|
|
|
1284
1308
|
|
|
1285
1309
|
|
|
1286
1310
|
|
|
1287
|
-
var
|
|
1311
|
+
var StatefulMixin_ = createPrivateStore();
|
|
1288
1312
|
|
|
1289
1313
|
function MixinRefImpl(mixin) {
|
|
1290
1314
|
this.mixin = mixin;
|
|
@@ -1298,7 +1322,7 @@ definePrototype(MixinRefImpl, {
|
|
|
1298
1322
|
function StatefulMixin() {
|
|
1299
1323
|
Mixin.call(this);
|
|
1300
1324
|
|
|
1301
|
-
|
|
1325
|
+
StatefulMixin_(this, {
|
|
1302
1326
|
elements: new Set(),
|
|
1303
1327
|
flush: watch(this, false),
|
|
1304
1328
|
dispose: [],
|
|
@@ -1316,18 +1340,18 @@ definePrototype(StatefulMixin, Mixin, {
|
|
|
1316
1340
|
},
|
|
1317
1341
|
|
|
1318
1342
|
get state() {
|
|
1319
|
-
var obj =
|
|
1343
|
+
var obj = StatefulMixin_(this);
|
|
1320
1344
|
|
|
1321
1345
|
var key = obj.prefix + obj.counter;
|
|
1322
1346
|
return obj.states[key] || (obj.states[key] = this.initState());
|
|
1323
1347
|
},
|
|
1324
1348
|
|
|
1325
1349
|
reset: function reset() {
|
|
1326
|
-
|
|
1350
|
+
StatefulMixin_(this).counter = 0;
|
|
1327
1351
|
return this;
|
|
1328
1352
|
},
|
|
1329
1353
|
next: function next() {
|
|
1330
|
-
|
|
1354
|
+
StatefulMixin_(this).counter++;
|
|
1331
1355
|
return this;
|
|
1332
1356
|
},
|
|
1333
1357
|
getRef: function getRef() {
|
|
@@ -1336,20 +1360,20 @@ definePrototype(StatefulMixin, Mixin, {
|
|
|
1336
1360
|
return function (current) {
|
|
1337
1361
|
state.element = current;
|
|
1338
1362
|
|
|
1339
|
-
if (current && setAdd(
|
|
1363
|
+
if (current && setAdd(StatefulMixin_(self).elements, current)) {
|
|
1340
1364
|
self.initElement(current, state);
|
|
1341
1365
|
}
|
|
1342
1366
|
};
|
|
1343
1367
|
},
|
|
1344
1368
|
elements: function elements() {
|
|
1345
|
-
return values(
|
|
1369
|
+
return values(StatefulMixin_(this).states).map(function (v) {
|
|
1346
1370
|
return v.element;
|
|
1347
1371
|
}).filter(function (v) {
|
|
1348
1372
|
return v;
|
|
1349
1373
|
});
|
|
1350
1374
|
},
|
|
1351
1375
|
onDispose: function onDispose(callback) {
|
|
1352
|
-
|
|
1376
|
+
StatefulMixin_(this).dispose.push(callback);
|
|
1353
1377
|
},
|
|
1354
1378
|
initState: function initState() {
|
|
1355
1379
|
return {
|
|
@@ -1361,7 +1385,7 @@ definePrototype(StatefulMixin, Mixin, {
|
|
|
1361
1385
|
var self = this;
|
|
1362
1386
|
var clone = inherit(Object.getPrototypeOf(self), self);
|
|
1363
1387
|
|
|
1364
|
-
|
|
1388
|
+
StatefulMixin_(clone, extend({}, StatefulMixin_(self), {
|
|
1365
1389
|
prefix: randomId() + '.',
|
|
1366
1390
|
counter: 0
|
|
1367
1391
|
}));
|
|
@@ -1369,7 +1393,7 @@ definePrototype(StatefulMixin, Mixin, {
|
|
|
1369
1393
|
return clone;
|
|
1370
1394
|
},
|
|
1371
1395
|
dispose: function dispose() {
|
|
1372
|
-
var state =
|
|
1396
|
+
var state = StatefulMixin_(this);
|
|
1373
1397
|
|
|
1374
1398
|
var states = state.states;
|
|
1375
1399
|
combineFn(state.dispose.splice(0))();
|
|
@@ -1741,6 +1765,8 @@ definePrototype(ScrollableMixin, ClassNameMixin, {
|
|
|
1741
1765
|
'scroller-snap-page': options.paged,
|
|
1742
1766
|
'scroller-page': options.pagedItemSelector,
|
|
1743
1767
|
'scroller-state': 'pageIndex'
|
|
1768
|
+
}, options.persistScroll && {
|
|
1769
|
+
'persist-scroll': ''
|
|
1744
1770
|
});
|
|
1745
1771
|
},
|
|
1746
1772
|
onPageIndexChanged: function onPageIndexChanged(callback) {
|