Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/seven-apricots-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/modal": patch
"@nextui-org/theme": patch
---

adjust modal position when keyboard appears (#2837)
18 changes: 16 additions & 2 deletions packages/components/modal/src/modal-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -42,6 +42,8 @@ const ModalContent = forwardRef<"div", ModalContentProps, KeysToOmit>((props, _)

const Component = as || DialogComponent || "div";

const viewport = useViewportSize();

const {dialogProps} = useDialog(
{
role,
Expand Down Expand Up @@ -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>
) : (
Expand All @@ -111,6 +123,8 @@ const ModalContent = forwardRef<"div", ModalContentProps, KeysToOmit>((props, _)
initial="exit"
variants={scaleInOut}
{...motionProps}
// @ts-ignore
style={viewportStyle}
>
{content}
</m.div>
Expand Down
1 change: 1 addition & 0 deletions packages/core/theme/src/components/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const modal = tv({
"z-50",
"overflow-x-auto",
"justify-center",
"h-[--visual-viewport-height]",
],
base: [
"flex",
Expand Down