Skip to content

Commit

Permalink
Fix dropdown menus in Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Oct 6, 2023
1 parent fdad94e commit d462a6f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/app/components/book-tile/book-tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { faCaretDown } from '@fortawesome/free-solid-svg-icons/faCaretDown';
import { FormattedMessage, useIntl } from 'react-intl';
import useAmazonAssociatesLink from '~/pages/details/common/get-this-title-files//amazon-associates-link';
import { usePrintCopyDialog, isRealPrintLink } from '~/pages/details/common/get-this-title-files/options';
import useActiveElementContext from '~/contexts/active-element';
import bookPromise from '~/models/book-titles';
import cn from 'classnames';
import './book-tile.scss';
Expand Down Expand Up @@ -35,20 +34,32 @@ function PrintOption({ bookInfo }) {

function GetTheBookDropdown({ bookInfo }) {
const [isOpen, toggle] = useToggle();
const activeElement = useActiveElementContext();
const ref = React.useRef();
const { slug } = bookInfo;
const buttonId = `${slug}-ddb`;
const menuId = `${slug}-ddm`;
const webviewLink = bookInfo.webviewRexLink || bookInfo.webviewLink;
const pdfLink =
bookInfo.highResolutionPdfUrl || bookInfo.lowResolutionPdfUrl;

React.useLayoutEffect(() => {
if (isOpen && !ref.current.contains(activeElement)) {
const toggleMenu = React.useCallback(
() => {
toggle();
}
}, [activeElement, isOpen, toggle]);
},
[toggle]
);

// This is supposed to close it when another opens
React.useEffect(() => {
const outsideClick = (e) => {
if (isOpen && !ref.current.contains(e.target)) {
toggle();
}
};

window.addEventListener('click', outsideClick);

return () => window.removeEventListener('click', outsideClick);
}, [isOpen, toggle]);

return (
<div className="navmenu" ref={ref}>
Expand All @@ -58,7 +69,7 @@ function GetTheBookDropdown({ bookInfo }) {
aria-haspopup="true"
aria-controls={menuId}
aria-expanded={isOpen}
onClick={() => toggle()}
onClick={toggleMenu}
>
<FormattedMessage
id="getTheBook"
Expand Down

0 comments on commit d462a6f

Please sign in to comment.