Skip to content

Commit

Permalink
chore: Initial tidying pass on Popper
Browse files Browse the repository at this point in the history
* Add a few missing types
* Let the code breathe a bit
  • Loading branch information
jherdman committed May 23, 2024
1 parent d3cc07c commit 56079b7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/Popper/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,50 @@ const Popper: React.FC<PopperProps> = React.forwardRef(
popperRef
) => {
let timeoutID;

const [isOpen, setIsOpen] = useState(defaultOpen);
const conditionallyApplyDelay = (fnc, delay, skipDelay = true) => {

const conditionallyApplyDelay = (fnc: () => void, delay: number, skipDelay = true) => {
if (!skipDelay) {
timeoutID = setTimeout(fnc, delay);
} else {
fnc();
}
};
const setPopUpState = (nextIsOpenState, skipDelay) => {

const setPopUpState = (nextIsOpenState: boolean, skipDelay: boolean) => {
clearTimeout(timeoutID);
conditionallyApplyDelay(() => setIsOpen(nextIsOpenState), nextIsOpenState ? showDelay : hideDelay, skipDelay);
};
const closePopUp = (skipDelay) => {

const closePopUp = (skipDelay: boolean) => {
setPopUpState(false, skipDelay);
};

useEffect(() => {
const handleKeyDown = (event) => {
switch (event.keyCode) {
const handleKeyDown = (event: KeyboardEvent) => {
switch (event.code) {
case keyCodes.ESC:
closePopUp();
break;
default:
break;
}
};

document.addEventListener("keydown", handleKeyDown);

const cleanup = () => {
document.removeEventListener("keydown", handleKeyDown);
clearTimeout(timeoutID);
};
return cleanup;
}, []);
const openPopUp = (skipDelay) => {

const openPopUp = (skipDelay: boolean) => {
setPopUpState(true, skipDelay);
};

const onClickEventHandlers = openOnClick
? {
onClick: () => {
Expand All @@ -96,12 +105,14 @@ const Popper: React.FC<PopperProps> = React.forwardRef(
},
}
: null;

const onHoverHandlers = openOnHover
? {
onMouseEnter: () => openPopUp(false),
onMouseLeave: () => closePopUp(false),
}
: null;

const eventHandlers = {
onFocus: () => openPopUp(false),
onBlur: () => {
Expand All @@ -110,6 +121,7 @@ const Popper: React.FC<PopperProps> = React.forwardRef(
...onHoverHandlers,
...onClickEventHandlers,
};

const transformInnerChildren = (elements) =>
makeArray(elements).map((element, i) => {
const transformedElement = wrapInFunction(element)({
Expand All @@ -127,13 +139,18 @@ const Popper: React.FC<PopperProps> = React.forwardRef(
key: i,
});
});

const renderInnerChildren = () => {
const innerChildren = children.props.children;
return typeof innerChildren !== "string" ? transformInnerChildren(innerChildren) : innerChildren;
};

const { t } = useTranslation();

const openLabel = openAriaLabel || t("open");

const closeLabel = closeAriaLabel || t("close");

return (
<Manager ref={popperRef}>
<Reference>
Expand Down

0 comments on commit 56079b7

Please sign in to comment.