brew-js-react 0.4.1 → 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.
@@ -1,4 +1,4 @@
1
- /*! brew-js-react v0.4.1 | (c) misonou | https://hackmd.io/@misonou/brew-js-react */
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"));
@@ -196,6 +196,7 @@ __webpack_require__.d(src_namespaceObject, {
196
196
  "FocusStateMixin": () => (FocusStateMixin),
197
197
  "LoadingStateMixin": () => (LoadingStateMixin),
198
198
  "Mixin": () => (Mixin),
199
+ "ScrollIntoViewMixin": () => (ScrollIntoViewMixin),
199
200
  "ScrollableMixin": () => (ScrollableMixin),
200
201
  "StatefulMixin": () => (StatefulMixin),
201
202
  "ViewStateContainer": () => (ViewStateContainer),
@@ -223,8 +224,10 @@ __webpack_require__.d(src_namespaceObject, {
223
224
  "useMixinRef": () => (useMixinRef),
224
225
  "useRouteParam": () => (useRouteParam),
225
226
  "useRouteState": () => (useRouteState),
227
+ "useScrollIntoViewMixin": () => (useScrollIntoViewMixin),
226
228
  "useScrollableMixin": () => (useScrollableMixin),
227
- "useViewContainerState": () => (useViewContainerState)
229
+ "useViewContainerState": () => (useViewContext),
230
+ "useViewContext": () => (useViewContext)
228
231
  });
229
232
 
230
233
  // EXTERNAL MODULE: external {"commonjs":"brew-js","commonjs2":"brew-js","amd":"brew-js","root":"brew"}
@@ -364,6 +367,7 @@ var domUtil_lib$util = external_commonjs_zeta_dom_commonjs2_zeta_dom_amd_zeta_do
364
367
  removeNode = domUtil_lib$util.removeNode,
365
368
  getClass = domUtil_lib$util.getClass,
366
369
  setClass = domUtil_lib$util.setClass,
370
+ getSafeAreaInset = domUtil_lib$util.getSafeAreaInset,
367
371
  getScrollOffset = domUtil_lib$util.getScrollOffset,
368
372
  getScrollParent = domUtil_lib$util.getScrollParent,
369
373
  getContentRect = domUtil_lib$util.getContentRect,
@@ -584,14 +588,19 @@ var setBaseUrl = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_br
584
588
 
585
589
 
586
590
 
591
+
592
+
593
+ var _ = createPrivateStore();
594
+
587
595
  var root = zeta_dom_dom.root;
588
596
  var routeMap = new Map();
589
597
  var usedParams = {};
590
598
  var sortedViews = [];
591
- var StateContext = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createContext(Object.freeze({
592
- container: root,
593
- active: true
599
+ var emitter = new ZetaEventContainer();
600
+ var rootContext = freeze(extend(new ViewContext(null, null), {
601
+ container: root
594
602
  }));
603
+ var StateContext = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createContext(rootContext);
595
604
  var errorView;
596
605
  /** @type {Partial<Zeta.ZetaEventType<"beforepageload", Brew.RouterEventMap, Element>>} */
597
606
 
@@ -599,7 +608,31 @@ var view_event = {};
599
608
  onAppInit(function () {
600
609
  app_app.on('beforepageload', function (e) {
601
610
  view_event = e;
611
+
612
+ _(rootContext).setPage(app_app.page);
613
+ });
614
+ });
615
+
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)
602
623
  });
624
+
625
+ watch(self, 'page', function (page, previousPage) {
626
+ emitter.emit('pagechange', self, {
627
+ previousPage: previousPage
628
+ });
629
+ });
630
+ }
631
+
632
+ definePrototype(ViewContext, {
633
+ on: function on(event, handler) {
634
+ return emitter.add(this, event, handler);
635
+ }
603
636
  });
604
637
 
605
638
  function ErrorBoundary() {
@@ -659,31 +692,40 @@ function ViewContainer() {
659
692
  this.stateId = history.state;
660
693
  }
661
694
 
695
+ ViewContainer.contextType = StateContext;
662
696
  definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react_root_React_.Component, {
663
697
  componentDidMount: function componentDidMount() {
664
698
  /** @type {any} */
665
699
  var self = this;
666
700
  self.componentWillUnmount = combineFn(watch(app_app.route, function () {
667
- self.setActive(self.getViewComponent() === self.currentViewComponent);
701
+ (self.setActive || noop)(self.getViewComponent() === self.currentViewComponent);
668
702
  }), app_app.on('beforepageload', function () {
669
703
  self.stateId = history.state;
670
- self.updateView();
671
- self.forceUpdate();
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
+ }
672
712
  }));
673
713
  },
674
714
  render: function render() {
675
715
  /** @type {any} */
676
716
  var self = this;
677
717
 
678
- if (history.state === self.stateId) {
718
+ if (history.state === self.stateId && self.context.active) {
679
719
  self.updateView();
680
720
  }
681
721
 
722
+ (self.onRender || noop)();
682
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);
683
724
  },
684
725
  updateView: function updateView() {
685
726
  var self = this;
686
727
  var V = self.getViewComponent();
728
+ self.updateOnNext = false;
687
729
 
688
730
  if (V) {
689
731
  // ensure the current path actually corresponds to the matched view
@@ -692,6 +734,8 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
692
734
 
693
735
  if (targetPath !== removeQueryAndHash(app_app.path)) {
694
736
  app_app.navigate(targetPath, true);
737
+ self.updateOnNext = true;
738
+ return;
695
739
  }
696
740
  }
697
741
 
@@ -711,6 +755,7 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
711
755
  });
712
756
  }
