Skip to content

Commit 69ff5f0

Browse files
fix#1471: every element is focusable on web (#1585)
As described in #1471, each SVG element is focusable/tabable on web by default. This can create issues in keyboard navigation. As explained by @jondkoon #1471 (comment), the TouchableMixin attaches blur handler which makes SVG elements focusable. So, to solve this, we only attach touchable mixin handlers if the element has any touchable props.
1 parent 2484baa commit 69ff5f0

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/ReactNativeSVG.web.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { NumberArray, NumberProp } from './lib/extract/types';
1111
import SvgTouchableMixin from './lib/SvgTouchableMixin';
1212
import { resolve } from './lib/resolve';
13+
import { getHasTouchableProperty } from './lib/util';
1314

1415
const createElement = cE || ucE;
1516

@@ -87,15 +88,10 @@ const prepare = <T extends BaseProps>(
8788
fontStyle,
8889
style,
8990
forwardedRef,
90-
onPress,
91-
onPressIn,
92-
onPressOut,
93-
onLongPress,
9491
// @ts-ignore
9592
...rest
9693
} = props;
97-
const hasTouchableProperty =
98-
onPress || onPressIn || onPressOut || onLongPress;
94+
const hasTouchableProperty = getHasTouchableProperty(props);
9995
const clean: {
10096
onStartShouldSetResponder?: (e: GestureResponderEvent) => boolean;
10197
onResponderMove?: (e: GestureResponderEvent) => void;
@@ -245,7 +241,11 @@ export class WebShape<
245241
) => boolean;
246242
constructor(props: P, context: C) {
247243
super(props, context);
248-
SvgTouchableMixin(this);
244+
const hasTouchableProperty = getHasTouchableProperty(props);
245+
246+
// Do not attach touchable mixin handlers if SVG element doesn't have a touchable prop
247+
if (hasTouchableProperty) SvgTouchableMixin(this);
248+
249249
this._remeasureMetricsOnActivation = remeasure.bind(this);
250250
}
251251
}

src/lib/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ export function pickNotNil(object: { [prop: string]: unknown }) {
1111
return result;
1212
}
1313

14+
export const getHasTouchableProperty = (props: any) => {
15+
return (
16+
props.onPress || props.onPressIn || props.onPressOut || props.onLongPress
17+
);
18+
};
19+
1420
export const idPattern = /#([^)]+)\)?$/;

0 commit comments

Comments
 (0)