Skip to content

Commit

Permalink
Allow passing context object through openModal + support jsx in modal…
Browse files Browse the repository at this point in the history
… subheader
  • Loading branch information
jaclarke committed Sep 19, 2024
1 parent 422907c commit 0c5b5a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
22 changes: 15 additions & 7 deletions shared/common/hooks/useModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,21 @@ export function useModal(): {
modal: JSX.Element | null;
openModal: (modal: JSX.Element | null, transition?: boolean) => () => void;
};
export function useModal(modal: JSX.Element): {
export function useModal<Ctx extends any = null>(
modal: (ctx: Ctx) => JSX.Element
): {
modal: JSX.Element | null;
openModal: (transition?: boolean) => () => void;
openModal: (ctx: Ctx, transition?: boolean) => () => void;
};
export function useModal(modal?: JSX.Element): {
export function useModal(modal?: (ctx: any) => JSX.Element): {
modal: JSX.Element | null;
openModal:
| ((modal: JSX.Element | null, transition?: boolean) => () => void)
| ((transition?: boolean) => () => void);
| ((ctx: any, transition?: boolean) => () => void);
} {
const ctx = useContext(modalContext);
const placeholder = useRef(<></>);
const modalCtx = useRef<any>();

if (!modal) {
return {
Expand All @@ -119,10 +122,15 @@ export function useModal(modal?: JSX.Element): {
return {
modal:
ctx.modal === placeholder.current
? createPortal(modal, document.getElementById("modal_target")!)
? createPortal(
modal(modalCtx.current),
document.getElementById("modal_target")!
)
: null,
openModal: (transition?: boolean) =>
ctx.openModal(placeholder.current, transition),
openModal: (_ctx: any, transition?: boolean) => {
modalCtx.current = _ctx;
return ctx.openModal(placeholder.current, transition);
},
};
}

Expand Down
2 changes: 1 addition & 1 deletion shared/common/newui/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {CrossIcon, WarningIcon} from "../icons";
export interface ModalProps {
className?: string;
title: string;
subheading?: string;
subheading?: string | JSX.Element;
onClose?: () => void;
noCloseOnOverlayClick?: boolean;
onSubmit?: () => void;
Expand Down
4 changes: 4 additions & 0 deletions shared/common/newui/modal/modal.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
font-style: normal;
font-weight: 400;
line-height: 18px;

b {
font-weight: 550;
}
}

@include darkTheme {
Expand Down
6 changes: 3 additions & 3 deletions shared/common/ui/navtabs/mobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export function MobileNavTabs({
Link,
extraMenu,
}: MobileNavTabsProps) {
const {modal, openModal} = useModal(
const {modal, openModal} = useModal(() => (
<MobileMenu
tabs={tabs}
currentTabId={currentTabId}
Link={Link}
extraMenu={extraMenu}
/>
);
));
const modalDisposer = useRef<null | (() => void)>(null);
const windowWidth = useWindowWidth();

Expand Down Expand Up @@ -61,7 +61,7 @@ export function MobileNavTabs({
</div>
<div
className={styles.menuButton}
onClick={() => (modalDisposer.current = openModal())}
onClick={() => (modalDisposer.current = openModal(null))}
>
<EllipsisIcon />
</div>
Expand Down

0 comments on commit 0c5b5a1

Please sign in to comment.