Skip to content

Commit d684e8e

Browse files
authored
feat: Add dividers prop to Modal to customize the weight for all dividers (#932)
1 parent bce333d commit d684e8e

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/modal/modal.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@ describe('ModalHeader', () => {
253253
rerender(<ModalHeader withDivider>Hello</ModalHeader>)
254254
expect(screen.getByRole('separator')).toBeInTheDocument()
255255
})
256+
257+
it('applies the dividerWeight prop to the divider', () => {
258+
renderModal(
259+
<Modal isOpen dividers="primary" aria-label="modal">
260+
<ModalHeader withDivider>Header</ModalHeader>
261+
</Modal>,
262+
)
263+
const divider = screen.getByRole('separator')
264+
expect(divider).toHaveClass('weight-primary')
265+
})
256266
})
257267

258268
describe('ModalFooter', () => {
@@ -296,6 +306,16 @@ describe('ModalFooter', () => {
296306
rerender(<ModalFooter withDivider>Hello</ModalFooter>)
297307
expect(screen.getByRole('separator')).toBeInTheDocument()
298308
})
309+
310+
it('applies the dividerWeight prop to the divider', () => {
311+
renderModal(
312+
<Modal isOpen dividers="primary" aria-label="modal">
313+
<ModalFooter withDivider>Footer</ModalFooter>
314+
</Modal>,
315+
)
316+
const divider = screen.getByRole('separator')
317+
expect(divider).toHaveClass('weight-primary')
318+
})
299319
})
300320

301321
describe('ModalActions', () => {

src/modal/modal.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { IconButtonProps, IconButton } from '../button'
1515
import styles from './modal.module.css'
1616
import type { ObfuscatedClassName } from '../utils/common-types'
1717
import { forwardRef } from 'react'
18+
import type { DividerProps } from '../divider'
1819

1920
type ModalWidth = 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | 'full'
2021
type ModalHeightMode = 'expand' | 'fitContent'
@@ -26,11 +27,13 @@ type ModalHeightMode = 'expand' | 'fitContent'
2627
type ModalContextValue = {
2728
onDismiss?(this: void): void
2829
height: ModalHeightMode
30+
dividers?: DividerProps['weight']
2931
}
3032

3133
const ModalContext = React.createContext<ModalContextValue>({
3234
onDismiss: undefined,
3335
height: 'fitContent',
36+
dividers: undefined,
3437
})
3538

3639
//
@@ -81,6 +84,11 @@ export interface ModalProps extends DivProps, ObfuscatedClassName {
8184
*/
8285
height?: ModalHeightMode
8386

87+
/**
88+
* The weight to apply to all dividers rendered inside the modal.
89+
*/
90+
dividers?: DividerProps['weight']
91+
8492
/**
8593
* Whether to set or not the focus initially to the first focusable element inside the modal.
8694
*/
@@ -152,6 +160,7 @@ export function Modal({
152160
isOpen,
153161
onDismiss,
154162
height = 'fitContent',
163+
dividers,
155164
width = 'medium',
156165
exceptionallySetClassName,
157166
exceptionallySetOverlayClassName,
@@ -175,9 +184,10 @@ export function Modal({
175184
)
176185
const store = useDialogStore({ open: isOpen, setOpen })
177186

178-
const contextValue: ModalContextValue = React.useMemo(() => ({ onDismiss, height }), [
187+
const contextValue: ModalContextValue = React.useMemo(() => ({ onDismiss, height, dividers }), [
179188
onDismiss,
180189
height,
190+
dividers,
181191
])
182192

183193
const portalRef = React.useRef<HTMLElement | null>(null)
@@ -378,6 +388,8 @@ export function ModalHeader({
378388
exceptionallySetClassName,
379389
...props
380390
}: ModalHeaderProps) {
391+
const { dividers } = React.useContext(ModalContext)
392+
381393
return (
382394
<>
383395
<Box
@@ -407,7 +419,7 @@ export function ModalHeader({
407419
)}
408420
</Columns>
409421
</Box>
410-
{withDivider ? <Divider /> : null}
422+
{withDivider ? <Divider weight={dividers} /> : null}
411423
</>
412424
)
413425
}
@@ -484,9 +496,11 @@ export function ModalFooter({
484496
withDivider = false,
485497
...props
486498
}: ModalFooterProps) {
499+
const { dividers } = React.useContext(ModalContext)
500+
487501
return (
488502
<>
489-
{withDivider ? <Divider /> : null}
503+
{withDivider ? <Divider weight={dividers} /> : null}
490504
<Box as="footer" {...props} className={exceptionallySetClassName} padding="large" />
491505
</>
492506
)

0 commit comments

Comments
 (0)