@plasmicapp/react-web 0.2.115 → 0.2.116

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/react-web",
3
- "version": "0.2.115",
3
+ "version": "0.2.116",
4
4
  "description": "plasmic library for rendering in the presentational style",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -105,5 +105,5 @@
105
105
  "react": ">=16.8.0",
106
106
  "react-dom": ">=16.8.0"
107
107
  },
108
- "gitHead": "22f6daf77a6ec05e118513807ef2319143f2bff8"
108
+ "gitHead": "8ad0410206b4a5962299d6bcadf89f235e914b58"
109
109
  }
@@ -14,3 +14,5 @@ export { PlasmicDataSourceContextProvider, PlasmicRootProvider, useCurrentUser,
14
14
  export { Stack } from "./render/Stack";
15
15
  export { genTranslatableString, Trans } from "./render/translation";
16
16
  export { useTrigger } from "./render/triggers";
17
+ export * from "./states/helpers";
18
+ export { $State, default as useDollarState, useCanvasDollarState, } from "./states/valtio";
@@ -1,5 +1,6 @@
1
1
  import classNames$1 from 'classnames';
2
2
  import get from 'dlv';
3
+ export { default as get } from 'dlv';
3
4
  import * as React from 'react';
4
5
  import React__default from 'react';
5
6
  import { c as createElementWithChildren, m as mergeProps, e as ensureNotArray, N as NONE, a as mergePropVals, i as isReactNode, b as isBrowser, u as useIsomorphicLayoutEffect } from './react-utils-7bc53870.js';
@@ -11,6 +12,9 @@ import { T as Trans } from './ssr-b16a1854.js';
11
12
  export { P as PlasmicRootProvider, T as Trans, g as genTranslatableString, u as useIsSSR } from './ssr-b16a1854.js';
12
13
  import ReactDOM__default from 'react-dom';
13
14
  import { useFocusRing } from '@react-aria/focus';
15
+ import deepEqual from 'fast-deep-equal';
16
+ import { proxy, ref, useSnapshot } from 'valtio';
17
+ import { subscribeKey } from 'valtio/utils';
14
18
  export { PlasmicDataSourceContextProvider, useCurrentUser } from '@plasmicapp/data-sources-context';
15
19
  import '@react-aria/ssr';
16
20
 
@@ -633,8 +637,410 @@ function useTrigger(trigger, opts) {
633
637
  return TRIGGER_TO_HOOK[trigger](opts);
634
638
  }
635
639
 
640
+ function generateStateOnChangeProp($state, stateName, dataReps) {
641
+ return function (val, path) { return set($state, __spreadArray(__spreadArray([stateName], __read(dataReps), false), __read(path), false), val); };
642
+ }
643
+ /**
644
+ * This function generate the state value prop for repeated states
645
+ * Example:
646
+ * - parent[][].counter[].count
647
+ * We need to pass `parent[index1][index2].counter to the child component
648
+ */
649
+ function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
650
+ ) {
651
+ return get($state, path);
652
+ }
653
+ /**
654
+ * Forked from https://github.com/lukeed/dset
655
+ * Changes: fixed setting a deep value to a proxy object
656
+ */
657
+ function set(obj, keys, val) {
658
+ keys = keys.split ? keys.split(".") : keys;
659
+ var i = 0, l = keys.length, t = obj, x, k;
660
+ while (i < l) {
661
+ k = keys[i++];
662
+ if (k === "__proto__" || k === "constructor" || k === "prototype")
663
+ break;
664
+ if (i === l) {
665
+ t[k] = val;
666
+ t = t[k];
667
+ }
668
+ else {
669
+ if (typeof (x = t[k]) === typeof keys) {
670
+ t = t[k] = x;
671
+ }
672
+ else if (keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".")) {
673
+ t[k] = {};
674
+ t = t[k];
675
+ }
676
+ else {
677
+ t[k] = [];
678
+ t = t[k];
679
+ }
680
+ }
681
+ }
682
+ }
683
+
684
+ var mkUntrackedValue = function (o) { return (typeof o === "object" ? ref(o) : o); };
685
+ var transformPathStringToObj = function (str) {
686
+ var splitStatePathPart = function (state) {
687
+ return state.endsWith("[]")
688
+ ? __spreadArray(__spreadArray([], __read(splitStatePathPart(state.slice(0, -2))), false), ["[]"], false) : [state];
689
+ };
690
+ return str.split(".").flatMap(splitStatePathPart);
691
+ };
692
+ function shallowEqual(a1, a2) {
693
+ if (a1.length !== a2.length) {
694
+ return false;
695
+ }
696
+ for (var i = 0; i < a1.length; i++) {
697
+ if (a1[i] !== a2[i]) {
698
+ return false;
699
+ }
700
+ }
701
+ return true;
702
+ }
703
+ function isNum(value) {
704
+ return typeof value === "symbol" ? false : !isNaN(+value);
705
+ }
706
+ function saveNewState($$state, path, spec) {
707
+ var key = JSON.stringify(path);
708
+ $$state.existingStates.set(key, { path: path, specKey: spec.path });
709
+ if (!$$state.statesInstanceBySpec.has(spec.path)) {
710
+ $$state.statesInstanceBySpec.set(spec.path, []);
711
+ }
712
+ $$state.statesInstanceBySpec
713
+ .get(spec.path)
714
+ .push({ path: path, specKey: spec.path });
715
+ }
716
+ function create$StateProxy($$state, handlers) {
717
+ var getNextKeyToSpecMap = function (currPath) {
718
+ return new Map(Object.entries(Object.values($$state.specsByKey)
719
+ .filter(function (spec) {
720
+ return shallowEqual(currPath.map(function (p) { return (isNum(p) ? "[]" : p); }), spec.pathObj.slice(0, currPath.length));
721
+ })
722
+ .reduce(function (agg, spec) {
723
+ var nextKey = spec.pathObj[currPath.length];
724
+ if (!(nextKey in agg)) {
725
+ agg[nextKey] = [];
726
+ }
727
+ agg[nextKey].push(spec);
728
+ return agg;
729
+ }, {})));
730
+ };
731
+ var rec = function (currPath) {
732
+ var nextKeyToSpecs = getNextKeyToSpecMap(currPath);
733
+ var getSpecForProperty = function (property) {
734
+ var _a, _b;
735
+ return nextKeyToSpecs.has("[]") && isNum(property)
736
+ ? (_a = nextKeyToSpecs.get("[]")) === null || _a === void 0 ? void 0 : _a[0]
737
+ : typeof property === "string" && nextKeyToSpecs.has(property)
738
+ ? (_b = nextKeyToSpecs.get(property)) === null || _b === void 0 ? void 0 : _b[0]
739
+ : undefined;
740
+ };
741
+ var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
742
+ isNum(property) ? +property : property,
743
+ ], false); };
744
+ return new Proxy(nextKeyToSpecs.has("[]") ? [] : {}, {
745
+ deleteProperty: function (target, property) {
746
+ var prefixPath = getNextPath(property);
747
+ var specKeysToUpdate = new Set();
748
+ $$state.existingStates.forEach(function (_a) {
749
+ var path = _a.path, specKey = _a.specKey;
750
+ if (path.length >= prefixPath.length &&
751
+ shallowEqual(path.slice(0, prefixPath.length), prefixPath)) {
752
+ deleteState($$state, path);
753
+ specKeysToUpdate.add(specKey);
754
+ }
755
+ });
756
+ specKeysToUpdate.forEach(function (specKey) {
757
+ var _a, _b;
758
+ var spec = $$state.specsByKey[specKey];
759
+ if (spec.onChangeProp) {
760
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get($$state.stateValues, currPath), currPath);
761
+ }
762
+ });
763
+ return Reflect.deleteProperty(target, property);
764
+ },
765
+ get: function (target, property, receiver) {
766
+ var _a, _b;
767
+ var spec = getSpecForProperty(property);
768
+ if (spec && typeof property !== "symbol") {
769
+ var nextPath = getNextPath(property);
770
+ if (spec.pathObj.length === currPath.length + 1) {
771
+ // reached the end of the spec
772
+ target[property] = (_b = (_a = handlers(nextPath, spec)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
773
+ }
774
+ else if (!(property in target)) {
775
+ target[property] = rec(nextPath);
776
+ }
777
+ }
778
+ return Reflect.get(target, property, receiver);
779
+ },
780
+ set: function (target, property, value, receiver) {
781
+ var e_1, _a;
782
+ var _b, _c, _d;
783
+ var spec = getSpecForProperty(property);
784
+ var nextPath = getNextPath(property);
785
+ if (spec && typeof property !== "symbol") {
786
+ if (spec.pathObj.length === currPath.length + 1) {
787
+ // reached the end of the spec
788
+ target[property] = (_c = (_b = handlers(nextPath, spec)).set) === null || _c === void 0 ? void 0 : _c.call(_b, target, property, value, receiver);
789
+ return Reflect.set(target, property, value, receiver);
790
+ }
791
+ else if (typeof value === "object") {
792
+ target[property] = rec(nextPath);
793
+ try {
794
+ for (var _e = __values(Object.keys(value)), _f = _e.next(); !_f.done; _f = _e.next()) {
795
+ var key = _f.value;
796
+ target[property][key] = value[key];
797
+ }
798
+ }
799
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
800
+ finally {
801
+ try {
802
+ if (_f && !_f.done && (_a = _e["return"])) _a.call(_e);
803
+ }
804
+ finally { if (e_1) throw e_1.error; }
805
+ }
806
+ return true;
807
+ }
808
+ }
809
+ if (property === "registerInitFunc" && currPath.length === 0) {
810
+ return Reflect.set(target, property, value, receiver);
811
+ }
812
+ if (nextKeyToSpecs.has("[]")) {
813
+ set($$state.stateValues, nextPath, value);
814
+ (_d = nextKeyToSpecs.get("[]")) === null || _d === void 0 ? void 0 : _d.forEach(function (spec) {
815
+ var _a, _b;
816
+ if (spec === null || spec === void 0 ? void 0 : spec.onChangeProp) {
817
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, nextPath);
818
+ }
819
+ });
820
+ return Reflect.set(target, property, value, receiver);
821
+ }
822
+ // invalid setting a value that doesn't make part of the spec
823
+ return false;
824
+ }
825
+ });
826
+ };
827
+ return rec([]);
828
+ }
829
+ var deleteState = function ($$state, path) {
830
+ var _a;
831
+ var key = JSON.stringify(path);
832
+ (_a = $$state.unsubscriptionsByState[key]) === null || _a === void 0 ? void 0 : _a.forEach(function (f) { return f(); });
833
+ delete $$state.unsubscriptionsByState[key];
834
+ $$state.existingStates["delete"](key);
835
+ // delete get($$state.stateValues, path.slice(-1))[path.slice(-1)[0]];
836
+ // delete get($$state.initStateValues, path.slice(-1))[path.slice(-1)[0]];
837
+ };
838
+ var getIndexes = function (path, spec) {
839
+ var indexes = [];
840
+ if (path.length !== spec.pathObj.length) {
841
+ throw new Error("Unexpected error: state path and spec path have different lengths");
842
+ }
843
+ for (var i = 0; i < spec.pathObj.length; i++) {
844
+ if (spec.pathObj[i] === "[]") {
845
+ indexes.push(path[i]);
846
+ }
847
+ }
848
+ return indexes;
849
+ };
850
+ function initializeStateValue($$state, initialStatePath, initialSpec) {
851
+ var _a;
852
+ var initialStateKey = JSON.stringify(initialStatePath);
853
+ var stateAccess = new Set();
854
+ var $state = create$StateProxy($$state, function (path, spec) { return ({
855
+ get: function () {
856
+ var key = JSON.stringify(path);
857
+ stateAccess.add({ path: path, spec: spec });
858
+ if (spec.valueProp) {
859
+ return !spec.isRepeated
860
+ ? $$state.props[spec.valueProp]
861
+ : get($$state.props[spec.valueProp], path.slice(1));
862
+ }
863
+ if ($$state.existingStates.has(key)) {
864
+ // is already initialized
865
+ return get($$state.stateValues, path);
866
+ }
867
+ else if (spec.initFunc) {
868
+ initializeStateValue($$state, path, spec);
869
+ }
870
+ return get($$state.stateValues, path);
871
+ },
872
+ set: function () {
873
+ throw new Error("Cannot update state values during initialization");
874
+ }
875
+ }); });
876
+ (_a = $$state.unsubscriptionsByState[initialStateKey]) === null || _a === void 0 ? void 0 : _a.forEach(function (f) { return f(); });
877
+ $$state.unsubscriptionsByState[initialStateKey] = [];
878
+ stateAccess.forEach(function (_a) {
879
+ var path = _a.path, spec = _a.spec;
880
+ var unsubscribe = subscribeKey(get($$state.stateValues, path.slice(-1)), path.slice(-1)[0], function () {
881
+ return set($$state.stateValues, initialStatePath, mkUntrackedValue(initialSpec.initFunc($$state.props, $state, getIndexes(path, spec))));
882
+ });
883
+ $$state.unsubscriptionsByState[initialStateKey].push(unsubscribe);
884
+ });
885
+ var untrackedInitialValue = mkUntrackedValue(initialSpec.initFunc($$state.props, $state, getIndexes(initialStatePath, initialSpec)));
886
+ set($$state.initStateValues, initialStatePath, untrackedInitialValue);
887
+ set($$state.stateValues, initialStatePath, untrackedInitialValue);
888
+ return untrackedInitialValue;
889
+ }
890
+ function useDollarState(specs, props) {
891
+ var $$state = React__default.useRef(proxy({
892
+ stateValues: {},
893
+ initStateValues: {},
894
+ specsByKey: Object.fromEntries(specs.map(function (spec) { return [
895
+ spec.path,
896
+ __assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path
897
+ .split(".")
898
+ .some(function (part) { return part.endsWith("[]"); }) }),
899
+ ]; })),
900
+ statesInstanceBySpec: new Map(),
901
+ existingStates: new Map(),
902
+ unsubscriptionsByState: {},
903
+ props: undefined,
904
+ registrationsQueue: []
905
+ })).current;
906
+ $$state.props = mkUntrackedValue(props);
907
+ var $state = React__default.useRef(Object.assign(create$StateProxy($$state, function (path, spec) {
908
+ var _a;
909
+ var key = JSON.stringify(path);
910
+ if (!$$state.existingStates.has(key)) {
911
+ saveNewState($$state, path, spec);
912
+ var untrackedValue = !spec.initFunc
913
+ ? mkUntrackedValue((_a = spec.initVal) !== null && _a !== void 0 ? _a : undefined)
914
+ : initializeStateValue($$state, path, spec);
915
+ set($$state.stateValues, path, untrackedValue);
916
+ set($$state.initStateValues, path, untrackedValue);
917
+ }
918
+ return {
919
+ get: function () {
920
+ if (spec.valueProp) {
921
+ var value = !spec.isRepeated
922
+ ? $$state.props[spec.valueProp]
923
+ : get($$state.props[spec.valueProp], path.slice(1));
924
+ return value;
925
+ }
926
+ else {
927
+ return get($$state.stateValues, path);
928
+ }
929
+ },
930
+ set: function (_t, _p, value) {
931
+ var _a, _b;
932
+ set($$state.stateValues, path, mkUntrackedValue(value));
933
+ if (spec.onChangeProp) {
934
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, path);
935
+ }
936
+ return true;
937
+ }
938
+ };
939
+ }), {
940
+ registerInitFunc: function (pathStr, f) {
941
+ var _a;
942
+ if ((_a = $$state.statesInstanceBySpec
943
+ .get(pathStr)) === null || _a === void 0 ? void 0 : _a.some(function (_a) {
944
+ var path = _a.path, specKey = _a.specKey;
945
+ return !deepEqual(get($$state.initStateValues, path), f(props, $state, getIndexes(path, $$state.specsByKey[specKey])));
946
+ })) {
947
+ $$state.registrationsQueue.push({ pathStr: pathStr, f: f });
948
+ }
949
+ }
950
+ })).current;
951
+ // For each spec with an initFunc, evaluate it and see if
952
+ // the init value has changed. If so, reset its state.
953
+ var resetSpecs = [];
954
+ $$state.existingStates.forEach(function (_a) {
955
+ var path = _a.path, specKey = _a.specKey;
956
+ var spec = $$state.specsByKey[specKey];
957
+ if (spec.initFunc) {
958
+ var newInit = spec.initFunc(props, $state, getIndexes(path, spec));
959
+ if (!deepEqual(newInit, get($$state.initStateValues, path))) {
960
+ resetSpecs.push({ path: path, spec: spec });
961
+ }
962
+ }
963
+ });
964
+ React__default.useLayoutEffect(function () {
965
+ resetSpecs.forEach(function (_a) {
966
+ var _b, _c;
967
+ var path = _a.path, spec = _a.spec;
968
+ var newInit = initializeStateValue($$state, path, spec);
969
+ if (spec.onChangeProp) {
970
+ (_c = (_b = $$state.props)[spec.onChangeProp]) === null || _c === void 0 ? void 0 : _c.call(_b, newInit, path);
971
+ }
972
+ });
973
+ }, [props, resetSpecs]);
974
+ React__default.useLayoutEffect(function () {
975
+ $$state.registrationsQueue.forEach(function (_a) {
976
+ var f = _a.f, pathStr = _a.pathStr;
977
+ $$state.specsByKey[pathStr].initFunc = f;
978
+ });
979
+ $$state.registrationsQueue = [];
980
+ }, [$$state.registrationsQueue]);
981
+ // Re-render if any value changed in one of these objects
982
+ useSnapshot($$state.stateValues, { sync: true });
983
+ useSnapshot($$state.specsByKey, { sync: true });
984
+ return $state;
985
+ }
986
+ // Simple version of $state useDollarState for read-only
987
+ function useCanvasDollarState(specs, props) {
988
+ var e_2, _a;
989
+ var $$state = proxy({
990
+ stateValues: {},
991
+ initStateValues: {},
992
+ specsByKey: Object.fromEntries(specs.map(function (spec) { return [
993
+ spec.path,
994
+ __assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path.split(".").some(function (part) { return part.endsWith("[]"); }) }),
995
+ ]; })),
996
+ statesInstanceBySpec: new Map(),
997
+ existingStates: new Map(),
998
+ unsubscriptionsByState: {},
999
+ props: undefined,
1000
+ registrationsQueue: []
1001
+ });
1002
+ $$state.props = mkUntrackedValue(props);
1003
+ var $state = create$StateProxy($$state, function (path, spec) {
1004
+ return {
1005
+ get: function () {
1006
+ return get($$state.stateValues, path);
1007
+ },
1008
+ set: function (_t, _p, value) {
1009
+ var _a, _b;
1010
+ set($$state.stateValues, path, mkUntrackedValue(value));
1011
+ if (spec.onChangeProp) {
1012
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, path);
1013
+ }
1014
+ return true;
1015
+ }
1016
+ };
1017
+ });
1018
+ try {
1019
+ for (var specs_1 = __values(specs), specs_1_1 = specs_1.next(); !specs_1_1.done; specs_1_1 = specs_1.next()) {
1020
+ var spec = specs_1_1.value;
1021
+ var path = transformPathStringToObj(spec.path);
1022
+ var init = spec.valueProp
1023
+ ? $$state.props[spec.valueProp]
1024
+ : spec.initVal
1025
+ ? spec.initVal
1026
+ : spec.initFunc
1027
+ ? initializeStateValue($$state, path, $$state.specsByKey[spec.path])
1028
+ : undefined;
1029
+ set($state, path, init);
1030
+ }
1031
+ }
1032
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1033
+ finally {
1034
+ try {
1035
+ if (specs_1_1 && !specs_1_1.done && (_a = specs_1["return"])) _a.call(specs_1);
1036
+ }
1037
+ finally { if (e_2) throw e_2.error; }
1038
+ }
1039
+ return $state;
1040
+ }
1041
+
636
1042
  // Utilities used by generated code
637
1043
  var classNames = classNames$1;
638
1044
 
639
- export { PlasmicIcon, PlasmicLink, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, hasVariant, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, useTrigger, wrapWithClassName };
1045
+ export { PlasmicIcon, PlasmicLink, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateValueProp, hasVariant, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, set, useCanvasDollarState, useDollarState, useTrigger, wrapWithClassName };
640
1046
  //# sourceMappingURL=index.js.map