@cntrl-site/sdk-nextjs 0.16.1 → 0.16.2
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/lib/common/useExemplary.js +13 -0
- package/lib/components/Article.js +4 -4
- package/lib/components/Item.js +13 -8
- package/lib/utils/ArticleRectManager/ArticleRectObserver.js +4 -0
- package/package.json +1 -1
- package/src/common/useExemplary.ts +9 -0
- package/src/components/Article.tsx +4 -4
- package/src/components/Item.tsx +20 -9
- package/src/utils/ArticleRectManager/ArticleRectObserver.ts +5 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useExemplary = void 0;
|
|
4
|
+
const useCntrlContext_1 = require("../provider/useCntrlContext");
|
|
5
|
+
const useLayoutContext_1 = require("../components/useLayoutContext");
|
|
6
|
+
const useExemplary = () => {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
|
|
9
|
+
const layout = (0, useLayoutContext_1.useLayoutContext)();
|
|
10
|
+
const exemplary = (_b = (_a = layouts.find(l => l.id === layout)) === null || _a === void 0 ? void 0 : _a.exemplary) !== null && _b !== void 0 ? _b : 1;
|
|
11
|
+
return exemplary;
|
|
12
|
+
};
|
|
13
|
+
exports.useExemplary = useExemplary;
|
|
@@ -16,21 +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 [
|
|
19
|
+
const [articleHeight, setArticleHeight] = (0, react_1.useState)(1);
|
|
20
20
|
(0, react_1.useEffect)(() => {
|
|
21
21
|
if (!articleRectObserver)
|
|
22
22
|
return;
|
|
23
23
|
return articleRectObserver.on('resize', (rect) => {
|
|
24
|
-
|
|
24
|
+
setArticleHeight(rect.height / rect.width);
|
|
25
25
|
});
|
|
26
26
|
}, [articleRectObserver]);
|
|
27
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) => {
|
|
28
28
|
const data = section.name ? sectionData[section.name] : {};
|
|
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,
|
|
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, articleHeight: articleHeight }, item.id))) }), section.id));
|
|
30
30
|
}) })) }), (0, jsx_runtime_1.jsx)(style_1.default, Object.assign({ id: id }, { children: `
|
|
31
31
|
.article {
|
|
32
32
|
position: relative;
|
|
33
|
-
overflow:
|
|
33
|
+
overflow: clip;
|
|
34
34
|
}
|
|
35
35
|
` }))] })));
|
|
36
36
|
};
|
package/lib/components/Item.js
CHANGED
|
@@ -26,6 +26,8 @@ const getItemTopStyle_1 = require("../utils/getItemTopStyle");
|
|
|
26
26
|
const useStickyItemTop_1 = require("./items/useStickyItemTop");
|
|
27
27
|
const getAnchoredItemTop_1 = require("../utils/getAnchoredItemTop");
|
|
28
28
|
const useLayoutContext_1 = require("./useLayoutContext");
|
|
29
|
+
const ArticleRectContext_1 = require("../provider/ArticleRectContext");
|
|
30
|
+
const useExemplary_1 = require("../common/useExemplary");
|
|
29
31
|
const itemsMap = {
|
|
30
32
|
[sdk_1.ArticleItemType.Rectangle]: RectangleItem_1.RectangleItem,
|
|
31
33
|
[sdk_1.ArticleItemType.Image]: ImageItem_1.ImageItem,
|
|
@@ -41,12 +43,13 @@ const RichTextWrapper = ({ isRichText, children }) => {
|
|
|
41
43
|
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ style: { transformOrigin: 'top left', transform: 'scale(var(--layout-deviation))' } }, { children: children })));
|
|
42
44
|
};
|
|
43
45
|
const noop = () => null;
|
|
44
|
-
const Item = ({ item, sectionId,
|
|
45
|
-
|
|
46
|
+
const Item = ({ item, sectionId, articleHeight }) => {
|
|
47
|
+
const itemWrapperRef = (0, react_1.useRef)(null);
|
|
48
|
+
const rectObserver = (0, react_1.useContext)(ArticleRectContext_1.ArticleRectContext);
|
|
46
49
|
const id = (0, react_1.useId)();
|
|
47
50
|
const { layouts } = (0, useCntrlContext_1.useCntrlContext)();
|
|
48
51
|
const layout = (0, useLayoutContext_1.useLayoutContext)();
|
|
49
|
-
const exemplary = (
|
|
52
|
+
const exemplary = (0, useExemplary_1.useExemplary)();
|
|
50
53
|
const [wrapperHeight, setWrapperHeight] = (0, react_1.useState)(undefined);
|
|
51
54
|
const [itemHeight, setItemHeight] = (0, react_1.useState)(undefined);
|
|
52
55
|
const { scale, scaleAnchor } = (0, useItemScale_1.useItemScale)(item, sectionId);
|
|
@@ -66,7 +69,10 @@ const Item = ({ item, sectionId, articleRatio }) => {
|
|
|
66
69
|
: undefined;
|
|
67
70
|
const sizingAxis = parseSizing(sizing);
|
|
68
71
|
const ItemComponent = itemsMap[item.type] || noop;
|
|
72
|
+
const sectionTop = rectObserver ? rectObserver.getSectionTop(sectionId) : 0;
|
|
69
73
|
const handleItemResize = (height) => {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
const itemSectionTop = (_b = (_a = itemWrapperRef.current) === null || _a === void 0 ? void 0 : _a.offsetTop) !== null && _b !== void 0 ? _b : 0;
|
|
70
76
|
if (!layout)
|
|
71
77
|
return;
|
|
72
78
|
const sticky = item.sticky[layout];
|
|
@@ -74,7 +80,7 @@ const Item = ({ item, sectionId, articleRatio }) => {
|
|
|
74
80
|
setWrapperHeight(undefined);
|
|
75
81
|
return;
|
|
76
82
|
}
|
|
77
|
-
const wrapperHeight = getStickyItemWrapperHeight(sticky, height,
|
|
83
|
+
const wrapperHeight = getStickyItemWrapperHeight(sticky, height, articleHeight, (sectionTop + itemSectionTop) / window.innerWidth);
|
|
78
84
|
setItemHeight(height);
|
|
79
85
|
setWrapperHeight(wrapperHeight);
|
|
80
86
|
};
|
|
@@ -86,7 +92,7 @@ const Item = ({ item, sectionId, articleRatio }) => {
|
|
|
86
92
|
top: stickyTop,
|
|
87
93
|
height: isRichText && itemHeight ? `${itemHeight * 100}vw` : 'unset'
|
|
88
94
|
};
|
|
89
|
-
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: `item-wrapper-${item.id}`, style: isInitialRef.current ? {} : Object.assign({ top, left }, (wrapperHeight ? { height: `${wrapperHeight * 100}vw` } : {})) }, { children: [(0, jsx_runtime_1.jsx)("div", Object.assign({ suppressHydrationWarning: true, className: `item-${item.id}`, style: isInitialRef.current ? {} : styles }, { children: (0, jsx_runtime_1.jsx)(RichTextWrapper, Object.assign({ isRichText: isRichText }, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: `item-${item.id}-inner`, style: {
|
|
95
|
+
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: `item-wrapper-${item.id}`, ref: itemWrapperRef, style: isInitialRef.current ? {} : Object.assign({ top, left }, (wrapperHeight !== undefined ? { height: `${wrapperHeight * 100}vw` } : {})) }, { children: [(0, jsx_runtime_1.jsx)("div", Object.assign({ suppressHydrationWarning: true, className: `item-${item.id}`, style: isInitialRef.current ? {} : styles }, { children: (0, jsx_runtime_1.jsx)(RichTextWrapper, Object.assign({ isRichText: isRichText }, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: `item-${item.id}-inner`, style: {
|
|
90
96
|
width: `${sizingAxis.x === sdk_1.ArticleItemSizingType.Manual
|
|
91
97
|
? isRichText
|
|
92
98
|
? `${width * exemplary}px`
|
|
@@ -123,7 +129,6 @@ const Item = ({ item, sectionId, articleRatio }) => {
|
|
|
123
129
|
pointer-events: none;
|
|
124
130
|
top: ${(0, getItemTopStyle_1.getItemTopStyle)(area.top, area.anchorSide)};
|
|
125
131
|
left: ${area.left * 100}vw;
|
|
126
|
-
height: ${sticky ? `${getStickyItemWrapperHeight(sticky, area.height, articleRatio) * 100}vw` : 'unset'};
|
|
127
132
|
transition: ${(0, HoverStyles_1.getTransitions)(['left', 'top'], hoverParams)};
|
|
128
133
|
}
|
|
129
134
|
.item-${item.id}-inner:hover {
|
|
@@ -137,9 +142,9 @@ const Item = ({ item, sectionId, articleRatio }) => {
|
|
|
137
142
|
` }))] })));
|
|
138
143
|
};
|
|
139
144
|
exports.Item = Item;
|
|
140
|
-
function getStickyItemWrapperHeight(sticky, itemHeight, articleHeight) {
|
|
145
|
+
function getStickyItemWrapperHeight(sticky, itemHeight, articleHeight, itemArticleOffset) {
|
|
141
146
|
var _a;
|
|
142
|
-
const end = (_a = sticky.to) !== null && _a !== void 0 ? _a : articleHeight;
|
|
147
|
+
const end = (_a = sticky.to) !== null && _a !== void 0 ? _a : articleHeight - itemArticleOffset - itemHeight;
|
|
143
148
|
return end - sticky.from + itemHeight;
|
|
144
149
|
}
|
|
145
150
|
function parseSizing(sizing = 'manual') {
|
|
@@ -29,6 +29,10 @@ class ArticleRectObserver extends EventEmitter_1.EventEmitter {
|
|
|
29
29
|
return 0;
|
|
30
30
|
return -(sectionTop / this.articleWidth - this.scrollPos);
|
|
31
31
|
}
|
|
32
|
+
getSectionTop(sectionId) {
|
|
33
|
+
const sectionTop = this.sectionsScrollMap.get(sectionId);
|
|
34
|
+
return sectionTop !== null && sectionTop !== void 0 ? sectionTop : 0;
|
|
35
|
+
}
|
|
32
36
|
get width() {
|
|
33
37
|
return this.articleWidth;
|
|
34
38
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useCntrlContext } from "../provider/useCntrlContext";
|
|
2
|
+
import { useLayoutContext } from "../components/useLayoutContext";
|
|
3
|
+
|
|
4
|
+
export const useExemplary = () => {
|
|
5
|
+
const { layouts } = useCntrlContext();
|
|
6
|
+
const layout = useLayoutContext();
|
|
7
|
+
const exemplary = layouts.find(l => l.id === layout)?.exemplary ?? 1;
|
|
8
|
+
return exemplary;
|
|
9
|
+
};
|
|
@@ -16,12 +16,12 @@ 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 [
|
|
19
|
+
const [articleHeight, setArticleHeight] = useState(1);
|
|
20
20
|
|
|
21
21
|
useEffect(() => {
|
|
22
22
|
if (!articleRectObserver) return;
|
|
23
23
|
return articleRectObserver.on('resize', (rect) => {
|
|
24
|
-
|
|
24
|
+
setArticleHeight(rect.height / rect.width);
|
|
25
25
|
});
|
|
26
26
|
}, [articleRectObserver]);
|
|
27
27
|
|
|
@@ -42,7 +42,7 @@ export const Article: FC<Props> = ({ article, sectionData }) => {
|
|
|
42
42
|
item={item}
|
|
43
43
|
key={item.id}
|
|
44
44
|
sectionId={section.id}
|
|
45
|
-
|
|
45
|
+
articleHeight={articleHeight}
|
|
46
46
|
/>
|
|
47
47
|
))}
|
|
48
48
|
</Section>
|
|
@@ -53,7 +53,7 @@ export const Article: FC<Props> = ({ article, sectionData }) => {
|
|
|
53
53
|
<JSXStyle id={id}>{`
|
|
54
54
|
.article {
|
|
55
55
|
position: relative;
|
|
56
|
-
overflow:
|
|
56
|
+
overflow: clip;
|
|
57
57
|
}
|
|
58
58
|
`}</JSXStyle>
|
|
59
59
|
</ArticleRectContext.Provider>
|
package/src/components/Item.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentType, FC, PropsWithChildren, useEffect, useId, useRef, useState } from 'react';
|
|
1
|
+
import { ComponentType, FC, PropsWithChildren, useContext, useEffect, useId, useRef, useState } from 'react';
|
|
2
2
|
import JSXStyle from 'styled-jsx/style';
|
|
3
3
|
import {
|
|
4
4
|
ArticleItemSizingType as SizingType,
|
|
@@ -26,6 +26,8 @@ import { getItemTopStyle } from '../utils/getItemTopStyle';
|
|
|
26
26
|
import { useStickyItemTop } from './items/useStickyItemTop';
|
|
27
27
|
import { getAnchoredItemTop } from '../utils/getAnchoredItemTop';
|
|
28
28
|
import { useLayoutContext } from './useLayoutContext';
|
|
29
|
+
import { ArticleRectContext } from "../provider/ArticleRectContext";
|
|
30
|
+
import { useExemplary } from "../common/useExemplary";
|
|
29
31
|
|
|
30
32
|
export interface ItemProps<I extends TArticleItemAny> {
|
|
31
33
|
item: I;
|
|
@@ -34,7 +36,7 @@ export interface ItemProps<I extends TArticleItemAny> {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
export interface ItemWrapperProps extends ItemProps<TArticleItemAny> {
|
|
37
|
-
|
|
39
|
+
articleHeight: number;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
const itemsMap: Record<ArticleItemType, ComponentType<ItemProps<any>>> = {
|
|
@@ -62,11 +64,13 @@ const RichTextWrapper: FC<PropsWithChildren<RTWrapperProps>> = ({ isRichText, ch
|
|
|
62
64
|
|
|
63
65
|
const noop = () => null;
|
|
64
66
|
|
|
65
|
-
export const Item: FC<ItemWrapperProps> = ({ item, sectionId,
|
|
67
|
+
export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleHeight }) => {
|
|
68
|
+
const itemWrapperRef = useRef<HTMLDivElement | null>(null);
|
|
69
|
+
const rectObserver = useContext(ArticleRectContext);
|
|
66
70
|
const id = useId();
|
|
67
71
|
const { layouts } = useCntrlContext();
|
|
68
72
|
const layout = useLayoutContext();
|
|
69
|
-
const exemplary =
|
|
73
|
+
const exemplary = useExemplary();
|
|
70
74
|
const [wrapperHeight, setWrapperHeight] = useState<undefined | number>(undefined);
|
|
71
75
|
const [itemHeight, setItemHeight] = useState<undefined | number>(undefined);
|
|
72
76
|
const { scale, scaleAnchor } = useItemScale(item, sectionId);
|
|
@@ -87,15 +91,17 @@ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleRatio }) =>
|
|
|
87
91
|
: undefined;
|
|
88
92
|
const sizingAxis = parseSizing(sizing);
|
|
89
93
|
const ItemComponent = itemsMap[item.type] || noop;
|
|
94
|
+
const sectionTop = rectObserver ? rectObserver.getSectionTop(sectionId) : 0;
|
|
90
95
|
|
|
91
96
|
const handleItemResize = (height: number) => {
|
|
97
|
+
const itemSectionTop = itemWrapperRef.current?.offsetTop ?? 0;
|
|
92
98
|
if (!layout) return;
|
|
93
99
|
const sticky = item.sticky[layout];
|
|
94
100
|
if (!sticky) {
|
|
95
101
|
setWrapperHeight(undefined);
|
|
96
102
|
return;
|
|
97
103
|
}
|
|
98
|
-
const wrapperHeight = getStickyItemWrapperHeight(sticky, height,
|
|
104
|
+
const wrapperHeight = getStickyItemWrapperHeight(sticky, height, articleHeight, (sectionTop + itemSectionTop) / window.innerWidth);
|
|
99
105
|
setItemHeight(height);
|
|
100
106
|
setWrapperHeight(wrapperHeight);
|
|
101
107
|
};
|
|
@@ -114,7 +120,8 @@ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleRatio }) =>
|
|
|
114
120
|
return (
|
|
115
121
|
<div
|
|
116
122
|
className={`item-wrapper-${item.id}`}
|
|
117
|
-
|
|
123
|
+
ref={itemWrapperRef}
|
|
124
|
+
style={isInitialRef.current ? {} : { top, left, ...(wrapperHeight !== undefined ? { height: `${wrapperHeight * 100}vw` } : {}) }}
|
|
118
125
|
>
|
|
119
126
|
<div
|
|
120
127
|
suppressHydrationWarning={true}
|
|
@@ -167,7 +174,6 @@ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleRatio }) =>
|
|
|
167
174
|
pointer-events: none;
|
|
168
175
|
top: ${getItemTopStyle(area.top, area.anchorSide)};
|
|
169
176
|
left: ${area.left * 100}vw;
|
|
170
|
-
height: ${sticky ? `${getStickyItemWrapperHeight(sticky, area.height, articleRatio) * 100}vw` : 'unset'};
|
|
171
177
|
transition: ${getTransitions(['left', 'top'], hoverParams)};
|
|
172
178
|
}
|
|
173
179
|
.item-${item.id}-inner:hover {
|
|
@@ -183,8 +189,13 @@ export const Item: FC<ItemWrapperProps> = ({ item, sectionId, articleRatio }) =>
|
|
|
183
189
|
);
|
|
184
190
|
};
|
|
185
191
|
|
|
186
|
-
function getStickyItemWrapperHeight(
|
|
187
|
-
|
|
192
|
+
function getStickyItemWrapperHeight(
|
|
193
|
+
sticky: TStickyParams,
|
|
194
|
+
itemHeight: number,
|
|
195
|
+
articleHeight: number,
|
|
196
|
+
itemArticleOffset: number,
|
|
197
|
+
) {
|
|
198
|
+
const end = sticky.to ?? articleHeight - itemArticleOffset - itemHeight;
|
|
188
199
|
return end - sticky.from + itemHeight;
|
|
189
200
|
}
|
|
190
201
|
|
|
@@ -30,6 +30,11 @@ export class ArticleRectObserver extends EventEmitter<EventMap> {
|
|
|
30
30
|
return - (sectionTop / this.articleWidth - this.scrollPos);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
getSectionTop(sectionId: string): number {
|
|
34
|
+
const sectionTop = this.sectionsScrollMap.get(sectionId);
|
|
35
|
+
return sectionTop ?? 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
33
38
|
get width(): number {
|
|
34
39
|
return this.articleWidth;
|
|
35
40
|
}
|