@alfalab/core-components-page-indicator 1.0.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/Component.d.ts +5 -0
- package/Component.js +15 -0
- package/components/bullet/Component.d.ts +4 -0
- package/components/bullet/Component.js +64 -0
- package/components/bullet/default.css +41 -0
- package/components/bullet/index.css +64 -0
- package/components/bullet/index.d.ts +1 -0
- package/components/bullet/index.js +9 -0
- package/components/bullet/inverted.css +41 -0
- package/components/bullet/static-inverted.css +41 -0
- package/components/bullet/static.css +41 -0
- package/components/bullet/utils/index.d.ts +1 -0
- package/components/bullet/utils/index.js +9 -0
- package/components/bullet/utils/utils.d.ts +9 -0
- package/components/bullet/utils/utils.js +120 -0
- package/components/dynamic/Component.d.ts +5 -0
- package/components/dynamic/Component.js +93 -0
- package/components/dynamic/default.css +43 -0
- package/components/dynamic/index.css +53 -0
- package/components/dynamic/index.d.ts +1 -0
- package/components/dynamic/index.js +9 -0
- package/components/dynamic/inverted.css +43 -0
- package/components/dynamic/static-inverted.css +43 -0
- package/components/dynamic/static.css +43 -0
- package/components/runner/Component.d.ts +5 -0
- package/components/runner/Component.js +45 -0
- package/components/runner/default.css +41 -0
- package/components/runner/index.css +37 -0
- package/components/runner/index.d.ts +1 -0
- package/components/runner/index.js +9 -0
- package/components/runner/inverted.css +41 -0
- package/components/runner/static-inverted.css +41 -0
- package/components/runner/static.css +41 -0
- package/components/step/Component.d.ts +5 -0
- package/components/step/Component.js +44 -0
- package/components/step/default.css +41 -0
- package/components/step/index.css +44 -0
- package/components/step/index.d.ts +1 -0
- package/components/step/index.js +9 -0
- package/components/step/inverted.css +41 -0
- package/components/step/static-inverted.css +41 -0
- package/components/step/static.css +41 -0
- package/cssm/Component.d.ts +5 -0
- package/cssm/Component.js +15 -0
- package/cssm/components/bullet/Component.d.ts +4 -0
- package/cssm/components/bullet/Component.js +59 -0
- package/cssm/components/bullet/default.module.css +40 -0
- package/cssm/components/bullet/index.d.ts +1 -0
- package/cssm/components/bullet/index.js +9 -0
- package/cssm/components/bullet/index.module.css +63 -0
- package/cssm/components/bullet/inverted.module.css +40 -0
- package/cssm/components/bullet/static-inverted.module.css +40 -0
- package/cssm/components/bullet/static.module.css +40 -0
- package/cssm/components/bullet/utils/index.d.ts +1 -0
- package/cssm/components/bullet/utils/index.js +9 -0
- package/cssm/components/bullet/utils/utils.d.ts +9 -0
- package/cssm/components/bullet/utils/utils.js +120 -0
- package/cssm/components/dynamic/Component.d.ts +5 -0
- package/cssm/components/dynamic/Component.js +88 -0
- package/cssm/components/dynamic/default.module.css +42 -0
- package/cssm/components/dynamic/index.d.ts +1 -0
- package/cssm/components/dynamic/index.js +9 -0
- package/cssm/components/dynamic/index.module.css +52 -0
- package/cssm/components/dynamic/inverted.module.css +42 -0
- package/cssm/components/dynamic/static-inverted.module.css +42 -0
- package/cssm/components/dynamic/static.module.css +42 -0
- package/cssm/components/runner/Component.d.ts +5 -0
- package/cssm/components/runner/Component.js +40 -0
- package/cssm/components/runner/default.module.css +40 -0
- package/cssm/components/runner/index.d.ts +1 -0
- package/cssm/components/runner/index.js +9 -0
- package/cssm/components/runner/index.module.css +36 -0
- package/cssm/components/runner/inverted.module.css +40 -0
- package/cssm/components/runner/static-inverted.module.css +40 -0
- package/cssm/components/runner/static.module.css +40 -0
- package/cssm/components/step/Component.d.ts +5 -0
- package/cssm/components/step/Component.js +39 -0
- package/cssm/components/step/default.module.css +40 -0
- package/cssm/components/step/index.d.ts +1 -0
- package/cssm/components/step/index.js +9 -0
- package/cssm/components/step/index.module.css +43 -0
- package/cssm/components/step/inverted.module.css +40 -0
- package/cssm/components/step/static-inverted.module.css +40 -0
- package/cssm/components/step/static.module.css +40 -0
- package/cssm/index.d.ts +1 -0
- package/cssm/index.js +15 -0
- package/cssm/types.d.ts +129 -0
- package/cssm/types.js +2 -0
- package/cssm/vars.css +32 -0
- package/esm/Component.d.ts +5 -0
- package/esm/Component.js +4 -0
- package/esm/components/bullet/Component.d.ts +4 -0
- package/esm/components/bullet/Component.js +55 -0
- package/esm/components/bullet/default.css +41 -0
- package/esm/components/bullet/index.css +64 -0
- package/esm/components/bullet/index.d.ts +1 -0
- package/esm/components/bullet/index.js +1 -0
- package/esm/components/bullet/inverted.css +41 -0
- package/esm/components/bullet/static-inverted.css +41 -0
- package/esm/components/bullet/static.css +41 -0
- package/esm/components/bullet/utils/index.d.ts +1 -0
- package/esm/components/bullet/utils/index.js +1 -0
- package/esm/components/bullet/utils/utils.d.ts +9 -0
- package/esm/components/bullet/utils/utils.js +116 -0
- package/esm/components/dynamic/Component.d.ts +5 -0
- package/esm/components/dynamic/Component.js +84 -0
- package/esm/components/dynamic/default.css +43 -0
- package/esm/components/dynamic/index.css +53 -0
- package/esm/components/dynamic/index.d.ts +1 -0
- package/esm/components/dynamic/index.js +1 -0
- package/esm/components/dynamic/inverted.css +43 -0
- package/esm/components/dynamic/static-inverted.css +43 -0
- package/esm/components/dynamic/static.css +43 -0
- package/esm/components/runner/Component.d.ts +5 -0
- package/esm/components/runner/Component.js +36 -0
- package/esm/components/runner/default.css +41 -0
- package/esm/components/runner/index.css +37 -0
- package/esm/components/runner/index.d.ts +1 -0
- package/esm/components/runner/index.js +1 -0
- package/esm/components/runner/inverted.css +41 -0
- package/esm/components/runner/static-inverted.css +41 -0
- package/esm/components/runner/static.css +41 -0
- package/esm/components/step/Component.d.ts +5 -0
- package/esm/components/step/Component.js +35 -0
- package/esm/components/step/default.css +41 -0
- package/esm/components/step/index.css +44 -0
- package/esm/components/step/index.d.ts +1 -0
- package/esm/components/step/index.js +1 -0
- package/esm/components/step/inverted.css +41 -0
- package/esm/components/step/static-inverted.css +41 -0
- package/esm/components/step/static.css +41 -0
- package/esm/index.d.ts +1 -0
- package/esm/index.js +4 -0
- package/esm/types.d.ts +129 -0
- package/esm/types.js +1 -0
- package/index.d.ts +1 -0
- package/index.js +15 -0
- package/modern/Component.d.ts +5 -0
- package/modern/Component.js +4 -0
- package/modern/components/bullet/Component.d.ts +4 -0
- package/modern/components/bullet/Component.js +50 -0
- package/modern/components/bullet/default.css +41 -0
- package/modern/components/bullet/index.css +64 -0
- package/modern/components/bullet/index.d.ts +1 -0
- package/modern/components/bullet/index.js +1 -0
- package/modern/components/bullet/inverted.css +41 -0
- package/modern/components/bullet/static-inverted.css +41 -0
- package/modern/components/bullet/static.css +41 -0
- package/modern/components/bullet/utils/index.d.ts +1 -0
- package/modern/components/bullet/utils/index.js +1 -0
- package/modern/components/bullet/utils/utils.d.ts +9 -0
- package/modern/components/bullet/utils/utils.js +114 -0
- package/modern/components/dynamic/Component.d.ts +5 -0
- package/modern/components/dynamic/Component.js +83 -0
- package/modern/components/dynamic/default.css +43 -0
- package/modern/components/dynamic/index.css +53 -0
- package/modern/components/dynamic/index.d.ts +1 -0
- package/modern/components/dynamic/index.js +1 -0
- package/modern/components/dynamic/inverted.css +43 -0
- package/modern/components/dynamic/static-inverted.css +43 -0
- package/modern/components/dynamic/static.css +43 -0
- package/modern/components/runner/Component.d.ts +5 -0
- package/modern/components/runner/Component.js +33 -0
- package/modern/components/runner/default.css +41 -0
- package/modern/components/runner/index.css +37 -0
- package/modern/components/runner/index.d.ts +1 -0
- package/modern/components/runner/index.js +1 -0
- package/modern/components/runner/inverted.css +41 -0
- package/modern/components/runner/static-inverted.css +41 -0
- package/modern/components/runner/static.css +41 -0
- package/modern/components/step/Component.d.ts +5 -0
- package/modern/components/step/Component.js +29 -0
- package/modern/components/step/default.css +41 -0
- package/modern/components/step/index.css +44 -0
- package/modern/components/step/index.d.ts +1 -0
- package/modern/components/step/index.js +1 -0
- package/modern/components/step/inverted.css +41 -0
- package/modern/components/step/static-inverted.css +41 -0
- package/modern/components/step/static.css +41 -0
- package/modern/index.d.ts +1 -0
- package/modern/index.js +4 -0
- package/modern/types.d.ts +129 -0
- package/modern/types.js +1 -0
- package/moderncssm/Component.d.ts +5 -0
- package/moderncssm/Component.js +4 -0
- package/moderncssm/components/bullet/Component.d.ts +4 -0
- package/moderncssm/components/bullet/Component.js +40 -0
- package/moderncssm/components/bullet/default.module.css +22 -0
- package/moderncssm/components/bullet/index.d.ts +1 -0
- package/moderncssm/components/bullet/index.js +1 -0
- package/moderncssm/components/bullet/index.module.css +56 -0
- package/moderncssm/components/bullet/inverted.module.css +22 -0
- package/moderncssm/components/bullet/static-inverted.module.css +22 -0
- package/moderncssm/components/bullet/static.module.css +22 -0
- package/moderncssm/components/bullet/utils/index.d.ts +1 -0
- package/moderncssm/components/bullet/utils/index.js +1 -0
- package/moderncssm/components/bullet/utils/utils.d.ts +9 -0
- package/moderncssm/components/bullet/utils/utils.js +114 -0
- package/moderncssm/components/dynamic/Component.d.ts +5 -0
- package/moderncssm/components/dynamic/Component.js +73 -0
- package/moderncssm/components/dynamic/default.module.css +24 -0
- package/moderncssm/components/dynamic/index.d.ts +1 -0
- package/moderncssm/components/dynamic/index.js +1 -0
- package/moderncssm/components/dynamic/index.module.css +40 -0
- package/moderncssm/components/dynamic/inverted.module.css +24 -0
- package/moderncssm/components/dynamic/static-inverted.module.css +24 -0
- package/moderncssm/components/dynamic/static.module.css +24 -0
- package/moderncssm/components/runner/Component.d.ts +5 -0
- package/moderncssm/components/runner/Component.js +23 -0
- package/moderncssm/components/runner/default.module.css +22 -0
- package/moderncssm/components/runner/index.d.ts +1 -0
- package/moderncssm/components/runner/index.js +1 -0
- package/moderncssm/components/runner/index.module.css +18 -0
- package/moderncssm/components/runner/inverted.module.css +22 -0
- package/moderncssm/components/runner/static-inverted.module.css +22 -0
- package/moderncssm/components/runner/static.module.css +22 -0
- package/moderncssm/components/step/Component.d.ts +5 -0
- package/moderncssm/components/step/Component.js +19 -0
- package/moderncssm/components/step/default.module.css +22 -0
- package/moderncssm/components/step/index.d.ts +1 -0
- package/moderncssm/components/step/index.js +1 -0
- package/moderncssm/components/step/index.module.css +27 -0
- package/moderncssm/components/step/inverted.module.css +22 -0
- package/moderncssm/components/step/static-inverted.module.css +22 -0
- package/moderncssm/components/step/static.module.css +22 -0
- package/moderncssm/index.d.ts +1 -0
- package/moderncssm/index.js +4 -0
- package/moderncssm/types.d.ts +129 -0
- package/moderncssm/types.js +1 -0
- package/moderncssm/vars.css +12 -0
- package/package.json +28 -0
- package/src/Component.tsx +6 -0
- package/src/components/bullet/Component.tsx +84 -0
- package/src/components/bullet/default.module.css +9 -0
- package/src/components/bullet/index.module.css +42 -0
- package/src/components/bullet/index.ts +1 -0
- package/src/components/bullet/inverted.module.css +9 -0
- package/src/components/bullet/static-inverted.module.css +9 -0
- package/src/components/bullet/static.module.css +9 -0
- package/src/components/bullet/utils/index.ts +1 -0
- package/src/components/bullet/utils/utils.ts +153 -0
- package/src/components/dynamic/Component.tsx +126 -0
- package/src/components/dynamic/default.module.css +11 -0
- package/src/components/dynamic/index.module.css +25 -0
- package/src/components/dynamic/index.ts +1 -0
- package/src/components/dynamic/inverted.module.css +11 -0
- package/src/components/dynamic/static-inverted.module.css +11 -0
- package/src/components/dynamic/static.module.css +11 -0
- package/src/components/runner/Component.tsx +39 -0
- package/src/components/runner/default.module.css +9 -0
- package/src/components/runner/index.module.css +6 -0
- package/src/components/runner/index.ts +1 -0
- package/src/components/runner/inverted.module.css +9 -0
- package/src/components/runner/static-inverted.module.css +9 -0
- package/src/components/runner/static.module.css +9 -0
- package/src/components/step/Component.tsx +37 -0
- package/src/components/step/default.module.css +9 -0
- package/src/components/step/index.module.css +14 -0
- package/src/components/step/index.ts +1 -0
- package/src/components/step/inverted.module.css +9 -0
- package/src/components/step/static-inverted.module.css +9 -0
- package/src/components/step/static.module.css +9 -0
- package/src/index.ts +1 -0
- package/src/types.ts +131 -0
- package/src/vars.css +22 -0
- package/types.d.ts +129 -0
- package/types.js +2 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const RENDER_ELEMENTS_COUNT = 5;
|
|
2
|
+
|
|
3
|
+
export function calcParams(
|
|
4
|
+
size: number,
|
|
5
|
+
gap: number,
|
|
6
|
+
activeElementIndex: number,
|
|
7
|
+
elementsCount: number,
|
|
8
|
+
): [
|
|
9
|
+
height: number,
|
|
10
|
+
width: number,
|
|
11
|
+
offset: number,
|
|
12
|
+
elementSize: (index: number) => number,
|
|
13
|
+
firstVisibleElementIndex: number,
|
|
14
|
+
lastVisibleElementIndex: number,
|
|
15
|
+
] {
|
|
16
|
+
const renderElementsCount = Math.min(elementsCount, RENDER_ELEMENTS_COUNT);
|
|
17
|
+
const firstIndex = 0;
|
|
18
|
+
const lastIndex = elementsCount - 1;
|
|
19
|
+
|
|
20
|
+
function findFirstVisibleElementIndex(): number {
|
|
21
|
+
const middle = (renderElementsCount - 1) / 2;
|
|
22
|
+
let leadingOffset: number;
|
|
23
|
+
let tailingOffset: number;
|
|
24
|
+
|
|
25
|
+
if (middle % 1 === 0) {
|
|
26
|
+
leadingOffset = middle;
|
|
27
|
+
tailingOffset = middle;
|
|
28
|
+
} else {
|
|
29
|
+
leadingOffset = Math.floor(middle);
|
|
30
|
+
tailingOffset = Math.ceil(middle);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// rebalance leadingOffset
|
|
34
|
+
const maybeFirstVisibleElementIndex = activeElementIndex - leadingOffset;
|
|
35
|
+
|
|
36
|
+
if (maybeFirstVisibleElementIndex < firstIndex) {
|
|
37
|
+
leadingOffset -= firstIndex - maybeFirstVisibleElementIndex;
|
|
38
|
+
}
|
|
39
|
+
const maybeLastActiveElementIndex = activeElementIndex + tailingOffset;
|
|
40
|
+
|
|
41
|
+
if (maybeLastActiveElementIndex > lastIndex) {
|
|
42
|
+
leadingOffset += maybeLastActiveElementIndex - lastIndex;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return activeElementIndex - leadingOffset;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const firstVisibleElementIndex = findFirstVisibleElementIndex();
|
|
49
|
+
const lastVisibleElementIndex = renderElementsCount + firstVisibleElementIndex - 1;
|
|
50
|
+
|
|
51
|
+
const largeSize = size;
|
|
52
|
+
const middleSize = 0.75 * size;
|
|
53
|
+
const smallSize = 0.5 * size;
|
|
54
|
+
|
|
55
|
+
// eslint-disable-next-line complexity
|
|
56
|
+
function computeElementSize(_: unknown, index: number): number {
|
|
57
|
+
if (index < firstVisibleElementIndex || index > lastVisibleElementIndex) {
|
|
58
|
+
return smallSize;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (elementsCount === RENDER_ELEMENTS_COUNT + 1 /* totalCount === 6 */) {
|
|
62
|
+
if (
|
|
63
|
+
(index === firstVisibleElementIndex &&
|
|
64
|
+
!(firstVisibleElementIndex === firstIndex)) ||
|
|
65
|
+
(index === lastVisibleElementIndex && !(lastVisibleElementIndex === lastIndex))
|
|
66
|
+
) {
|
|
67
|
+
return middleSize;
|
|
68
|
+
}
|
|
69
|
+
} else if (elementsCount > RENDER_ELEMENTS_COUNT + 1 /* totalCount > 6 */) {
|
|
70
|
+
if (firstVisibleElementIndex === firstIndex) {
|
|
71
|
+
if (!(lastVisibleElementIndex === lastIndex)) {
|
|
72
|
+
if (index === lastVisibleElementIndex) {
|
|
73
|
+
if (activeElementIndex === lastVisibleElementIndex - 1) {
|
|
74
|
+
return middleSize;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return smallSize;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (index === lastVisibleElementIndex - 1) {
|
|
81
|
+
return middleSize;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (lastVisibleElementIndex === lastIndex) {
|
|
86
|
+
if (!(firstVisibleElementIndex === firstIndex)) {
|
|
87
|
+
if (index === firstVisibleElementIndex) {
|
|
88
|
+
if (activeElementIndex === firstVisibleElementIndex + 1) {
|
|
89
|
+
return middleSize;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return smallSize;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (index === firstVisibleElementIndex + 1) {
|
|
96
|
+
return middleSize;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (
|
|
102
|
+
(index === firstVisibleElementIndex &&
|
|
103
|
+
!(firstVisibleElementIndex === firstIndex)) ||
|
|
104
|
+
(index === lastVisibleElementIndex && !(lastVisibleElementIndex === lastIndex))
|
|
105
|
+
) {
|
|
106
|
+
return middleSize;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return largeSize;
|
|
111
|
+
}
|
|
112
|
+
const elementSizes = Array.from({ length: elementsCount }, computeElementSize);
|
|
113
|
+
const width = renderElementsCount * (largeSize + gap) - gap;
|
|
114
|
+
|
|
115
|
+
function actualWidth() {
|
|
116
|
+
return elementSizes
|
|
117
|
+
.slice(firstVisibleElementIndex, lastVisibleElementIndex + 1)
|
|
118
|
+
.map((elementSize, index, array) =>
|
|
119
|
+
index === array.length - 1 ? elementSize : elementSize + gap,
|
|
120
|
+
)
|
|
121
|
+
.reduce((a, b) => a + b, 0);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function offset() {
|
|
125
|
+
const extraOffset =
|
|
126
|
+
firstVisibleElementIndex === firstIndex
|
|
127
|
+
? 0
|
|
128
|
+
: (width - actualWidth()) / (lastVisibleElementIndex === lastIndex ? 1 : 2);
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
extraOffset -
|
|
132
|
+
elementSizes
|
|
133
|
+
.slice(firstIndex, firstVisibleElementIndex)
|
|
134
|
+
.map((elementSize) => elementSize + gap)
|
|
135
|
+
.reduce((a, b) => a + b, 0)
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function height() {
|
|
140
|
+
return Math.max(size, largeSize, middleSize, smallSize);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return [
|
|
144
|
+
height(),
|
|
145
|
+
width,
|
|
146
|
+
offset(),
|
|
147
|
+
function elementSize(index: number) {
|
|
148
|
+
return elementSizes[index];
|
|
149
|
+
},
|
|
150
|
+
firstVisibleElementIndex,
|
|
151
|
+
lastVisibleElementIndex,
|
|
152
|
+
];
|
|
153
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import cn from 'classnames';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
hasOwnProperty,
|
|
6
|
+
isFn,
|
|
7
|
+
isNonNullable,
|
|
8
|
+
isNullable,
|
|
9
|
+
noop,
|
|
10
|
+
} from '@alfalab/core-components-shared';
|
|
11
|
+
|
|
12
|
+
import { PageIndicatorDynamicProps } from '../../types';
|
|
13
|
+
|
|
14
|
+
import defaultColors from './default.module.css';
|
|
15
|
+
import styles from './index.module.css';
|
|
16
|
+
import invertedColors from './inverted.module.css';
|
|
17
|
+
import staticColors from './static.module.css';
|
|
18
|
+
import staticInvertedColors from './static-inverted.module.css';
|
|
19
|
+
|
|
20
|
+
const colorsStyle = {
|
|
21
|
+
inverted: invertedColors,
|
|
22
|
+
default: defaultColors,
|
|
23
|
+
static: staticColors,
|
|
24
|
+
'static-inverted': staticInvertedColors,
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
const FULL_PROGRESS = 100;
|
|
28
|
+
|
|
29
|
+
export const PageIndicatorDynamic: React.FC<PageIndicatorDynamicProps> = (props) => {
|
|
30
|
+
const {
|
|
31
|
+
size: height = 8,
|
|
32
|
+
gap = 8,
|
|
33
|
+
activeElementWidth = height * 7,
|
|
34
|
+
elements: count = 10,
|
|
35
|
+
activeElement: indexFromProps,
|
|
36
|
+
onActiveElementChange,
|
|
37
|
+
defaultActiveElement: defaultIndex = 0,
|
|
38
|
+
active = true,
|
|
39
|
+
duration: durationFromProps = 3000,
|
|
40
|
+
cycle = false,
|
|
41
|
+
colors = 'default',
|
|
42
|
+
} = props;
|
|
43
|
+
const [activeIndex, setActiveIndex] = useState<number | undefined>(
|
|
44
|
+
indexFromProps ?? defaultIndex,
|
|
45
|
+
);
|
|
46
|
+
const [progress, setProgress] = useState(0);
|
|
47
|
+
const inProgress = progress < FULL_PROGRESS;
|
|
48
|
+
const activeIndexInProps = hasOwnProperty(props, 'activeElement');
|
|
49
|
+
let duration: number;
|
|
50
|
+
|
|
51
|
+
if (active && isNonNullable(activeIndex) && inProgress) {
|
|
52
|
+
duration = isFn(durationFromProps) ? durationFromProps(activeIndex) : durationFromProps;
|
|
53
|
+
} else {
|
|
54
|
+
duration = -1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// getDerivedStateFromProps
|
|
58
|
+
if (activeIndexInProps && !(activeIndex === indexFromProps)) {
|
|
59
|
+
setActiveIndex(indexFromProps);
|
|
60
|
+
setProgress(0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (duration === -1) {
|
|
65
|
+
return noop;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const interval = duration / FULL_PROGRESS;
|
|
69
|
+
const timer = setInterval(
|
|
70
|
+
() => setProgress((prevProgress) => Math.min(prevProgress + 1, FULL_PROGRESS)),
|
|
71
|
+
interval,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
return () => clearInterval(timer);
|
|
75
|
+
}, [duration]);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const isLast = activeIndex === count - 1;
|
|
79
|
+
|
|
80
|
+
if (inProgress || isNullable(activeIndex) || (isLast && !cycle)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const nextActiveIndex = isLast && cycle ? 0 : activeIndex + 1;
|
|
85
|
+
|
|
86
|
+
onActiveElementChange?.(nextActiveIndex);
|
|
87
|
+
|
|
88
|
+
if (activeIndexInProps) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
setActiveIndex(nextActiveIndex);
|
|
93
|
+
setProgress(0);
|
|
94
|
+
}, [activeIndex, activeIndexInProps, count, cycle, inProgress, onActiveElementChange]);
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<ol className={styles.pageIndicator} style={{ height, gap }}>
|
|
98
|
+
{Array.from({ length: count }, (_, index) => {
|
|
99
|
+
const isActive = index === activeIndex;
|
|
100
|
+
const style: React.CSSProperties = {
|
|
101
|
+
width: isActive ? activeElementWidth : height,
|
|
102
|
+
borderRadius: height / 2,
|
|
103
|
+
};
|
|
104
|
+
const progressStyle: React.CSSProperties | undefined = isActive
|
|
105
|
+
? { transform: `translateX(${progress - FULL_PROGRESS}%)` }
|
|
106
|
+
: undefined;
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<li
|
|
110
|
+
key={index}
|
|
111
|
+
style={style}
|
|
112
|
+
className={cn(styles.element, colorsStyle[colors].element, {
|
|
113
|
+
[styles.active]: isActive,
|
|
114
|
+
[colorsStyle[colors].active]: isActive,
|
|
115
|
+
})}
|
|
116
|
+
>
|
|
117
|
+
<div
|
|
118
|
+
style={progressStyle}
|
|
119
|
+
className={cn(styles.progress, colorsStyle[colors].progress)}
|
|
120
|
+
/>
|
|
121
|
+
</li>
|
|
122
|
+
);
|
|
123
|
+
})}
|
|
124
|
+
</ol>
|
|
125
|
+
);
|
|
126
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
@import '../../vars.css';
|
|
2
|
+
|
|
3
|
+
.pageIndicator {
|
|
4
|
+
margin: 0;
|
|
5
|
+
padding: 0;
|
|
6
|
+
display: flex;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.element {
|
|
10
|
+
list-style-type: none;
|
|
11
|
+
overflow: hidden;
|
|
12
|
+
transition: width var(--page-indicator-animation-duration) linear;
|
|
13
|
+
|
|
14
|
+
&:not(.active) {
|
|
15
|
+
.progress {
|
|
16
|
+
display: none;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.progress {
|
|
21
|
+
width: 100%;
|
|
22
|
+
height: 100%;
|
|
23
|
+
will-change: transform;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Component';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import cn from 'classnames';
|
|
3
|
+
|
|
4
|
+
import { PageIndicatorRunnerProps } from '../../types';
|
|
5
|
+
|
|
6
|
+
import defaultColors from './default.module.css';
|
|
7
|
+
import styles from './index.module.css';
|
|
8
|
+
import invertedColors from './inverted.module.css';
|
|
9
|
+
import staticColors from './static.module.css';
|
|
10
|
+
import staticInvertedColors from './static-inverted.module.css';
|
|
11
|
+
|
|
12
|
+
const colorsStyle = {
|
|
13
|
+
inverted: invertedColors,
|
|
14
|
+
default: defaultColors,
|
|
15
|
+
static: staticColors,
|
|
16
|
+
'static-inverted': staticInvertedColors,
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
export const PageIndicatorRunner: React.FC<PageIndicatorRunnerProps> = ({
|
|
20
|
+
activeElement,
|
|
21
|
+
elements: count = 10,
|
|
22
|
+
size = 4,
|
|
23
|
+
colors = 'default',
|
|
24
|
+
}) => (
|
|
25
|
+
<div
|
|
26
|
+
className={cn(styles.pageIndicator, colorsStyle[colors].pageIndicator)}
|
|
27
|
+
style={{ height: size, borderRadius: size / 2 }}
|
|
28
|
+
>
|
|
29
|
+
<div
|
|
30
|
+
style={{
|
|
31
|
+
transform: `translate(${activeElement * 100}%, 0)`,
|
|
32
|
+
height: size,
|
|
33
|
+
width: `${100 / count}%`,
|
|
34
|
+
borderRadius: size / 2,
|
|
35
|
+
}}
|
|
36
|
+
className={cn(styles.element, colorsStyle[colors].element)}
|
|
37
|
+
/>
|
|
38
|
+
</div>
|
|
39
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Component';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import cn from 'classnames';
|
|
3
|
+
|
|
4
|
+
import { PageIndicatorStepProps } from '../../types';
|
|
5
|
+
|
|
6
|
+
import defaultColors from './default.module.css';
|
|
7
|
+
import styles from './index.module.css';
|
|
8
|
+
import invertedColors from './inverted.module.css';
|
|
9
|
+
import staticColors from './static.module.css';
|
|
10
|
+
import staticInvertedColors from './static-inverted.module.css';
|
|
11
|
+
|
|
12
|
+
const colorsStyle = {
|
|
13
|
+
inverted: invertedColors,
|
|
14
|
+
default: defaultColors,
|
|
15
|
+
static: staticColors,
|
|
16
|
+
'static-inverted': staticInvertedColors,
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
export const PageIndicatorStep: React.FC<PageIndicatorStepProps> = ({
|
|
20
|
+
activeElement,
|
|
21
|
+
elements: count = 10,
|
|
22
|
+
size = 4,
|
|
23
|
+
gap = 4,
|
|
24
|
+
colors = 'default',
|
|
25
|
+
}) => (
|
|
26
|
+
<ol className={styles.pageIndicator} style={{ height: size, gap }}>
|
|
27
|
+
{Array.from({ length: count }, (_, index) => (
|
|
28
|
+
<li
|
|
29
|
+
key={index}
|
|
30
|
+
style={{ height: size, borderRadius: size / 2 }}
|
|
31
|
+
className={cn(styles.element, colorsStyle[colors].element, {
|
|
32
|
+
[colorsStyle[colors].active]: index === activeElement,
|
|
33
|
+
})}
|
|
34
|
+
/>
|
|
35
|
+
))}
|
|
36
|
+
</ol>
|
|
37
|
+
);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
@import '../../vars.css';
|
|
2
|
+
|
|
3
|
+
.pageIndicator {
|
|
4
|
+
margin: 0;
|
|
5
|
+
padding: 0;
|
|
6
|
+
display: flex;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.element {
|
|
10
|
+
list-style-type: none;
|
|
11
|
+
flex-grow: 1;
|
|
12
|
+
transition: backgroud var(--page-indicator-animation-duration)
|
|
13
|
+
var(--page-indicator-animation-timing-function);
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Component';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Component';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
export interface PageIndicatorDynamicProps {
|
|
2
|
+
/**
|
|
3
|
+
* Индекс выбранного элемента по-умолчанию
|
|
4
|
+
* @default 0
|
|
5
|
+
*/
|
|
6
|
+
defaultActiveElement?: number;
|
|
7
|
+
/**
|
|
8
|
+
* Индекс выбранного элемента
|
|
9
|
+
*/
|
|
10
|
+
activeElement?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Количество элементов (минимум 2)
|
|
13
|
+
* @default 10
|
|
14
|
+
*/
|
|
15
|
+
elements?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Высота компонента
|
|
18
|
+
* @default 8
|
|
19
|
+
*/
|
|
20
|
+
size?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Ширина выбранного элемента
|
|
23
|
+
* @default size * 7
|
|
24
|
+
*/
|
|
25
|
+
activeElementWidth?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Расстояние между элементами
|
|
28
|
+
* @default 8
|
|
29
|
+
*/
|
|
30
|
+
gap?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Продолжительность прогресса выбранного элемента, в миллисекундах
|
|
33
|
+
* @default 3000
|
|
34
|
+
*/
|
|
35
|
+
duration?: (activeElement: number) => number | number;
|
|
36
|
+
/**
|
|
37
|
+
* Обработчик при изменении выбранного элемента. Вызывается при заполнении прогресса предыдущего выбранного элемента
|
|
38
|
+
*/
|
|
39
|
+
onActiveElementChange?: (nextActiveElement: number) => void;
|
|
40
|
+
/**
|
|
41
|
+
* Зациклен ли обход элементов
|
|
42
|
+
* @default false
|
|
43
|
+
*/
|
|
44
|
+
cycle?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Активен ли прогресс выбранного элемента
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
active?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Набор цветов для компонента
|
|
52
|
+
* @default default
|
|
53
|
+
*/
|
|
54
|
+
colors?: 'default' | 'inverted' | 'static' | 'static-inverted';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface PageIndicatorBulletProps {
|
|
58
|
+
/**
|
|
59
|
+
* Индекс выбранного элемента
|
|
60
|
+
*/
|
|
61
|
+
activeElement: number;
|
|
62
|
+
/**
|
|
63
|
+
* Количество элементов (минимум 2)
|
|
64
|
+
* @default 10
|
|
65
|
+
*/
|
|
66
|
+
elements?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Высота компонента
|
|
69
|
+
* @default 8
|
|
70
|
+
*/
|
|
71
|
+
size?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Расстояние между элементами
|
|
74
|
+
* @default 8
|
|
75
|
+
*/
|
|
76
|
+
gap?: number;
|
|
77
|
+
/**
|
|
78
|
+
* Набор цветов для компонента
|
|
79
|
+
* @default default
|
|
80
|
+
*/
|
|
81
|
+
colors?: 'default' | 'inverted' | 'static' | 'static-inverted';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface PageIndicatorStepProps {
|
|
85
|
+
/**
|
|
86
|
+
* Индекс выбранного элемента
|
|
87
|
+
*/
|
|
88
|
+
activeElement: number;
|
|
89
|
+
/**
|
|
90
|
+
* Количество элементов (минимум 2)
|
|
91
|
+
* @default 10
|
|
92
|
+
*/
|
|
93
|
+
elements?: number;
|
|
94
|
+
/**
|
|
95
|
+
* Высота компонента
|
|
96
|
+
* @default 4
|
|
97
|
+
*/
|
|
98
|
+
size?: number;
|
|
99
|
+
/**
|
|
100
|
+
* Расстояние между элементами
|
|
101
|
+
* @default 4
|
|
102
|
+
*/
|
|
103
|
+
gap?: number;
|
|
104
|
+
/**
|
|
105
|
+
* Набор цветов для компонента
|
|
106
|
+
* @default default
|
|
107
|
+
*/
|
|
108
|
+
colors?: 'default' | 'inverted' | 'static' | 'static-inverted';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface PageIndicatorRunnerProps {
|
|
112
|
+
/**
|
|
113
|
+
* Индекс выбранного элемента
|
|
114
|
+
*/
|
|
115
|
+
activeElement: number;
|
|
116
|
+
/**
|
|
117
|
+
* Количество элементов (минимум 2)
|
|
118
|
+
* @default 10
|
|
119
|
+
*/
|
|
120
|
+
elements?: number;
|
|
121
|
+
/**
|
|
122
|
+
* Высота компонента
|
|
123
|
+
* @default 4
|
|
124
|
+
*/
|
|
125
|
+
size?: number;
|
|
126
|
+
/**
|
|
127
|
+
* Набор цветов для компонента
|
|
128
|
+
* @default default
|
|
129
|
+
*/
|
|
130
|
+
colors?: 'default' | 'inverted' | 'static' | 'static-inverted';
|
|
131
|
+
}
|