Skip to content

Commit ff7db03

Browse files
committed
feat: enhance GenericModal with border variant and alignment options; add escape key handling for focus trap
1 parent 30bf7b1 commit ff7db03

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/Shared/Components/DTFocusTrap/DTFocusTrap.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,24 @@ const DTFocusTrap = ({
3838
[onEscape, deactivateFocusOnEscape],
3939
)
4040

41+
// Focus escape key bind when focus trap is avoided
42+
const handleEscapeKeyBind = (e: KeyboardEvent) => {
43+
if (e.key === 'Escape') {
44+
onEscape(e)
45+
}
46+
}
47+
4148
useEffect(() => {
49+
if (avoidFocusTrap) {
50+
document.addEventListener('keydown', handleEscapeKeyBind)
51+
}
4252
preventBodyScroll(true)
4353

4454
return () => {
4555
preventBodyScroll(false)
56+
if (avoidFocusTrap) {
57+
document.removeEventListener('keydown', handleEscapeKeyBind)
58+
}
4659
}
4760
}, [])
4861

src/Shared/Components/GenericModal/GenericModal.component.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { noop, stopPropagation } from '@Common/Helper'
2121
import { Backdrop, Button, ButtonStyleType, ButtonVariantType, Icon } from '@Shared/Components'
2222
import { ComponentSizeType } from '@Shared/constants'
2323

24-
import { MODAL_WIDTH_TO_CLASS_NAME_MAP } from './constants'
24+
import { BORDER_VARIANT_TO_CLASS_NAME_MAP, MODAL_WIDTH_TO_CLASS_NAME_MAP } from './constants'
2525
import { GenericModalProvider, useGenericModalContext } from './GenericModal.context'
2626
import { GenericModalFooterProps, GenericModalHeaderProps, GenericModalProps } from './types'
2727

@@ -88,6 +88,8 @@ const GenericModal = ({
8888
closeOnBackdropClick = false,
8989
children,
9090
avoidFocusTrap = false,
91+
borderVariant = 'secondary',
92+
alignCenter = false,
9193
}: PropsWithChildren<GenericModalProps>) => (
9294
<AnimatePresence>
9395
{open && (
@@ -98,7 +100,7 @@ const GenericModal = ({
98100
avoidFocusTrap={avoidFocusTrap}
99101
>
100102
<motion.div
101-
className={`shadow__modal flexbox-col bg__primary border__secondary br-${borderRadius} dc__m-auto mt-40 dc__overflow-hidden ${MODAL_WIDTH_TO_CLASS_NAME_MAP[width]}`}
103+
className={`shadow__modal flexbox-col bg__primary ${BORDER_VARIANT_TO_CLASS_NAME_MAP[borderVariant]} br-${borderRadius} dc__m-auto ${alignCenter ? '' : 'mt-40'} dc__overflow-hidden ${MODAL_WIDTH_TO_CLASS_NAME_MAP[width]}`}
102104
exit={{ y: 100, opacity: 0, scale: 0.75, transition: { duration: 0.35 } }}
103105
initial={{ y: 100, opacity: 0, scale: 0.75 }}
104106
animate={{ y: 0, opacity: 1, scale: 1 }}

src/Shared/Components/GenericModal/constants.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { GenericModalProps } from './types'
17+
import { BorderVariantType, GenericModalProps } from './types'
1818

1919
export const MODAL_WIDTH_TO_CLASS_NAME_MAP: Record<GenericModalProps['width'], string> = {
2020
450: 'w-450',
2121
500: 'w-500',
2222
600: 'w-600',
2323
800: 'w-800',
2424
}
25+
26+
export const BORDER_VARIANT_TO_CLASS_NAME_MAP: Record<BorderVariantType, string> = {
27+
secondary: 'border__secondary',
28+
none: '',
29+
'secondary-translucent': 'border__secondary-translucent',
30+
}

src/Shared/Components/GenericModal/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import { BackdropProps } from '../Backdrop'
1818
import { ButtonProps } from '../Button'
1919

20+
export type BorderVariantType = 'secondary' | 'none' | 'secondary-translucent'
21+
2022
export interface GenericModalProps extends Partial<Pick<BackdropProps, 'onEscape' | 'avoidFocusTrap'>> {
2123
/** Unique identifier for the modal */
2224
name: string
@@ -35,6 +37,8 @@ export interface GenericModalProps extends Partial<Pick<BackdropProps, 'onEscape
3537
* @default false
3638
*/
3739
closeOnBackdropClick?: boolean
40+
borderVariant?: BorderVariantType
41+
alignCenter?: boolean
3842
}
3943

4044
export interface GenericModalContextType extends Pick<GenericModalProps, 'name' | 'onClose'> {}

0 commit comments

Comments
 (0)