Skip to content

Commit

Permalink
feat: update modal
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Jul 18, 2024
1 parent d509263 commit 434081b
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 275 deletions.
7 changes: 3 additions & 4 deletions demo/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import "@/styles/globals.css"

import { Metadata } from "next"
import { clsxm } from "~/lib/helper"
import { MobileDetector } from "rc-modal-sheet/src/helpers/mobile-detector"
import { ModalStackContainer } from "rc-modal-sheet/src/helpers/motion"
import { MobileDetector } from "rc-modal-sheet/mobile-detector"
import { ModalStackContainer } from "rc-modal-sheet/motion"

import "./index.css"

import type { SVGProps } from "react"
import Script from "next/script"
import { PresentSheet } from "~/sheet"
import { GeistMono } from "geist/font/mono"
import { GeistSans } from "geist/font/sans"

import { siteConfig } from "@/config/site"
Expand Down Expand Up @@ -81,7 +80,7 @@ export default function RootLayout({ children }: RootLayoutProps) {
<ThemeProvider>
<div className="relative flex min-h-screen flex-col">
<SiteHeader />
<ModalStackContainer clickOutsideToDismiss>
<ModalStackContainer overlay={false} clickOutsideToDismiss>
<MobileDetector />
<div className="flex-1">
<Hero />
Expand Down
1 change: 0 additions & 1 deletion demo/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import remarkMdxFrontmatter from "remark-mdx-frontmatter"
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
transpilePackages: ["rc-modal-sheet"],
pageExtensions: ["js", "jsx", "mdx", "ts", "tsx"],
output: process.env.NODE_ENV === "production" ? "export" : "standalone",
}
Expand Down
1 change: 0 additions & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "next-template",
"version": "0.0.2",
"private": true,
"type": "module",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand Down
92 changes: 42 additions & 50 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,51 @@
"type": "git",
"url": "https://github.com/Innei/rc-modal"
},
"main": "./src/index.ts",
"exports": {
".": {
"import": "./src/index.ts"
},
"./src/*": {
"import": "./src/*"
}
},
"files": [
"dist",
"readme.md",
"tsconfig.json",
"src",
"styles.d.ts"
],
"publishConfig": {
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./index.css": {
"require": {
"type": "./styles.d.ts",
"default": "./dist/index.css"
},
"import": {
"type": "./styles.d.ts",
"default": "./dist/index.css"
}
},
"./motion": {
"require": "./dist/helpers/motion.js",
"import": "./dist/helpers/motion.mjs"
},
"./m": {
"require": "./dist/helpers/m.js",
"import": "./dist/helpers/m.mjs"
},
"./mobile-detector": {
"require": "./dist/helpers/mobile-detector.js",
"import": "./dist/helpers/mobile-detector.mjs"
},
"./dist/*": {
"require": "./dist/*",
"import": "./dist/*"
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./index.css": {
"require": {
"type": "./styles.d.ts",
"default": "./dist/index.css"
},
"./*": [
"./*",
"./*.d.ts"
]
}
"import": {
"type": "./styles.d.ts",
"default": "./dist/index.css"
}
},
"./motion": {
"require": "./dist/helpers/motion.js",
"import": "./dist/helpers/motion.mjs"
},
"./m": {
"require": "./dist/helpers/m.js",
"import": "./dist/helpers/m.mjs"
},
"./mobile-detector": {
"require": "./dist/helpers/mobile-detector.js",
"import": "./dist/helpers/mobile-detector.mjs"
},
"./dist/*": {
"require": "./dist/*",
"import": "./dist/*"
},
"./*": [
"./*",
"./*.d.ts"
]
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -96,7 +85,8 @@
"prebuild": "rm -rf lib && rm -rf esm",
"build": "vite build && tailwindcss -i src/tw.css -o dist/index.css",
"prepare": "husky install",
"release": "bump"
"release": "bump",
"dev": "vite build --watch"
},
"devDependencies": {
"@innei/bump-version": "1.5.10",
Expand All @@ -106,6 +96,7 @@
"@radix-ui/react-dialog": "1.0.5",
"@types/node": "20.14.8",
"@types/react": "18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "4.3.1",
"dts-bundle-generator": "^9.5.1",
"esbuild": "0.21.5",
Expand All @@ -117,6 +108,7 @@
"postcss-nested": "6.0.1",
"prettier": "3.3.2",
"react": "18.3.1",
"react-dom": "^18.3.1",
"rollup-plugin-preserve-directives": "0.4.0",
"tailwindcss": "3.4.4",
"tsc-alias": "1.8.10",
Expand All @@ -141,4 +133,4 @@
"tailwind-merge": "^2.3.0",
"vaul": "^0.9.1"
}
}
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 0 additions & 26 deletions src/dialog/DialogOverlay.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/dialog/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/modal/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MODAL_STACK_Z_INDEX = 100
16 changes: 15 additions & 1 deletion src/modal/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
useSetIsMobile,
} from '~/providers'

