Skip to content

Commit

Permalink
feat(components): pass close function to disclosure root component
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlfischer committed Dec 7, 2023
1 parent f5ab60b commit e07830d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/components/navigation/navigation-disclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,31 @@ const NavigationDisclosureButton = ({ children, LeftIcon }: NavigationDisclosure
);
};

type CloseFunction = (
focusableElement?: HTMLElement | React.MutableRefObject<HTMLElement | null>
) => void;

type ChildrenType = (({ close }: { close: CloseFunction }) => React.ReactNode) | React.ReactNode;

const renderDisclosureChildren = ({
children,
close,
}: {
children: ChildrenType;
close: CloseFunction;
}) => {
return typeof children === "function" ? children({ close }) : children;
};

export interface NavigationDisclosureProps {
children: React.ReactNode;
children: ChildrenType;
defaultOpen?: boolean;
}

const NavigationDisclosure = ({ children, defaultOpen }: NavigationDisclosureProps) => {
return (
<Disclosure as="div" defaultOpen={defaultOpen}>
{children}
{({ close }) => <>{renderDisclosureChildren({ children, close })}</>}
</Disclosure>
);
};
Expand Down

0 comments on commit e07830d

Please sign in to comment.