Skip to content

Commit 36f5449

Browse files
committed
feat(ui-alerts): expand Alert's liveRegion to accept direct DOM element
INSTUI-4454
1 parent c827833 commit 36f5449

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

packages/ui-alerts/src/Alert/__tests__/Alert.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ describe('<Alert />', () => {
148148
expect(liveRegion).toHaveAttribute('aria-live', 'polite')
149149
})
150150

151+
it('should accept a direct DOM element as liveRegion', async () => {
152+
const liveRegion = document.getElementById('_alertLiveRegion')!
153+
render(
154+
<Alert
155+
variant="success"
156+
transition="none"
157+
liveRegion={liveRegion}
158+
liveRegionPoliteness="polite"
159+
isLiveRegionAtomic
160+
>
161+
Success: Sample alert text.
162+
</Alert>
163+
)
164+
165+
expect(liveRegion).toHaveTextContent('Success: Sample alert text.')
166+
expect(liveRegion).toHaveAttribute('aria-atomic', 'true')
167+
})
168+
151169
it('should render an icon when provided as the `renderIcon` prop', () => {
152170
const { container } = render(<Alert renderCustomIcon={<IconGroupLine />} />)
153171
const icon = container.querySelector('svg[class$="-svgIcon"]')

packages/ui-alerts/src/Alert/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ class Alert extends Component<AlertProps, AlertState> {
136136
}
137137

138138
// duck type for a dom node
139-
isDOMNode(n: Element | null) {
139+
isDOMNode(n: Element | null | undefined) {
140140
return n && typeof n === 'object' && n.nodeType === 1
141141
}
142142

143143
getLiveRegion() {
144-
let lr = null
145-
if (typeof this.props.liveRegion === 'function') {
146-
lr = this.props.liveRegion()
147-
}
144+
const lr =
145+
typeof this.props.liveRegion === 'function'
146+
? this.props.liveRegion()
147+
: this.props.liveRegion
148148

149149
return this.isDOMNode(lr) ? lr : null
150150
}

packages/ui-alerts/src/Alert/props.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ type AlertOwnProps = {
5151
*/
5252
variantScreenReaderLabel?: string
5353
/**
54-
* Function that returns the DIV where screenreader alerts will be placed.
54+
* A DOM element or function that returns an element where screenreader alerts will be placed.
5555
*/
56-
liveRegion?: () => Element
56+
liveRegion?: Element | null | (() => Element | null | undefined)
5757
/**
5858
* Choose the politeness level of screenreader alerts, sets the value of
5959
* `aria-live`.
@@ -132,7 +132,7 @@ const propTypes: PropValidators<PropKeys> = {
132132
children: PropTypes.node,
133133
variant: PropTypes.oneOf(['info', 'success', 'warning', 'error']),
134134
margin: PropTypes.string,
135-
liveRegion: PropTypes.func,
135+
liveRegion: PropTypes.oneOfType([PropTypes.func, PropTypes.element]),
136136
liveRegionPoliteness: PropTypes.oneOf(['polite', 'assertive']),
137137
isLiveRegionAtomic: PropTypes.bool,
138138
screenReaderOnly: PropTypes.bool,

0 commit comments

Comments
 (0)