Skip to content

Chip - Increased hit slop for accessibility #3746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 8, 2025

Conversation

nitzanyiz
Copy link
Collaborator

Description

Increased chip hit slop to fix 48x48 hit target.

Changelog

Chips - Increased chip hit slop to hit 48x48 hit target for accessibility

Additional info

N/A

…etContainerSize logic. Added useMemo for hitSlop to enhance performance.
@nitzanyiz nitzanyiz self-assigned this Jun 3, 2025
@nitzanyiz nitzanyiz requested a review from Inbal-Tish June 3, 2025 09:35
@nitzanyiz nitzanyiz assigned Inbal-Tish and unassigned nitzanyiz Jun 3, 2025
const height = ('height' in containerSize ? containerSize.height : containerSize?.minHeight) ?? 0;
const width = containerSize?.width ?? 48;
return {
top: Math.max(0, (48 - height) / 2),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can calc each once

: {[width]: size, [height]: size};
const width = typeof size === 'object' ? _.get(size, 'width') : size;
const height = typeof size === 'object' ? _.get(size, 'height') : size;
return useSizeAsMinimum ? {minWidth: width, minHeight: height} : {width, height};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would separate the return to a different method so getContainerSize will only return the size, i.e. height and width so you can use it in the hitSlop calc and wont have to extract it from the style object.

Copy link
Collaborator Author

@nitzanyiz nitzanyiz Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand. To something like this?

  const chipSize = useMemo(() => {
    const width = typeof size === 'object' ? _.get(size, 'width') : size;
    const height = typeof size === 'object' ? _.get(size, 'height') : size;
    return {width, height};
  }, [size]);

  const containerSizeStyle = useMemo(() => {
    const {width, height} = chipSize;
    return useSizeAsMinimum ? {minWidth: width, minHeight: height} : {width, height};
  }, [chipSize]);

  const Container = onPress ? TouchableOpacity : View;
  const hitSlop = useMemo(() => {
    const {width = 48, height = 48} = chipSize;
    const verticalHitSlop = Math.max(0, (48 - height) / 2);
    const horizontalHitSlop = Math.max(0, (48 - width) / 2);
    return {
      top: verticalHitSlop,
      bottom: verticalHitSlop,
      left: horizontalHitSlop,
      right: horizontalHitSlop
    };
  }, [chipSize]);

  return (
    <Container
      activeOpacity={1}
      onPress={onPress}
      style={[styles.container, {backgroundColor}, {borderRadius}, containerStyle, containerSizeStyle]}
      .
      .
      .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

return (
<Container
activeOpacity={1}
onPress={onPress}
style={[styles.container, {backgroundColor}, {borderRadius}, containerStyle, getContainerSize()]}
testID={testID}
hitSlop={onPress ? hitSlop : undefined}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we care if the View gets the hitSlop as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I can pass it...

@Inbal-Tish Inbal-Tish assigned nitzanyiz and unassigned Inbal-Tish Jun 5, 2025
@nitzanyiz nitzanyiz assigned Inbal-Tish and unassigned nitzanyiz Jun 8, 2025
@Inbal-Tish Inbal-Tish assigned nitzanyiz and unassigned Inbal-Tish Jun 8, 2025
@nitzanyiz nitzanyiz assigned Inbal-Tish and unassigned nitzanyiz Jun 8, 2025
? {[width]: _.get(size, 'width'), [height]: _.get(size, 'height')}
: {[width]: size, [height]: size};
const chipSize = useMemo(() => {
const width = typeof size === 'object' ? _.get(size, 'width') : size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get rid of the _.get

@Inbal-Tish Inbal-Tish merged commit 72325c2 into master Jun 8, 2025
1 check passed
@Inbal-Tish Inbal-Tish deleted the fix/accessibitiliy-chipHitTarget branch June 8, 2025 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants