react-fancy-circular-carousel 1.0.7 → 1.1.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/FancyCarousel.css +25 -3
- package/LICENSE +1 -1
- package/README.md +64 -26
- package/{FancyCarousel.d.ts → cjs/FancyCarousel.d.ts} +11 -6
- package/cjs/FancyCarousel.js +180 -0
- package/cjs/index.d.ts +4 -0
- package/cjs/index.js +6 -0
- package/esm/FancyCarousel.d.ts +29 -0
- package/esm/FancyCarousel.js +153 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +3 -0
- package/esm/package.json +1 -0
- package/package.json +25 -3
- package/FancyCarousel.js +0 -83
- package/index.d.ts +0 -2
- package/index.js +0 -2
package/FancyCarousel.css
CHANGED
|
@@ -26,16 +26,19 @@
|
|
|
26
26
|
.fancy-carousel-navigation-button {
|
|
27
27
|
color: white;
|
|
28
28
|
border-radius: 50%;
|
|
29
|
-
outline: none;
|
|
30
29
|
border: none;
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
.fancy-carousel-navigation-button:hover {
|
|
34
|
-
outline: none;
|
|
35
33
|
border: none;
|
|
36
34
|
cursor: pointer;
|
|
37
35
|
}
|
|
38
36
|
|
|
37
|
+
/* keep the focus ring for keyboard users only */
|
|
38
|
+
.fancy-carousel-navigation-button:focus:not(:focus-visible), .fancy-carousel-wrapper-element:focus:not(:focus-visible) {
|
|
39
|
+
outline: none;
|
|
40
|
+
}
|
|
41
|
+
|
|
39
42
|
.fancy-carousel-element {
|
|
40
43
|
position: absolute;
|
|
41
44
|
background: transparent;
|
|
@@ -51,6 +54,25 @@
|
|
|
51
54
|
border-radius: 50%;
|
|
52
55
|
}
|
|
53
56
|
|
|
54
|
-
.
|
|
57
|
+
.fancy-carousel-image, .fancy-carousel-central-image {
|
|
58
|
+
object-fit: cover;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.fancy-carousel-item {
|
|
62
|
+
border-radius: 50%;
|
|
63
|
+
overflow: hidden;
|
|
64
|
+
display: flex;
|
|
65
|
+
align-items: center;
|
|
66
|
+
justify-content: center;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.fancy-carousel-hidden {
|
|
55
70
|
display: none;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@media (prefers-reduced-motion: reduce) {
|
|
74
|
+
.fancy-carousel-border, .fancy-carousel, .fancy-carousel-element,
|
|
75
|
+
.fancy-carousel-image, .fancy-carousel-central-image, .fancy-carousel-item {
|
|
76
|
+
transition: none !important;
|
|
77
|
+
}
|
|
56
78
|
}
|
package/LICENSE
CHANGED
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<div align="center"><p align="center"><a href="https://www.npmjs.com/package/react-fancy-circular-carousel"><img src="
|
|
1
|
+
<div align="center"><p align="center"><a href="https://www.npmjs.com/package/react-fancy-circular-carousel"><img src="./src/assets/React.webp" alt="React Logo - Carousel UI Component" width="140px" /></a></p></div>
|
|
2
2
|
|
|
3
3
|
<h1 align="center">React Circular Carousel Component</h1>
|
|
4
4
|
|
|
@@ -9,6 +9,7 @@ Try the demo [here](https://carousel-demopage.netlify.app).
|
|
|
9
9
|
- Circular image carousel
|
|
10
10
|
- Everything you see is customisable
|
|
11
11
|
- Get focus element to add in your own components
|
|
12
|
+
- Images or any custom elements, keyboard and click navigation
|
|
12
13
|
- Smooth, easy to implement, fantastic UI
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
@@ -18,7 +19,7 @@ Try the demo [here](https://carousel-demopage.netlify.app).
|
|
|
18
19
|
Include the styles by importing FancyCarousel.css
|
|
19
20
|
|
|
20
21
|
```jsx
|
|
21
|
-
import '
|
|
22
|
+
import 'react-fancy-circular-carousel/FancyCarousel.css';
|
|
22
23
|
```
|
|
23
24
|
|
|
24
25
|
## Quickstart
|
|
@@ -27,7 +28,9 @@ Try the demo [here](https://carousel-demopage.netlify.app).
|
|
|
27
28
|
|
|
28
29
|
| **Parameter** | **Type** | **Description** |
|
|
29
30
|
|:---|:---|:---|
|
|
30
|
-
| images | String[] | `mandatory` List of image sources |
|
|
31
|
+
| images | String[] | `mandatory` List of image sources (unless you pass children instead) |
|
|
32
|
+
| imageAlts | String[] | `optional` Alt text for each image, in the same order as `images` |
|
|
33
|
+
| children | React.ReactNode | `optional` Custom elements to place around the carousel instead of images |
|
|
31
34
|
| setFocusElement | Function | `optional` SetState method to get focus element in parent component |
|
|
32
35
|
| offsetAngle | Number | `optional` Angle in degrees to offset the carousel elements |
|
|
33
36
|
| carouselRadius | Number | `optional` Carousel radius in px |
|
|
@@ -38,38 +41,41 @@ Try the demo [here](https://carousel-demopage.netlify.app).
|
|
|
38
41
|
| focusElementStyling | React.CSSProperties | `optional` Custom styling for the element in focus |
|
|
39
42
|
| border | Boolean | `optional` Enable/disable bordering |
|
|
40
43
|
| borderWidth | Number | `optional` Border stroke width property in px |
|
|
41
|
-
| borderHexColor | String | `optional` Border hex color
|
|
44
|
+
| borderHexColor | String | `optional` Border hex color, with or without the '#' |
|
|
42
45
|
| autoRotateTime | Number | `optional` Time in seconds for an auto rotating carousel |
|
|
46
|
+
| pauseOnHover | Boolean | `optional` Pause auto rotation while hovered or focused with the keyboard (default true) |
|
|
43
47
|
| transitionTime | Number | `optional` Time in seconds for the rotating transition |
|
|
44
|
-
| navigationTextSize | Number | `optional`
|
|
48
|
+
| navigationTextSize | Number | `optional` Arrow font size in rem |
|
|
45
49
|
| navigationButtonRadius | Number | `optional` Navigation button radius in px |
|
|
46
|
-
| navigationButtonColor | String | `optional`
|
|
47
|
-
| navigationButtonBgColor | String | `optional` Navigation button hex color
|
|
50
|
+
| navigationButtonColor | String | `optional` Arrow hex color, with or without the '#' |
|
|
51
|
+
| navigationButtonBgColor | String | `optional` Navigation button hex color, with or without the '#' |
|
|
48
52
|
| navigationButtonStyling | React.CSSProperties | `optional` Custom styling for navigation buttons |
|
|
49
|
-
|
|
53
|
+
| showNavigation | Boolean | `optional` Show the navigation buttons (default: shown unless auto rotating) |
|
|
54
|
+
| rotateOnClick | Boolean | `optional` Clicking an element rotates it into focus (default true) |
|
|
55
|
+
| responsive | Boolean | `optional` Fill the width of the parent element, scaling every size down to fit (never above the sizes given) |
|
|
50
56
|
|
|
51
57
|
### Usage
|
|
52
58
|
|
|
53
59
|
```jsx
|
|
54
|
-
|
|
55
60
|
import React from "react";
|
|
56
61
|
|
|
57
62
|
import FancyCarousel from "react-fancy-circular-carousel";
|
|
58
|
-
import '
|
|
63
|
+
import 'react-fancy-circular-carousel/FancyCarousel.css';
|
|
59
64
|
|
|
60
65
|
import image1 from './assets/image1.png';
|
|
61
66
|
import image2 from './assets/image2.png';
|
|
62
67
|
import image3 from './assets/image3.png';
|
|
63
68
|
import image4 from './assets/image4.png';
|
|
64
69
|
|
|
65
|
-
|
|
70
|
+
|
|
71
|
+
const YourComponent = () => {
|
|
66
72
|
|
|
67
73
|
const images = [image1, image2, image3, image4];
|
|
68
74
|
|
|
69
75
|
return <div className="carousel">
|
|
70
76
|
<FancyCarousel images={images} />
|
|
71
77
|
</div>
|
|
72
|
-
|
|
78
|
+
};
|
|
73
79
|
|
|
74
80
|
export default YourComponent;
|
|
75
81
|
```
|
|
@@ -77,21 +83,20 @@ export default YourComponent;
|
|
|
77
83
|
### Retrieve focus element
|
|
78
84
|
|
|
79
85
|
Utility using which one can know which element is currently at focus and may add relevant components around it.
|
|
80
|
-
|
|
81
86
|
For eg. if the carousel portrays images of cities, this feature may help you update the description in the information box along side the carousel.
|
|
82
87
|
|
|
83
88
|
```jsx
|
|
84
89
|
import React, { useState } from "react";
|
|
85
90
|
|
|
86
91
|
import FancyCarousel from "react-fancy-circular-carousel";
|
|
87
|
-
import '
|
|
92
|
+
import 'react-fancy-circular-carousel/FancyCarousel.css';
|
|
88
93
|
|
|
89
94
|
import image1 from './assets/image1.png';
|
|
90
95
|
import image2 from './assets/image2.png';
|
|
91
96
|
import image3 from './assets/image3.png';
|
|
92
97
|
import image4 from './assets/image4.png';
|
|
93
98
|
|
|
94
|
-
const YourComponent = () =>
|
|
99
|
+
const YourComponent = () => {
|
|
95
100
|
|
|
96
101
|
// the focus element would be the array index of the image currently at focus
|
|
97
102
|
const [focusElement, setFocusElement] = useState(0);
|
|
@@ -104,8 +109,42 @@ const YourComponent = () => (
|
|
|
104
109
|
<div className="info-box-wrapper">
|
|
105
110
|
<p> {info[focusElement]} </p>
|
|
106
111
|
</div>
|
|
107
|
-
|
|
108
|
-
|
|
112
|
+
</div>
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export default YourComponent;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Keyboard and imports
|
|
119
|
+
|
|
120
|
+
The left and right arrow keys turn the carousel when it has keyboard focus. The component is also available as a named export, along with its props type:
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
import { FancyCarousel, FancyCarouselProps } from "react-fancy-circular-carousel";
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Custom elements
|
|
127
|
+
|
|
128
|
+
Pass children instead of `images` to place your own elements around the carousel. Each child is clipped to a circle, and the element in focus is also shown in the centre, so it is rendered twice.
|
|
129
|
+
|
|
130
|
+
```jsx
|
|
131
|
+
import React from "react";
|
|
132
|
+
|
|
133
|
+
import FancyCarousel from "react-fancy-circular-carousel";
|
|
134
|
+
import 'react-fancy-circular-carousel/FancyCarousel.css';
|
|
135
|
+
|
|
136
|
+
const cities = ['Delhi', 'Mumbai', 'Bengaluru', 'Kolkata'];
|
|
137
|
+
|
|
138
|
+
const YourComponent = () => {
|
|
139
|
+
|
|
140
|
+
return <div className="carousel">
|
|
141
|
+
<FancyCarousel>
|
|
142
|
+
{cities.map(city => (
|
|
143
|
+
<div key={city} className="city-card">{city}</div>
|
|
144
|
+
))}
|
|
145
|
+
</FancyCarousel>
|
|
146
|
+
</div>
|
|
147
|
+
};
|
|
109
148
|
|
|
110
149
|
export default YourComponent;
|
|
111
150
|
```
|
|
@@ -113,7 +152,6 @@ export default YourComponent;
|
|
|
113
152
|
### Customisations
|
|
114
153
|
|
|
115
154
|
Customise the carousel to your needs such as modifying the carousel size, image size, adding styling such as drop shadow, adding auto rotate, etc
|
|
116
|
-
|
|
117
155
|
The focusElementStyling and navigationButtonStyling props are of type React.CSSProperties, which is essentially a JavaScript object containing camelcased CSS properties and their corresponding values.
|
|
118
156
|
|
|
119
157
|
To know more check the official documentation [here](https://legacy.reactjs.org/docs/dom-elements.html#style).
|
|
@@ -122,14 +160,14 @@ To know more check the official documentation [here](https://legacy.reactjs.org/
|
|
|
122
160
|
import React from "react";
|
|
123
161
|
|
|
124
162
|
import FancyCarousel from "react-fancy-circular-carousel";
|
|
125
|
-
import '
|
|
163
|
+
import 'react-fancy-circular-carousel/FancyCarousel.css';
|
|
126
164
|
|
|
127
165
|
import image1 from './assets/image1.png';
|
|
128
166
|
import image2 from './assets/image2.png';
|
|
129
167
|
import image3 from './assets/image3.png';
|
|
130
168
|
import image4 from './assets/image4.png';
|
|
131
169
|
|
|
132
|
-
const YourComponent = () =>
|
|
170
|
+
const YourComponent = () => {
|
|
133
171
|
|
|
134
172
|
const images = [image1, image2, image3, image4];
|
|
135
173
|
|
|
@@ -145,7 +183,7 @@ const YourComponent = () => (
|
|
|
145
183
|
borderHexColor={'1c364f'}
|
|
146
184
|
/>
|
|
147
185
|
</div>
|
|
148
|
-
|
|
186
|
+
};
|
|
149
187
|
|
|
150
188
|
export default YourComponent;
|
|
151
189
|
```
|
|
@@ -154,16 +192,16 @@ export default YourComponent;
|
|
|
154
192
|
|
|
155
193
|
With navigators
|
|
156
194
|
|
|
157
|
-

|
|
158
196
|
|
|
159
197
|
With auto rotation
|
|
160
198
|
|
|
161
|
-

|
|
162
200
|
|
|
163
|
-
##
|
|
201
|
+
## NPM Module
|
|
164
202
|
|
|
165
|
-
[
|
|
166
|
-
Try it out for yourselves and ⭐
|
|
203
|
+
[Npm Page](https://www.npmjs.com/package/react-fancy-circular-carousel)
|
|
204
|
+
Try it out for yourselves and ⭐ this repo if you like it!
|
|
167
205
|
|
|
168
206
|
## License
|
|
169
207
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { FC, CSSProperties } from 'react';
|
|
2
|
-
interface
|
|
3
|
-
images
|
|
4
|
-
|
|
1
|
+
import { FC, ReactNode, CSSProperties } from 'react';
|
|
2
|
+
export interface FancyCarouselProps {
|
|
3
|
+
images?: string[];
|
|
4
|
+
imageAlts?: string[];
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
setFocusElement?: (focusElement: number) => void;
|
|
5
7
|
offsetAngle?: number;
|
|
6
8
|
carouselRadius?: number;
|
|
7
9
|
centralImageRadius?: number;
|
|
@@ -19,6 +21,9 @@ interface CarouselInfo {
|
|
|
19
21
|
navigationButtonBgColor?: string;
|
|
20
22
|
navigationButtonColor?: string;
|
|
21
23
|
navigationButtonStyling?: CSSProperties;
|
|
24
|
+
rotateOnClick?: boolean;
|
|
25
|
+
pauseOnHover?: boolean;
|
|
26
|
+
showNavigation?: boolean;
|
|
27
|
+
responsive?: boolean;
|
|
22
28
|
}
|
|
23
|
-
export declare const FancyCarousel: FC<
|
|
24
|
-
export {};
|
|
29
|
+
export declare const FancyCarousel: FC<FancyCarouselProps>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.FancyCarousel = void 0;
|
|
27
|
+
const react_1 = __importStar(require("react"));
|
|
28
|
+
const useIsomorphicLayoutEffect = (typeof window !== 'undefined') ? react_1.useLayoutEffect : react_1.useEffect;
|
|
29
|
+
// matches the margin on .fancy-carousel-element in FancyCarousel.css
|
|
30
|
+
const elementMargin = 2;
|
|
31
|
+
// colours may be given with or without the '#', as 3 or 6 digit hex
|
|
32
|
+
const hex = (color) => color.replace(/^#/, '');
|
|
33
|
+
const FancyCarousel = ({ images, imageAlts = [], children, setFocusElement = () => { }, offsetAngle = 0, carouselRadius: baseCarouselRadius = 400, centralImageRadius: baseCentralImageRadius = 125, centralImageBoxShadow = '5px 10px 18px #888888', peripheralImageRadius: basePeripheralImageRadius = 75, peripheralImageBoxShadow = '5px 10px 18px #888888', focusElementStyling = {}, border = true, borderWidth: baseBorderWidth = 5, borderHexColor = 'CB786C', autoRotateTime = 0, transitionTime = 0.5, navigationTextSize: baseNavigationTextSize = 2, navigationButtonRadius: baseNavigationButtonRadius = 32.5, navigationButtonBgColor = 'CB786C', navigationButtonColor = 'FFFFFF', navigationButtonStyling = {}, rotateOnClick = true, pauseOnHover = true, showNavigation = !autoRotateTime, responsive = false }) => {
|
|
34
|
+
// number of steps the ring has turned, +1 for every "next"
|
|
35
|
+
const [step, setStep] = (0, react_1.useState)(0);
|
|
36
|
+
const [hovered, setHovered] = (0, react_1.useState)(false);
|
|
37
|
+
const [keyboardFocused, setKeyboardFocused] = (0, react_1.useState)(false);
|
|
38
|
+
const paused = pauseOnHover && (hovered || keyboardFocused);
|
|
39
|
+
// scale factor for all sizes, 0 until measured so nothing animates in from full size
|
|
40
|
+
const [scale, setScale] = (0, react_1.useState)(responsive ? 0 : 1);
|
|
41
|
+
const wrapperRef = (0, react_1.useRef)(null);
|
|
42
|
+
useIsomorphicLayoutEffect(() => {
|
|
43
|
+
const wrapper = wrapperRef.current;
|
|
44
|
+
if (!responsive || !wrapper) {
|
|
45
|
+
setScale(1);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// the wrapper fills its parent and ignores its own contents (contain: inline-size),
|
|
49
|
+
// so its width never depends on the scale; fall back to the viewport if it has none
|
|
50
|
+
const fit = () => {
|
|
51
|
+
const width = (wrapper.clientWidth || document.documentElement.clientWidth) - 2 * elementMargin;
|
|
52
|
+
setScale(Math.min(1, width / (2 * (baseCarouselRadius + basePeripheralImageRadius))));
|
|
53
|
+
};
|
|
54
|
+
fit();
|
|
55
|
+
const observer = new ResizeObserver(fit);
|
|
56
|
+
observer.observe(wrapper);
|
|
57
|
+
return () => observer.disconnect();
|
|
58
|
+
}, [responsive, baseCarouselRadius, basePeripheralImageRadius]);
|
|
59
|
+
const carouselRadius = baseCarouselRadius * scale;
|
|
60
|
+
const centralImageRadius = baseCentralImageRadius * scale;
|
|
61
|
+
const peripheralImageRadius = basePeripheralImageRadius * scale;
|
|
62
|
+
const borderWidth = baseBorderWidth * scale;
|
|
63
|
+
const navigationTextSize = baseNavigationTextSize * scale;
|
|
64
|
+
const navigationButtonRadius = baseNavigationButtonRadius * scale;
|
|
65
|
+
// images take priority; otherwise any children are placed around the ring
|
|
66
|
+
const childItems = react_1.Children.toArray(children);
|
|
67
|
+
const noOfImages = (images) ? images.length : childItems.length;
|
|
68
|
+
const theta = 360 / noOfImages;
|
|
69
|
+
// derived from the step so it stays in range when the number of items changes
|
|
70
|
+
const focusElement = (noOfImages) ? ((step % noOfImages) + noOfImages) % noOfImages : 0;
|
|
71
|
+
const carouselOrientation = (noOfImages) ? step * theta : 0;
|
|
72
|
+
const borderElement = (border) ? `url("data:image/svg+xml,%3csvg width=\'100%25\' height=\'100%25\' xmlns=\'http://www.w3.org/2000/svg\'%3e%3crect width=\'100%25\' height=\'100%25\' fill=\'none\' rx=\'${carouselRadius * 2}\' ry=\'${carouselRadius * 2}\' stroke=\'%23${hex(borderHexColor)}\' stroke-width=\'${borderWidth}\' stroke-dasharray=\'6%2c 24\' stroke-dashoffset=\'0\' stroke-linecap=\'square\'/%3e%3c/svg%3e")` : '';
|
|
73
|
+
const rotateRight = () => {
|
|
74
|
+
setStep(prev => prev + 1);
|
|
75
|
+
};
|
|
76
|
+
const rotateLeft = () => {
|
|
77
|
+
setStep(prev => prev - 1);
|
|
78
|
+
};
|
|
79
|
+
// turn the shorter way round until the given element is in focus
|
|
80
|
+
const rotateTo = (index) => {
|
|
81
|
+
setStep(prev => {
|
|
82
|
+
let delta = (((index - prev) % noOfImages) + noOfImages) % noOfImages;
|
|
83
|
+
if (delta > noOfImages / 2)
|
|
84
|
+
delta -= noOfImages;
|
|
85
|
+
return prev + delta;
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
(0, react_1.useEffect)(() => {
|
|
89
|
+
setFocusElement(focusElement);
|
|
90
|
+
}, [focusElement]);
|
|
91
|
+
// restarts after every step, so a manual turn gets the full wait before the next automatic one
|
|
92
|
+
(0, react_1.useEffect)(() => {
|
|
93
|
+
if (!autoRotateTime || paused)
|
|
94
|
+
return;
|
|
95
|
+
const timer = setTimeout(rotateRight, autoRotateTime * 1000);
|
|
96
|
+
return () => clearTimeout(timer);
|
|
97
|
+
}, [autoRotateTime, step, paused]);
|
|
98
|
+
const onKeyDown = (event) => {
|
|
99
|
+
const target = event.target;
|
|
100
|
+
if (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName))
|
|
101
|
+
return;
|
|
102
|
+
if (event.key === 'ArrowLeft')
|
|
103
|
+
rotateLeft();
|
|
104
|
+
else if (event.key === 'ArrowRight')
|
|
105
|
+
rotateRight();
|
|
106
|
+
else
|
|
107
|
+
return;
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
};
|
|
110
|
+
// pause for keyboard focus only; a mouse click also focuses the carousel but shouldn't stop it
|
|
111
|
+
const onFocus = (event) => {
|
|
112
|
+
let keyboard = true;
|
|
113
|
+
// older browsers don't know :focus-visible, so treat any focus as keyboard focus there
|
|
114
|
+
try {
|
|
115
|
+
keyboard = event.target.matches(':focus-visible');
|
|
116
|
+
}
|
|
117
|
+
catch (_a) { }
|
|
118
|
+
if (keyboard)
|
|
119
|
+
setKeyboardFocused(true);
|
|
120
|
+
};
|
|
121
|
+
const onBlur = (event) => {
|
|
122
|
+
if (!event.currentTarget.contains(event.relatedTarget))
|
|
123
|
+
setKeyboardFocused(false);
|
|
124
|
+
};
|
|
125
|
+
var newCoordinates = [];
|
|
126
|
+
for (let index = 0; index < noOfImages; index++) {
|
|
127
|
+
newCoordinates.push([carouselRadius - peripheralImageRadius + carouselRadius * Math.cos(2 * Math.PI * index / noOfImages), carouselRadius - peripheralImageRadius + carouselRadius * Math.sin(2 * Math.PI * index / noOfImages)]);
|
|
128
|
+
}
|
|
129
|
+
// rotate point around (carouselRadius-peripheralRadius, carouselRadius-peripheralRadius) by (offset+90) deg
|
|
130
|
+
const totalDeviation = offsetAngle * Math.PI / 180 + Math.PI / 2;
|
|
131
|
+
var rotatedCoordinates = [];
|
|
132
|
+
const centerCoordinate = carouselRadius - peripheralImageRadius;
|
|
133
|
+
newCoordinates.forEach((item, index) => {
|
|
134
|
+
rotatedCoordinates.push([centerCoordinate + (item[0] - centerCoordinate) * Math.cos(totalDeviation) - (item[1] - centerCoordinate) * Math.sin(totalDeviation), centerCoordinate + (item[0] - centerCoordinate) * Math.sin(totalDeviation) + (item[1] - centerCoordinate) * Math.cos(totalDeviation)]);
|
|
135
|
+
});
|
|
136
|
+
const content = (index, radius, className) => {
|
|
137
|
+
var _a;
|
|
138
|
+
return ((images) ?
|
|
139
|
+
react_1.default.createElement("img", { className: className, src: images[index], alt: (_a = imageAlts[index]) !== null && _a !== void 0 ? _a : '', style: { width: `${radius * 2}px`, height: `${radius * 2}px`, transition: `${transitionTime}s` } }) :
|
|
140
|
+
react_1.default.createElement("div", { className: "fancy-carousel-item", style: { width: `${radius * 2}px`, height: `${radius * 2}px`, transition: `${transitionTime}s` } }, childItems[index]));
|
|
141
|
+
};
|
|
142
|
+
// in responsive mode, pad by the peripheral image radius so images on the ring's edge stay inside
|
|
143
|
+
const wrapperStyle = (responsive) ? { width: '100%', boxSizing: 'border-box', contain: 'inline-size', padding: `${peripheralImageRadius + elementMargin}px` } : {};
|
|
144
|
+
if (!scale || !noOfImages)
|
|
145
|
+
return react_1.default.createElement("div", { className: "fancy-carousel-wrapper-element", ref: wrapperRef, style: wrapperStyle });
|
|
146
|
+
return (react_1.default.createElement("div", { className: "fancy-carousel-wrapper-element", ref: wrapperRef, style: wrapperStyle, role: "region", "aria-roledescription": "carousel", "aria-label": "Carousel", tabIndex: 0, onKeyDown: onKeyDown, onFocus: onFocus, onBlur: onBlur, onMouseEnter: () => setHovered(true), onMouseLeave: () => setHovered(false) },
|
|
147
|
+
react_1.default.createElement("div", { className: "fancy-carousel-border", style: { backgroundImage: borderElement, height: `${carouselRadius * 2}px`, width: `${carouselRadius * 2}px`, transition: `${transitionTime}s` } },
|
|
148
|
+
react_1.default.createElement("div", { className: "fancy-carousel", style: {
|
|
149
|
+
transform: `rotate(${carouselOrientation}deg)`,
|
|
150
|
+
height: `${carouselRadius * 2}px`, width: `${carouselRadius * 2}px`,
|
|
151
|
+
transition: `${transitionTime}s`
|
|
152
|
+
} },
|
|
153
|
+
rotatedCoordinates.map((coordinate, index) => (react_1.default.createElement("div", { className: "fancy-carousel-element", key: index, onClick: (rotateOnClick && index !== focusElement) ? () => rotateTo(index) : undefined, style: Object.assign({
|
|
154
|
+
transform: `rotate(${-carouselOrientation}deg)`, width: `${peripheralImageRadius * 2}px`, height: `${peripheralImageRadius * 2}px`,
|
|
155
|
+
// take off the margin so each element is centred on the ring it rotates with
|
|
156
|
+
left: `${coordinate[0] - elementMargin}px`,
|
|
157
|
+
bottom: `${coordinate[1] - elementMargin}px`,
|
|
158
|
+
boxShadow: `${peripheralImageBoxShadow}`,
|
|
159
|
+
transition: `${transitionTime}s`,
|
|
160
|
+
cursor: (rotateOnClick && index !== focusElement) ? 'pointer' : undefined
|
|
161
|
+
}, ((index === focusElement) ? focusElementStyling : {})) }, content(index, peripheralImageRadius, 'fancy-carousel-image')))),
|
|
162
|
+
react_1.default.createElement("div", { className: "fancy-carousel-element central-img", key: noOfImages, style: {
|
|
163
|
+
transform: `rotate(${-carouselOrientation}deg)`, width: `${centralImageRadius * 2}px`, height: `${centralImageRadius * 2}px`,
|
|
164
|
+
left: `${carouselRadius - centralImageRadius - elementMargin}px`, bottom: `${carouselRadius - centralImageRadius - elementMargin}px`,
|
|
165
|
+
boxShadow: `${centralImageBoxShadow}`,
|
|
166
|
+
transition: `${transitionTime}s`
|
|
167
|
+
} }, content(focusElement, centralImageRadius, 'fancy-carousel-central-image')))),
|
|
168
|
+
react_1.default.createElement("div", { className: "fancy-carousel-navigators " + ((showNavigation) ? "" : "fancy-carousel-hidden"), style: { gap: `${carouselRadius * 2}px`, marginLeft: `-${navigationButtonRadius * 1.8}px` } },
|
|
169
|
+
react_1.default.createElement("button", { type: "button", "aria-label": "Previous image", className: "fancy-carousel-navigation-button", onClick: rotateLeft, style: Object.assign({
|
|
170
|
+
width: `${navigationButtonRadius * 2}px`, height: `${navigationButtonRadius * 2}px`,
|
|
171
|
+
backgroundColor: `#${hex(navigationButtonBgColor)}`, color: `#${hex(navigationButtonColor)}`,
|
|
172
|
+
fontSize: `${navigationTextSize}rem`
|
|
173
|
+
}, navigationButtonStyling) }, "\u2039"),
|
|
174
|
+
react_1.default.createElement("button", { type: "button", "aria-label": "Next image", className: "fancy-carousel-navigation-button", onClick: rotateRight, style: Object.assign({
|
|
175
|
+
width: `${navigationButtonRadius * 2}px`, height: `${navigationButtonRadius * 2}px`,
|
|
176
|
+
backgroundColor: `#${hex(navigationButtonBgColor)}`, color: `#${hex(navigationButtonColor)}`,
|
|
177
|
+
fontSize: `${navigationTextSize}rem`
|
|
178
|
+
}, navigationButtonStyling) }, "\u203A"))));
|
|
179
|
+
};
|
|
180
|
+
exports.FancyCarousel = FancyCarousel;
|
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FancyCarousel = void 0;
|
|
4
|
+
const FancyCarousel_js_1 = require("./FancyCarousel.js");
|
|
5
|
+
Object.defineProperty(exports, "FancyCarousel", { enumerable: true, get: function () { return FancyCarousel_js_1.FancyCarousel; } });
|
|
6
|
+
exports.default = FancyCarousel_js_1.FancyCarousel;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FC, ReactNode, CSSProperties } from 'react';
|
|
2
|
+
export interface FancyCarouselProps {
|
|
3
|
+
images?: string[];
|
|
4
|
+
imageAlts?: string[];
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
setFocusElement?: (focusElement: number) => void;
|
|
7
|
+
offsetAngle?: number;
|
|
8
|
+
carouselRadius?: number;
|
|
9
|
+
centralImageRadius?: number;
|
|
10
|
+
centralImageBoxShadow?: string;
|
|
11
|
+
peripheralImageRadius?: number;
|
|
12
|
+
peripheralImageBoxShadow?: string;
|
|
13
|
+
focusElementStyling?: CSSProperties;
|
|
14
|
+
border?: boolean;
|
|
15
|
+
borderWidth?: number;
|
|
16
|
+
borderHexColor?: string;
|
|
17
|
+
autoRotateTime?: number;
|
|
18
|
+
transitionTime?: number;
|
|
19
|
+
navigationTextSize?: number;
|
|
20
|
+
navigationButtonRadius?: number;
|
|
21
|
+
navigationButtonBgColor?: string;
|
|
22
|
+
navigationButtonColor?: string;
|
|
23
|
+
navigationButtonStyling?: CSSProperties;
|
|
24
|
+
rotateOnClick?: boolean;
|
|
25
|
+
pauseOnHover?: boolean;
|
|
26
|
+
showNavigation?: boolean;
|
|
27
|
+
responsive?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare const FancyCarousel: FC<FancyCarouselProps>;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import React, { Children, useState, useEffect, useLayoutEffect, useRef } from 'react';
|
|
2
|
+
const useIsomorphicLayoutEffect = (typeof window !== 'undefined') ? useLayoutEffect : useEffect;
|
|
3
|
+
// matches the margin on .fancy-carousel-element in FancyCarousel.css
|
|
4
|
+
const elementMargin = 2;
|
|
5
|
+
// colours may be given with or without the '#', as 3 or 6 digit hex
|
|
6
|
+
const hex = (color) => color.replace(/^#/, '');
|
|
7
|
+
export const FancyCarousel = ({ images, imageAlts = [], children, setFocusElement = () => { }, offsetAngle = 0, carouselRadius: baseCarouselRadius = 400, centralImageRadius: baseCentralImageRadius = 125, centralImageBoxShadow = '5px 10px 18px #888888', peripheralImageRadius: basePeripheralImageRadius = 75, peripheralImageBoxShadow = '5px 10px 18px #888888', focusElementStyling = {}, border = true, borderWidth: baseBorderWidth = 5, borderHexColor = 'CB786C', autoRotateTime = 0, transitionTime = 0.5, navigationTextSize: baseNavigationTextSize = 2, navigationButtonRadius: baseNavigationButtonRadius = 32.5, navigationButtonBgColor = 'CB786C', navigationButtonColor = 'FFFFFF', navigationButtonStyling = {}, rotateOnClick = true, pauseOnHover = true, showNavigation = !autoRotateTime, responsive = false }) => {
|
|
8
|
+
// number of steps the ring has turned, +1 for every "next"
|
|
9
|
+
const [step, setStep] = useState(0);
|
|
10
|
+
const [hovered, setHovered] = useState(false);
|
|
11
|
+
const [keyboardFocused, setKeyboardFocused] = useState(false);
|
|
12
|
+
const paused = pauseOnHover && (hovered || keyboardFocused);
|
|
13
|
+
// scale factor for all sizes, 0 until measured so nothing animates in from full size
|
|
14
|
+
const [scale, setScale] = useState(responsive ? 0 : 1);
|
|
15
|
+
const wrapperRef = useRef(null);
|
|
16
|
+
useIsomorphicLayoutEffect(() => {
|
|
17
|
+
const wrapper = wrapperRef.current;
|
|
18
|
+
if (!responsive || !wrapper) {
|
|
19
|
+
setScale(1);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
// the wrapper fills its parent and ignores its own contents (contain: inline-size),
|
|
23
|
+
// so its width never depends on the scale; fall back to the viewport if it has none
|
|
24
|
+
const fit = () => {
|
|
25
|
+
const width = (wrapper.clientWidth || document.documentElement.clientWidth) - 2 * elementMargin;
|
|
26
|
+
setScale(Math.min(1, width / (2 * (baseCarouselRadius + basePeripheralImageRadius))));
|
|
27
|
+
};
|
|
28
|
+
fit();
|
|
29
|
+
const observer = new ResizeObserver(fit);
|
|
30
|
+
observer.observe(wrapper);
|
|
31
|
+
return () => observer.disconnect();
|
|
32
|
+
}, [responsive, baseCarouselRadius, basePeripheralImageRadius]);
|
|
33
|
+
const carouselRadius = baseCarouselRadius * scale;
|
|
34
|
+
const centralImageRadius = baseCentralImageRadius * scale;
|
|
35
|
+
const peripheralImageRadius = basePeripheralImageRadius * scale;
|
|
36
|
+
const borderWidth = baseBorderWidth * scale;
|
|
37
|
+
const navigationTextSize = baseNavigationTextSize * scale;
|
|
38
|
+
const navigationButtonRadius = baseNavigationButtonRadius * scale;
|
|
39
|
+
// images take priority; otherwise any children are placed around the ring
|
|
40
|
+
const childItems = Children.toArray(children);
|
|
41
|
+
const noOfImages = (images) ? images.length : childItems.length;
|
|
42
|
+
const theta = 360 / noOfImages;
|
|
43
|
+
// derived from the step so it stays in range when the number of items changes
|
|
44
|
+
const focusElement = (noOfImages) ? ((step % noOfImages) + noOfImages) % noOfImages : 0;
|
|
45
|
+
const carouselOrientation = (noOfImages) ? step * theta : 0;
|
|
46
|
+
const borderElement = (border) ? `url("data:image/svg+xml,%3csvg width=\'100%25\' height=\'100%25\' xmlns=\'http://www.w3.org/2000/svg\'%3e%3crect width=\'100%25\' height=\'100%25\' fill=\'none\' rx=\'${carouselRadius * 2}\' ry=\'${carouselRadius * 2}\' stroke=\'%23${hex(borderHexColor)}\' stroke-width=\'${borderWidth}\' stroke-dasharray=\'6%2c 24\' stroke-dashoffset=\'0\' stroke-linecap=\'square\'/%3e%3c/svg%3e")` : '';
|
|
47
|
+
const rotateRight = () => {
|
|
48
|
+
setStep(prev => prev + 1);
|
|
49
|
+
};
|
|
50
|
+
const rotateLeft = () => {
|
|
51
|
+
setStep(prev => prev - 1);
|
|
52
|
+
};
|
|
53
|
+
// turn the shorter way round until the given element is in focus
|
|
54
|
+
const rotateTo = (index) => {
|
|
55
|
+
setStep(prev => {
|
|
56
|
+
let delta = (((index - prev) % noOfImages) + noOfImages) % noOfImages;
|
|
57
|
+
if (delta > noOfImages / 2)
|
|
58
|
+
delta -= noOfImages;
|
|
59
|
+
return prev + delta;
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
setFocusElement(focusElement);
|
|
64
|
+
}, [focusElement]);
|
|
65
|
+
// restarts after every step, so a manual turn gets the full wait before the next automatic one
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!autoRotateTime || paused)
|
|
68
|
+
return;
|
|
69
|
+
const timer = setTimeout(rotateRight, autoRotateTime * 1000);
|
|
70
|
+
return () => clearTimeout(timer);
|
|
71
|
+
}, [autoRotateTime, step, paused]);
|
|
72
|
+
const onKeyDown = (event) => {
|
|
73
|
+
const target = event.target;
|
|
74
|
+
if (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName))
|
|
75
|
+
return;
|
|
76
|
+
if (event.key === 'ArrowLeft')
|
|
77
|
+
rotateLeft();
|
|
78
|
+
else if (event.key === 'ArrowRight')
|
|
79
|
+
rotateRight();
|
|
80
|
+
else
|
|
81
|
+
return;
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
};
|
|
84
|
+
// pause for keyboard focus only; a mouse click also focuses the carousel but shouldn't stop it
|
|
85
|
+
const onFocus = (event) => {
|
|
86
|
+
let keyboard = true;
|
|
87
|
+
// older browsers don't know :focus-visible, so treat any focus as keyboard focus there
|
|
88
|
+
try {
|
|
89
|
+
keyboard = event.target.matches(':focus-visible');
|
|
90
|
+
}
|
|
91
|
+
catch (_a) { }
|
|
92
|
+
if (keyboard)
|
|
93
|
+
setKeyboardFocused(true);
|
|
94
|
+
};
|
|
95
|
+
const onBlur = (event) => {
|
|
96
|
+
if (!event.currentTarget.contains(event.relatedTarget))
|
|
97
|
+
setKeyboardFocused(false);
|
|
98
|
+
};
|
|
99
|
+
var newCoordinates = [];
|
|
100
|
+
for (let index = 0; index < noOfImages; index++) {
|
|
101
|
+
newCoordinates.push([carouselRadius - peripheralImageRadius + carouselRadius * Math.cos(2 * Math.PI * index / noOfImages), carouselRadius - peripheralImageRadius + carouselRadius * Math.sin(2 * Math.PI * index / noOfImages)]);
|
|
102
|
+
}
|
|
103
|
+
// rotate point around (carouselRadius-peripheralRadius, carouselRadius-peripheralRadius) by (offset+90) deg
|
|
104
|
+
const totalDeviation = offsetAngle * Math.PI / 180 + Math.PI / 2;
|
|
105
|
+
var rotatedCoordinates = [];
|
|
106
|
+
const centerCoordinate = carouselRadius - peripheralImageRadius;
|
|
107
|
+
newCoordinates.forEach((item, index) => {
|
|
108
|
+
rotatedCoordinates.push([centerCoordinate + (item[0] - centerCoordinate) * Math.cos(totalDeviation) - (item[1] - centerCoordinate) * Math.sin(totalDeviation), centerCoordinate + (item[0] - centerCoordinate) * Math.sin(totalDeviation) + (item[1] - centerCoordinate) * Math.cos(totalDeviation)]);
|
|
109
|
+
});
|
|
110
|
+
const content = (index, radius, className) => {
|
|
111
|
+
var _a;
|
|
112
|
+
return ((images) ?
|
|
113
|
+
React.createElement("img", { className: className, src: images[index], alt: (_a = imageAlts[index]) !== null && _a !== void 0 ? _a : '', style: { width: `${radius * 2}px`, height: `${radius * 2}px`, transition: `${transitionTime}s` } }) :
|
|
114
|
+
React.createElement("div", { className: "fancy-carousel-item", style: { width: `${radius * 2}px`, height: `${radius * 2}px`, transition: `${transitionTime}s` } }, childItems[index]));
|
|
115
|
+
};
|
|
116
|
+
// in responsive mode, pad by the peripheral image radius so images on the ring's edge stay inside
|
|
117
|
+
const wrapperStyle = (responsive) ? { width: '100%', boxSizing: 'border-box', contain: 'inline-size', padding: `${peripheralImageRadius + elementMargin}px` } : {};
|
|
118
|
+
if (!scale || !noOfImages)
|
|
119
|
+
return React.createElement("div", { className: "fancy-carousel-wrapper-element", ref: wrapperRef, style: wrapperStyle });
|
|
120
|
+
return (React.createElement("div", { className: "fancy-carousel-wrapper-element", ref: wrapperRef, style: wrapperStyle, role: "region", "aria-roledescription": "carousel", "aria-label": "Carousel", tabIndex: 0, onKeyDown: onKeyDown, onFocus: onFocus, onBlur: onBlur, onMouseEnter: () => setHovered(true), onMouseLeave: () => setHovered(false) },
|
|
121
|
+
React.createElement("div", { className: "fancy-carousel-border", style: { backgroundImage: borderElement, height: `${carouselRadius * 2}px`, width: `${carouselRadius * 2}px`, transition: `${transitionTime}s` } },
|
|
122
|
+
React.createElement("div", { className: "fancy-carousel", style: {
|
|
123
|
+
transform: `rotate(${carouselOrientation}deg)`,
|
|
124
|
+
height: `${carouselRadius * 2}px`, width: `${carouselRadius * 2}px`,
|
|
125
|
+
transition: `${transitionTime}s`
|
|
126
|
+
} },
|
|
127
|
+
rotatedCoordinates.map((coordinate, index) => (React.createElement("div", { className: "fancy-carousel-element", key: index, onClick: (rotateOnClick && index !== focusElement) ? () => rotateTo(index) : undefined, style: Object.assign({
|
|
128
|
+
transform: `rotate(${-carouselOrientation}deg)`, width: `${peripheralImageRadius * 2}px`, height: `${peripheralImageRadius * 2}px`,
|
|
129
|
+
// take off the margin so each element is centred on the ring it rotates with
|
|
130
|
+
left: `${coordinate[0] - elementMargin}px`,
|
|
131
|
+
bottom: `${coordinate[1] - elementMargin}px`,
|
|
132
|
+
boxShadow: `${peripheralImageBoxShadow}`,
|
|
133
|
+
transition: `${transitionTime}s`,
|
|
134
|
+
cursor: (rotateOnClick && index !== focusElement) ? 'pointer' : undefined
|
|
135
|
+
}, ((index === focusElement) ? focusElementStyling : {})) }, content(index, peripheralImageRadius, 'fancy-carousel-image')))),
|
|
136
|
+
React.createElement("div", { className: "fancy-carousel-element central-img", key: noOfImages, style: {
|
|
137
|
+
transform: `rotate(${-carouselOrientation}deg)`, width: `${centralImageRadius * 2}px`, height: `${centralImageRadius * 2}px`,
|
|
138
|
+
left: `${carouselRadius - centralImageRadius - elementMargin}px`, bottom: `${carouselRadius - centralImageRadius - elementMargin}px`,
|
|
139
|
+
boxShadow: `${centralImageBoxShadow}`,
|
|
140
|
+
transition: `${transitionTime}s`
|
|
141
|
+
} }, content(focusElement, centralImageRadius, 'fancy-carousel-central-image')))),
|
|
142
|
+
React.createElement("div", { className: "fancy-carousel-navigators " + ((showNavigation) ? "" : "fancy-carousel-hidden"), style: { gap: `${carouselRadius * 2}px`, marginLeft: `-${navigationButtonRadius * 1.8}px` } },
|
|
143
|
+
React.createElement("button", { type: "button", "aria-label": "Previous image", className: "fancy-carousel-navigation-button", onClick: rotateLeft, style: Object.assign({
|
|
144
|
+
width: `${navigationButtonRadius * 2}px`, height: `${navigationButtonRadius * 2}px`,
|
|
145
|
+
backgroundColor: `#${hex(navigationButtonBgColor)}`, color: `#${hex(navigationButtonColor)}`,
|
|
146
|
+
fontSize: `${navigationTextSize}rem`
|
|
147
|
+
}, navigationButtonStyling) }, "\u2039"),
|
|
148
|
+
React.createElement("button", { type: "button", "aria-label": "Next image", className: "fancy-carousel-navigation-button", onClick: rotateRight, style: Object.assign({
|
|
149
|
+
width: `${navigationButtonRadius * 2}px`, height: `${navigationButtonRadius * 2}px`,
|
|
150
|
+
backgroundColor: `#${hex(navigationButtonBgColor)}`, color: `#${hex(navigationButtonColor)}`,
|
|
151
|
+
fontSize: `${navigationTextSize}rem`
|
|
152
|
+
}, navigationButtonStyling) }, "\u203A"))));
|
|
153
|
+
};
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
package/esm/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-fancy-circular-carousel",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Interactive circular carousel with image elements highly customisable as per the developer's needs",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "cjs/index.js",
|
|
6
|
+
"module": "esm/index.js",
|
|
7
|
+
"types": "esm/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./esm/index.d.ts",
|
|
12
|
+
"default": "./esm/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./cjs/index.d.ts",
|
|
16
|
+
"default": "./cjs/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./FancyCarousel.css": "./FancyCarousel.css",
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
6
22
|
"scripts": {
|
|
7
23
|
"clean": "rm -rf dist",
|
|
8
|
-
"build": "npm run clean && tsc && cp package.json README.md ./dist"
|
|
24
|
+
"build": "npm run clean && tsc && tsc -p tsconfig.cjs.json && echo '{\"type\": \"module\"}' > dist/esm/package.json && cp package.json README.md LICENSE src/FancyCarousel.css ./dist",
|
|
25
|
+
"test": "npm run build && node test/carousel.test.cjs"
|
|
9
26
|
},
|
|
27
|
+
"sideEffects": ["*.css"],
|
|
10
28
|
"keywords": ["react", "slider", "carousel", "image-slider"],
|
|
11
29
|
"author": {
|
|
12
30
|
"name": "Ninad Sutrave",
|
|
@@ -23,8 +41,12 @@
|
|
|
23
41
|
"email" : "ninadsutrave@gmail.com"
|
|
24
42
|
},
|
|
25
43
|
"license": "MIT",
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": ">=16.8.0"
|
|
46
|
+
},
|
|
26
47
|
"devDependencies": {
|
|
27
48
|
"@types/react": "^18.0.28",
|
|
49
|
+
"jsdom": "^25.0.1",
|
|
28
50
|
"react": "^18.2.0",
|
|
29
51
|
"react-dom": "^18.2.0",
|
|
30
52
|
"typescript": "^5.0.2"
|
package/FancyCarousel.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import React, { useState } from 'react';
|
|
2
|
-
export const FancyCarousel = ({ images, setFocusElement = () => { }, offsetAngle = 0, carouselRadius = 400, centralImageRadius = 125, centralImageBoxShadow = '5px 10px 18px #888888', peripheralImageRadius = 75, peripheralImageBoxShadow = '5px 10px 18px #888888', focusElementStyling = {}, border = true, borderWidth = 5, borderHexColor = 'CB786C', autoRotateTime = 0, transitionTime = 1.5, navigationTextSize = 2, navigationButtonRadius = 32.5, navigationButtonBgColor = 'CB786C', navigationButtonColor = 'FFFFFF', navigationButtonStyling = {} }) => {
|
|
3
|
-
const [carousel, setCarousel] = useState({
|
|
4
|
-
carouselOrietation: 0,
|
|
5
|
-
elementOrientation: 0,
|
|
6
|
-
focusElement: 0
|
|
7
|
-
});
|
|
8
|
-
setFocusElement(carousel.focusElement);
|
|
9
|
-
if (autoRotateTime) {
|
|
10
|
-
setTimeout(() => {
|
|
11
|
-
rotateRight();
|
|
12
|
-
}, autoRotateTime * 1000);
|
|
13
|
-
}
|
|
14
|
-
const noOfImages = images.length;
|
|
15
|
-
const theta = 360 / noOfImages;
|
|
16
|
-
const borderElement = (border) ? `url("data:image/svg+xml,%3csvg width=\'100%25\' height=\'100%25\' xmlns=\'http://www.w3.org/2000/svg\'%3e%3crect width=\'100%25\' height=\'100%25\' fill=\'none\' rx=\'${carouselRadius * 2}\' ry=\'${carouselRadius * 2}\' stroke=\'%23${borderHexColor}FF\' stroke-width=\'${borderWidth}\' stroke-dasharray=\'6%2c 24\' stroke-dashoffset=\'0\' stroke-linecap=\'square\'/%3e%3c/svg%3e")` : '';
|
|
17
|
-
const rotateRight = () => {
|
|
18
|
-
setCarousel({
|
|
19
|
-
carouselOrietation: carousel.carouselOrietation + theta,
|
|
20
|
-
elementOrientation: carousel.elementOrientation - theta,
|
|
21
|
-
focusElement: (carousel.focusElement < (noOfImages - 1)) ? carousel.focusElement + 1 : 0
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
const rotateLeft = () => {
|
|
25
|
-
setCarousel({
|
|
26
|
-
carouselOrietation: carousel.carouselOrietation - theta,
|
|
27
|
-
elementOrientation: carousel.elementOrientation + theta,
|
|
28
|
-
focusElement: (carousel.focusElement > 0) ? carousel.focusElement - 1 : noOfImages - 1
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
var newCoordinates = [];
|
|
32
|
-
images.forEach((item, index) => {
|
|
33
|
-
newCoordinates.push([carouselRadius - peripheralImageRadius + carouselRadius * Math.cos(2 * Math.PI * index / noOfImages), carouselRadius - peripheralImageRadius + carouselRadius * Math.sin(2 * Math.PI * index / noOfImages)]);
|
|
34
|
-
});
|
|
35
|
-
// rotate point around (carouselRadius-peripheralRadius, carouselRadius-peripheralRadius) by (offset+90) deg
|
|
36
|
-
const totalDeviation = offsetAngle * Math.PI / 180 + Math.PI / 2;
|
|
37
|
-
var rotatedCoordinates = [];
|
|
38
|
-
const centerCoordinate = carouselRadius - peripheralImageRadius;
|
|
39
|
-
newCoordinates.forEach((item, index) => {
|
|
40
|
-
rotatedCoordinates.push([centerCoordinate + (item[0] - centerCoordinate) * Math.cos(totalDeviation) - (item[1] - centerCoordinate) * Math.sin(totalDeviation), centerCoordinate + (item[0] - centerCoordinate) * Math.sin(totalDeviation) + (item[1] - centerCoordinate) * Math.cos(totalDeviation)]);
|
|
41
|
-
});
|
|
42
|
-
return (React.createElement("div", { className: "fancy-carousel-wrapper-element" },
|
|
43
|
-
React.createElement("div", { className: "fancy-carousel-border", style: { backgroundImage: borderElement, height: `${carouselRadius * 2}px`, width: `${carouselRadius * 2}px`, transition: `${transitionTime}` } },
|
|
44
|
-
React.createElement("div", { className: "fancy-carousel", style: {
|
|
45
|
-
transform: `rotate(${carousel.carouselOrietation}deg)`,
|
|
46
|
-
height: `${carouselRadius * 2}px`, width: `${carouselRadius * 2}px`
|
|
47
|
-
} },
|
|
48
|
-
images.map((item, index) => ((index !== carousel.focusElement) ?
|
|
49
|
-
React.createElement("div", { className: "fancy-carousel-element", key: index, style: {
|
|
50
|
-
transform: `rotate(${carousel.elementOrientation}deg)`, width: `${peripheralImageRadius * 2}px`, height: `${peripheralImageRadius * 2}px`,
|
|
51
|
-
left: `${rotatedCoordinates[index][0]}px`,
|
|
52
|
-
bottom: `${rotatedCoordinates[index][1]}px`,
|
|
53
|
-
boxShadow: `${peripheralImageBoxShadow}`,
|
|
54
|
-
transition: `${transitionTime}`
|
|
55
|
-
} },
|
|
56
|
-
React.createElement("img", { className: "fancy-carousel-image", src: item, style: { width: `${peripheralImageRadius * 2}px`, height: `${peripheralImageRadius * 2}px` } })) :
|
|
57
|
-
React.createElement("div", { className: "fancy-carousel-element", key: index, style: Object.assign({
|
|
58
|
-
transform: `rotate(${carousel.elementOrientation}deg)`, width: `${peripheralImageRadius * 2}px`, height: `${peripheralImageRadius * 2}px`,
|
|
59
|
-
left: `${rotatedCoordinates[index][0]}px`,
|
|
60
|
-
bottom: `${rotatedCoordinates[index][1]}px`,
|
|
61
|
-
boxShadow: `${peripheralImageBoxShadow}`,
|
|
62
|
-
transition: `${transitionTime}`
|
|
63
|
-
}, focusElementStyling) },
|
|
64
|
-
React.createElement("img", { className: "fancy-carousel-image", src: item, style: { width: `${peripheralImageRadius * 2}px`, height: `${peripheralImageRadius * 2}px`, transition: `${transitionTime}` } })))),
|
|
65
|
-
React.createElement("div", { className: "fancy-carousel-element central-img", key: noOfImages, style: {
|
|
66
|
-
transform: `rotate(${carousel.elementOrientation}deg)`, width: `${centralImageRadius * 2}px`, height: `${centralImageRadius * 2}px`,
|
|
67
|
-
left: `${carouselRadius - centralImageRadius - 10}px`, bottom: `${carouselRadius - centralImageRadius - 10}px`,
|
|
68
|
-
boxShadow: `${centralImageBoxShadow}`,
|
|
69
|
-
transition: `${transitionTime}`
|
|
70
|
-
} },
|
|
71
|
-
React.createElement("img", { className: "fancy-carousel-central-image", src: images[carousel.focusElement], style: { width: `${centralImageRadius * 2}px`, height: `${centralImageRadius * 2}px`, transition: `${transitionTime}` } })))),
|
|
72
|
-
React.createElement("div", { className: "fancy-carousel-navigators " + ((autoRotateTime) ? "invisible" : ""), style: { gap: `${carouselRadius * 2}px`, marginLeft: `-${navigationButtonRadius * 1.8}px` } },
|
|
73
|
-
React.createElement("button", { className: "fancy-carousel-navigation-button", onClick: rotateLeft, style: Object.assign({
|
|
74
|
-
width: `${navigationButtonRadius * 2}px`, height: `${navigationButtonRadius * 2}px`,
|
|
75
|
-
backgroundColor: `#${navigationButtonBgColor}`, color: `#${navigationButtonColor}`,
|
|
76
|
-
fontSize: `${navigationTextSize}rem`
|
|
77
|
-
}, navigationButtonStyling) }, "\u2193"),
|
|
78
|
-
React.createElement("button", { className: "fancy-carousel-navigation-button", onClick: rotateRight, style: Object.assign({
|
|
79
|
-
width: `${navigationButtonRadius * 2}px`, height: `${navigationButtonRadius * 2}px`,
|
|
80
|
-
backgroundColor: `#${navigationButtonBgColor}`, color: `#${navigationButtonColor}`,
|
|
81
|
-
fontSize: `${navigationTextSize}rem`
|
|
82
|
-
}, navigationButtonStyling) }, "\u2193"))));
|
|
83
|
-
};
|
package/index.d.ts
DELETED
package/index.js
DELETED