@cntrl-site/sdk-nextjs 0.11.1 → 0.12.1

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 (49) hide show
  1. package/lib/common/useKeyframeValue.js +10 -10
  2. package/lib/components/Article.js +1 -1
  3. package/lib/components/Item.js +6 -15
  4. package/lib/components/Section.js +4 -1
  5. package/lib/components/items/ImageItem.js +3 -3
  6. package/lib/components/items/RectangleItem.js +25 -13
  7. package/lib/components/items/RichTextItem.js +2 -2
  8. package/lib/components/items/VideoItem.js +3 -3
  9. package/lib/components/items/VimeoEmbed.js +3 -3
  10. package/lib/components/items/YoutubeEmbed.js +3 -3
  11. package/lib/components/items/useEmbedVideoItem.js +2 -2
  12. package/lib/components/items/useFileItem.js +5 -5
  13. package/lib/components/items/useItemSticky.js +5 -5
  14. package/lib/components/items/useRectangleItem.js +5 -5
  15. package/lib/components/useItemAngle.js +2 -2
  16. package/lib/components/useItemDimensions.js +2 -2
  17. package/lib/components/useItemPosition.js +2 -2
  18. package/lib/components/useItemScale.js +2 -2
  19. package/lib/utils/ArticleRectManager/ArticleRectObserver.js +36 -18
  20. package/lib/utils/ArticleRectManager/useArticleRectObserver.js +1 -1
  21. package/lib/utils/ArticleRectManager/useSectionRegistry.js +14 -0
  22. package/lib/utils/StickyManager/StickyManager.js +2 -2
  23. package/package.json +2 -2
  24. package/src/common/useKeyframeValue.ts +10 -9
  25. package/src/components/Article.tsx +1 -1
  26. package/src/components/Item.tsx +8 -17
  27. package/src/components/Section.tsx +5 -1
  28. package/src/components/items/ImageItem.tsx +3 -3
  29. package/src/components/items/RectangleItem.tsx +26 -14
  30. package/src/components/items/RichTextItem.tsx +2 -2
  31. package/src/components/items/VideoItem.tsx +3 -3
  32. package/src/components/items/VimeoEmbed.tsx +3 -3
  33. package/src/components/items/YoutubeEmbed.tsx +3 -3
  34. package/src/components/items/useEmbedVideoItem.ts +2 -1
  35. package/src/components/items/useFileItem.ts +7 -3
  36. package/src/components/items/useItemSticky.ts +5 -6
  37. package/src/components/items/useRectangleItem.ts +6 -2
  38. package/src/components/useItemAngle.ts +3 -2
  39. package/src/components/useItemDimensions.ts +2 -1
  40. package/src/components/useItemPosition.ts +2 -1
  41. package/src/components/useItemScale.ts +3 -2
  42. package/src/utils/ArticleRectManager/ArticleRectObserver.ts +32 -15
  43. package/src/utils/ArticleRectManager/useArticleRectObserver.ts +1 -1
  44. package/src/utils/ArticleRectManager/useSectionRegistry.ts +11 -0
  45. package/src/utils/StickyManager/StickyManager.ts +2 -2
  46. package/.idea/inspectionProfiles/Project_Default.xml +0 -15
  47. package/cntrl-site-sdk-nextjs-0.10.2.tgz +0 -0
  48. package/cntrl-site-sdk-nextjs-0.11.1.tgz +0 -0
  49. package/lib/utils/castObject.js +0 -10
@@ -11,7 +11,7 @@ const useCurrentLayout_1 = require("./useCurrentLayout");
11
11
  const KeyframesContext_1 = require("../provider/KeyframesContext");
12
12
  const Animator_1 = require("../utils/Animator/Animator");
13
13
  const emptyDeps = [];
14
- const useKeyframeValue = (item, itemParamsGetter, animatorGetter, deps = emptyDeps) => {
14
+ const useKeyframeValue = (item, itemParamsGetter, animatorGetter, sectionId, deps = emptyDeps) => {
15
15
  const animatorGetterRef = (0, react_1.useRef)(animatorGetter);
16
16
  const itemParamsGetterRef = (0, react_1.useRef)(itemParamsGetter);
17
17
  animatorGetterRef.current = animatorGetter;
@@ -50,27 +50,27 @@ const useKeyframeValue = (item, itemParamsGetter, animatorGetter, deps = emptyDe
50
50
  setAdjustedValue(paramValue);
51
51
  }, [paramValue]);
52
52
  (0, react_1.useEffect)(() => {
53
- if (!articleRectObserver)
53
+ if (!articleRectObserver || !animator)
54
54
  return;
55
- const scroll = articleRectObserver.scroll;
55
+ const scroll = articleRectObserver.getSectionScroll(sectionId);
56
56
  handleKeyframeValue(scroll);
57
- }, [articleRectObserver, handleKeyframeValue]);
57
+ }, [articleRectObserver, handleKeyframeValue, animator]);
58
58
  (0, react_1.useEffect)(() => {
59
- if (!articleRectObserver)
59
+ if (!articleRectObserver || !animator)
60
60
  return;
61
61
  return articleRectObserver.on('resize', () => {
62
- const scroll = articleRectObserver.scroll;
62
+ const scroll = articleRectObserver.getSectionScroll(sectionId);
63
63
  handleKeyframeValue(scroll);
64
64
  });
65
- }, [handleKeyframeValue, articleRectObserver]);
65
+ }, [handleKeyframeValue, articleRectObserver, animator]);
66
66
  (0, react_1.useEffect)(() => {
67
- if (!articleRectObserver)
67
+ if (!articleRectObserver || !animator)
68
68
  return;
69
69
  return articleRectObserver.on('scroll', () => {
70
- const scroll = articleRectObserver.scroll;
70
+ const scroll = articleRectObserver.getSectionScroll(sectionId);
71
71
  handleKeyframeValue(scroll);
72
72
  });
73
- }, [handleKeyframeValue, articleRectObserver]);
73
+ }, [handleKeyframeValue, articleRectObserver, animator]);
74
74
  return adjustedValue;
