mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 23:12:13 +00:00
Button component implemented
This commit is contained in:
parent
b235fbb235
commit
fcfb5bd7af
3 changed files with 47 additions and 0 deletions
42
src/common/Button/Button.js
Normal file
42
src/common/Button/Button.js
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
const React = require('react');
|
||||||
|
const { useFocusable } = require('stremio-navigation');
|
||||||
|
|
||||||
|
const ENTER_KEY_CODE = 13;
|
||||||
|
|
||||||
|
const Button = React.forwardRef(({ children, ...props }, ref) => {
|
||||||
|
const focusable = useFocusable();
|
||||||
|
const tabIndex = (props.tabIndex === null || isNaN(props.tabIndex)) ?
|
||||||
|
(focusable ? 0 : -1)
|
||||||
|
:
|
||||||
|
props.tabIndex;
|
||||||
|
const onKeyUp = React.useCallback((event) => {
|
||||||
|
if (typeof props.onKeyUp === 'function') {
|
||||||
|
props.onKeyUp(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.keyCode === ENTER_KEY_CODE && !event.nativeEvent.clickPrevented) {
|
||||||
|
event.currentTarget.click();
|
||||||
|
}
|
||||||
|
}, [props.onKeyUp]);
|
||||||
|
const onMouseDown = React.useCallback((event) => {
|
||||||
|
if (typeof props.onMouseDown === 'function') {
|
||||||
|
props.onMouseDown(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!event.nativeEvent.blurPrevented) {
|
||||||
|
event.preventDefault();
|
||||||
|
if (document.activeElement instanceof HTMLElement) {
|
||||||
|
document.activeElement.blur();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [props.onMouseDown]);
|
||||||
|
return React.createElement(
|
||||||
|
typeof props.href === 'string' ? 'a' : 'div',
|
||||||
|
{ ...props, ref, tabIndex, onKeyUp, onMouseDown },
|
||||||
|
children
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Button.displayName = 'Button';
|
||||||
|
|
||||||
|
module.exports = Button;
|
||||||
3
src/common/Button/index.js
Normal file
3
src/common/Button/index.js
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
const Button = require('./Button');
|
||||||
|
|
||||||
|
module.exports = Button;
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
const Button = require('./Button');
|
||||||
const Checkbox = require('./Checkbox');
|
const Checkbox = require('./Checkbox');
|
||||||
const ColorPicker = require('./ColorPicker');
|
const ColorPicker = require('./ColorPicker');
|
||||||
const Input = require('./Input');
|
const Input = require('./Input');
|
||||||
|
|
@ -14,6 +15,7 @@ const useBinaryState = require('./useBinaryState');
|
||||||
const useLocationHash = require('./useLocationHash');
|
const useLocationHash = require('./useLocationHash');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
Button,
|
||||||
Checkbox,
|
Checkbox,
|
||||||
ColorPicker,
|
ColorPicker,
|
||||||
Input,
|
Input,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue