fix(modal): modal position when keyboard appears (#3691)

* feat(theme): add h-[--visual-viewport-height] to modal wrapper

* fix(modal): apply --visual-viewport-height

* chore(changeset): add changeset
This commit is contained in:
աӄա 2024-09-01 09:34:24 +08:00 committed by GitHub
parent 59ea6b7457
commit 3e06ef64cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,6 @@
---
"@nextui-org/modal": patch
"@nextui-org/theme": patch
---
adjust modal position when keyboard appears (#2837)

View File

@ -8,7 +8,7 @@ import {TRANSITION_VARIANTS} from "@nextui-org/framer-utils";
import {CloseIcon} from "@nextui-org/shared-icons";
import {domAnimation, LazyMotion, m} from "framer-motion";
import {useDialog} from "@react-aria/dialog";
import {chain, mergeProps} from "@react-aria/utils";
import {chain, mergeProps, useViewportSize} from "@react-aria/utils";
import {HTMLNextUIProps} from "@nextui-org/system";
import {KeyboardEvent} from "react";
@ -42,6 +42,8 @@ const ModalContent = forwardRef<"div", ModalContentProps, KeysToOmit>((props, _)
const Component = as || DialogComponent || "div";
const viewport = useViewportSize();
const {dialogProps} = useDialog(
{
role,
@ -97,8 +99,18 @@ const ModalContent = forwardRef<"div", ModalContentProps, KeysToOmit>((props, _)
);
}, [backdrop, disableAnimation, getBackdropProps]);
// set the height dynamically to avoid keyboard covering the bottom modal
const viewportStyle = {
"--visual-viewport-height": viewport.height + "px",
};
const contents = disableAnimation ? (
<div className={slots.wrapper({class: classNames?.wrapper})} data-slot="wrapper">
<div
className={slots.wrapper({class: classNames?.wrapper})}
data-slot="wrapper"
// @ts-ignore
style={viewportStyle}
>
{content}
</div>
) : (
@ -111,6 +123,8 @@ const ModalContent = forwardRef<"div", ModalContentProps, KeysToOmit>((props, _)
initial="exit"
variants={scaleInOut}
{...motionProps}
// @ts-ignore
style={viewportStyle}
>
{content}
</m.div>

View File

@ -32,6 +32,7 @@ const modal = tv({
"z-50",
"overflow-x-auto",
"justify-center",
"h-[--visual-viewport-height]",
],
base: [
"flex",