@meduza/ui-kit-2 1.3.1 → 1.4.0
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/dist/RelatedCarousel/RelatedCarousel.types.d.ts +5 -0
- package/dist/RelatedCarousel/index.d.ts +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/ui-kit-2.cjs.development.js +346 -241
- package/dist/ui-kit-2.cjs.development.js.map +1 -1
- package/dist/ui-kit-2.cjs.production.min.js +1 -1
- package/dist/ui-kit-2.cjs.production.min.js.map +1 -1
- package/dist/ui-kit-2.esm.js +346 -242
- package/dist/ui-kit-2.esm.js.map +1 -1
- package/dist/ui-kit-game.css +91 -0
- package/dist/ui-kit.css +91 -0
- package/package.json +1 -1
- package/src/Lazy/index.tsx +7 -20
- package/src/RelatedCarousel/RelatedCarousel.module.css +90 -0
- package/src/RelatedCarousel/RelatedCarousel.stories.module.css +53 -0
- package/src/RelatedCarousel/RelatedCarousel.stories.tsx +48 -0
- package/src/RelatedCarousel/RelatedCarousel.test.tsx +27 -0
- package/src/RelatedCarousel/RelatedCarousel.types.ts +6 -0
- package/src/RelatedCarousel/index.tsx +167 -0
- package/src/RelatedCarousel/mock.json +17 -0
- package/src/index.tsx +1 -0
package/dist/ui-kit-game.css
CHANGED
|
@@ -6926,6 +6926,97 @@ ul.VersesBlock-module_root__CrgSZ:first-child {
|
|
|
6926
6926
|
transform: translateX(16px);
|
|
6927
6927
|
}
|
|
6928
6928
|
|
|
6929
|
+
.RelatedCarousel-module_root__G-Eub {
|
|
6930
|
+
position: relative;
|
|
6931
|
+
}
|
|
6932
|
+
|
|
6933
|
+
.RelatedCarousel-module_track__UVQ2n {
|
|
6934
|
+
display: flex;
|
|
6935
|
+
gap: 20px;
|
|
6936
|
+
|
|
6937
|
+
margin: 0 -20px;
|
|
6938
|
+
padding: 0 20px;
|
|
6939
|
+
|
|
6940
|
+
overflow-x: auto;
|
|
6941
|
+
|
|
6942
|
+
list-style: none;
|
|
6943
|
+
overscroll-behavior-x: contain;
|
|
6944
|
+
|
|
6945
|
+
scroll-snap-type: x mandatory;
|
|
6946
|
+
scroll-padding-left: 20px;
|
|
6947
|
+
-webkit-overflow-scrolling: touch;
|
|
6948
|
+
|
|
6949
|
+
scrollbar-width: none;
|
|
6950
|
+
}
|
|
6951
|
+
|
|
6952
|
+
.RelatedCarousel-module_track__UVQ2n::-webkit-scrollbar {
|
|
6953
|
+
display: none;
|
|
6954
|
+
}
|
|
6955
|
+
|
|
6956
|
+
.RelatedCarousel-module_slide__MI9tF {
|
|
6957
|
+
position: relative;
|
|
6958
|
+
|
|
6959
|
+
flex: 0 0 85%;
|
|
6960
|
+
min-width: 0;
|
|
6961
|
+
|
|
6962
|
+
overflow: hidden;
|
|
6963
|
+
|
|
6964
|
+
scroll-snap-align: start;
|
|
6965
|
+
scroll-snap-stop: always;
|
|
6966
|
+
}
|
|
6967
|
+
|
|
6968
|
+
.RelatedCarousel-module_dots__mOSIC {
|
|
6969
|
+
--dotStep: 7px;
|
|
6970
|
+
|
|
6971
|
+
width: calc(var(--dotsVisible) * var(--dotStep));
|
|
6972
|
+
margin: 28px auto 0;
|
|
6973
|
+
|
|
6974
|
+
overflow: hidden;
|
|
6975
|
+
}
|
|
6976
|
+
|
|
6977
|
+
.RelatedCarousel-module_dotsTrack__V7bnX {
|
|
6978
|
+
display: flex;
|
|
6979
|
+
|
|
6980
|
+
transform: translateX(calc(var(--dotsStart) * var(--dotStep) * -1));
|
|
6981
|
+
|
|
6982
|
+
transition: transform 0.25s ease;
|
|
6983
|
+
}
|
|
6984
|
+
|
|
6985
|
+
.RelatedCarousel-module_dotCell__kl64s {
|
|
6986
|
+
display: flex;
|
|
6987
|
+
flex: 0 0 auto;
|
|
6988
|
+
align-items: center;
|
|
6989
|
+
justify-content: center;
|
|
6990
|
+
|
|
6991
|
+
width: var(--dotStep);
|
|
6992
|
+
height: var(--dotStep);
|
|
6993
|
+
}
|
|
6994
|
+
|
|
6995
|
+
.RelatedCarousel-module_dot__Jo52r {
|
|
6996
|
+
width: 4px;
|
|
6997
|
+
height: 4px;
|
|
6998
|
+
|
|
6999
|
+
background-color: #d4d4d4;
|
|
7000
|
+
|
|
7001
|
+
border-radius: 50%;
|
|
7002
|
+
|
|
7003
|
+
transition: width 0.25s ease, height 0.25s ease, background-color 0.2s;
|
|
7004
|
+
}
|
|
7005
|
+
|
|
7006
|
+
.RelatedCarousel-module_isSmall__ZP5hd {
|
|
7007
|
+
width: 2px;
|
|
7008
|
+
height: 2px;
|
|
7009
|
+
}
|
|
7010
|
+
|
|
7011
|
+
.RelatedCarousel-module_isTiny__mjhtD {
|
|
7012
|
+
width: 1px;
|
|
7013
|
+
height: 1px;
|
|
7014
|
+
}
|
|
7015
|
+
|
|
7016
|
+
.RelatedCarousel-module_isActive__Nz2aj {
|
|
7017
|
+
background-color: #000;
|
|
7018
|
+
}
|
|
7019
|
+
|
|
6929
7020
|
.ToolbarButton-module_root__upsEH {
|
|
6930
7021
|
display: block;
|
|
6931
7022
|
|
package/dist/ui-kit.css
CHANGED
|
@@ -6926,6 +6926,97 @@ ul.VersesBlock-module_root__CrgSZ:first-child {
|
|
|
6926
6926
|
transform: translateX(16px);
|
|
6927
6927
|
}
|
|
6928
6928
|
|
|
6929
|
+
.RelatedCarousel-module_root__G-Eub {
|
|
6930
|
+
position: relative;
|
|
6931
|
+
}
|
|
6932
|
+
|
|
6933
|
+
.RelatedCarousel-module_track__UVQ2n {
|
|
6934
|
+
display: flex;
|
|
6935
|
+
gap: 20px;
|
|
6936
|
+
|
|
6937
|
+
margin: 0 -20px;
|
|
6938
|
+
padding: 0 20px;
|
|
6939
|
+
|
|
6940
|
+
overflow-x: auto;
|
|
6941
|
+
|
|
6942
|
+
list-style: none;
|
|
6943
|
+
overscroll-behavior-x: contain;
|
|
6944
|
+
|
|
6945
|
+
scroll-snap-type: x mandatory;
|
|
6946
|
+
scroll-padding-left: 20px;
|
|
6947
|
+
-webkit-overflow-scrolling: touch;
|
|
6948
|
+
|
|
6949
|
+
scrollbar-width: none;
|
|
6950
|
+
}
|
|
6951
|
+
|
|
6952
|
+
.RelatedCarousel-module_track__UVQ2n::-webkit-scrollbar {
|
|
6953
|
+
display: none;
|
|
6954
|
+
}
|
|
6955
|
+
|
|
6956
|
+
.RelatedCarousel-module_slide__MI9tF {
|
|
6957
|
+
position: relative;
|
|
6958
|
+
|
|
6959
|
+
flex: 0 0 85%;
|
|
6960
|
+
min-width: 0;
|
|
6961
|
+
|
|
6962
|
+
overflow: hidden;
|
|
6963
|
+
|
|
6964
|
+
scroll-snap-align: start;
|
|
6965
|
+
scroll-snap-stop: always;
|
|
6966
|
+
}
|
|
6967
|
+
|
|
6968
|
+
.RelatedCarousel-module_dots__mOSIC {
|
|
6969
|
+
--dotStep: 7px;
|
|
6970
|
+
|
|
6971
|
+
width: calc(var(--dotsVisible) * var(--dotStep));
|
|
6972
|
+
margin: 28px auto 0;
|
|
6973
|
+
|
|
6974
|
+
overflow: hidden;
|
|
6975
|
+
}
|
|
6976
|
+
|
|
6977
|
+
.RelatedCarousel-module_dotsTrack__V7bnX {
|
|
6978
|
+
display: flex;
|
|
6979
|
+
|
|
6980
|
+
transform: translateX(calc(var(--dotsStart) * var(--dotStep) * -1));
|
|
6981
|
+
|
|
6982
|
+
transition: transform 0.25s ease;
|
|
6983
|
+
}
|
|
6984
|
+
|
|
6985
|
+
.RelatedCarousel-module_dotCell__kl64s {
|
|
6986
|
+
display: flex;
|
|
6987
|
+
flex: 0 0 auto;
|
|
6988
|
+
align-items: center;
|
|
6989
|
+
justify-content: center;
|
|
6990
|
+
|
|
6991
|
+
width: var(--dotStep);
|
|
6992
|
+
height: var(--dotStep);
|
|
6993
|
+
}
|
|
6994
|
+
|
|
6995
|
+
.RelatedCarousel-module_dot__Jo52r {
|
|
6996
|
+
width: 4px;
|
|
6997
|
+
height: 4px;
|
|
6998
|
+
|
|
6999
|
+
background-color: #d4d4d4;
|
|
7000
|
+
|
|
7001
|
+
border-radius: 50%;
|
|
7002
|
+
|
|
7003
|
+
transition: width 0.25s ease, height 0.25s ease, background-color 0.2s;
|
|
7004
|
+
}
|
|
7005
|
+
|
|
7006
|
+
.RelatedCarousel-module_isSmall__ZP5hd {
|
|
7007
|
+
width: 2px;
|
|
7008
|
+
height: 2px;
|
|
7009
|
+
}
|
|
7010
|
+
|
|
7011
|
+
.RelatedCarousel-module_isTiny__mjhtD {
|
|
7012
|
+
width: 1px;
|
|
7013
|
+
height: 1px;
|
|
7014
|
+
}
|
|
7015
|
+
|
|
7016
|
+
.RelatedCarousel-module_isActive__Nz2aj {
|
|
7017
|
+
background-color: #000;
|
|
7018
|
+
}
|
|
7019
|
+
|
|
6929
7020
|
.ToolbarButton-module_root__upsEH {
|
|
6930
7021
|
display: block;
|
|
6931
7022
|
|
package/package.json
CHANGED
package/src/Lazy/index.tsx
CHANGED
|
@@ -1,39 +1,26 @@
|
|
|
1
|
-
import React
|
|
1
|
+
import React from 'react'
|
|
2
2
|
import { useInView } from 'react-intersection-observer'
|
|
3
3
|
import { makeClassName } from '../utils/makeClassName'
|
|
4
4
|
import { LazyProps } from './Lazy.types'
|
|
5
5
|
|
|
6
6
|
import styles from './Lazy.module.css'
|
|
7
7
|
|
|
8
|
-
export const Lazy: React.FC<LazyProps> = ({ children, threshold }) => {
|
|
9
|
-
const [ref, inView
|
|
10
|
-
threshold: threshold
|
|
8
|
+
export const Lazy: React.FC<LazyProps> = ({ children, threshold = 0 }) => {
|
|
9
|
+
const [ref, inView] = useInView({
|
|
10
|
+
threshold: threshold,
|
|
11
|
+
triggerOnce: true
|
|
11
12
|
})
|
|
12
13
|
|
|
13
|
-
const [visible, setVisible] = useState(false)
|
|
14
|
-
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
if (entry && entry.boundingClientRect && entry.boundingClientRect.y < 0) {
|
|
17
|
-
setVisible(true)
|
|
18
|
-
}
|
|
19
|
-
}, [entry])
|
|
20
|
-
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
if (inView) {
|
|
23
|
-
setVisible(true)
|
|
24
|
-
}
|
|
25
|
-
}, [inView])
|
|
26
|
-
|
|
27
14
|
return (
|
|
28
15
|
<div
|
|
29
16
|
className={makeClassName([
|
|
30
17
|
[styles.root, true],
|
|
31
|
-
[styles.visible,
|
|
18
|
+
[styles.visible, inView]
|
|
32
19
|
])}
|
|
33
20
|
ref={ref}
|
|
34
21
|
data-testid="lazy"
|
|
35
22
|
>
|
|
36
|
-
{
|
|
23
|
+
{inView && children}
|
|
37
24
|
</div>
|
|
38
25
|
)
|
|
39
26
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
.root {
|
|
2
|
+
position: relative;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.track {
|
|
6
|
+
display: flex;
|
|
7
|
+
gap: 20px;
|
|
8
|
+
|
|
9
|
+
margin: 0 -20px;
|
|
10
|
+
padding: 0 20px;
|
|
11
|
+
|
|
12
|
+
overflow-x: auto;
|
|
13
|
+
|
|
14
|
+
list-style: none;
|
|
15
|
+
overscroll-behavior-x: contain;
|
|
16
|
+
|
|
17
|
+
scroll-snap-type: x mandatory;
|
|
18
|
+
scroll-padding-left: 20px;
|
|
19
|
+
-webkit-overflow-scrolling: touch;
|
|
20
|
+
|
|
21
|
+
scrollbar-width: none;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.track::-webkit-scrollbar {
|
|
25
|
+
display: none;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.slide {
|
|
29
|
+
position: relative;
|
|
30
|
+
|
|
31
|
+
flex: 0 0 85%;
|
|
32
|
+
min-width: 0;
|
|
33
|
+
|
|
34
|
+
overflow: hidden;
|
|
35
|
+
|
|
36
|
+
scroll-snap-align: start;
|
|
37
|
+
scroll-snap-stop: always;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.dots {
|
|
41
|
+
--dotStep: 7px;
|
|
42
|
+
|
|
43
|
+
width: calc(var(--dotsVisible) * var(--dotStep));
|
|
44
|
+
margin: 28px auto 0;
|
|
45
|
+
|
|
46
|
+
overflow: hidden;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.dotsTrack {
|
|
50
|
+
display: flex;
|
|
51
|
+
|
|
52
|
+
transform: translateX(calc(var(--dotsStart) * var(--dotStep) * -1));
|
|
53
|
+
|
|
54
|
+
transition: transform 0.25s ease;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.dotCell {
|
|
58
|
+
display: flex;
|
|
59
|
+
flex: 0 0 auto;
|
|
60
|
+
align-items: center;
|
|
61
|
+
justify-content: center;
|
|
62
|
+
|
|
63
|
+
width: var(--dotStep);
|
|
64
|
+
height: var(--dotStep);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.dot {
|
|
68
|
+
width: 4px;
|
|
69
|
+
height: 4px;
|
|
70
|
+
|
|
71
|
+
background-color: #d4d4d4;
|
|
72
|
+
|
|
73
|
+
border-radius: 50%;
|
|
74
|
+
|
|
75
|
+
transition: width 0.25s ease, height 0.25s ease, background-color 0.2s;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.isSmall {
|
|
79
|
+
width: 2px;
|
|
80
|
+
height: 2px;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.isTiny {
|
|
84
|
+
width: 1px;
|
|
85
|
+
height: 1px;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.isActive {
|
|
89
|
+
background-color: #000;
|
|
90
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
.root {
|
|
2
|
+
display: block;
|
|
3
|
+
|
|
4
|
+
max-width: 375px;
|
|
5
|
+
margin: 0 auto;
|
|
6
|
+
padding: 0 20px;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.block {
|
|
10
|
+
margin: 16px -20px;
|
|
11
|
+
padding: 8px 20px 20px;
|
|
12
|
+
|
|
13
|
+
background-color: #f7f7f7;
|
|
14
|
+
border-top: 4px solid #b88b58;
|
|
15
|
+
|
|
16
|
+
box-shadow: 0 1px 0 0 rgba(0, 0, 0, 0.1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.header {
|
|
20
|
+
margin: 0 0 25px;
|
|
21
|
+
|
|
22
|
+
letter-spacing: 1px;
|
|
23
|
+
text-transform: uppercase;
|
|
24
|
+
|
|
25
|
+
color: #7f7f7f;
|
|
26
|
+
|
|
27
|
+
font-size: 12px;
|
|
28
|
+
font-weight: 600;
|
|
29
|
+
line-height: 17px;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.card {
|
|
33
|
+
background-color: #fff;
|
|
34
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.cover {
|
|
38
|
+
display: block;
|
|
39
|
+
|
|
40
|
+
width: 100%;
|
|
41
|
+
aspect-ratio: 3 / 2;
|
|
42
|
+
|
|
43
|
+
background-color: #d5d5d5;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.title {
|
|
47
|
+
margin: 0;
|
|
48
|
+
padding: 10px 12px 14px;
|
|
49
|
+
|
|
50
|
+
font-size: 18px;
|
|
51
|
+
font-weight: 700;
|
|
52
|
+
line-height: 130%;
|
|
53
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { RelatedCarousel } from './'
|
|
3
|
+
import { PreviewWrapper } from '../_storybook/PreviewWrapper'
|
|
4
|
+
|
|
5
|
+
import styles from './RelatedCarousel.stories.module.css'
|
|
6
|
+
import mock from './mock.json'
|
|
7
|
+
|
|
8
|
+
export default {
|
|
9
|
+
title: 'Main / RelatedCarousel',
|
|
10
|
+
component: RelatedCarousel,
|
|
11
|
+
parameters: {
|
|
12
|
+
themeWrapperSideBySide: true
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface CardProps {
|
|
17
|
+
title: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const Card: React.FC<CardProps> = ({ title }) => (
|
|
21
|
+
<div className={styles.card}>
|
|
22
|
+
<span className={styles.cover} />
|
|
23
|
+
<p className={styles.title}>{title}</p>
|
|
24
|
+
</div>
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
const Example: React.FC = () => {
|
|
28
|
+
return (
|
|
29
|
+
<div className={styles.root}>
|
|
30
|
+
<div className={styles.block}>
|
|
31
|
+
<h3 className={styles.header}>{mock.title}</h3>
|
|
32
|
+
<RelatedCarousel ariaLabel={mock.title}>
|
|
33
|
+
{mock.related.map((item) => (
|
|
34
|
+
<Card key={item.id} title={item.title} />
|
|
35
|
+
))}
|
|
36
|
+
</RelatedCarousel>
|
|
37
|
+
</div>
|
|
38
|
+
</div>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const Default: React.FC = () => (
|
|
43
|
+
<>
|
|
44
|
+
<PreviewWrapper theme="light">
|
|
45
|
+
<Example />
|
|
46
|
+
</PreviewWrapper>
|
|
47
|
+
</>
|
|
48
|
+
)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { render } from '@testing-library/react'
|
|
3
|
+
import { RelatedCarousel } from './'
|
|
4
|
+
|
|
5
|
+
import styles from './RelatedCarousel.module.css'
|
|
6
|
+
|
|
7
|
+
describe('Related Carousel', () => {
|
|
8
|
+
const renderComponent = () =>
|
|
9
|
+
render(
|
|
10
|
+
<RelatedCarousel>
|
|
11
|
+
<div>Первая</div>
|
|
12
|
+
<div>Вторая</div>
|
|
13
|
+
</RelatedCarousel>
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
it('should have root style', () => {
|
|
17
|
+
const { getByTestId } = renderComponent()
|
|
18
|
+
|
|
19
|
+
expect(getByTestId('related-carousel')).toHaveClass(styles.root)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should render a dot per slide', () => {
|
|
23
|
+
const { getAllByRole } = renderComponent()
|
|
24
|
+
|
|
25
|
+
expect(getAllByRole('button')).toHaveLength(2)
|
|
26
|
+
})
|
|
27
|
+
})
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import { RelatedCarouselProps } from './RelatedCarousel.types'
|
|
4
|
+
import makeClassName from '../utils/makeClassName'
|
|
5
|
+
|
|
6
|
+
import styles from './RelatedCarousel.module.css'
|
|
7
|
+
|
|
8
|
+
const VISIBLE_DOTS = 6
|
|
9
|
+
const EDGE_DOTS = 2
|
|
10
|
+
|
|
11
|
+
type DotsStyle = React.CSSProperties & Record<string, number>
|
|
12
|
+
|
|
13
|
+
const getDotsStart = (
|
|
14
|
+
activeIndex: number,
|
|
15
|
+
currentStart: number,
|
|
16
|
+
total: number
|
|
17
|
+
): number => {
|
|
18
|
+
if (total <= VISIBLE_DOTS) {
|
|
19
|
+
return 0
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let start = currentStart
|
|
23
|
+
|
|
24
|
+
if (activeIndex < start + EDGE_DOTS) {
|
|
25
|
+
start = activeIndex - EDGE_DOTS
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (activeIndex > start + VISIBLE_DOTS - 1 - EDGE_DOTS) {
|
|
29
|
+
start = activeIndex - VISIBLE_DOTS + 1 + EDGE_DOTS
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return Math.max(0, Math.min(start, total - VISIBLE_DOTS))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const getDotScale = (
|
|
36
|
+
index: number,
|
|
37
|
+
start: number,
|
|
38
|
+
total: number
|
|
39
|
+
): 'full' | 'small' | 'tiny' => {
|
|
40
|
+
const position = index - start
|
|
41
|
+
const hasBefore = start > 0
|
|
42
|
+
const hasAfter = start + VISIBLE_DOTS < total
|
|
43
|
+
|
|
44
|
+
if (hasBefore && position === 0) {
|
|
45
|
+
return 'tiny'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (hasBefore && position === 1) {
|
|
49
|
+
return 'small'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (hasAfter && position === VISIBLE_DOTS - 1) {
|
|
53
|
+
return 'tiny'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (hasAfter && position === VISIBLE_DOTS - 2) {
|
|
57
|
+
return 'small'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return 'full'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const getSlideStep = (track: HTMLElement | null): number => {
|
|
64
|
+
if (!track) {
|
|
65
|
+
return 0
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const [first, second] = Array.from(track.children) as HTMLElement[]
|
|
69
|
+
|
|
70
|
+
if (!first) {
|
|
71
|
+
return 0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return second ? second.offsetLeft - first.offsetLeft : first.offsetWidth
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const RelatedCarousel: React.FC<RelatedCarouselProps> = ({
|
|
78
|
+
children,
|
|
79
|
+
ariaLabel
|
|
80
|
+
}) => {
|
|
81
|
+
const trackRef = useRef<HTMLUListElement>(null)
|
|
82
|
+
const frameRef = useRef(0)
|
|
83
|
+
const [activeIndex, setActiveIndex] = useState(0)
|
|
84
|
+
const [dotsStart, setDotsStart] = useState(0)
|
|
85
|
+
|
|
86
|
+
const slides = React.Children.toArray(children)
|
|
87
|
+
const lastIndex = slides.length - 1
|
|
88
|
+
const total = slides.length
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
const track = trackRef.current
|
|
92
|
+
|
|
93
|
+
if (!track) {
|
|
94
|
+
return undefined
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const handleScroll = () => {
|
|
98
|
+
cancelAnimationFrame(frameRef.current)
|
|
99
|
+
|
|
100
|
+
frameRef.current = requestAnimationFrame(() => {
|
|
101
|
+
const step = getSlideStep(track)
|
|
102
|
+
|
|
103
|
+
if (!step) {
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const index = Math.max(
|
|
108
|
+
0,
|
|
109
|
+
Math.min(lastIndex, Math.round(track.scrollLeft / step))
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
setActiveIndex(index)
|
|
113
|
+
setDotsStart((start) => getDotsStart(index, start, lastIndex + 1))
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
track.addEventListener('scroll', handleScroll, { passive: true })
|
|
118
|
+
|
|
119
|
+
return () => {
|
|
120
|
+
cancelAnimationFrame(frameRef.current)
|
|
121
|
+
track.removeEventListener('scroll', handleScroll)
|
|
122
|
+
}
|
|
123
|
+
}, [lastIndex])
|
|
124
|
+
|
|
125
|
+
const dotsStyle: DotsStyle = {
|
|
126
|
+
'--dotsVisible': Math.min(total, VISIBLE_DOTS),
|
|
127
|
+
'--dotsStart': dotsStart
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<div
|
|
132
|
+
data-testid="related-carousel"
|
|
133
|
+
className={makeClassName([[styles.root, true]])}
|
|
134
|
+
role="group"
|
|
135
|
+
aria-label={ariaLabel}
|
|
136
|
+
>
|
|
137
|
+
<ul className={styles.track} ref={trackRef}>
|
|
138
|
+
{slides.map((slide, index) => (
|
|
139
|
+
<li className={styles.slide} key={index}>
|
|
140
|
+
{slide}
|
|
141
|
+
</li>
|
|
142
|
+
))}
|
|
143
|
+
</ul>
|
|
144
|
+
<div aria-hidden="true" className={styles.dots} style={dotsStyle}>
|
|
145
|
+
<div className={styles.dotsTrack}>
|
|
146
|
+
{slides.map((_slide, index) => {
|
|
147
|
+
const isActive = index === activeIndex
|
|
148
|
+
const scale = getDotScale(index, dotsStart, total)
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<span key={index} className={styles.dotCell}>
|
|
152
|
+
<span
|
|
153
|
+
className={makeClassName([
|
|
154
|
+
[styles.dot, true],
|
|
155
|
+
[styles.isActive, isActive],
|
|
156
|
+
[styles.isSmall, scale === 'small'],
|
|
157
|
+
[styles.isTiny, scale === 'tiny']
|
|
158
|
+
])}
|
|
159
|
+
/>
|
|
160
|
+
</span>
|
|
161
|
+
)
|
|
162
|
+
})}
|
|
163
|
+
</div>
|
|
164
|
+
</div>
|
|
165
|
+
</div>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": "Читайте также",
|
|
3
|
+
"related": [
|
|
4
|
+
{
|
|
5
|
+
"id": "0",
|
|
6
|
+
"title": "На российскую больницу впервые подали в суд за отказ в посещении умирающего в реанимации родственника"
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"id": "1",
|
|
10
|
+
"title": "Мосгорсуд присудил компенсацию семье, которую не пустили в реанимацию к умирающей"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"id": "2",
|
|
14
|
+
"title": "«Реанимация должна быть открытой». Интервью с врачом о том, почему в России до сих пор не пускают к родственникам"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -27,6 +27,7 @@ export * from './ImportantLead'
|
|
|
27
27
|
export * from './ListBlock'
|
|
28
28
|
export * from './ListViewSwitcher'
|
|
29
29
|
export * from './RelatedBlock'
|
|
30
|
+
export * from './RelatedCarousel'
|
|
30
31
|
export * from './RichTitle'
|
|
31
32
|
export * from './SimpleBlock'
|
|
32
33
|
export * from './SimpleTitle'
|