713
757
 
758
+ (self.cancelPrevious || noop)();
714
759
  var onComponentLoaded;
715
760
  var promise = new Promise(function (resolve) {
716
761
  onComponentLoaded = resolve;
@@ -726,13 +771,12 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
726
771
  });
727
772
  notifyAsync(element, promise);
728
773
  });
774
+ var state = new ViewContext(V, app_app.page);
729
775
  var viewProps = freeze({
730
776
  navigationType: view_event.navigationType,
777
+ viewContext: state,
731
778
  viewData: view_event.data || {}
732
779
  });
733
- var state = {
734
- view: V
735
- };
736
780
  var view = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(StateContext.Provider, {
737
781
  key: routeMap.get(V).id,
738
782
  value: state
@@ -742,14 +786,16 @@ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react
742
786
  onComponentLoaded: onComponentLoaded,
743
787
  viewProps: viewProps
744
788
  }))));
745
- extend(self, {
789
+ extend(self, _(state), {
790
+ cancelPrevious: onComponentLoaded,
746
791
  currentPath: app_app.path,
747
792
  currentView: view,
748
- currentViewComponent: V,
749
- setActive: defineObservableProperty(state, 'active', true, true)
793
+ currentViewComponent: V
750
794
  });
751
795
  (view_event.waitFor || noop)(promise);
752
796
  }
797
+
798
+ (self.setPage || noop)(app_app.page);
753
799
  },
