@cntrl-site/sdk-nextjs 0.14.3 → 0.14.4

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.
Files changed (38) hide show
  1. package/cntrl-site-sdk-nextjs-0.14.3.tgz +0 -0
  2. package/cntrl-site-sdk-nextjs-0.14.4.tgz +0 -0
  3. package/lib/common/useCurrentLayout.js +1 -6
  4. package/lib/common/useKeyframeValue.js +2 -2
  5. package/lib/components/Article.js +5 -4
  6. package/lib/components/ArticleWrapper.js +35 -0
  7. package/lib/components/Item.js +6 -4
  8. package/lib/components/items/useEmbedVideoItem.js +2 -2
  9. package/lib/components/items/useFileItem.js +2 -2
  10. package/lib/components/items/useRectangleItem.js +2 -2
  11. package/lib/components/items/useRichTextItem.js +3 -3
  12. package/lib/components/items/useRichTextItemValues.js +2 -2
  13. package/lib/components/items/useStickyItemTop.js +12 -6
  14. package/lib/components/useItemDimensions.js +2 -2
  15. package/lib/components/useItemPosition.js +10 -4
  16. package/lib/components/useItemScale.js +5 -4
  17. package/lib/components/useLayoutContext.js +10 -0
  18. package/lib/components/useSectionColor.js +5 -3
  19. package/lib/provider/LayoutContext.js +5 -0
  20. package/package.json +1 -1
  21. package/src/common/useCurrentLayout.ts +2 -9
  22. package/src/common/useKeyframeValue.ts +3 -3
  23. package/src/components/Article.tsx +15 -12
  24. package/src/components/ArticleWrapper.tsx +36 -0
  25. package/src/components/Item.tsx +5 -4
  26. package/src/components/items/useEmbedVideoItem.ts +2 -2
  27. package/src/components/items/useFileItem.ts +2 -2
  28. package/src/components/items/useRectangleItem.ts +2 -2
  29. package/src/components/items/useRichTextItem.ts +3 -3
  30. package/src/components/items/useRichTextItemValues.ts +2 -3
  31. package/src/components/items/useStickyItemTop.ts +11 -7
  32. package/src/components/useItemDimensions.ts +2 -2
  33. package/src/components/useItemPosition.ts +8 -4
  34. package/src/components/useItemScale.ts +6 -5
  35. package/src/components/useLayoutContext.ts +7 -0
  36. package/src/components/useSectionColor.ts +4 -3
  37. package/src/provider/LayoutContext.ts +3 -0
  38. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