75
75
  };
76
76
  exports.useKeyframeValue = useKeyframeValue;
@@ -17,7 +17,7 @@ const Article = ({ article, sectionData }) => {
17
17
  const id = (0, react_1.useId)();
18
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
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 }, item.id))) }, section.id));
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
21
  }) }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
22
22
  .article {
23
23
  position: relative;
@@ -19,7 +19,6 @@ const useCntrlContext_1 = require("../provider/useCntrlContext");
19
19
  const useItemPosition_1 = require("./useItemPosition");
20
20
  const useItemDimensions_1 = require("./useItemDimensions");
21
21
  const useItemSticky_1 = require("./items/useItemSticky");
22
- const castObject_1 = require("../utils/castObject");
23
22
  const useCurrentLayout_1 = require("../common/useCurrentLayout");
24
23
  const useItemScale_1 = require("./useItemScale");
25
24
  const ScaleAnchorMap_1 = require("../utils/ScaleAnchorMap");
@@ -33,16 +32,14 @@ const itemsMap = {
33
32
  [sdk_1.ArticleItemType.Custom]: CustomItem_1.CustomItem
34
33
  };
35
34
  const noop = () => null;
36
- const Item = ({ item }) => {
35
+ const Item = ({ item, sectionId }) => {
37
36
  const id = (0, react_1.useId)();
38
37
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
39
- const { scale, scaleAnchor } = (0, useItemScale_1.useItemScale)(item);
40
- const position = (0, useItemPosition_1.useItemPosition)(item);
38
+ const { scale, scaleAnchor } = (0, useItemScale_1.useItemScale)(item, sectionId);
39
+ const position = (0, useItemPosition_1.useItemPosition)(item, sectionId);
41
40
  const layout = (0, useCurrentLayout_1.useCurrentLayout)();
42
- const [ref, setRef] = (0, react_1.useState)(null);
43
- const [parentOffsetTop, setParentOffsetTop] = (0, react_1.useState)(0);
44
- const { top, isFixed } = (0, useItemSticky_1.useItemSticky)(position.top, parentOffsetTop, item);
45
- const { width, height } = (0, useItemDimensions_1.useItemDimensions)(item);
41
+ const { top, isFixed } = (0, useItemSticky_1.useItemSticky)(position.top, item, sectionId);
42
+ const { width, height } = (0, useItemDimensions_1.useItemDimensions)(item, sectionId);
46
43
  const layoutValues = [item.area];
47
44
  const isInitialRef = (0, react_1.useRef)(true);
48
45
  if (item.layoutParams) {
@@ -56,12 +53,6 @@ const Item = ({ item }) => {
56
53
  (0, react_1.useEffect)(() => {
57
54
  isInitialRef.current = false;
58
55
  }, []);
59
- (0, react_1.useEffect)(() => {
60
- if (!ref)
61
- return;
62
- const offsetParent = (0, castObject_1.castObject)(ref.offsetParent, HTMLElement);
63
- setParentOffsetTop(offsetParent.offsetTop / window.innerWidth);
64
- }, [ref]);
65
56
  const styles = {
66
57
  transform: `scale(${scale})`,
67
58
  transformOrigin: ScaleAnchorMap_1.ScaleAnchorMap[scaleAnchor],
@@ -70,7 +61,7 @@ const Item = ({ item }) => {
70
61
  height: `${sizingAxis.y === sdk_1.ArticleItemSizingType.Manual ? `${height * 100}vw` : 'unset'}`,
71
62
  top
72
63
  };
73
- return ((0, jsx_runtime_1.jsxs)("div", { suppressHydrationWarning: true, className: `item-${item.id}`, ref: setRef, style: isInitialRef.current ? {} : { ...styles, position: isFixed ? 'fixed' : 'absolute' }, children: [(0, jsx_runtime_1.jsx)(ItemComponent, { item: item }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
64
+ return ((0, jsx_runtime_1.jsxs)("div", { suppressHydrationWarning: true, className: `item-${item.id}`, style: isInitialRef.current ? {} : { ...styles, position: isFixed ? 'fixed' : 'absolute' }, children: [(0, jsx_runtime_1.jsx)(ItemComponent, { item: item, sectionId: sectionId }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
74
65
  ${(0, sdk_1.getLayoutStyles)(layouts, layoutValues, ([area, layoutParams]) => {
75
66
  const sizingAxis = parseSizing(layoutParams.sizing);
76
67
  return (`
@@ -10,11 +10,14 @@ const style_1 = __importDefault(require("styled-jsx/style"));
10
10
  const sdk_1 = require("@cntrl-site/sdk");
11
11
  const useCntrlContext_1 = require("../provider/useCntrlContext");
12
12
  const useSectionColor_1 = require("./useSectionColor");
13
+ const useSectionRegistry_1 = require("../utils/ArticleRectManager/useSectionRegistry");
13
14
  const Section = ({ section, data, children }) => {
14
15
  const id = (0, react_1.useId)();
16
+ const sectionRef = (0, react_1.useRef)(null);
15
17
  const { layouts, customSections } = (0, useCntrlContext_1.useCntrlContext)();
16
18
  const backgroundColor = (0, useSectionColor_1.useSectionColor)(section.color);
17
19
  const SectionComponent = section.name ? customSections.getComponent(section.name) : undefined;
20
+ (0, useSectionRegistry_1.useSectionRegistry)(section.id, sectionRef.current);
18
21
  const getSectionVisibilityStyles = () => {
19
22
  return layouts
20
23
  .sort((a, b) => a.startsWith - b.startsWith)
@@ -33,7 +36,7 @@ const Section = ({ section, data, children }) => {
33
36
  return (0, jsx_runtime_1.jsx)(SectionComponent, { data: data, children: children });
34
37
  return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `section-${section.id}`, id: section.name, style: {
35
38
  backgroundColor: backgroundColor
36
- }, children: children }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
39
+ }, ref: sectionRef, children: children }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
37
40
  ${(0, sdk_1.getLayoutStyles)(layouts, [section.height], ([height]) => (`
38
41
  .section-${section.id} {
39
42
  height: ${getSectionHeight(height)};
@@ -11,10 +11,10 @@ const sdk_1 = require("@cntrl-site/sdk");
11
11
  const LinkWrapper_1 = require("../LinkWrapper");
12
12
  const useFileItem_1 = require("./useFileItem");
13
13
  const useItemAngle_1 = require("../useItemAngle");
14
- const ImageItem = ({ item }) => {
14
+ const ImageItem = ({ item, sectionId }) => {
15
15
  const id = (0, react_1.useId)();
16
- const { radius, strokeWidth, opacity, strokeColor } = (0, useFileItem_1.useFileItem)(item);
17
- const angle = (0, useItemAngle_1.useItemAngle)(item);
16
+ const { radius, strokeWidth, opacity, strokeColor } = (0, useFileItem_1.useFileItem)(item, sectionId);
17
+ const angle = (0, useItemAngle_1.useItemAngle)(item, sectionId);
18
18
  const borderColor = (0, react_1.useMemo)(() => sdk_1.CntrlColor.parse(strokeColor).toCss(), [strokeColor]);
19
19
  return ((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, { url: item.link?.url, children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `image-wrapper-${item.id}`, style: {
20
20
  borderRadius: `${radius * 100}vw`,
@@ -11,26 +11,38 @@ const sdk_1 = require("@cntrl-site/sdk");
11
11
  const LinkWrapper_1 = require("../LinkWrapper");
12
12
  const useRectangleItem_1 = require("./useRectangleItem");
13
13
  const useItemAngle_1 = require("../useItemAngle");
14
- const RectangleItem = ({ item }) => {
14
+ const RectangleItem = ({ item, sectionId }) => {
15
15
  const id = (0, react_1.useId)();
16
- const { fillColor, radius, strokeWidth, strokeColor } = (0, useRectangleItem_1.useRectangleItem)(item);
17
- const angle = (0, useItemAngle_1.useItemAngle)(item);
18
- const backgroundColor = (0, react_1.useMemo)(() => sdk_1.CntrlColor.parse(fillColor).toCss(), [fillColor]);
19
- const borderColor = (0, react_1.useMemo)(() => sdk_1.CntrlColor.parse(strokeColor).toCss(), [strokeColor]);
16
+ const { fillColor, radius, strokeWidth, strokeColor } = (0, useRectangleItem_1.useRectangleItem)(item, sectionId);
17
+ const angle = (0, useItemAngle_1.useItemAngle)(item, sectionId);
18
+ const backgroundColor = (0, react_1.useMemo)(() => sdk_1.CntrlColor.parse(fillColor), [fillColor]);
19
+ const borderColor = (0, react_1.useMemo)(() => sdk_1.CntrlColor.parse(strokeColor), [strokeColor]);
20
20
  return ((0, jsx_runtime_1.jsx)(LinkWrapper_1.LinkWrapper, { url: item.link?.url, children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: `rectangle-${item.id}`, style: {
21
- backgroundColor: `${backgroundColor}`,
21
+ backgroundColor: `${backgroundColor.toCss()}`,
22
22
  borderRadius: `${radius * 100}vw`,
23
23
  borderWidth: `${strokeWidth * 100}vw`,
24
- borderColor: `${borderColor}`,
24
+ borderColor: `${borderColor.toCss()}`,
25
25
  transform: `rotate(${angle}deg)`
26
26
  } }), (0, jsx_runtime_1.jsx)(style_1.default, { id: id, children: `
27
- .rectangle-${item.id} {
28
- position: absolute;
29
- width: 100%;
30
- height: 100%;
31
- border-style: solid;
32
- box-sizing: border-box;
27
+ @supports (color: oklch(42% 0.3 90 / 1)) {
28
+ .rectangle-${item.id} {
29
+ background-color: ${backgroundColor.fmt('oklch')};
30
+ border-color: ${borderColor.fmt('oklch')};
33
31
  }
32
+ }
33
+ @supports not (color: oklch(42% 0.3 90 / 1)) {
34
+ .rectangle-${item.id} {
35
+ background-color: ${backgroundColor.fmt('rgba')};
36
+ border-color: ${borderColor.fmt('rgba')};
37
+ }
38
+ }
39
+ .rectangle-${item.id} {
40
+ position: absolute;
41
+ width: 100%;
42
+ height: 100%;
43
+ border-style: solid;
44
+ box-sizing: border-box;
45
+ }
34
46
  ` })] }) }));
35
47
  };
36
48
  exports.RectangleItem = RectangleItem;
@@ -8,9 +8,9 @@ const jsx_runtime_1 = require("react/jsx-runtime");
8
8
  const style_1 = __importDefault(require("styled-jsx/style"));
9
9
  const useRichTextItem_1 = require("./useRichTextItem");
10
10
  const useItemAngle_1 = require("../useItemAngle");
11
- const RichTextItem = ({ item }) => {
11
+ const RichTextItem = ({ item, sectionId }) => {
12
12
  const [content, styles, preset] = (0, useRichTextItem_1.useRichTextItem)(item);
13
- const angle = (0, useItemAngle_1.useItemAngle)(item);
13
+ const angle = (0, useItemAngle_1.useItemAngle)(item, sectionId);
14
14
  const className = preset ? `cntrl-preset-${preset.id}` : undefined;
15
15
  return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: className, style: {
16
16
  transform: `rotate(${angle}deg)`
@@ -11,10 +11,10 @@ const sdk_1 = require("@cntrl-site/sdk");
11
11
  const LinkWrapper_1 = require("../LinkWrapper");
12
12
  const useFileItem_1 = require("./useFileItem");
13
13
  const useItemAngle_1 = require("../useItemAngle");
14
- const VideoItem = ({ item }) => {
14
+ const VideoItem = ({ item, sectionId }) => {
15
15
  const id = (0, react_1.useId)();
16
- const { radius, strokeWidth, strokeColor, opacity } = (0, useFileItem_1.useFileItem)(item);
17
- const angle = (0, useItemAngle_1.useItemAngle)(item);
16
+ const { radius, strokeWidth, strokeColor, opacity } = (0, useFileItem_1.useFileItem)(item, sectionId);
17
+ const angle = (0, useItemAngle_1.useItemAngle)(item, sectionId);
18
18
  const borderColor = (0, react_1.useMemo)(() => sdk_1.CntrlColor.parse(strokeColor).toCss(), [strokeColor]);
19
19
  return ((0, jsx_runtime_1.jsxs)(LinkWrapper_1.LinkWrapper, { url: item.link?.url, children: [(0, jsx_runtime_1.jsx)("div", { className: `video-wrapper-${item.id}`, style: {
20
20
  borderRadius: `${radius * 100}vw`,
@@ -10,10 +10,10 @@ const style_1 = __importDefault(require("styled-jsx/style"));
10
10
  const LinkWrapper_1 = require("../LinkWrapper");
11
11
  const useEmbedVideoItem_1 = require("./useEmbedVideoItem");
12
12
  const useItemAngle_1 = require("../useItemAngle");
13
- const VimeoEmbedItem = ({ item }) => {
13
+ const VimeoEmbedItem = ({ item, sectionId }) => {
14
14
  const id = (0, react_1.useId)();
15
- const { radius } = (0, useEmbedVideoItem_1.useEmbedVideoItem)(item);
16
- const angle = (0, useItemAngle_1.useItemAngle)(item);
15
+ const { radius } = (0, useEmbedVideoItem_1.useEmbedVideoItem)(item, sectionId);
16
+ const angle = (0, useItemAngle_1.useItemAngle)(item, sectionId);
17
17
  const { autoplay, controls, loop, muted, pictureInPicture, url } = item.commonParams;
18
18
  const getValidVimeoUrl = (url) => {
19
19
  const validURL = new URL(url);
@@ -11,11 +11,11 @@ const LinkWrapper_1 = require("../LinkWrapper");
11
11
  const getValidYoutubeUrl_1 = require("../../utils/getValidYoutubeUrl");
12
12
  const useEmbedVideoItem_1 = require("./useEmbedVideoItem");
13
13
  const useItemAngle_1 = require("../useItemAngle");
14
- const YoutubeEmbedItem = ({ item }) => {
14
+ const YoutubeEmbedItem = ({ item, sectionId }) => {
15
15
  const id = (0, react_1.useId)();
16
16
  const { autoplay, controls, url } = item.commonParams;
17
- const { radius } = (0, useEmbedVideoItem_1.useEmbedVideoItem)(item);
18
- const angle = (0, useItemAngle_1.useItemAngle)(item);
17
+ const { radius } = (0, useEmbedVideoItem_1.useEmbedVideoItem)(item, sectionId);
18
+ const angle = (0, useItemAngle_1.useItemAngle)(item, sectionId);
19
19
  const getValidYoutubeUrl = (url) => {
20
20
  const newUrl = new URL(url);
21
21
  const id = (0, getValidYoutubeUrl_1.getYoutubeId)(newUrl);
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useEmbedVideoItem = void 0;
4
4
  const useCurrentLayout_1 = require("../../common/useCurrentLayout");
5
5
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
6
- const useEmbedVideoItem = (item) => {
6
+ const useEmbedVideoItem = (item, sectionId) => {
7
7
  const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
8
8
  const radius = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
9
9
  if (!layoutId)
10
10
  return 0;
11
11
  const layoutParams = item.layoutParams[layoutId];
12
12
  return 'radius' in layoutParams ? layoutParams.radius : 0;
13
- }, (animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius, [layoutId]);
13
+ }, (animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius, sectionId, [layoutId]);
14
14
  return { radius };
15
15
  };
16
16
  exports.useEmbedVideoItem = useEmbedVideoItem;
@@ -4,32 +4,32 @@ exports.useFileItem = void 0;
4
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
5
5
  const useCurrentLayout_1 = require("../../common/useCurrentLayout");
6
6
  const defaultColor = 'rgba(0, 0, 0, 1)';
7
- const useFileItem = (item) => {
7
+ const useFileItem = (item, sectionId) => {
8
8
  const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
9
9
  const radius = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
10
10
  if (!layoutId)
11
11
  return 0;
12
12
  const layoutParams = item.layoutParams[layoutId];
13
13
  return 'radius' in layoutParams ? layoutParams.radius : 0;
14
- }, (animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius, [layoutId]);
14
+ }, (animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius, sectionId, [layoutId]);
15
15
  const strokeWidth = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
16
16
  if (!layoutId)
17
17
  return 0;
18
18
  const layoutParams = item.layoutParams[layoutId];
19
19
  return 'strokeWidth' in layoutParams ? layoutParams.strokeWidth : 0;
20
- }, (animator, scroll, value) => animator.getBorderWidth({ borderWidth: value }, scroll).borderWidth, [layoutId]);
20
+ }, (animator, scroll, value) => animator.getBorderWidth({ borderWidth: value }, scroll).borderWidth, sectionId, [layoutId]);
21
21
  const opacity = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
22
22
  if (!layoutId)
23
23
  return 0;
24
24
  const layoutParams = item.layoutParams[layoutId];
25
25
  return 'opacity' in layoutParams ? layoutParams.opacity : 0;
26
- }, (animator, scroll, value) => animator.getOpacity({ opacity: value }, scroll).opacity, [layoutId]);
26
+ }, (animator, scroll, value) => animator.getOpacity({ opacity: value }, scroll).opacity, sectionId, [layoutId]);
27
27
  const strokeColor = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
28
28
  if (!layoutId)
29
29
  return defaultColor;
30
30
  const layoutParams = item.layoutParams[layoutId];
31
31
  return 'strokeColor' in layoutParams ? layoutParams.strokeColor : defaultColor;
32
- }, (animator, scroll, value) => animator.getBorderColor({ color: value }, scroll).color);
32
+ }, (animator, scroll, value) => animator.getBorderColor({ color: value }, scroll).color, sectionId);
33
33
  return { radius, strokeWidth, opacity, strokeColor };
34
34
  };
35
35
  exports.useFileItem = useFileItem;
@@ -6,7 +6,7 @@ const sdk_1 = require("@cntrl-site/sdk");
6
6
  const StickyManager_1 = require("../../utils/StickyManager/StickyManager");
7
7
  const useCurrentLayout_1 = require("../../common/useCurrentLayout");
8
8
  const ArticleRectContext_1 = require("../../provider/ArticleRectContext");
9
- const useItemSticky = (top, parentOffsetTop, item) => {
9
+ const useItemSticky = (top, item, sectionId) => {
10
10
  const [isFixed, setIsFixed] = (0, react_1.useState)(false);
11
11
  const [adjustedTop, setAdjustedTop] = (0, react_1.useState)(top);
12
12
  const articleRectObserver = (0, react_1.useContext)(ArticleRectContext_1.ArticleRectContext);
@@ -15,18 +15,18 @@ const useItemSticky = (top, parentOffsetTop, item) => {
15
15
  const stickyManager = (0, react_1.useMemo)(() => new StickyManager_1.StickyManager(sticky), [sticky]);
16
16
  const handleSticky = (0, react_1.useCallback)((scroll) => {
17
17
  setIsFixed(stickyManager.getIsSticky(scroll));
18
- setAdjustedTop(stickyManager.getPosition(scroll, top, parentOffsetTop));
19
- }, [top, stickyManager, parentOffsetTop]);
18
+ setAdjustedTop(stickyManager.getPosition(scroll, top));
19
+ }, [top, stickyManager]);
20
20
  (0, react_1.useEffect)(() => {
21
21
  if (!articleRectObserver)
22
22
  return;
23
- handleSticky(articleRectObserver.scroll);
23
+ handleSticky(articleRectObserver.getSectionScroll(sectionId));
24
24
  }, [handleSticky, articleRectObserver]);
25
25
  (0, react_1.useEffect)(() => {
26
26
  if (!articleRectObserver || !sticky)
27
27
  return;
28
28
  return articleRectObserver.on('scroll', () => {
29
- handleSticky(articleRectObserver.scroll);
29
+ handleSticky(articleRectObserver.getSectionScroll(sectionId));
30
30
  });
31
31
  }, [handleSticky, articleRectObserver, sticky]);
32
32
  return {
@@ -4,32 +4,32 @@ exports.useRectangleItem = void 0;
4
4
  const useKeyframeValue_1 = require("../../common/useKeyframeValue");
5
5
  const useCurrentLayout_1 = require("../../common/useCurrentLayout");
6
6
  const defaultColor = 'rgba(0, 0, 0, 1)';
7
- const useRectangleItem = (item) => {
7
+ const useRectangleItem = (item, sectionId) => {
8
8
  const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
9
9
  const radius = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
10
10
  if (!layoutId)
11
11
  return 0;
12
12
  const layoutParams = item.layoutParams[layoutId];
13
13
  return 'radius' in layoutParams ? layoutParams.radius : 0;
14
- }, (animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius, [layoutId]);
14
+ }, (animator, scroll, value) => animator.getRadius({ radius: value }, scroll).radius, sectionId, [layoutId]);
15
15
  const strokeWidth = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
16
16
  if (!layoutId)
17
17
  return 0;
18
18
  const layoutParams = item.layoutParams[layoutId];
19
19
  return 'strokeWidth' in layoutParams ? layoutParams.strokeWidth : 0;
20
- }, (animator, scroll, value) => animator.getBorderWidth({ borderWidth: value }, scroll).borderWidth, [layoutId]);
20
+ }, (animator, scroll, value) => animator.getBorderWidth({ borderWidth: value }, scroll).borderWidth, sectionId, [layoutId]);
21
21
  const fillColor = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
22
22
  if (!layoutId)
23
23
  return defaultColor;
24
24
  const layoutParams = item.layoutParams[layoutId];
25
25
  return 'fillColor' in layoutParams ? layoutParams.fillColor : defaultColor;
26
- }, (animator, scroll, value) => animator.getColor({ color: value }, scroll).color, [layoutId]);
26
+ }, (animator, scroll, value) => animator.getColor({ color: value }, scroll).color, sectionId, [layoutId]);
27
27
  const strokeColor = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => {
28
28
  if (!layoutId)
29
29
  return defaultColor;
30
30
  const layoutParams = item.layoutParams[layoutId];
31
31
  return 'strokeColor' in layoutParams ? layoutParams.strokeColor : defaultColor;
32
- }, (animator, scroll, value) => animator.getBorderColor({ color: value }, scroll).color, [layoutId]);
32
+ }, (animator, scroll, value) => animator.getBorderColor({ color: value }, scroll).color, sectionId, [layoutId]);
33
33
  return { fillColor, strokeWidth, radius, strokeColor };
34
34
  };
35
35
  exports.useRectangleItem = useRectangleItem;
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useItemAngle = void 0;
4
4
  const useKeyframeValue_1 = require("../common/useKeyframeValue");
5
- const useItemAngle = (item) => {
6
- const { angle } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => ({ angle: layoutId ? item.area[layoutId].angle : 0 }), (animator, scroll, value) => animator.getRotation(value, scroll));
5
+ const useItemAngle = (item, sectionId) => {
6
+ const { angle } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => ({ angle: layoutId ? item.area[layoutId].angle : 0 }), (animator, scroll, value) => animator.getRotation(value, scroll), sectionId);
7
7
  return angle;
8
8
  };
9
9
  exports.useItemAngle = useItemAngle;
@@ -11,9 +11,9 @@ const defaultArea = {
11
11
  angle: 0,
12
12
  zIndex: 0
13
13
  };
14
- const useItemDimensions = (item) => {
14
+ const useItemDimensions = (item, sectionId) => {
15
15
  const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
16
- const { width, height } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => layoutId ? item.area[layoutId] : defaultArea, (animator, scroll, value) => animator.getDimensions(value, scroll), [layoutId]);
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
  };
19
19
  exports.useItemDimensions = useItemDimensions;
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useItemPosition = void 0;
4
4
  const useKeyframeValue_1 = require("../common/useKeyframeValue");
5
5
  const useCurrentLayout_1 = require("../common/useCurrentLayout");
6
- const useItemPosition = (item) => {
6
+ const useItemPosition = (item, sectionId) => {
7
7
  const layoutId = (0, useCurrentLayout_1.useCurrentLayout)();
8
- const { top, left } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => item.area[layoutId], (animator, scroll, value) => animator.getPositions(value, scroll), [layoutId]);
8
+ const { top, left } = (0, useKeyframeValue_1.useKeyframeValue)(item, (item, layoutId) => item.area[layoutId], (animator, scroll, value) => animator.getPositions(value, scroll), sectionId, [layoutId]);
9
9
  return { top, left };
10
10
  };
11
11
  exports.useItemPosition = useItemPosition;
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useItemScale = void 0;
4
4
  const useKeyframeValue_1 = require("../common/useKeyframeValue");
5
5
  const useCurrentLayout_1 = require("../common/useCurrentLayout");
6
- const useItemScale = (item) => {
6
+ const useItemScale = (item, sectionId) => {
7
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));
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
9
  const scaleAnchor = item.area[layout].scaleAnchor;
10
10
  return { scale, scaleAnchor };
11
11
  };
@@ -6,35 +6,33 @@ class ArticleRectObserver extends EventEmitter_1.EventEmitter {
6
6
  constructor() {
7
7
  super();
8
8
  this.articleWidth = 0;
9
+ this.registry = new Map();
9
10
  this.scrollPos = window.scrollY;
10
11
  this.animationFrame = NaN;
12
+ this.parent = undefined;
13
+ this.sectionsScrollMap = new Map();
11
14
  this.handleScroll = (scroll) => {
12
15
  this.setScroll(scroll / this.articleWidth);
13
16
  };
14
- this.handleResize = (entries) => {
15
- const [entry] = entries;
16
- if (!entry)
17
- return;
18
- const element = entry.target;
19
- if (!(element instanceof HTMLElement))
20
- return;
21
- this.notify(element);
22
- this.setScroll(window.scrollY / this.articleWidth);
23
- this.emit('resize', undefined);
24
- };
25
- this.resizeObserver = new ResizeObserver(this.handleResize);
17
+ this.resizeObserver = new ResizeObserver(this.handleResize.bind(this));
26
18
  }
27
19
  get scroll() {
28
20
  return this.scrollPos;
29
21
  }
22
+ getSectionScroll(sectionId) {
23
+ const sectionTop = this.sectionsScrollMap.get(sectionId);
24
+ if (sectionTop === undefined)
25
+ return 0;
26
+ return -(sectionTop / this.articleWidth - this.scrollPos);
27
+ }
30
28
  get width() {
31
29
  return this.articleWidth;
32
30
  }
33
31
  setScroll(scroll) {
34
32
  this.scrollPos = scroll;
35
33
  }
36
- start(el) {
37
- this.resizeObserver.observe(el);
34
+ init(parent) {
35
+ this.parent = parent;
38
36
  const onScroll = () => {
39
37
  this.handleScroll(window.scrollY);
40
38
  if (!isNaN(this.animationFrame))
@@ -46,7 +44,7 @@ class ArticleRectObserver extends EventEmitter_1.EventEmitter {
46
44
  };
47
45
  window.addEventListener('scroll', onScroll);
48
46
  return () => {
49
- this.resizeObserver.unobserve(el);
47
+ this.parent = undefined;
50
48
  window.removeEventListener('scroll', onScroll);
51
49
  if (!isNaN(this.animationFrame)) {
52
50
  window.cancelAnimationFrame(this.animationFrame);
@@ -54,9 +52,29 @@ class ArticleRectObserver extends EventEmitter_1.EventEmitter {
54
52
  }
55
53
  };
56
54
  }
57
- notify(el) {
58
- const elBoundary = el.getBoundingClientRect();
59
- this.articleWidth = elBoundary.width;
55
+ register(el, sectionId) {
56
+ this.registry.set(sectionId, el);
57
+ this.resizeObserver.observe(el);
58
+ return () => {
59
+ this.registry.delete(sectionId);
60
+ this.resizeObserver.unobserve(el);
61
+ };
62
+ }
63
+ handleResize() {
64
+ if (!this.parent)
65
+ return;
66
+ const parentBoundary = this.parent.getBoundingClientRect();
67
+ this.articleWidth = parentBoundary.width;
68
+ this.setScroll(window.scrollY / this.articleWidth);
69
+ this.emit('resize', undefined);
70
+ for (const sectionId of this.registry.keys()) {
71
+ const el = this.registry.get(sectionId);
72
+ if (!el)
73
+ continue;
74
+ const rect = el.getBoundingClientRect();
75
+ this.sectionsScrollMap.set(sectionId, rect.top - parentBoundary.top);
76
+ }
60
77
  }
78
+ ;
61
79
  }
62
80
  exports.ArticleRectObserver = ArticleRectObserver;
@@ -11,7 +11,7 @@ const useArticleRectObserver = (el) => {
11
11
  (0, react_1.useEffect)(() => {
12
12
  if (!el || !articleRectObserver)
13
13
  return;
14
- return articleRectObserver.start(el);
14
+ return articleRectObserver.init(el);
15
15
  }, [el, articleRectObserver]);
16
16
  return articleRectObserver;
17
17
  };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useSectionRegistry = void 0;
4
+ const react_1 = require("react");
5
+ const ArticleRectContext_1 = require("../../provider/ArticleRectContext");
6
+ const useSectionRegistry = (sectionId, el) => {
7
+ const articleRectObserver = (0, react_1.useContext)(ArticleRectContext_1.ArticleRectContext);
8
+ (0, react_1.useEffect)(() => {
9
+ if (!el || !articleRectObserver)
10
+ return;
11
+ return articleRectObserver.register(el, sectionId);
12
+ }, [el, articleRectObserver]);
13
+ };
14
+ exports.useSectionRegistry = useSectionRegistry;
@@ -10,11 +10,11 @@ class StickyManager {
10
10
  return false;
11
11
  return (this.sticky.from <= scroll) && (!this.sticky.to || this.sticky.to >= scroll);
12
12
  }
13
- getPosition(scroll, top, offsetTop) {
13
+ getPosition(scroll, top) {
14
14
  if (!this.sticky)
15
15
  return top;
16
16
  if (this.getIsSticky(scroll)) {
17
- return top - this.sticky.from + offsetTop;
17
+ return top - this.sticky.from;
18
18
  }
19
19
  if (this.sticky.to !== undefined && this.sticky.to <= scroll) {
20
20
  return top + this.sticky.to - this.sticky.from;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "0.11.1",
3
+ "version": "0.12.1",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "homepage": "https://github.com/cntrl-site/sdk-nextjs#readme",
23
23
  "dependencies": {
24
- "@cntrl-site/sdk": "^1.1.1",
24
+ "@cntrl-site/sdk": "^1.1.3",
25
25
  "html-react-parser": "^3.0.1",
26
26
  "styled-jsx": "^5.0.2"
27
27
  },
@@ -14,6 +14,7 @@ export const useKeyframeValue = <T>(
14
14
  item: TArticleItemAny,
15
15
  itemParamsGetter: ItemParamGetter<T>,
16
16
  animatorGetter: AnimatorGetter<T>,
17
+ sectionId: string,
17
18
  deps: DependencyList = emptyDeps
18
19
  ) => {
19
20
  const animatorGetterRef = useRef(animatorGetter);
@@ -59,26 +60,26 @@ export const useKeyframeValue = <T>(
59
60
  }, [paramValue]);
60
61
 
61
62
  useEffect(() => {
62
- if (!articleRectObserver) return;
63
- const scroll = articleRectObserver.scroll;
63
+ if (!articleRectObserver || !animator) return;
64
+ const scroll = articleRectObserver.getSectionScroll(sectionId);
64
65
  handleKeyframeValue(scroll);
65
- }, [articleRectObserver, handleKeyframeValue])
66
+ }, [articleRectObserver, handleKeyframeValue, animator])
66
67
 
67
68
  useEffect(() => {
68
- if (!articleRectObserver) return;
69
+ if (!articleRectObserver || !animator) return;
69
70
  return articleRectObserver.on('resize', () => {
70
- const scroll = articleRectObserver.scroll;
71
+ const scroll = articleRectObserver.getSectionScroll(sectionId);
71
72
  handleKeyframeValue(scroll);
72
73
  });
73
- }, [handleKeyframeValue, articleRectObserver]);
74
+ }, [handleKeyframeValue, articleRectObserver, animator]);
74
75
 
75
76
  useEffect(() => {
76
- if (!articleRectObserver) return;
77
+ if (!articleRectObserver || !animator) return;
77
78
  return articleRectObserver.on('scroll', () => {
78
- const scroll = articleRectObserver.scroll;
79
+ const scroll = articleRectObserver.getSectionScroll(sectionId);
79
80
  handleKeyframeValue(scroll);
80
81
  });
81
- }, [handleKeyframeValue, articleRectObserver]);
82
+ }, [handleKeyframeValue, articleRectObserver, animator]);
82
83
  return adjustedValue;
83
84
  };
84
85