754
800
  getViewComponent: function getViewComponent() {
755
801
  var props = this.props;
@@ -822,6 +868,7 @@ function createViewComponent(factory) {
822
868
  if (isThenable(children)) {
823
869
  promise = children;
824
870
  children = null;
871
+ catchAsync(promise);
825
872
  }
826
873
 
827
874
  var state = (0,external_zeta_dom_react_.useAsync)(function () {
@@ -842,7 +889,7 @@ function createViewComponent(factory) {
842
889
  };
843
890
  }
844
891
 
845
- function useViewContainerState() {
892
+ function useViewContext() {
846
893
  return external_commonjs_react_commonjs2_react_amd_react_root_React_.useContext(StateContext);
847
894
  }
848
895
  function isViewMatched(view) {
@@ -922,6 +969,7 @@ function navigateTo(view, params, data, replace) {
922
969
  function redirectTo(view, params, data) {
923
970
  return navigateTo(view, params, data, true);
924
971
  }
972
+
925
973
  ;// CONCATENATED MODULE: ./src/hooks.js
926
974
 
927
975
 
@@ -930,7 +978,7 @@ function redirectTo(view, params, data) {
930
978
 
931
979
 
932
980
 
933
- var emitter = new ZetaEventContainer();
981
+ var hooks_emitter = new ZetaEventContainer();
934
982
 
935
983
  function getCurrentStates() {
936
984
  return app_app.historyStorage.current;
@@ -952,7 +1000,7 @@ definePrototype(ViewState, {
952
1000
  },
953
1001
  onPopState: function onPopState(callback) {
954
1002
  throwNotFunction(callback);
955
- return emitter.add(this, 'popstate', function (e) {
1003
+ return hooks_emitter.add(this, 'popstate', function (e) {
956
1004
  callback.call(this, e.newValue);
957
1005
  });
958
1006
  }
@@ -968,11 +1016,12 @@ function useAppReadyState() {
968
1016
  };
969
1017
  }
970
1018
  function useRouteParam(name, defaultValue) {
971
- var container = useViewContainerState();
1019
+ var container = useViewContext();
1020
+ var params = container.page.params;
972
1021
  var route = app_app.route;
973
- var value = route[name] || '';
1022
+ var value = params[name] || '';
974
1023
  var ref = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useRef)(value);
975
- var forceUpdate = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)()[1];
1024
+ var forceUpdate = (0,external_zeta_dom_react_.useUpdateTrigger)();
976
1025
  (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useEffect)(function () {
977
1026
  var setValue = function setValue() {
978
1027
  var current = route[name] || '';
@@ -980,7 +1029,7 @@ function useRouteParam(name, defaultValue) {
980
1029
  if (current !== ref.current) {
981
1030
  if (container.active) {
982
1031
  ref.current = current;
983
- forceUpdate({});
1032
+ forceUpdate();
984
1033
  } else {
985
1034
  watch(container, 'active', setValue);
986
1035
  }
@@ -1000,14 +1049,14 @@ function useRouteParam(name, defaultValue) {
1000
1049
  }, [name, defaultValue]);
1001
1050
  ref.current = value;
1002
1051
 
1003
- if (defaultValue !== undefined && (!value || name === 'remainingSegments' && value === '/')) {
1004
- app_app.navigate(route.getPath(extend({}, route, kv(name, defaultValue))), true);
1052
+ if (defaultValue && container.active && (!value || name === 'remainingSegments' && value === '/')) {
1053
+ app_app.navigate(route.getPath(extend({}, params, kv(name, defaultValue))), true);
1005
1054
  }
1006
1055
 
1007
1056
  return value;
1008
1057
  }
1009
1058
  function useRouteState(key, defaultValue, snapshotOnUpdate) {
1010
- var container = useViewContainerState();
1059
+ var container = useViewContext();
1011
1060
  var cur = getCurrentStates();
1012
1061
  var state = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(cur.has(key) ? cur.get(key) : defaultValue);
1013
1062
 
@@ -1033,7 +1082,7 @@ function useRouteState(key, defaultValue, snapshotOnUpdate) {
1033
1082
  return state;
1034
1083
  }
1035
1084
  function ViewStateContainer(props) {
1036
- var container = useViewContainerState();
1085
+ var container = useViewContext();
1037
1086
  var provider = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(function () {
1038
1087
  var cache = {};
1039
1088
  return {
@@ -1046,7 +1095,7 @@ function ViewStateContainer(props) {
1046
1095
  var store = state.store;
1047
1096
 
1048
1097
  if (store && (store !== cur && cur.has(key) || key !== state.key)) {
1049
- emitter.emit('popstate', state, {
1098
+ hooks_emitter.emit('popstate', state, {
1050
1099
  newValue: value
1051
1100
  });
1052
1101
  state.value = value;
@@ -1259,7 +1308,7 @@ var observe_lib$dom = external_commonjs_zeta_dom_commonjs2_zeta_dom_amd_zeta_dom
1259
1308
 
1260
1309
 
1261
1310
 
1262
- var _ = createPrivateStore();
1311
+ var StatefulMixin_ = createPrivateStore();
1263
1312
 
1264
1313
  function MixinRefImpl(mixin) {
1265
1314
  this.mixin = mixin;
@@ -1273,7 +1322,7 @@ definePrototype(MixinRefImpl, {
1273
1322
  function StatefulMixin() {
1274
1323
  Mixin.call(this);
1275
1324
 
1276
- _(this, {
1325
+ StatefulMixin_(this, {
1277
1326
  elements: new Set(),
1278
1327
  flush: watch(this, false),
1279
1328
  dispose: [],
@@ -1291,18 +1340,18 @@ definePrototype(StatefulMixin, Mixin, {
1291
1340
  },
1292
1341
 
1293
1342
  get state() {
1294
- var obj = _(this);
1343
+ var obj = StatefulMixin_(this);
1295
1344
 
1296
1345
  var key = obj.prefix + obj.counter;
1297
1346
  return obj.states[key] || (obj.states[key] = this.initState());
1298
1347
  },
1299
1348
 
1300
1349
  reset: function reset() {
1301
- _(this).counter = 0;
1350
+ StatefulMixin_(this).counter = 0;
1302
1351
  return this;
1303
1352
  },
1304
1353
  next: function next() {
1305
- _(this).counter++;
1354
+ StatefulMixin_(this).counter++;
1306
1355
  return this;
1307
1356
  },
1308
1357
  getRef: function getRef() {
@@ -1311,20 +1360,20 @@ definePrototype(StatefulMixin, Mixin, {
1311
1360
  return function (current) {
1312
1361
  state.element = current;
1313
1362
 
1314
- if (current && setAdd(_(self).elements, current)) {
1363
+ if (current && setAdd(StatefulMixin_(self).elements, current)) {
1315
1364
  self.initElement(current, state);
1316
1365
  }
1317
1366
  };
1318
1367
  },
1319
1368
  elements: function elements() {
1320
- return values(_(this).states).map(function (v) {
1369
+ return values(StatefulMixin_(this).states).map(function (v) {
1321
1370
  return v.element;
1322
1371
  }).filter(function (v) {
1323
1372
  return v;
1324
1373
  });
1325
1374
  },
1326
1375
  onDispose: function onDispose(callback) {
1327
- _(this).dispose.push(callback);
1376
+ StatefulMixin_(this).dispose.push(callback);
1328
1377
  },
1329
1378
  initState: function initState() {
1330
1379
  return {
@@ -1336,7 +1385,7 @@ definePrototype(StatefulMixin, Mixin, {
1336
1385
  var self = this;
1337
1386
  var clone = inherit(Object.getPrototypeOf(self), self);
1338
1387
 
1339
- _(clone, extend({}, _(self), {
1388
+ StatefulMixin_(clone, extend({}, StatefulMixin_(self), {
1340
1389
  prefix: randomId() + '.',
1341
1390
  counter: 0
1342
1391
  }));
@@ -1344,7 +1393,7 @@ definePrototype(StatefulMixin, Mixin, {
1344
1393
  return clone;
1345
1394
  },
1346
1395
  dispose: function dispose() {
1347
- var state = _(this);
1396
+ var state = StatefulMixin_(this);
1348
1397
 
1349
1398
  var states = state.states;
1350
1399
  combineFn(state.dispose.splice(0))();
@@ -1716,6 +1765,8 @@ definePrototype(ScrollableMixin, ClassNameMixin, {
1716
1765
  'scroller-snap-page': options.paged,
1717
1766
  'scroller-page': options.pagedItemSelector,
1718
1767
  'scroller-state': 'pageIndex'
1768
+ }, options.persistScroll && {
1769
+ 'persist-scroll': ''
1719
1770
  });
1720
1771
  },
1721
1772
  onPageIndexChanged: function onPageIndexChanged(callback) {
@@ -1751,6 +1802,29 @@ each('destroy enable disable setOptions refresh scrollPadding stop scrollLeft sc
1751
1802
  return obj.scrollable.apply(obj, [v].concat(makeArray(arguments)));
1752
1803
  });
1753
1804
  });
1805
+ ;// CONCATENATED MODULE: ./src/mixins/ScrollIntoViewMixin.js
1806
+
1807
+
1808
+
1809
+ function ScrollIntoViewMixin() {
1810
+ StatefulMixin.call(this);
1811
+ }
1812
+ definePrototype(ScrollIntoViewMixin, StatefulMixin, {
1813
+ when: function when(deps) {
1814
+ var state = this.state;
1815
+
1816
+ var callback = state.callback || (state.callback = function () {
1817
+ scrollIntoView(state.element);
1818
+ });
1819
+
1820
+ if (state.deps && !equal(deps, state.deps)) {
1821
+ setImmediateOnce(callback);
1822
+ }
1823
+
1824
+ state.deps = makeArray(deps);
1825
+ return this;
1826
+ }
1827
+ });
1754
1828
  ;// CONCATENATED MODULE: ./src/mixin.js
1755
1829
 
1756
1830
 
@@ -1766,6 +1840,7 @@ each('destroy enable disable setOptions refresh scrollPadding stop scrollLeft sc
1766
1840
 
1767
1841
 
1768
1842
 
1843
+
1769
1844
  function extendSelf(options) {
1770
1845
  extend(this, options);
1771
1846
  }
@@ -1784,6 +1859,7 @@ var useFlyoutMixin = createUseFunction(FlyoutMixin);
1784
1859
  var useFocusStateMixin = createUseFunction(FocusStateMixin);
1785
1860
  var useLoadingStateMixin = createUseFunction(LoadingStateMixin);
1786
1861
  var useScrollableMixin = createUseFunction(ScrollableMixin);
1862
+ var useScrollIntoViewMixin = createUseFunction(ScrollIntoViewMixin);
1787
1863
  function useMixin(ctor) {
1788
1864
  return (0,external_zeta_dom_react_.useSingleton)(function () {
1789
1865
  return new ctor();