@cntrl-site/sdk-nextjs 0.16.0 → 0.16.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.
@@ -16,13 +16,21 @@ const Article = ({ article, sectionData }) => {
16
16
  const articleRef = (0, react_1.useRef)(null);
17
17
  const articleRectObserver = (0, useArticleRectObserver_1.useArticleRectObserver)(articleRef.current);
18
18
  const id = (0, react_1.useId)();
19
+ const [articleRatio, setArticleRatio] = (0, react_1.useState)(1);
20
+ (0, react_1.useEffect)(() => {
21
+ if (!articleRectObserver)
22
+ return;
23
+ return articleRectObserver.on('resize', (rect) => {
24
+ setArticleRatio(rect.height / rect.width);
25
+ });
26
+ }, [articleRectObserver]);
19
27
  return ((0, jsx_runtime_1.jsxs)(ArticleRectContext_1.ArticleRectContext.Provider, Object.assign({ value: articleRectObserver }, { children: [(0, jsx_runtime_1.jsx)(ArticleWrapper_1.ArticleWrapper, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: "article", ref: articleRef }, { children: article.sections.map((section, i) => {
20
28
  const data = section.name ? sectionData[section.name] : {};
21
- return ((0, jsx_runtime_1.jsx)(Section_1.Section, Object.assign({ 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));
29
+ return ((0, jsx_runtime_1.jsx)(Section_1.Section, Object.assign({ section: section, data: data }, { children: article.sections[i].items.map(item => ((0, jsx_runtime_1.jsx)(Item_1.Item, { item: item, sectionId: section.id, articleRatio: articleRatio }, item.id))) }), section.id));
22
30
  }) })) }), (0, jsx_runtime_1.jsx)(style_1.default, Object.assign({ id: id }, { children: `
23
31
  .article {
24
32
  position: relative;
25
- overflow: clip;
33
+ overflow: hidden;
26
34
  }
27
35
  ` }))] })));
28
36
  };
@@ -41,7 +41,7 @@ const RichTextWrapper = ({ isRichText, children }) => {
41
41
  return ((0, jsx_runtime_1.jsx)("div", Object.assign({ style: { transformOrigin: 'top left', transform: 'scale(var(--layout-deviation))' } }, { children: children })));
42
42
  };
43
43
  const noop = () => null;