Binary file
Binary file
@@ -24,12 +24,7 @@ const useCurrentLayout = () => {
24
24
  const getCurrentLayout = (0, react_1.useCallback)((articleWidth) => {
25
25
  return layoutRanges.find(l => articleWidth >= l.start && articleWidth < l.end).layoutId;
26
26
  }, [layoutRanges]);
27
- const [layoutId, setLayoutId] = (0, react_1.useState)(getCurrentLayout(0));
28
- (0, react_1.useEffect)(() => {
29
- if (typeof window !== 'undefined') {
30
- getCurrentLayout(window.innerWidth);
31
- }
32
- }, []);
27
+ const [layoutId, setLayoutId] = (0, react_1.useState)(undefined);
33
28
  (0, react_1.useEffect)(() => {
34
29
  if (!articleRectObserver)
35
30
  return;
@@ -7,9 +7,9 @@ exports.useKeyframeValue = void 0;
7
7
  const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
8
8
  const react_1 = require("react");
9
9
  const ArticleRectContext_1 = require("../provider/ArticleRectContext");
10
- const useCurrentLayout_1 = require("./useCurrentLayout");
11
10
  const KeyframesContext_1 = require("../provider/KeyframesContext");
12
11
  const Animator_1 = require("../utils/Animator/Animator");
12
+ const useLayoutContext_1 = require("../components/useLayoutContext");
13
13
  const emptyDeps = [];
14
14
  const useKeyframeValue = (item, itemParamsGetter, animatorGetter, sectionId, deps = emptyDeps) => {
15
15
  const animatorGetterRef = (0, react_1.useRef)(animatorGetter);
@@ -17,7 +17,7 @@ const useKeyframeValue = (item, itemParamsGetter, animatorGetter, sectionId, dep
17
17
  animatorGetterRef.current = animatorGetter;
18
18
  itemParamsGetterRef.current = itemParamsGetter;
19
19
  const articleRectObserver = (0, react_1.useContext)(ArticleRectContext_1.ArticleRectContext);
20
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
20
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
21
21
  const keyframesRepo = (0, react_1.useContext)(KeyframesContext_1.KeyframesContext);
22
22
  const keyframes = (0, react_1.useMemo)(() => keyframesRepo.getItemKeyframes(item.id), [item.id, keyframesRepo]);
23
23
  const paramValue = (0, react_1.useMemo)(() => {
@@ -11,14 +11,15 @@ const Section_1 = require("./Section");
11
11
  const Item_1 = require("./Item");
12
12
  const useArticleRectObserver_1 = require("../utils/ArticleRectManager/useArticleRectObserver");
13
13
  const ArticleRectContext_1 = require("../provider/ArticleRectContext");
14
+ const ArticleWrapper_1 = require("./ArticleWrapper");
14
15
  const Article = ({ article, sectionData }) => {
15
16
  const articleRef = (0, react_1.useRef)(null);
16
17
  const articleRectObserver = (0, useArticleRectObserver_1.useArticleRectObserver)(articleRef.current);
17
18
  const id = (0, react_1.useId)();
18
- return ((0, jsx_runtime_1.jsxs)(ArticleRectContext_1.ArticleRectContext.Provider, { value: articleRectObserver, children: [(0, jsx_runtime_1.jsx)("div", { className: "article", ref: articleRef, children: article.sections.map((section, i) => {
19
- const data = section.name ? sectionData[section.name] : {};
20
- return ((0, jsx_runtime_1.jsx)(Section_1.Section, { section: section, data: data, children: article.sections[i].items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { item: item, sectionId: section.id }, item.id))) }, section.id));
21
- }) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
19
+ return ((0, jsx_runtime_1.jsxs)(ArticleRectContext_1.ArticleRectContext.Provider, { value: articleRectObserver, children: [(0, jsx_runtime_1.jsx)(ArticleWrapper_1.ArticleWrapper, { children: (0, jsx_runtime_1.jsx)("div", { className: "article", ref: articleRef, children: article.sections.map((section, i) => {
20
+ const data = section.name ? sectionData[section.name] : {};
21
+ return ((0, jsx_runtime_1.jsx)(Section_1.Section, { section: section, data: data, children: article.sections[i].items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { item: item, sectionId: section.id }, item.id))) }, section.id));
22
+ }) }) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
22
23
  .article {
23
24
  position: relative;
24
25
  overflow: clip;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ArticleWrapper = void 0;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const react_1 = require("react");
9
+ const style_1 = __importDefault(require("styled-jsx/style"));
10
+ const useCurrentLayout_1 = require("../common/useCurrentLayout");
11
+ const LayoutContext_1 = require("../provider/LayoutContext");
12
+ const ArticleWrapper = ({ children }) => {
13
+ const id = (0, react_1.useId)();
14
+ const [isPageLoaded, setIsPageLoaded] = (0, react_1.useState)(false);
15
+ const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
16
+ (0, react_1.useEffect)(() => {
17
+ const onPageLoad = () => {
18
+ setIsPageLoaded(true);
19
+ };
20
+ if (document.readyState === 'complete') {
21
+ onPageLoad();
22
+ }
23
+ else {
24
+ window.addEventListener('load', onPageLoad);
25
+ return () => window.removeEventListener('load', onPageLoad);
26
+ }
27
+ }, []);
28
+ return ((0, jsx_runtime_1.jsxs)(LayoutContext_1.LayoutContext.Provider, { value: layoutId, children: [(0, jsx_runtime_1.jsx)("div", { className: "article-wrapper", style: { opacity: layoutId && isPageLoaded ? 1 : 0 }, children: children }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
29
+ .article-wrapper {
30
+ opacity: 1;
31
+ transition: opacity 0.2s ease;
32
+ }
33
+ ` })] }));
34
+ };
35
+ exports.ArticleWrapper = ArticleWrapper;
@@ -18,7 +18,6 @@ const CustomItem_1 = require("./items/CustomItem");
18
18
  const useCntrlContext_1 = require("../provider/useCntrlContext");
19
19
  const useItemPosition_1 = require("./useItemPosition");
20
20
  const useItemDimensions_1 = require("./useItemDimensions");
21
- const useCurrentLayout_1 = require("../common/useCurrentLayout");
22
21
  const useItemScale_1 = require("./useItemScale");
23
22
  const ScaleAnchorMap_1 = require("../utils/ScaleAnchorMap");
24
23
  const useSectionHeightMap_1 = require("./useSectionHeightMap");
@@ -26,6 +25,7 @@ const HoverStyles_1 = require("../utils/HoverStyles/HoverStyles");
26
25
  const getItemTopStyle_1 = require("../utils/getItemTopStyle");
27
26
  const useStickyItemTop_1 = require("./items/useStickyItemTop");
28
27
  const getAnchoredItemTop_1 = require("../utils/getAnchoredItemTop");
28
+ const useLayoutContext_1 = require("./useLayoutContext");
29
29
  const itemsMap = {
30
30
  [sdk_1.ArticleItemType.Rectangle]: RectangleItem_1.RectangleItem,
31
31
  [sdk_1.ArticleItemType.Image]: ImageItem_1.ImageItem,
@@ -39,12 +39,12 @@ const noop = () => null;
39
39
  const Item = ({ item, sectionId }) => {
40
40
  const id = (0, react_1.useId)();
41
41
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
42
+ const layout = (0, useLayoutContext_1.useLayoutContext)();
42
43
  const [wrapperHeight, setWrapperHeight] = (0, react_1.useState)(undefined);
43
44
  const { scale, scaleAnchor } = (0, useItemScale_1.useItemScale)(item, sectionId);
44
45
  const { top, left } = (0, useItemPosition_1.useItemPosition)(item, sectionId);
45
46
  const sectionHeight = (0, useSectionHeightMap_1.useSectionHeightData)(sectionId);
46
47
  const stickyTop = (0, useStickyItemTop_1.useStickyItemTop)(item, sectionHeight, sectionId);
47
- const layout = (0, useCurrentLayout_1.useCurrentLayout)();
48
48
  const { width, height } = (0, useItemDimensions_1.useItemDimensions)(item, sectionId);
49
49
  const layoutValues = [item.area, item.hidden, item.state.hover];
50
50
  const isInitialRef = (0, react_1.useRef)(true);
@@ -53,12 +53,14 @@ const Item = ({ item, sectionId }) => {
53
53
  if (item.layoutParams) {
54
54
  layoutValues.push(item.layoutParams);
55
55
  }
56
- const sizing = isItemType(item, sdk_1.ArticleItemType.RichText)
56
+ const sizing = layout && isItemType(item, sdk_1.ArticleItemType.RichText)
57
57
  ? item.layoutParams[layout].sizing
58
58
  : undefined;
59
59
  const sizingAxis = parseSizing(sizing);
60
60
  const ItemComponent = itemsMap[item.type] || noop;
61
61
  const handleItemResize = (height) => {
62
+ if (!layout)
63
+ return;
62
64
  const sticky = item.sticky[layout];
63
65
  if (!sticky) {
64
66
  setWrapperHeight(undefined);
@@ -77,7 +79,7 @@ const Item = ({ item, sectionId }) => {
77
79
  height: `${sizingAxis.y === sdk_1.ArticleItemSizingType.Manual ? `${height * 100}vw` : 'unset'}`,
78
80
  top: stickyTop
79
81
  };
80
- return ((0, jsx_runtime_1.jsxs)("div", { className: `item-wrapper-${item.id}`, style: { top, left, ...(wrapperHeight ? { height: `${wrapperHeight * 100}vw` } : {}) }, children: [(0, jsx_runtime_1.jsx)("div", { suppressHydrationWarning: true, className: `item-${item.id}`, style: isInitialRef.current ? {} : styles, children: (0, jsx_runtime_1.jsx)(ItemComponent, { item: item, sectionId: sectionId, onResize: handleItemResize }) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
82
+ return ((0, jsx_runtime_1.jsxs)("div", { className: `item-wrapper-${item.id}`, style: isInitialRef.current ? {} : { top, left, ...(wrapperHeight ? { height: `${wrapperHeight * 100}vw` } : {}) }, children: [(0, jsx_runtime_1.jsx)("div", { suppressHydrationWarning: true, className: `item-${item.id}`, style: isInitialRef.current ? {} : styles, children: (0, jsx_runtime_1.jsx)(ItemComponent, { item: item, sectionId: sectionId, onResize: handleItemResize }) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
81
83
  ${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([area, hidden, hoverParams, sticky, sectionHeight, layoutParams]) => {
82
84
  const sizingAxis = parseSizing(layoutParams.sizing);
83
85
  return (`
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useEmbedVideoItem = void 0;
4
- const useCurrentLayout_1 = require("../../common/useCurrentLayout");
5
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
5
+ const useLayoutContext_1 = require("../useLayoutContext");
6
6
  const useEmbedVideoItem = (item, sectionId) => {
7
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
7
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
8
8
  const radius = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
9
9
  if (!layoutId)
10
10
  return 0;
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useFileItem = void 0;
4
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
5
- const useCurrentLayout_1 = require("../../common/useCurrentLayout");
5
+ const useLayoutContext_1 = require("../useLayoutContext");
6
6
  const defaultColor = 'rgba(0, 0, 0, 1)';
7
7
  const useFileItem = (item, sectionId) => {
8
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
8
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
9
9
  const radius = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
10
10
  if (!layoutId)
11
11
  return 0;
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useRectangleItem = void 0;
4
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
5
- const useCurrentLayout_1 = require("../../common/useCurrentLayout");
5
+ const useLayoutContext_1 = require("../useLayoutContext");
6
6
  const defaultColor = 'rgba(0, 0, 0, 1)';
7
7
  const useRectangleItem = (item, sectionId) => {
8
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
8
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
9
9
  const radius = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
10
10
  if (!layoutId)
11
11
  return 0;
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useRichTextItem = void 0;
4
- const useCurrentLayout_1 = require("../../common/useCurrentLayout");
5
4
  const RichTextConverter_1 = require("../../utils/RichTextConverter/RichTextConverter");
6
5
  const useCntrlContext_1 = require("../../provider/useCntrlContext");
6
+ const useLayoutContext_1 = require("../useLayoutContext");
7
7
  const richTextConv = new RichTextConverter_1.RichTextConverter();
8
8
  const useRichTextItem = (item) => {
9
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
9
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
10
10
  const { layouts, typePresets } = (0, useCntrlContext_1.useCntrlContext)();
11
- const presetId = item.layoutParams[layoutId].preset;
11
+ const presetId = layoutId ? item.layoutParams[layoutId].preset : null;
12
12
  const preset = presetId
13
13
  ? typePresets?.presets.find(p => p.id === presetId) ?? null
14
14
  : null;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useRichTextItemValues = void 0;
4
- const useCurrentLayout_1 = require("../../common/useCurrentLayout");
5
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
5
+ const useLayoutContext_1 = require("../useLayoutContext");
6
6
  const useRichTextItemValues = (item, sectionId) => {
7
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
7
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
8
8
  const angle = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => ({ angle: layoutId ? item.area[layoutId].angle : 0 }), (animator, scroll, value) => animator.getRotation(value, scroll), sectionId);
9
9
  const blur = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
10
10
  if (!layoutId)
@@ -1,14 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useStickyItemTop = void 0;
4
- const useCurrentLayout_1 = require("../../common/useCurrentLayout");
4
+ const sdk_1 = require("@cntrl-site/sdk");
5
5
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
6
6
  const getAnchoredItemTop_1 = require("../../utils/getAnchoredItemTop");
7
+ const useLayoutContext_1 = require("../useLayoutContext");
7
8
  function useStickyItemTop(item, sectionHeightMap, sectionId) {
8
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
9
- const { top } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => item.area[layoutId], (animator, scroll, value) => animator.getPositions(value, scroll), sectionId, [layoutId]);
10
- const sticky = item.sticky[layoutId];
11
- const sectionHeight = sectionHeightMap[layoutId];
12
- return sticky ? `${(0, getAnchoredItemTop_1.getAnchoredItemTop)(top - sticky.from, sectionHeight, item.area[layoutId].anchorSide)}` : 0;
9
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
10
+ const { top } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
11
+ if (!layoutId)
12
+ return { top: 0, left: 0 };
13
+ return item.area[layoutId];
14
+ }, (animator, scroll, value) => animator.getPositions(value, scroll), sectionId, [layoutId]);
15
+ const sticky = layoutId ? item.sticky[layoutId] : undefined;
16
+ const sectionHeight = layoutId ? sectionHeightMap[layoutId] : '0vh';
17
+ const anchorSide = layoutId ? item.area[layoutId].anchorSide : sdk_1.AnchorSide.Top;
18
+ return sticky ? `${(0, getAnchoredItemTop_1.getAnchoredItemTop)(top - sticky.from, sectionHeight, anchorSide)}` : 0;
13
19
  }
14
20
  exports.useStickyItemTop = useStickyItemTop;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useItemDimensions = void 0;
4
- const useCurrentLayout_1 = require("../common/useCurrentLayout");
5
4
  const useKeyframeValue_1 = require("../common/useKeyframeValue");
5
+ const useLayoutContext_1 = require("./useLayoutContext");
6
6
  const defaultArea = {
7
7
  left: 0,
8
8
  top: 0,
@@ -12,7 +12,7 @@ const defaultArea = {
12
12
  zIndex: 0
13
13
  };
14
14
  const useItemDimensions = (item, sectionId) => {
15
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
15
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
16
16
  const { width, height } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => layoutId ? item.area[layoutId] : defaultArea, (animator, scroll, value) => animator.getDimensions(value, scroll), sectionId, [layoutId]);
17
17
  return { width, height };
18
18
  };
@@ -1,14 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useItemPosition = void 0;
4
+ const sdk_1 = require("@cntrl-site/sdk");
4
5
  const useKeyframeValue_1 = require("../common/useKeyframeValue");
5
- const useCurrentLayout_1 = require("../common/useCurrentLayout");
6
6
  const getItemTopStyle_1 = require("../utils/getItemTopStyle");
7
+ const useLayoutContext_1 = require("./useLayoutContext");
7
8
  const useItemPosition = (item, sectionId) => {
8
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
9
- const { top, left } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => item.area[layoutId], (animator, scroll, value) => animator.getPositions(value, scroll), sectionId, [layoutId]);
9
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
10
+ const { top, left } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
11
+ if (!layoutId)
12
+ return { top: 0, left: -10 };
13
+ return item.area[layoutId];
14
+ }, (animator, scroll, value) => animator.getPositions(value, scroll), sectionId, [layoutId]);
15
+ const anchorSide = layoutId ? item.area[layoutId].anchorSide : sdk_1.AnchorSide.Top;
10
16
  return {
11
- top: (0, getItemTopStyle_1.getItemTopStyle)(top, item.area[layoutId].anchorSide),
17
+ top: (0, getItemTopStyle_1.getItemTopStyle)(top, anchorSide),
12
18
  left: `${left * 100}vw`
13
19
  };
14
20
  };
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useItemScale = void 0;
4
+ const sdk_1 = require("@cntrl-site/sdk");
4
5
  const useKeyframeValue_1 = require("../common/useKeyframeValue");
5
- const useCurrentLayout_1 = require("../common/useCurrentLayout");
6
+ const useLayoutContext_1 = require("./useLayoutContext");
6
7
  const useItemScale = (item, sectionId) => {
7
- const layout = (0, useCurrentLayout_1.useCurrentLayout)();
8
- const { scale } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => ({ scale: layoutId ? item.area[layoutId].scale : 1 }), (animator, scroll, value) => animator.getScale(value, scroll), sectionId);
9
- const scaleAnchor = item.area[layout].scaleAnchor;
8
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
9
+ const { scale } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => ({ scale: layoutId ? item.area[layoutId].scale : 1 }), (animator, scroll, value) => animator.getScale(value, scroll), sectionId, [layoutId]);
10
+ const scaleAnchor = layoutId ? item.area[layoutId].scaleAnchor : sdk_1.ScaleAnchor.MiddleCenter;
10
11
  return { scale, scaleAnchor };
11
12
  };
12
13
  exports.useItemScale = useItemScale;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useLayoutContext = void 0;
4
+ const react_1 = require("react");
5
+ const LayoutContext_1 = require("../provider/LayoutContext");
6
+ function useLayoutContext() {
7
+ const layoutId = (0, react_1.useContext)(LayoutContext_1.LayoutContext);
8
+ return layoutId;
9
+ }
10
+ exports.useLayoutContext = useLayoutContext;
@@ -1,17 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSectionColor = void 0;
4
- const useCurrentLayout_1 = require("../common/useCurrentLayout");
5
4
  const react_1 = require("react");
6
5
  const sdk_1 = require("@cntrl-site/sdk");
6
+ const useLayoutContext_1 = require("./useLayoutContext");
7
7
  const DEFAULT_COLOR = 'rgba(0, 0, 0, 0)';
8
8
  const useSectionColor = (colorData) => {
9
- const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
9
+ const layoutId = (0, useLayoutContext_1.useLayoutContext)();
10
10
  return (0, react_1.useMemo)(() => {
11
+ if (!layoutId)
12
+ return sdk_1.CntrlColor.parse(DEFAULT_COLOR);
11
13
  const layoutColor = colorData[layoutId];
12
14
  return sdk_1.CntrlColor.parse(!layoutColor
13
15
  ? DEFAULT_COLOR
14
16
  : layoutColor);
15
- }, []);
17
+ }, [layoutId]);
16
18
  };
17
19
  exports.useSectionColor = useSectionColor;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LayoutContext = void 0;
4
+ const react_1 = require("react");
5
+ exports.LayoutContext = (0, react_1.createContext)(undefined);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "0.14.3",
3
+ "version": "0.14.4",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -8,7 +8,7 @@ interface LayoutData {
8
8
  end: number;
9
9
  }
10
10
 
11
- export const useCurrentLayout = (): string => {
11
+ export const useCurrentLayout = (): string | undefined => {
12
12
  const { layouts } = useCntrlContext();
13
13
  const articleRectObserver = useContext(ArticleRectContext);
14
14
  const layoutRanges = useMemo(() => {
@@ -28,14 +28,7 @@ export const useCurrentLayout = (): string => {
28
28
  const getCurrentLayout = useCallback((articleWidth: number) => {
29
29
  return layoutRanges.find(l => articleWidth >= l.start && articleWidth < l.end)!.layoutId;
30
30
  }, [layoutRanges]);
31
- const [layoutId, setLayoutId] = useState<string>(getCurrentLayout(0));
32
-
33
- useEffect(() => {
34
- if (typeof window !== 'undefined') {
35
- getCurrentLayout(window.innerWidth);
36
- }
37
- }, []);
38
-
31
+ const [layoutId, setLayoutId] = useState<string | undefined>(undefined);
39
32
  useEffect(() => {
40
33
  if (!articleRectObserver) return;
41
34
  return articleRectObserver.on('resize', () => {
@@ -2,12 +2,12 @@ import { KeyframeType, TArticleItemAny } from '@cntrl-site/sdk';
2
2
  import isEqual from 'lodash.isequal';
3
3
  import { DependencyList, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
4
4
  import { ArticleRectContext } from '../provider/ArticleRectContext';
5
- import { useCurrentLayout } from './useCurrentLayout';
6
5
  import { KeyframesContext } from '../provider/KeyframesContext';
7
6
  import { AnimationData, Animator } from '../utils/Animator/Animator';
7
+ import { useLayoutContext } from '../components/useLayoutContext';
8
8
 
9
9
  export type AnimatorGetter<T> = (animator: Animator, scroll: number, value: T) => T;
10
- type ItemParamGetter<T> = (item: TArticleItemAny, layoutId: string) => T;
10
+ type ItemParamGetter<T> = (item: TArticleItemAny, layoutId: string | undefined) => T;
11
11
  const emptyDeps: DependencyList = [];
12
12
 
13
13
  export const useKeyframeValue = <T>(
@@ -24,7 +24,7 @@ export const useKeyframeValue = <T>(
24
24
  itemParamsGetterRef.current = itemParamsGetter;
25
25
 
26
26
  const articleRectObserver = useContext(ArticleRectContext);
27
- const layoutId = useCurrentLayout();
27
+ const layoutId = useLayoutContext();
28
28
  const keyframesRepo = useContext(KeyframesContext);
29
29
  const keyframes = useMemo(() => keyframesRepo.getItemKeyframes(item.id), [item.id, keyframesRepo]);
30
30
  const paramValue = useMemo<T>(() => {
@@ -5,6 +5,7 @@ import { Section } from './Section';
5
5
  import { Item } from './Item';
6
6
  import { useArticleRectObserver } from '../utils/ArticleRectManager/useArticleRectObserver';
7
7
  import { ArticleRectContext } from '../provider/ArticleRectContext';
8
+ import { ArticleWrapper } from './ArticleWrapper';
8
9
 
9
10
  interface Props {
10
11
  article: TArticle;
@@ -18,18 +19,20 @@ export const Article: FC<Props> = ({ article, sectionData }) => {
18
19
 
19
20
  return (
20
21
  <ArticleRectContext.Provider value={articleRectObserver}>
21
- <div className="article" ref={articleRef}>
22
- {article.sections.map((section, i) => {
23
- const data = section.name ? sectionData[section.name] : {};
24
- return (
25
- <Section section={section} key={section.id} data={data}>
26
- {article.sections[i].items.map(item => (
27
- <Item item={item} key={item.id} sectionId={section.id} />
28
- ))}
29
- </Section>
30
- );
31
- })}
32
- </div>
22
+ <ArticleWrapper>
23
+ <div className="article" ref={articleRef}>
24
+ {article.sections.map((section, i) => {
25
+ const data = section.name ? sectionData[section.name] : {};
26
+ return (
27
+ <Section section={section} key={section.id} data={data}>
28
+ {article.sections[i].items.map(item => (
29
+ <Item item={item} key={item.id} sectionId={section.id} />
30
+ ))}
31
+ </Section>
32
+ );
33
+ })}
34
+ </div>
35
+ </ArticleWrapper>
33
36
  <JSXStyle id={id}>{`
34
37
  .article {
35
38
  position: relative;
@@ -0,0 +1,36 @@
1
+ import React, { FC, PropsWithChildren, useEffect, useId, useState } from 'react';
2
+ import JSXStyle from 'styled-jsx/style';
3
+ import { useCurrentLayout } from '../common/useCurrentLayout';
4
+ import { LayoutContext } from '../provider/LayoutContext';
5
+
6
+ export const ArticleWrapper: FC<PropsWithChildren<{}>> = ({ children }) => {
7
+ const id = useId();
8
+ const [isPageLoaded, setIsPageLoaded] = useState(false);
9
+ const layoutId = useCurrentLayout();
10
+
11
+ useEffect(() => {
12
+ const onPageLoad = () => {
13
+ setIsPageLoaded(true);
14
+ };
15
+ if (document.readyState === 'complete') {
16
+ onPageLoad();
17
+ } else {
18
+ window.addEventListener('load', onPageLoad);
19
+ return () => window.removeEventListener('load', onPageLoad);
20
+ }
21
+ }, []);
22
+
23
+ return (
24
+ <LayoutContext.Provider value={layoutId}>
25
+ <div className="article-wrapper" style={{ opacity: layoutId && isPageLoaded ? 1 : 0 }}>
26
+ {children}
27
+ </div>
28
+ <JSXStyle id={id}>{`
29
+ .article-wrapper {
30
+ opacity: 1;
31
+ transition: opacity 0.2s ease;
32
+ }
33
+ `}</JSXStyle>
34
+ </LayoutContext.Provider>
35
+ );
36
+ };
@@ -20,7 +20,6 @@ import { CustomItem } from './items/CustomItem';
20
20
  import { useCntrlContext } from '../provider/useCntrlContext';
21
21
  import { useItemPosition } from './useItemPosition';
22
22
  import { useItemDimensions } from './useItemDimensions';
23
- import { useCurrentLayout } from '../common/useCurrentLayout';
24
23
  import { useItemScale } from './useItemScale';
25
24
  import { ScaleAnchorMap } from '../utils/ScaleAnchorMap';
26
25
  import { useSectionHeightData } from './useSectionHeightMap';
@@ -28,6 +27,7 @@ import { getHoverStyles, getTransitions } from '../utils/HoverStyles/HoverStyles
28
27
  import { getItemTopStyle } from '../utils/getItemTopStyle';
29
28
  import { useStickyItemTop } from './items/useStickyItemTop';
30
29
  import { getAnchoredItemTop } from '../utils/getAnchoredItemTop';
30
+ import { useLayoutContext } from './useLayoutContext';
31
31
 
32
32
  export interface ItemProps<I extends TArticleItemAny> {
33
33
  item: I;
@@ -50,12 +50,12 @@ const noop = () => null;
50
50
  export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
51
51
  const id = useId();
52
52
  const { layouts } = useCntrlContext();
53
+ const layout = useLayoutContext();
53
54
  const [wrapperHeight, setWrapperHeight] = useState<undefined | number>(undefined);
54
55
  const { scale, scaleAnchor } = useItemScale(item, sectionId);
55
56
  const { top, left } = useItemPosition(item, sectionId);
56
57
  const sectionHeight = useSectionHeightData(sectionId);
57
58
  const stickyTop = useStickyItemTop(item, sectionHeight, sectionId);
58
- const layout = useCurrentLayout();
59
59
  const { width, height } = useItemDimensions(item, sectionId);
60
60
  const layoutValues: Record<string, any>[] = [item.area, item.hidden, item.state.hover];
61
61
  const isInitialRef = useRef(true);
@@ -65,13 +65,14 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
65
65
  layoutValues.push(item.layoutParams);
66
66
  }
67
67
 
68
- const sizing = isItemType(item, ArticleItemType.RichText)
68
+ const sizing = layout && isItemType(item, ArticleItemType.RichText)
69
69
  ? item.layoutParams[layout].sizing
70
70
  : undefined;
71
71
  const sizingAxis = parseSizing(sizing);
72
72
  const ItemComponent = itemsMap[item.type] || noop;
73
73
 
74
74
  const handleItemResize = (height: number) => {
75
+ if (!layout) return;
75
76
  const sticky = item.sticky[layout];
76
77
  if (!sticky) {
77
78
  setWrapperHeight(undefined);
@@ -96,7 +97,7 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
96
97
  return (
97
98
  <div
98
99
  className={`item-wrapper-${item.id}`}
99
- style={{ top, left, ...(wrapperHeight ? { height: `${wrapperHeight * 100}vw` } : {}) }}
100
+ style={isInitialRef.current ? {} : { top, left, ...(wrapperHeight ? { height: `${wrapperHeight * 100}vw` } : {}) }}
100
101
  >
101
102
  <div
102
103
  suppressHydrationWarning={true}
@@ -1,9 +1,9 @@
1
1
  import { TVimeoEmbedItem, TYoutubeEmbedItem } from '@cntrl-site/core';
2
- import { useCurrentLayout } from '../../common/useCurrentLayout';
3
2
  import { useKeyframeValue } from '../../common/useKeyframeValue';
3
+ import { useLayoutContext } from '../useLayoutContext';
4
4
 
5
5
  export const useEmbedVideoItem = (item: TVimeoEmbedItem | TYoutubeEmbedItem, sectionId: string) => {
6
- const layoutId = useCurrentLayout();
6
+ const layoutId = useLayoutContext();
7
7
  const radius = useKeyframeValue(
8
8
  item,
9
9
  (item, layoutId) => {
@@ -1,11 +1,11 @@
1
1
  import { TImageItem, TVideoItem } from '@cntrl-site/sdk';
2
2
  import { useKeyframeValue } from '../../common/useKeyframeValue';
3
- import { useCurrentLayout } from '../../common/useCurrentLayout';
3
+ import { useLayoutContext } from '../useLayoutContext';
4
4
 
5
5
  const defaultColor = 'rgba(0, 0, 0, 1)';
6
6
 
7
7
  export const useFileItem = (item: TImageItem | TVideoItem, sectionId: string) => {
8
- const layoutId = useCurrentLayout();
8
+ const layoutId = useLayoutContext();
9
9
  const radius = useKeyframeValue(
10
10
  item,
11
11
  (item, layoutId) => {
@@ -1,11 +1,11 @@
1
1
  import { TRectangleItem } from '@cntrl-site/sdk';
2
2
  import { useKeyframeValue } from '../../common/useKeyframeValue';
3
- import { useCurrentLayout } from '../../common/useCurrentLayout';
3
+ import { useLayoutContext } from '../useLayoutContext';
4
4
 
5
5
  const defaultColor = 'rgba(0, 0, 0, 1)';
6
6
 
7
7
  export const useRectangleItem = (item: TRectangleItem, sectionId: string) => {
8
- const layoutId = useCurrentLayout();
8
+ const layoutId = useLayoutContext();
9
9
  const radius = useKeyframeValue(
10
10
  item,
11
11
  (item, layoutId) => {
@@ -1,15 +1,15 @@
1
1
  import { TRichTextItem, TTypePresetEntry } from '@cntrl-site/sdk';
2
- import { useCurrentLayout } from '../../common/useCurrentLayout';
3
2
  import { RichTextConverter } from '../../utils/RichTextConverter/RichTextConverter';
4
3
  import { useCntrlContext } from '../../provider/useCntrlContext';
5
4
  import { ReactNode } from 'react';
5
+ import { useLayoutContext } from '../useLayoutContext';
6
6
 
7
7
  const richTextConv = new RichTextConverter();
8
8
 
9
9
  export const useRichTextItem = (item: TRichTextItem): [ReactNode[], string, TTypePresetEntry | null] => {
10
- const layoutId = useCurrentLayout();
10
+ const layoutId = useLayoutContext();
11
11
  const { layouts, typePresets } = useCntrlContext();
12
- const presetId = item.layoutParams[layoutId].preset;
12
+ const presetId = layoutId ? item.layoutParams[layoutId].preset : null;
13
13
  const preset = presetId
14
14
  ? typePresets?.presets.find(p => p.id === presetId) ?? null
15
15
  : null;
@@ -1,10 +1,9 @@
1
- import { useCurrentLayout } from '../../common/useCurrentLayout';
2
1
  import { useKeyframeValue } from '../../common/useKeyframeValue';
3
2
  import { TRichTextItem } from '@cntrl-site/sdk';
3
+ import { useLayoutContext } from '../useLayoutContext';
4
4
 
5
5
  export const useRichTextItemValues = (item: TRichTextItem, sectionId: string) => {
6
- const layoutId = useCurrentLayout();
7
-
6
+ const layoutId = useLayoutContext();
8
7
  const angle = useKeyframeValue(
9
8
  item,
10
9
  (item, layoutId) => ({ angle: layoutId ? item.area[layoutId].angle : 0 }),
@@ -1,18 +1,22 @@
1
- import { TArticleItemAny } from '@cntrl-site/sdk';
2
- import { useCurrentLayout } from '../../common/useCurrentLayout';
1
+ import { AnchorSide, TArticleItemAny } from '@cntrl-site/sdk';
3
2
  import { useKeyframeValue } from '../../common/useKeyframeValue';
4
3
  import { getAnchoredItemTop } from '../../utils/getAnchoredItemTop';
4
+ import { useLayoutContext } from '../useLayoutContext';
5
5
 
6
6
  export function useStickyItemTop(item: TArticleItemAny, sectionHeightMap: Record<string, string>, sectionId: string) {
7
- const layoutId = useCurrentLayout();
7
+ const layoutId = useLayoutContext();
8
8
  const { top } = useKeyframeValue<{ top: number; left: number }>(
9
9
  item,
10
- (item, layoutId) => item.area[layoutId],
10
+ (item, layoutId) => {
11
+ if (!layoutId) return { top: 0, left: 0 }
12
+ return item.area[layoutId];
13
+ },
11
14
  (animator, scroll, value) => animator.getPositions(value, scroll),
12
15
  sectionId,
13
16
  [layoutId]
14
17
  );
15
- const sticky = item.sticky[layoutId];
16
- const sectionHeight = sectionHeightMap[layoutId];
17
- return sticky ? `${getAnchoredItemTop(top - sticky.from, sectionHeight, item.area[layoutId].anchorSide)}` : 0
18
+ const sticky = layoutId ? item.sticky[layoutId] : undefined;
19
+ const sectionHeight = layoutId ? sectionHeightMap[layoutId] : '0vh';
20
+ const anchorSide = layoutId ? item.area[layoutId].anchorSide : AnchorSide.Top;
21
+ return sticky ? `${getAnchoredItemTop(top - sticky.from, sectionHeight, anchorSide)}` : 0
18
22
  }
@@ -1,6 +1,6 @@
1
1
  import { TArticleItemAny } from '@cntrl-site/sdk';
2
- import { useCurrentLayout } from '../common/useCurrentLayout';
3
2
  import { useKeyframeValue } from '../common/useKeyframeValue';
3
+ import { useLayoutContext } from './useLayoutContext';
4
4
 
5
5
  const defaultArea = {
6
6
  left: 0,
@@ -12,7 +12,7 @@ const defaultArea = {
12
12
  };
13
13
 
14
14
  export const useItemDimensions = (item: TArticleItemAny, sectionId: string) => {
15
- const layoutId = useCurrentLayout();
15
+ const layoutId = useLayoutContext();
16
16
  const { width, height } = useKeyframeValue<{ width: number; height: number }>(
17
17
  item,
18
18
  (item, layoutId) => layoutId ? item.area[layoutId] : defaultArea,
@@ -1,19 +1,23 @@
1
1
  import { AnchorSide, TArticleItemAny } from '@cntrl-site/sdk';
2
2
  import { useKeyframeValue } from '../common/useKeyframeValue';
3
- import { useCurrentLayout } from '../common/useCurrentLayout';
4
3
  import { getItemTopStyle } from '../utils/getItemTopStyle';
4
+ import { useLayoutContext } from './useLayoutContext';
5
5
 
6
6
  export const useItemPosition = (item: TArticleItemAny, sectionId: string) => {
7
- const layoutId = useCurrentLayout();
7
+ const layoutId = useLayoutContext();
8
8
  const { top, left } = useKeyframeValue<{ top: number; left: number }>(
9
9
  item,
10
- (item, layoutId) => item.area[layoutId],
10
+ (item, layoutId) => {
11
+ if (!layoutId) return { top: 0, left: -10 };
12
+ return item.area[layoutId]
13
+ },
11
14
  (animator, scroll, value) => animator.getPositions(value, scroll),
12
15
  sectionId,
13
16
  [layoutId]
14
17
  );
18
+ const anchorSide = layoutId ? item.area[layoutId].anchorSide : AnchorSide.Top;
15
19
  return {
16
- top: getItemTopStyle(top, item.area[layoutId].anchorSide),
20
+ top: getItemTopStyle(top, anchorSide),
17
21
  left: `${left * 100}vw`
18
22
  };
19
23
  };
@@ -1,16 +1,17 @@
1
- import { TArticleItemAny } from '@cntrl-site/sdk';
1
+ import { ScaleAnchor, TArticleItemAny } from '@cntrl-site/sdk';
2
2
  import { useKeyframeValue } from '../common/useKeyframeValue';
3
- import { useCurrentLayout } from '../common/useCurrentLayout';
3
+ import { useLayoutContext } from './useLayoutContext';
4
4
 
5
5
  export const useItemScale = (item: TArticleItemAny, sectionId: string) => {
6
- const layout = useCurrentLayout();
6
+ const layoutId = useLayoutContext();
7
7
  const { scale } = useKeyframeValue(
8
8
  item,
9
9
  (item, layoutId) => ({ scale: layoutId ? item.area[layoutId].scale : 1 }),
10
10
  (animator, scroll, value) => animator.getScale(value, scroll),
11
- sectionId
11
+ sectionId,
12
+ [layoutId]
12
13
  );
13
- const scaleAnchor = item.area[layout].scaleAnchor;
14
+ const scaleAnchor = layoutId ? item.area[layoutId].scaleAnchor : ScaleAnchor.MiddleCenter;
14
15
 
15
16
  return { scale, scaleAnchor };
16
17
  };
@@ -0,0 +1,7 @@
1
+ import { useContext } from 'react';
2
+ import { LayoutContext } from '../provider/LayoutContext';
3
+
4
+ export function useLayoutContext() {
5
+ const layoutId = useContext(LayoutContext);
6
+ return layoutId;
7
+ }
@@ -1,18 +1,19 @@
1
- import { useCurrentLayout } from '../common/useCurrentLayout';
2
1
  import { useMemo } from 'react';
3
2
  import { CntrlColor } from '@cntrl-site/sdk';
3
+ import { useLayoutContext } from './useLayoutContext';
4
4
 
5
5
  type LayoutIdentifier = string;
6
6
  const DEFAULT_COLOR = 'rgba(0, 0, 0, 0)';
7
7
 
8
8
  export const useSectionColor = (colorData: Record<LayoutIdentifier, string | null>): CntrlColor => {
9
- const layoutId = useCurrentLayout();
9
+ const layoutId = useLayoutContext();
10
10
  return useMemo(() => {
11
+ if (!layoutId) return CntrlColor.parse(DEFAULT_COLOR);
11
12
  const layoutColor = colorData[layoutId];
12
13
  return CntrlColor.parse(
13
14
  !layoutColor
14
15
  ? DEFAULT_COLOR
15
16
  : layoutColor
16
17
  );
17
- }, []);
18
+ }, [layoutId]);
18
19
  };
@@ -0,0 +1,3 @@
1
+ import { createContext } from 'react';
2
+
3
+ export const LayoutContext = createContext<string | undefined>(undefined);
@@ -1,5 +0,0 @@
1
- <component name="ProjectCodeStyleConfiguration">
2
- <state>
3
- <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
4
- </state>
5
- </component>