import React, { useEffect, useRef } from 'react' import withDefaults from '../utils/with-defaults' import useTheme from '../use-theme' import CSSTransition from '../shared/css-transition' import { isChildElement } from '../utils/collections' interface Props { className?: string visible?: boolean } const defaultProps = { className: '', visible: false } export type ModalWrapperProps = Props & typeof defaultProps const ModalWrapper: React.FC> = ({ className, children, visible, ...props }) => { const theme = useTheme() const modalContent = useRef(null) const tabStart = useRef(null) const tabEnd = useRef(null) useEffect(() => { if (!visible) return const activeElement = document.activeElement const isChild = isChildElement(modalContent.current, activeElement) if (isChild) return tabStart.current && tabStart.current.focus() }, [visible]) const onKeyDown = (event: React.KeyboardEvent) => { const isTabDown = event.keyCode === 9 if (!visible || !isTabDown) return const activeElement = document.activeElement if (event.shiftKey) { if (activeElement === tabStart.current) { tabEnd.current && tabEnd.current.focus() } } else { if (activeElement === tabEnd.current) { tabStart.current && tabStart.current.focus() } } } return (