();
- const [focused, setFocused] = useState(true);
- const [rnd, setRnd] = useState(0);
- const [reduced, setReduced] = useState(data);
+};
- useEffect(() => {
- window.addEventListener('blur', () => setFocused(false));
- window.addEventListener('focus', () => setFocused(true));
- const int = interval
- ? setInterval(() => setRnd(Math.random() * 10), interval)
- : 0;
+const cx = classNames.bind(styles);
- return () => {
- window.removeEventListener('blur', () => setFocused(false));
- window.removeEventListener('focus', () => () => setFocused(true));
- if (interval) clearInterval(int);
- };
- }, []);
+export default function Carousel(props: Props) {
+ const {
+ slides: data, size, animation, animation: { interval },
+ } = props;
- useEffect(() => {
- if (focused) getNext();
- }, [rnd]);
+ const [current, setCurrent] = useState(0);
+ const [move, setMove] = useState(0);
+ const [slides] = useState(data.length <= 3 ? [...data, ...data, ...data] : data);
+ const [focused, setFocused] = useState(true);
+ const [reduced] = useState(data);
const getPrev = (m = move, c = current) => {
setMove(m + 1);
@@ -56,22 +40,28 @@ const Carousel: React.FC<{
setCurrent(i);
};
- const getNext = (m = move, c = current) => {
- setMove(move - 1);
+ const getNext = useCallback((m = move, c = current) => {
+ setMove(m - 1);
setCurrent(c + 1 >= reduced.length ? 0 : c + 1);
- };
+ }, [current, move, reduced.length]);
- const getOffsets = () => {
- const half = Math.floor(slides.length / 2);
- const gap = half - current;
- let arr = slides.map((s, i) => i - half);
- gap > 0 ? arr.push(...arr.splice(0, gap)) : arr.unshift(...arr.splice(gap));
+ useEffect(() => {
+ window.addEventListener('blur', () => setFocused(false));
+ window.addEventListener('focus', () => setFocused(true));
+ const int = interval ? setInterval(() => focused && getNext(), interval) : 0;
- return arr;
- };
+ return () => {
+ window.removeEventListener('blur', () => setFocused(false));
+ window.removeEventListener('focus', () => () => setFocused(true));
+ if (interval) clearInterval(int);
+ };
+ }, [focused, getNext, interval]);
const cns = {
- dot: (selected: boolean) => cx({ dot: true, selected }),
+ dot: (selected: boolean) => cx({
+ dot: true,
+ selected,
+ }),
};
return (
@@ -86,7 +76,7 @@ const Carousel: React.FC<{
{slides.map((s, key) => (
- getPrev()}>
+
+
{data.map((i, key) => (
- getPos(key)}
- >
+
))}
- getNext()}>
+
+
);
-};
-
-export default Carousel;
+}
diff --git a/src/easing/index.ts b/src/easing/index.ts
index b2fadb2..f54930a 100644
--- a/src/easing/index.ts
+++ b/src/easing/index.ts
@@ -1,8 +1,9 @@
-const pow = Math.pow;
-const sqrt = Math.sqrt;
-const sin = Math.sin;
-const cos = Math.cos;
-const PI = Math.PI;
+const {
+ sqrt,
+ sin,
+ cos,
+ PI,
+} = Math;
const c1 = 1.70158;
const c2 = c1 * 1.525;
const c3 = c1 + 1;
@@ -15,70 +16,54 @@ const bounceOut = (x: number): number => {
if (x < 1 / d1) {
return n1 * x * x;
- } else if (x < 2 / d1) {
- return n1 * (x -= 1.5 / d1) * x + 0.75;
- } else if (x < 2.5 / d1) {
- return n1 * (x -= 2.25 / d1) * x + 0.9375;
- } else {
- return n1 * (x -= 2.625 / d1) * x + 0.984375;
}
+ if (x < 2 / d1) {
+ const dx = x - 1.5 / d1;
+ return n1 * (x - 1.5 / d1) * dx + 0.75;
+ }
+ if (x < 2.5 / d1) {
+ const dx = x - 2.25 / d1;
+ return n1 * (x - 2.25 / d1) * dx + 0.9375;
+ }
+ const dx = x - 2.625 / d1;
+ return n1 * (x - 2.625 / d1) * dx + 0.984375;
};
export const easeInQuad = (x: number) => x * x;
export const easeOutQuad = (x: number) => 1 - (1 - x) * (1 - x);
-export const easeInOutQuad = (x: number) =>
- x < 0.5 ? 2 * x * x : 1 - pow(-2 * x + 2, 2) / 2;
+export const easeInOutQuad = (x: number) => (x < 0.5 ? 2 * x * x : 1 - (-2 * x + 2) ** 2 / 2);
export const easeInCubic = (x: number) => x * x * x;
-export const easeOutCubic = (x: number) => 1 - pow(1 - x, 3);
-export const easeInOutCubic = (x: number) =>
- x < 0.5 ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2;
+export const easeOutCubic = (x: number) => 1 - (1 - x) ** 3;
+export const easeInOutCubic = (x: number) => (x < 0.5 ? 4 * x * x * x : 1 - (-2 * x + 2) ** 3 / 2);
export const easeInQuart = (x: number) => x * x * x * x;
-export const easeOutQuart = (x: number) => 1 - pow(1 - x, 4);
-export const easeInOutQuart = (x: number) =>
- x < 0.5 ? 8 * x * x * x * x : 1 - pow(-2 * x + 2, 4) / 2;
+export const easeOutQuart = (x: number) => 1 - (1 - x) ** 4;
+export const easeInOutQuart = (x: number) => (x < 0.5 ? 8 * x * x * x * x : 1 - (-2 * x + 2) ** 4 / 2);
export const easeInQuint = (x: number) => x * x * x * x * x;
-export const easeOutQuint = (x: number) => 1 - pow(1 - x, 5);
-export const easeInOutQuint = (x: number) =>
- x < 0.5 ? 16 * x * x * x * x * x : 1 - pow(-2 * x + 2, 5) / 2;
+export const easeOutQuint = (x: number) => 1 - (1 - x) ** 5;
+export const easeInOutQuint = (x: number) => (x < 0.5 ? 16 * x * x * x * x * x : 1 - (-2 * x + 2) ** 5 / 2);
export const easeInSine = (x: number) => 1 - cos((x * PI) / 2);
export const easeOutSine = (x: number) => sin((x * PI) / 2);
export const easeInOutSine = (x: number) => -(cos(PI * x) - 1) / 2;
-export const easeInExpo = (x: number) => (x === 0 ? 0 : pow(2, 10 * x - 10));
-export const easeOutExpo = (x: number) => (x === 1 ? 1 : 1 - pow(2, -10 * x));
-export const easeInOutExpo = (x: number) =>
- x === 0
- ? 0
- : x === 1
- ? 1
- : x < 0.5
- ? pow(2, 20 * x - 10) / 2
- : (2 - pow(2, -20 * x + 10)) / 2;
-export const easeInCirc = (x: number) => 1 - sqrt(1 - pow(x, 2));
-export const easeOutCirc = (x: number) => sqrt(1 - pow(x - 1, 2));
-export const easeInOutCirc = (x: number) =>
- x < 0.5
- ? (1 - sqrt(1 - pow(2 * x, 2))) / 2
- : (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2;
+export const easeInExpo = (x: number) => (x === 0 ? 0 : 2 ** (10 * x - 10));
+export const easeOutExpo = (x: number) => (x === 1 ? 1 : 1 - 2 ** (-10 * x));
+export const easeInOutExpo = (x: number) => (x === 0 ? 0 : x === 1 ? 1 : x < 0.5 ? 2 ** (20 * x - 10) / 2 : (2 - 2 ** (-20 * x + 10)) / 2);
+export const easeInCirc = (x: number) => 1 - sqrt(1 - x ** 2);
+export const easeOutCirc = (x: number) => sqrt(1 - (x - 1) ** 2);
+export const easeInOutCirc = (x: number) => (x < 0.5 ? (1 - sqrt(1 - (2 * x) ** 2)) / 2 : (sqrt(1 - (-2 * x + 2) ** 2) + 1) / 2);
export const easeInBack = (x: number) => c3 * x * x * x - c1 * x * x;
-export const easeOutBack = (x: number) =>
- 1 + c3 * pow(x - 1, 3) + c1 * pow(x - 1, 2);
-export const easeInOutBack = (x: number) =>
- x < 0.5
- ? (pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
- : (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
-export const easeInElastic = (x: number) =>
- x === 0 ? 0 : x === 1 ? 1 : -pow(2, 10 * x - 10) * sin((x * 10 - 10.75) * c4);
-export const easeOutElastic = (x: number) =>
- x === 0 ? 0 : x === 1 ? 1 : pow(2, -10 * x) * sin((x * 10 - 0.75) * c4) + 1;
-export const easeInOutElastic = (x: number) =>
- x === 0
- ? 0
- : x === 1
+export const easeOutBack = (x: number) => 1 + c3 * (x - 1) ** 3 + c1 * (x - 1) ** 2;
+export const easeInOutBack = (x: number) => (x < 0.5
+ ? ((2 * x) ** 2 * ((c2 + 1) * 2 * x - c2)) / 2
+ : ((2 * x - 2) ** 2 * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2);
+export const easeInElastic = (x: number) => (x === 0 ? 0 : x === 1 ? 1 : -(2 ** (10 * x - 10)) * sin((x * 10 - 10.75) * c4));
+export const easeOutElastic = (x: number) => (x === 0 ? 0 : x === 1 ? 1 : 2 ** (-10 * x) * sin((x * 10 - 0.75) * c4) + 1);
+export const easeInOutElastic = (x: number) => (x === 0
+ ? 0
+ : x === 1
? 1
: x < 0.5
- ? -(pow(2, 20 * x - 10) * sin((20 * x - 11.125) * c5)) / 2
- : (pow(2, -20 * x + 10) * sin((20 * x - 11.125) * c5)) / 2 + 1;
+ ? -(2 ** (20 * x - 10) * sin((20 * x - 11.125) * c5)) / 2
+ : (2 ** (-20 * x + 10) * sin((20 * x - 11.125) * c5)) / 2 + 1);
export const easeInBounce = (x: number) => 1 - bounceOut(1 - x);
export const easeOutBounce = bounceOut;
-export const easeInOutBounce = (x: number) =>
- x < 0.5 ? (1 - bounceOut(1 - 2 * x)) / 2 : (1 + bounceOut(2 * x - 1)) / 2;
+export const easeInOutBounce = (x: number) => (x < 0.5 ? (1 - bounceOut(1 - 2 * x)) / 2 : (1 + bounceOut(2 * x - 1)) / 2);
diff --git a/test/easing/easing.test.ts b/test/easing/easing.test.ts
index ee0bcab..5cf5291 100644
--- a/test/easing/easing.test.ts
+++ b/test/easing/easing.test.ts
@@ -1,8 +1,11 @@
-import { easeInBounce } from '../../src/easing';
import { expect } from 'chai';
+import { easeInBounce, easeOutBounce } from '../../src/easing';
-describe('easing', () => {
- it('easeInBounce', () => {
- expect(easeInBounce(2)).to.equal(-6.5625);
- });
+describe('easing', function suite() {
+ it('easeInBounce', function fn() {
+ expect(easeInBounce(2)).to.equal(-6.5625);
+ });
+ it('easeOutBounce', function fn() {
+ expect(easeOutBounce(2)).to.equal(9.25);
+ });
});