-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFlipper.js
90 lines (79 loc) · 2.51 KB
/
Flipper.js
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
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { useCallback, useRef } from 'react';
import useDir from './hooks/useDir';
import useScrollBarPercentage from './hooks/useScrollBarPercentage';
import useScrollOneLeft from './hooks/useScrollOneLeft';
import useScrollOneRight from './hooks/useScrollOneRight';
const Flipper = ({ 'aria-label': ariaLabel, blurFocusOnClick, children, mode }) => {
const [dir] = useDir();
const [scrollBarPercentage] = useScrollBarPercentage();
const buttonRef = useRef();
const left = mode === 'left';
const scrollOneLeft = useScrollOneLeft();
const scrollOneRight = useScrollOneRight();
const handleClick = useCallback(() => {
left ? scrollOneLeft() : scrollOneRight();
blurFocusOnClick && buttonRef.current && buttonRef.current.blur();
}, [blurFocusOnClick, buttonRef, left, scrollOneLeft, scrollOneRight]);
const handleKeyDown = useCallback(
event => {
const { key } = event;
if (key === 'Enter' || key === ' ') {
event.preventDefault();
left ? scrollOneLeft() : scrollOneRight();
}
},
[left, scrollOneLeft, scrollOneRight]
);
let hide;
if (dir === 'rtl') {
if (left) {
hide = scrollBarPercentage === '100%' || scrollBarPercentage === '-100%';
} else {
hide = scrollBarPercentage === '0%';
}
} else {
if (left) {
hide = scrollBarPercentage === '0%';
} else {
hide = scrollBarPercentage === '100%';
}
}
return (
<button
aria-label={ariaLabel || (left ? 'left' : 'right')}
className={classNames('react-film__flipper', 'react-film__main__overlay', {
'react-film__flipper--left': left,
'react-film__flipper--right': !left
})}
onClick={handleClick}
onKeyDown={handleKeyDown}
ref={buttonRef}
type="button"
>
<div
className={classNames('react-film__flipper__slider', 'react-film__main__slider', {
'react-film__main__slider--hide': hide,
'react-film__main__slider--left': left,
'react-film__main__slider--right': !left
})}
>
<div className="react-film__flipper__body">{children}</div>
</div>
</button>
);
};
Flipper.defaultProps = {
'aria-label': undefined,
blurFocusOnClick: false,
children: undefined,
mode: 'left'
};
Flipper.propTypes = {
'aria-label': PropTypes.string,
blurFocusOnClick: PropTypes.bool,
children: PropTypes.any,
mode: PropTypes.oneOf(['left', 'right'])
};
export default Flipper;