Skip to content

Option allowOutsideClick should accept a boolean #86

@SeanMcP

Description

@SeanMcP

Description

The prop type for allowOutsideClick is func:

allowOutsideClick: PropTypes.func,

This is fine (and mimics the focus-trap options), but the name implies that it should also accept a boolean. "allows" is one of those words – like "has", "is", and "will" – that indicates to the user that the value is either true or false.

Expected behavior

Passing true to the allowOutsideClick works as expected.

Actual behavior

A run-time warning is logged and the option is not active.

Possible solution

In this loop:

for (const optionName in specifiedFocusTrapOptions) {
if (
!Object.prototype.hasOwnProperty.call(
specifiedFocusTrapOptions,
optionName
)
) {
continue;
}
if (optionName === 'returnFocusOnDeactivate') {
continue;
}
tailoredFocusTrapOptions[optionName] =
specifiedFocusTrapOptions[optionName];
}

We could add a type check like:

if (optionName === 'allowOutsideClick' && typeof specifiedFocusTrapOptions[optionName] === 'boolean') {
    tailoredFocusTrapOptions[optionName] = () => specifiedFocusTrapOptions[optionName]
}

Then update the prop types at the bottom to be:

allowOutsideClick: PropTypes.oneOfType([
    PropTypes.func,
    PropTypes.bool
]),

Conclusion

This is a small patch that would make this little feature a little more usable. I'd be happy to contribute and make this addition, but yield to the maintainers for further discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions