@meduza/ui-kit-2 1.3.0 → 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.
@@ -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'