generated from kevinfarrugia/performance-first
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.jsx
109 lines (106 loc) · 3.68 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable */
import "glider-js";
import "glider-js/glider.min.css";
import React from "react";
export const GliderComponent = React.forwardRef((props, ref) => {
const innerRef = React.useRef(null);
const gliderRef = React.useRef();
const isMountedRef = React.useRef(false);
const makeGliderOptions = () => ({
...props,
arrows:
(props.hasArrows && {
next:
(props.arrows && props.arrows.next && props.arrows.next) ||
".glider-next",
prev:
(props.arrows && props.arrows.prev && props.arrows.prev) ||
".glider-prev",
}) ||
undefined,
dots: (props.hasDots && props.dots) || "#dots" || undefined,
});
// On mount initialize the glider and hook up events
React.useLayoutEffect(() => {
if (!innerRef.current) {
return;
}
// @ts-ignore
const glider = new Glider(innerRef.current, makeGliderOptions());
gliderRef.current = glider;
const addEventListener = (event, fn) => {
if (typeof fn === "function" && innerRef.current) {
innerRef.current.addEventListener(event, fn);
}
};
addEventListener("glider-slide-visible", props.onSlideVisible);
addEventListener("glider-loaded", props.onLoad);
addEventListener("glider-animated", props.onAnimated);
addEventListener("glider-remove", props.onRemove);
addEventListener("glider-refresh", props.onRefresh);
addEventListener("glider-add", props.onAdd);
addEventListener("glider-destroy", props.onDestroy);
addEventListener("glider-slide-hidden", props.onSlideHidden);
if (props.scrollToSlide) {
glider.scrollItem(props.scrollToSlide - 1);
} else if (props.scrollToPage) {
glider.scrollItem(props.scrollToPage - 1, true);
}
}, []);
React.useEffect(() => {
isMountedRef.current = true;
return () => {
const removeEventListener = (event, fn) => {
if (typeof fn === "function" && innerRef.current) {
innerRef.current.removeEventListener(event, fn);
}
};
removeEventListener("glider-slide-visible", props.onSlideVisible);
removeEventListener("glider-loaded", props.onLoad);
removeEventListener("glider-animated", props.onAnimated);
removeEventListener("glider-remove", props.onRemove);
removeEventListener("glider-refresh", props.onRefresh);
removeEventListener("glider-add", props.onAdd);
removeEventListener("glider-destroy", props.onDestroy);
removeEventListener("glider-slide-hidden", props.onSlideHidden);
if (gliderRef.current) {
gliderRef.current.destroy();
}
};
}, []);
// When the props update, update the glider
React.useEffect(() => {
if (!(gliderRef.current && isMountedRef.current)) {
return;
}
gliderRef.current.setOption(makeGliderOptions(), true);
gliderRef.current.refresh(true);
}, [props]);
// Expose the glider instance to the user so they can call the methods too
React.useImperativeHandle(ref, () => gliderRef.current);
return React.createElement(
"div",
{ className: "glider-contain" },
props.hasArrows &&
!props.arrows &&
React.createElement(
"button",
{ role: "button", className: "glider-prev", id: "glider-prev" },
props.iconLeft || "«"
),
React.createElement(
"div",
{ className: props.className, ref: innerRef },
props.children
),
props.hasDots && !props.dots && React.createElement("div", { id: "dots" }),
props.hasArrows &&
!props.arrows &&
React.createElement(
"button",
{ role: "button", className: "glider-next", id: "glider-next" },
props.iconRight || "»"
)
);
});
export default GliderComponent;