From e07830dc49d5e4fe9665a1718c24b33de074d8ac Mon Sep 17 00:00:00 2001 From: Manuel Date: Thu, 7 Dec 2023 15:34:54 +0100 Subject: [PATCH 1/2] feat(components): pass close function to disclosure root component --- .../navigation/navigation-disclosure.tsx | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/components/navigation/navigation-disclosure.tsx b/src/components/navigation/navigation-disclosure.tsx index 5a6dda32..d57edc6a 100644 --- a/src/components/navigation/navigation-disclosure.tsx +++ b/src/components/navigation/navigation-disclosure.tsx @@ -16,15 +16,31 @@ const NavigationDisclosureButton = ({ children, LeftIcon }: NavigationDisclosure ); }; +type CloseFunction = ( + focusableElement?: HTMLElement | React.MutableRefObject +) => 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 ( - {children} + {({ close }) => <>{renderDisclosureChildren({ children, close })}} ); }; From e5c094870d712cfff52c2e6329c7d2d429ba47b6 Mon Sep 17 00:00:00 2001 From: Manuel Date: Thu, 7 Dec 2023 16:17:19 +0100 Subject: [PATCH 2/2] fix(components): user interfaces instead of type --- .../navigation/navigation-disclosure.tsx | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/components/navigation/navigation-disclosure.tsx b/src/components/navigation/navigation-disclosure.tsx index d57edc6a..4bfffdba 100644 --- a/src/components/navigation/navigation-disclosure.tsx +++ b/src/components/navigation/navigation-disclosure.tsx @@ -16,27 +16,29 @@ const NavigationDisclosureButton = ({ children, LeftIcon }: NavigationDisclosure ); }; -type CloseFunction = ( - focusableElement?: HTMLElement | React.MutableRefObject -) => void; +interface CloseFunction { + (focusableElement?: HTMLElement | React.MutableRefObject): void; +} + +interface ChildrenType { + (props: { close: CloseFunction }): React.ReactNode; +} -type ChildrenType = (({ close }: { close: CloseFunction }) => React.ReactNode) | React.ReactNode; +export interface NavigationDisclosureProps { + children: ChildrenType | React.ReactNode; + defaultOpen?: boolean; +} const renderDisclosureChildren = ({ children, close, }: { - children: ChildrenType; + children: ChildrenType | React.ReactNode; close: CloseFunction; }) => { return typeof children === "function" ? children({ close }) : children; }; -export interface NavigationDisclosureProps { - children: ChildrenType; - defaultOpen?: boolean; -} - const NavigationDisclosure = ({ children, defaultOpen }: NavigationDisclosureProps) => { return (