Workaround IOS focus bug when clicking on buttons

This commit is contained in:
Chi Vinh Le
2018-05-16 21:37:35 +02:00
parent cb9945053f
commit 2eac8f48d6
2 changed files with 14 additions and 9 deletions
@@ -7,21 +7,17 @@ class Button extends React.Component {
render() { render() {
const { const {
className, className,
title,
onClick,
children, children,
active, active,
activeClassName, activeClassName,
disabled, ...rest
} = this.props; } = this.props;
return ( return (
<button <button
className={cn(className, styles.button, { className={cn(className, styles.button, {
[cn(styles.active, activeClassName)]: active, [cn(styles.active, activeClassName)]: active,
})} })}
title={title} {...rest}
onClick={onClick}
disabled={disabled}
> >
{children} {children}
</button> </button>
@@ -32,11 +28,8 @@ class Button extends React.Component {
Button.propTypes = { Button.propTypes = {
className: PropTypes.string, className: PropTypes.string,
activeClassName: PropTypes.string, activeClassName: PropTypes.string,
title: PropTypes.string,
onClick: PropTypes.func,
children: PropTypes.node, children: PropTypes.node,
active: PropTypes.bool, active: PropTypes.bool,
disabled: PropTypes.bool,
}; };
export default Button; export default Button;
@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Button from '../components/Button'; import Button from '../components/Button';
import bowser from 'bowser';
/** /**
* createToggle creates a button that can be active, inactive or disabled * createToggle creates a button that can be active, inactive or disabled
@@ -32,7 +33,17 @@ const createToggle = (
this.execCommand(); this.execCommand();
}; };
// Detect whether there was focus on the RTE before the click.
hadFocusBeforeClick = false;
handleMouseDown = () => (this.hadFocusBeforeClick = this.props.api.focused);
handleClick = () => { handleClick = () => {
// Skip IOS when the focus was not there before.
// IOS fails to focus to the RTE correctly and scrolls to nirvana.
// See https://www.pivotaltracker.com/story/show/157607216
if (!this.hadFocusBeforeClick && bowser.ios) {
return;
}
this.props.api.focus(); this.props.api.focus();
this.formatToggle(); this.formatToggle();
this.props.api.focus(); this.props.api.focus();
@@ -62,6 +73,7 @@ const createToggle = (
<Button <Button
className={className} className={className}
title={title} title={title}
onMouseDown={this.handleMouseDown}
onClick={this.handleClick} onClick={this.handleClick}
active={this.state.active} active={this.state.active}
disabled={disabled || this.state.disabled} disabled={disabled || this.state.disabled}