44
- const Item = ({ item, sectionId }) => {
44
+ const Item = ({ item, sectionId, articleRatio }) => {
45
45
  var _a, _b;
46
46
  const id = (0, react_1.useId)();
47
47
  const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
@@ -74,7 +74,7 @@ const Item = ({ item, sectionId }) => {
74
74
  setWrapperHeight(undefined);
75
75
  return;
76
76
  }
77
- const wrapperHeight = getStickyItemWrapperHeight(sticky, height);
77
+ const wrapperHeight = getStickyItemWrapperHeight(sticky, height, articleRatio);
78
78
  setItemHeight(height);
79
79
  setWrapperHeight(wrapperHeight);
80
80
  };
@@ -123,7 +123,7 @@ const Item = ({ item, sectionId }) => {
123
123
  pointer-events: none;
124
124
  top: ${(0, getItemTopStyle_1.getItemTopStyle)(area.top, area.anchorSide)};
125
125
  left: ${area.left * 100}vw;
126
- height: ${sticky ? `${getStickyItemWrapperHeight(sticky, area.height) * 100}vw` : 'unset'};
126
+ height: ${sticky ? `${getStickyItemWrapperHeight(sticky, area.height, articleRatio) * 100}vw` : 'unset'};
127
127
  transition: ${(0, HoverStyles_1.getTransitions)(['left', 'top'], hoverParams)};
128
128
  }
129
129
  .item-${item.id}-inner:hover {
@@ -137,9 +137,9 @@ const Item = ({ item, sectionId }) => {
137
137
  ` }))] })));
138
138
  };
139
139
  exports.Item = Item;
140
- function getStickyItemWrapperHeight(sticky, itemHeight) {
140
+ function getStickyItemWrapperHeight(sticky, itemHeight, articleHeight) {
141
141
  var _a;
142
- const end = (_a = sticky.to) !== null && _a !== void 0 ? _a : 100;
142
+ const end = (_a = sticky.to) !== null && _a !== void 0 ? _a : articleHeight;
143
143
  return end - sticky.from + itemHeight;
144
144
  }
145
145
  function parseSizing(sizing = 'manual') {
@@ -70,7 +70,7 @@ class ArticleRectObserver extends EventEmitter_1.EventEmitter {
70
70
  const parentBoundary = this.parent.getBoundingClientRect();
71
71
  this.articleWidth = parentBoundary.width;
72
72
  this.setScroll(window.scrollY / this.articleWidth);
73
- this.emit('resize', undefined);
73
+ this.emit('resize', parentBoundary);
74
74
  for (const sectionId of this.registry.keys()) {
75
75
  const el = this.registry.get(sectionId);
76
76
  if (!el)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/sdk-nextjs",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "description": "SDK for Next.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -1,4 +1,4 @@
1
- import { FC, useId, useRef } from 'react';
1
+ import { FC, useEffect, useId, useRef, useState } from 'react';
2
2
  import JSXStyle from 'styled-jsx/style';
3
3
  import { TArticle } from '@cntrl-site/sdk';
4
4
  import { Section } from './Section';
@@ -16,6 +16,14 @@ export const Article: FC<Props> = ({ article, sectionData }) => {
16
16
  const articleRef = useRef<HTMLDivElement | null>(null);
17
17
  const articleRectObserver = useArticleRectObserver(articleRef.current);
18
18
  const id = useId();
19
+ const [articleRatio, setArticleRatio] = useState(1);
20
+
21
+ useEffect(() => {
22
+ if (!articleRectObserver) return;
23
+ return articleRectObserver.on('resize', (rect) => {
24
+ setArticleRatio(rect.height / rect.width);
25
+ });
26
+ }, [articleRectObserver]);
19
27
 
20
28
  return (
21
29
  <ArticleRectContext.Provider value={articleRectObserver}>
@@ -24,9 +32,18 @@ export const Article: FC<Props> = ({ article, sectionData }) => {
24
32
  {article.sections.map((section, i) => {
25
33
  const data = section.name ? sectionData[section.name] : {};
26
34
  return (
27
- <Section section={section} key={section.id} data={data}>
35
+ <Section
36
+ section={section}
37
+ key={section.id}
38
+ data={data}
39
+ >
28
40
  {article.sections[i].items.map(item => (
29
- <Item item={item} key={item.id} sectionId={section.id} />
41
+ <Item
42
+ item={item}
43
+ key={item.id}
44
+ sectionId={section.id}
45
+ articleRatio={articleRatio}
46
+ />
30
47
  ))}
31
48
  </Section>
32
49
  );
@@ -36,7 +53,7 @@ export const Article: FC<Props> = ({ article, sectionData }) => {
36
53
  <JSXStyle id={id}>{`
37
54
  .article {
38
55
  position: relative;
39
- overflow: clip;
56
+ overflow: hidden;
40
57
  }
41
58
  `}</JSXStyle>
42
59
  </ArticleRectContext.Provider>
@@ -1,14 +1,12 @@
1
1
  import { ComponentType, FC, PropsWithChildren, useEffect, useId, useRef, useState } from 'react';
2
2
  import JSXStyle from 'styled-jsx/style';
3
3
  import {
4
- AnchorSide,
5
4
  ArticleItemSizingType as SizingType,
6
5
  ArticleItemType,
7
6
  getLayoutStyles,
8
7
  TArticleItem,
9
8
  TStickyParams,
10
9
  TArticleItemAny,
11
-
12
10
  } from '@cntrl-site/sdk';
13
11
  import { RectangleItem } from './items/RectangleItem';
14
12
  import { ImageItem } from './items/ImageItem';
@@ -35,6 +33,10 @@ export interface ItemProps<I extends TArticleItemAny> {
35
33
  onResize?: (height: number) => void;
36
34
  }
37
35
 
36
+ export interface ItemWrapperProps extends ItemProps<TArticleItemAny> {
37
+ articleRatio: number; // height / width
38
+ }
39
+
38
40
  const itemsMap: Record<ArticleItemType, ComponentType<ItemProps<any>>> = {
39
41
  [ArticleItemType.Rectangle]: RectangleItem,
40
42
  [ArticleItemType.Image]: ImageItem,
@@ -60,7 +62,7 @@ const RichTextWrapper: FC<PropsWithChildren<RTWrapperProps>> = ({ isRichText, ch
60
62
 
61
63
  const noop = () => null;
62
64
 
63
- export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
65
+ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleRatio }) => {
64
66
  const id = useId();
65
67
  const { layouts } = useCntrlContext();
66
68
  const layout = useLayoutContext();
@@ -93,7 +95,7 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
93
95
  setWrapperHeight(undefined);
94
96
  return;
95
97
  }
96
- const wrapperHeight = getStickyItemWrapperHeight(sticky, height);
98
+ const wrapperHeight = getStickyItemWrapperHeight(sticky, height, articleRatio);
97
99
  setItemHeight(height);
98
100
  setWrapperHeight(wrapperHeight);
99
101
  };
@@ -165,7 +167,7 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
165
167
  pointer-events: none;
166
168
  top: ${getItemTopStyle(area.top, area.anchorSide)};
167
169
  left: ${area.left * 100}vw;
168
- height: ${sticky ? `${getStickyItemWrapperHeight(sticky, area.height) * 100}vw` : 'unset'};
170
+ height: ${sticky ? `${getStickyItemWrapperHeight(sticky, area.height, articleRatio) * 100}vw` : 'unset'};
169
171
  transition: ${getTransitions(['left', 'top'], hoverParams)};
170
172
  }
171
173
  .item-${item.id}-inner:hover {
@@ -181,8 +183,8 @@ export const Item: FC<ItemProps<TArticleItemAny>> = ({ item, sectionId}) => {
181
183
  );
182
184
  };
183
185
 
184
- function getStickyItemWrapperHeight(sticky: TStickyParams, itemHeight: number): number {
185
- const end = sticky.to ?? 100;
186
+ function getStickyItemWrapperHeight(sticky: TStickyParams, itemHeight: number, articleHeight: number): number {
187
+ const end = sticky.to ?? articleHeight;
186
188
  return end - sticky.from + itemHeight;
187
189
  }
188
190
 
@@ -3,7 +3,7 @@ import ResizeObserver from 'resize-observer-polyfill';
3
3
 
4
4
  interface EventMap {
5
5
  'scroll': undefined;
6
- 'resize': undefined;
6
+ 'resize': DOMRect;
7
7
  }
8
8
 
9
9
  export class ArticleRectObserver extends EventEmitter<EventMap> {
@@ -77,7 +77,7 @@ export class ArticleRectObserver extends EventEmitter<EventMap> {
77
77
  const parentBoundary = this.parent.getBoundingClientRect();
78
78
  this.articleWidth = parentBoundary.width;
79
79
  this.setScroll(window.scrollY / this.articleWidth);
80
- this.emit('resize', undefined);
80
+ this.emit('resize', parentBoundary);
81
81
  for (const sectionId of this.registry.keys()) {
82
82
  const el = this.registry.get(sectionId);
83
83
  if (!el) continue;