Skip to content

Commit

Permalink
Feat/export modal manager (#7338)
Browse files Browse the repository at this point in the history
Co-authored-by: Segun Adebayo <joseshegs@gmail.com>
  • Loading branch information
bertho-zero and segunadebayo committed May 28, 2023
1 parent 3a164da commit 0167b68
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/five-fishes-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@chakra-ui/modal": patch
---

Exports `modalManager` and returns the index of the modal from `useModal` to
allow creating custom components similar to modals
1 change: 1 addition & 0 deletions packages/components/modal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ export { ModalOverlay } from "./modal-overlay"
export type { ModalOverlayProps } from "./modal-overlay"
export { useModal } from "./use-modal"
export type { UseModalProps, UseModalReturn } from "./use-modal"
export { useModalManager } from "./modal-manager"
6 changes: 3 additions & 3 deletions packages/components/modal/src/modal-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ModalManager {
}
}

export const manager = new ModalManager()
export const modalManager = new ModalManager()

export function useModalManager(ref: RefObject<HTMLElement>, isOpen?: boolean) {
const [index, setIndex] = useState(0)
Expand All @@ -36,12 +36,12 @@ export function useModalManager(ref: RefObject<HTMLElement>, isOpen?: boolean) {
if (!node) return

if (isOpen) {
const index = manager.add(node)
const index = modalManager.add(node)
setIndex(index)
}

return () => {
manager.remove(node)
modalManager.remove(node)
setIndex(0)
}
}, [isOpen, ref])
Expand Down
7 changes: 4 additions & 3 deletions packages/components/modal/src/use-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mergeRefs } from "@chakra-ui/react-use-merge-refs"
import { hideOthers } from "aria-hidden"

import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react"
import { manager, useModalManager } from "./modal-manager"
import { modalManager, useModalManager } from "./modal-manager"

export interface UseModalProps {
/**
Expand Down Expand Up @@ -86,7 +86,7 @@ export function useModal(props: UseModalProps) {
/**
* Hook used to manage multiple or nested modals
*/
useModalManager(dialogRef, isOpen)
const index = useModalManager(dialogRef, isOpen)

const mouseDownTarget = useRef<EventTarget | null>(null)

Expand Down Expand Up @@ -145,7 +145,7 @@ export function useModal(props: UseModalProps) {
/**
* When you click on the overlay, we want to remove only the topmost modal
*/
if (!manager.isTopModal(dialogRef.current)) return
if (!modalManager.isTopModal(dialogRef.current)) return

if (closeOnOverlayClick) {
onClose?.()
Expand Down Expand Up @@ -178,6 +178,7 @@ export function useModal(props: UseModalProps) {
overlayRef,
getDialogProps,
getDialogContainerProps,
index,
}
}

Expand Down

1 comment on commit 0167b68

@vercel
Copy link

@vercel vercel bot commented on 0167b68 May 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.