Skip to content

Commit

Permalink
Merge pull request #67 from abusix/pla-882-pass-close-function-to-dis…
Browse files Browse the repository at this point in the history
…closure-root-component

feat(components): pass close function to disclosure root component
  • Loading branch information
mnlfischer authored Dec 8, 2023
2 parents 7a5f5ca + e5c0948 commit 6d85175
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/components/navigation/navigation-disclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,33 @@ const NavigationDisclosureButton = ({ children, LeftIcon }: NavigationDisclosure
);
};

interface CloseFunction {
(focusableElement?: HTMLElement | React.MutableRefObject<HTMLElement | null>): void;
}

interface ChildrenType {
(props: { close: CloseFunction }): React.ReactNode;
}

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

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

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

0 comments on commit 6d85175

Please sign in to comment.