import { MODAL_STACK_Z_INDEX } from './constants'
import { Modal } from './modal'
import { ModalOverlay } from './overlay'

export const ModalStackContainer: FC<
ModalStackContainerProps & PropsWithChildren
Expand Down Expand Up @@ -61,11 +63,23 @@ const SetMobile: FC<{ m: boolean }> = ({ m }) => {
const ModalStack = memo(() => {
const stack = useModalStackInternal()

const forceOverlay = stack.some((item) => item.overlay)

return (
<AnimatePresence mode="popLayout">
{stack.map((item, index) => {
return <Modal key={item.id} item={item} index={index} />
return (
<Modal
isTop={index === stack.length - 1}
key={item.id}
item={item}
index={index}
/>
)
})}
{stack.length > 0 && forceOverlay && (
<ModalOverlay zIndex={MODAL_STACK_Z_INDEX + stack.length - 1} />
)}
</AnimatePresence>
)
})
11 changes: 8 additions & 3 deletions src/modal/declarative-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import type { ModalProps } from './types'
import { clsxm } from '~/lib/helper'
import { useModalStackInternal } from '~/providers'

import { MODAL_STACK_Z_INDEX } from './constants'
import { Modal } from './modal'
import { ModalOverlay } from './overlay'

export interface DeclarativeModalProps extends Omit<ModalProps, 'content'> {
open?: boolean
Expand Down Expand Up @@ -41,9 +43,12 @@ const DeclarativeModalImpl: FC<DeclarativeModalProps> = ({
return (
<AnimatePresence>
{open && (
<Modal onClose={onOpenChange} index={index} item={item}>
{children}
</Modal>
<>
<ModalOverlay zIndex={MODAL_STACK_Z_INDEX - 10 + index} />
<Modal onClose={onOpenChange} isTop index={index} item={item}>
{children}
</Modal>
</>
)}
</AnimatePresence>
)
Expand Down
20 changes: 17 additions & 3 deletions src/modal/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ export const useModalStack = (options?: ModalStackOptions) => {
return {
present: useCallback(
(props: ModalProps & { id?: string }) => {
const modalId = `${id}-${++currentCount.current}`
setModalStack((p) => {
const fallbackModelId = `${id}-${++currentCount.current}`
const modalId = props.id ?? fallbackModelId

setModalStack((currentStack) => {
const existingModal = currentStack.find((item) => item.id === modalId)

if (existingModal) {
// Move to top
const index = currentStack.indexOf(existingModal)
return [
...currentStack.slice(0, index),
...currentStack.slice(index + 1),
existingModal,
]
}

const modalProps = {
...props,
id: props.id ?? modalId,
wrapper,
}
modalIdToPropsMap[modalProps.id] = modalProps
return p.concat(modalProps)
return currentStack.concat(modalProps)
})

return () => {
Expand Down
Loading

0 comments on commit 434081b

Please sign in to comment.