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

feat: add initialFocus support to Modal component #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
73 changes: 53 additions & 20 deletions src/Components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Dialog, Transition } from '@headlessui/react';
import classNames from 'classnames';
import React, { PropsWithChildren } from 'react';
import React, { PropsWithChildren, Ref, forwardRef } from 'react';
import ReactDOM from 'react-dom';

export interface ModalProps {
isOpen: boolean;
onClose(): void;
maxWidth?: string;
initialFocus?: React.RefObject<HTMLElement>;
}

const Modal = ({ isOpen, onClose, maxWidth = 'lg', children }: PropsWithChildren<ModalProps>) => {
const Modal = ({
isOpen,
onClose,
maxWidth = 'lg',
children,
initialFocus,
}: PropsWithChildren<ModalProps>) => {
const maxWidthClass = {
sm: 'sm:max-w-sm',
md: 'sm:max-w-md',
Expand All @@ -30,6 +37,7 @@ const Modal = ({ isOpen, onClose, maxWidth = 'lg', children }: PropsWithChildren
className="fixed z-10 inset-0 overflow-y-auto"
open={isOpen}
onClose={onClose}
initialFocus={initialFocus}
>
<div className="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<Transition.Child
Expand Down Expand Up @@ -73,24 +81,49 @@ const Modal = ({ isOpen, onClose, maxWidth = 'lg', children }: PropsWithChildren
);
};

const ModalHeader = ({ children, className }: PropsWithChildren<{ className?: string }>) => {
return (
<div className={classNames('text-lg font-medium text-gray-900 mb-4 px-5 pt-5', className)}>
{children}
</div>
);
};
const ModalHeader = forwardRef(
(
{ children, className }: PropsWithChildren<{ className?: string }>,
ref?: Ref<HTMLDivElement>
) => {
return (
<div
ref={ref}
className={classNames('text-lg font-medium text-gray-900 mb-4 px-5 pt-5', className)}
>
{children}
</div>
);
}
);

const ModalBody = ({ children, className }: PropsWithChildren<{ className?: string }>) => {
return <div className={classNames('px-5 pt-0', className)}>{children}</div>;
};
const ModalBody = forwardRef(
(
{ children, className }: PropsWithChildren<{ className?: string }>,
ref?: Ref<HTMLDivElement>
) => {
return (
<div ref={ref} className={classNames('px-5 pt-0', className)}>
{children}
</div>
);
}
);

const ModalActions = ({ children, className }: PropsWithChildren<{ className?: string }>) => {
return (
<div className={classNames('px-5 py-4 mt-4 sm:px-6 sm:flex sm:flex-row-reverse', className)}>
{children}
</div>
);
};
const ModalActions = forwardRef(
(
{ children, className }: PropsWithChildren<{ className?: string }>,
ref?: Ref<HTMLDivElement>
) => {
return (
<div
ref={ref}
className={classNames('px-5 py-4 mt-4 sm:px-6 sm:flex sm:flex-row-reverse', className)}
>
{children}
</div>
);
}
);

export { Modal, ModalHeader, ModalBody, ModalActions };
export { Modal, ModalActions, ModalBody, ModalHeader };
Loading