Skip to content

Commit fe4a894

Browse files
authored
chore(Alert): Allowed for non header elements to be set at the Alert title (#8518)
* chore(Alert): Allowed for non header elements to be set at the Alert title * fix lint issues * update docs wording * fix github's lint warnings * clean up useEffect deps * fix wording
1 parent eb5b294 commit fe4a894

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

packages/react-core/src/components/Alert/Alert.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export interface AlertProps extends Omit<React.HTMLProps<HTMLDivElement>, 'actio
5757
timeoutAnimation?: number;
5858
/** Title of the alert. */
5959
title: React.ReactNode;
60-
/** Sets the heading level to use for the alert title. Default is h4. */
60+
/** @deprecated Sets the heading level to use for the alert title. Default is h4. */
6161
titleHeadingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
62+
/** Sets the element to use as the alert title. Default is h4. */
63+
component?: keyof JSX.IntrinsicElements;
6264
/** Adds accessible text to the alert toggle. */
6365
toggleAriaLabel?: string;
6466
/** Position of the tooltip which is displayed if text is truncated. */
@@ -99,7 +101,8 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
99101
actionClose,
100102
actionLinks,
101103
title,
102-
titleHeadingLevel: TitleHeadingLevel = 'h4',
104+
titleHeadingLevel,
105+
component = 'h4',
103106
children = '',
104107
className = '',
105108
ouiaId,
@@ -126,6 +129,14 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
126129
);
127130

128131
const titleRef = React.useRef(null);
132+
const TitleComponent = (titleHeadingLevel || component) as any;
133+
if (titleHeadingLevel !== undefined) {
134+
// eslint-disable-next-line no-console
135+
console.warn(
136+
'Alert: titleHeadingLevel is deprecated, please use the newer component prop instead to set the alert title element.'
137+
);
138+
}
139+
129140
const divRef = React.useRef<HTMLDivElement>();
130141
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
131142
React.useEffect(() => {
@@ -145,12 +156,12 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
145156
const [containsFocus, setContainsFocus] = useState<boolean | undefined>();
146157
const dismissed = timedOut && timedOutAnimation && !isMouseOver && !containsFocus;
147158
React.useEffect(() => {
148-
timeout = timeout === true ? 8000 : Number(timeout);
149-
if (timeout > 0) {
150-
const timer = setTimeout(() => setTimedOut(true), timeout);
159+
const calculatedTimeout = timeout === true ? 8000 : Number(timeout);
160+
if (calculatedTimeout > 0) {
161+
const timer = setTimeout(() => setTimedOut(true), calculatedTimeout);
151162
return () => clearTimeout(timer);
152163
}
153-
}, []);
164+
}, [timeout]);
154165
React.useEffect(() => {
155166
const onDocumentFocus = () => {
156167
if (divRef.current) {
@@ -172,10 +183,10 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
172183
const timer = setTimeout(() => setTimedOutAnimation(true), timeoutAnimation);
173184
return () => clearTimeout(timer);
174185
}
175-
}, [containsFocus, isMouseOver]);
186+
}, [containsFocus, isMouseOver, timeoutAnimation]);
176187
React.useEffect(() => {
177188
dismissed && onTimeout();
178-
}, [dismissed]);
189+
}, [dismissed, onTimeout]);
179190

180191
const [isExpanded, setIsExpanded] = useState(false);
181192
const onToggleExpand = () => {
@@ -197,13 +208,13 @@ export const Alert: React.FunctionComponent<AlertProps> = ({
197208
return null;
198209
}
199210
const Title = (
200-
<TitleHeadingLevel
211+
<TitleComponent
201212
{...(isTooltipVisible && { tabIndex: 0 })}
202213
ref={titleRef}
203214
className={css(styles.alertTitle, truncateTitle && styles.modifiers.truncate)}
204215
>
205216
{getHeadingContent}
206-
</TitleHeadingLevel>
217+
</TitleComponent>
207218
);
208219

209220
return (

packages/react-core/src/components/Alert/examples/Alert.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ PatternFly supports several properties and variations that can be used to add ex
5050

5151
* As demonstrated in the 3rd and 4th variations below, use the `actionClose` property to add an `<AlertActionCloseButton>` component, which can be used to manage and customize alert dismissals. `actionClose` can be used with or without the presence of `actionLinks`.
5252

53-
* As demonstrated in the 5th and 6th variations below, use the `titleHeadingLevel` property to set the heading level of an alert title. Headings should be ordered by their level and heading levels should not be skipped. For example, a heading of an `h2` level should not be followed directly by an `h4`.
54-
53+
* As demonstrated in the 5th and 6th variations below, use the `component` property to set the element for an alert title.
54+
* If the `description` prop is not passed in, then the `component` prop should be set to a non-heading element such as a `span` or `div`.
55+
* If the `description` prop is passed in, then the `component` prop should be a heading element. Headings should be ordered by their level and heading levels should not be skipped. For example, a heading of an `h2` level should not be followed directly by an `h4`.
5556
```ts
5657
import React from 'react';
5758
import { Alert, AlertActionCloseButton, AlertActionLink } from '@patternfly/react-core';
@@ -88,8 +89,10 @@ import { Alert, AlertActionCloseButton, AlertActionLink } from '@patternfly/reac
8889
}
8990
/>
9091
<Alert variant="success" title="Success alert title" actionClose={<AlertActionCloseButton onClose={() => alert('Clicked the close button')} />} />
91-
<Alert variant="success" title="h1 Success alert title" titleHeadingLevel="h1" />
92-
<Alert variant="success" title="h6 Success alert title" titleHeadingLevel="h6" />
92+
<Alert variant="success" title="div success alert title" component="div" />
93+
<Alert variant="success" title="h6 Success alert title" component="h6" >
94+
<p>Short alert description</p>
95+
</Alert>
9396
</React.Fragment>
9497
```
9598
### Alert timeout

0 commit comments

Comments
 (0)