Skip to content

Commit

Permalink
feat: update modal accessibility improvements
Browse files Browse the repository at this point in the history
feat: update modal accessibility improvements
  • Loading branch information
Chisomchima authored and kabaros committed Mar 18, 2024
1 parent ad2f436 commit 9e7abc3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/modal/src/modal/close-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const createClickHandler = (onClick) => (event) => {

export const CloseButton = ({ onClick }) => (
<button
title="Close modal dialog"
data-test="dhis2-modal-close-button"
onClick={createClickHandler(onClick)}
aria-label="Close modal dialog"
>
<IconCross16 />
<style jsx>{`
Expand Down
23 changes: 22 additions & 1 deletion components/modal/src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Layer } from '@dhis2-ui/layer'
import { spacers, spacersNum, sharedPropTypes } from '@dhis2/ui-constants'
import cx from 'classnames'
import PropTypes from 'prop-types'
import React from 'react'
import React, { useEffect } from 'react'
import { resolve } from 'styled-jsx/css'
import { CloseButton } from './close-button.js'

Expand All @@ -28,6 +28,27 @@ export const Modal = ({
small,
}) => {
const layerStyles = resolveLayerStyles(hide)

useEffect(() => {
if (hide) {
return
}

const handleKeyDown = (event) => {
event.preventDefault()
if (event.key === 'Escape' && onClose) {
event.stopPropagation()
onClose()
}
}

document.addEventListener('keydown', handleKeyDown)

return () => {
document.removeEventListener('keydown', handleKeyDown)
}
}, [hide, onClose])

return (
<Layer
onBackdropClick={onClose}
Expand Down
42 changes: 41 additions & 1 deletion components/modal/src/modal/modal.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
import { render, screen } from '@testing-library/react'
import { render, screen, fireEvent } from '@testing-library/react'
import React from 'react'
import { CloseButton } from './close-button.js'
import { Modal } from './modal.js'

describe('Modal', () => {
describe('Modal Accessibility', () => {
it('closes when ESC key is pressed', () => {
const onCloseMock = jest.fn()
render(<Modal onClose={onCloseMock} />)

const modalElement = screen.getByRole('dialog')
fireEvent.keyDown(modalElement, { key: 'Escape', code: 'Escape' })
expect(onCloseMock).toHaveBeenCalled()
})

it('does not close when "Enter" is pressed', () => {
const onCloseMock = jest.fn()
render(<Modal onClose={onCloseMock} />)

const modalElement = screen.getByRole('dialog')
fireEvent.keyDown(modalElement, { key: 'Enter', code: 'Enter' })
expect(onCloseMock).not.toHaveBeenCalled()
})

it('does not close when "SpaceBar" is pressed', () => {
const onCloseMock = jest.fn()
render(<Modal onClose={onCloseMock} />)

const modalElement = screen.getByRole('dialog')
fireEvent.keyDown(modalElement, { key: ' ', code: ' ' })
expect(onCloseMock).not.toHaveBeenCalled()
})

it('has a close button with proper accessibility attributes', async () => {
render(<CloseButton />)
const closeButton = await screen.findByLabelText(
'Close modal dialog'
)

expect(closeButton).toBeInTheDocument()
expect(closeButton.tagName).toBe('BUTTON')
})
})

describe('Regular dimensions', () => {
it('has the correct dimension styles in its default state', () => {
render(<Modal />)
Expand Down

0 comments on commit 9e7abc3

Please sign in to comment.