Skip to content

Commit

Permalink
Fix the Input clear button on macOS
Browse files Browse the repository at this point in the history
The button focus behavior is inconsistent across operating systems and browsers. On macOS Firefox and Safari, the Input clear button was broken. A onFocusIn/onFocusOut solution is not possible due to React not supporting it.

- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
- https://zellwk.com/blog/inconsistent-button-behavior/
- facebook/react#6410
  • Loading branch information
darekkay committed Jan 17, 2020
1 parent c2654cf commit dcaed99
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- :bug: Let the Image always fit the whole widget.
- :bug: Fix the Input clear button on macOS.
- :book: Update documentation.

## 1.1.3 (2020-01-14)
Expand Down
8 changes: 6 additions & 2 deletions src/components/forms/input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ const Input: React.FC<Props> = memo(props => {
} = props;
const [isFocused, setFocused] = useState(false);
const { t } = useTranslation();
const clearValue = () => setValue("");
return (
<Label text={label}>
<div
className="w-full relative"
onFocus={event => {
// replace with onFocusIn / onFocusOut when implemented in React:
// https://github.com/facebook/react/issues/6410
onFocus={() => {
setFocused(true);
}}
onBlur={event => {
Expand Down Expand Up @@ -60,7 +63,8 @@ const Input: React.FC<Props> = memo(props => {
className="absolute right-0 h-full"
size={ButtonSize.Small}
variant={ButtonVariant.Unstyled}
onClick={() => setValue("")}
onClick={clearValue}
onMouseDown={clearValue}
aria-label={t("common.clear")}
>
<Icon name="times" />
Expand Down

0 comments on commit dcaed99

Please sign in to comment.