Skip to content

Commit 3e2c01a

Browse files
committed
fix: Update examples pages
1 parent bf310ca commit 3e2c01a

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

pages/tooltip/component-usage.page.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ function DisabledActionsExample() {
6767
onMouseLeave={() => setShowDelete(false)}
6868
style={{ display: 'inline-block' }}
6969
>
70-
<Button disabled={true} iconName="remove">
70+
<Button
71+
aria-disabled="true"
72+
iconName="remove"
73+
onClick={e => e.preventDefault()}
74+
nativeButtonAttributes={{
75+
onFocus: () => setShowDelete(true),
76+
onBlur: () => setShowDelete(false),
77+
}}
78+
>
7179
Delete
7280
</Button>
7381
{showDelete && (
@@ -87,7 +95,16 @@ function DisabledActionsExample() {
8795
onMouseLeave={() => setShowSave(false)}
8896
style={{ display: 'inline-block' }}
8997
>
90-
<Button disabled={true} variant="primary" iconName="upload">
98+
<Button
99+
aria-disabled="true"
100+
variant="primary"
101+
iconName="upload"
102+
onClick={e => e.preventDefault()}
103+
nativeButtonAttributes={{
104+
onFocus: () => setShowSave(true),
105+
onBlur: () => setShowSave(false),
106+
}}
107+
>
91108
Save
92109
</Button>
93110
{showSave && (
@@ -107,7 +124,15 @@ function DisabledActionsExample() {
107124
onMouseLeave={() => setShowDownload(false)}
108125
style={{ display: 'inline-block' }}
109126
>
110-
<Button disabled={true} iconName="download">
127+
<Button
128+
aria-disabled="true"
129+
iconName="download"
130+
onClick={e => e.preventDefault()}
131+
nativeButtonAttributes={{
132+
onFocus: () => setShowDownload(true),
133+
onBlur: () => setShowDownload(false),
134+
}}
135+
>
111136
Download Report
112137
</Button>
113138
{showDownload && (

pages/tooltip/simple.page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ export default function TooltipSimple() {
296296
ref={disabledRef}
297297
onMouseEnter={() => setShowDisabled(true)}
298298
onMouseLeave={() => setShowDisabled(false)}
299+
onFocus={() => setShowDisabled(true)}
300+
onBlur={() => setShowDisabled(false)}
301+
tabIndex={0}
299302
style={{ display: 'inline-block' }}
300303
>
301304
<Button disabled={true} iconName="upload">
@@ -389,6 +392,9 @@ export default function TooltipSimple() {
389392
ref={codeRef}
390393
onMouseEnter={() => setShowCode(true)}
391394
onMouseLeave={() => setShowCode(false)}
395+
onFocus={() => setShowCode(true)}
396+
onBlur={() => setShowCode(false)}
397+
tabIndex={0}
392398
style={{ display: 'inline-block' }}
393399
>
394400
<span

src/internal/components/tooltip/index.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export interface TooltipProps {
2323
onDismiss?: () => void;
2424
}
2525

26+
// Generate unique ID for older React versions
27+
let tooltipIdCounter = 0;
28+
const generateTooltipId = () => `internal-tooltip-${++tooltipIdCounter}`;
29+
2630
export default function Tooltip({
2731
value,
2832
trackRef,
@@ -33,6 +37,23 @@ export default function Tooltip({
3337
hideOnOverscroll,
3438
onDismiss,
3539
}: TooltipProps) {
40+
const tooltipId = React.useMemo(() => generateTooltipId(), []);
41+
42+
// Add aria-describedby to the tracked element for accessibility
43+
React.useEffect(() => {
44+
const element = trackRef.current;
45+
if (element) {
46+
element.setAttribute('aria-describedby', tooltipId);
47+
}
48+
49+
return () => {
50+
// Clean up aria-describedby when tooltip unmounts
51+
if (element) {
52+
element.removeAttribute('aria-describedby');
53+
}
54+
};
55+
}, [tooltipId, trackRef]);
56+
3657
if (!trackKey && (typeof value === 'string' || typeof value === 'number')) {
3758
trackKey = value;
3859
}
@@ -63,7 +84,7 @@ export default function Tooltip({
6384

6485
return (
6586
<Portal>
66-
<div className={styles.root} data-testid={trackKey}>
87+
<div className={styles.root} data-testid={trackKey} id={tooltipId} role="tooltip">
6788
<Transition in={true}>
6889
{() => (
6990
<PopoverContainer

src/tooltip/internal.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import styles from './styles.css.js';
1818

1919
type InternalTooltipComponentProps = InternalTooltipProps & InternalBaseComponentProps;
2020

21+
// Generate unique ID for older React versions
22+
let tooltipIdCounter = 0;
23+
const generateTooltipId = () => `tooltip-${++tooltipIdCounter}`;
24+
2125
export default function InternalTooltip({
2226
content,
2327
getTrack,
@@ -30,11 +34,23 @@ export default function InternalTooltip({
3034
}: InternalTooltipComponentProps) {
3135
const baseProps = getBaseProps(restProps);
3236
const trackRef = React.useRef<HTMLElement | SVGElement | null>(null);
37+
const tooltipId = React.useMemo(() => generateTooltipId(), []);
3338

3439
// Update the ref with the current tracked element
3540
React.useEffect(() => {
36-
trackRef.current = getTrack();
37-
});
41+
const element = getTrack();
42+
trackRef.current = element;
43+
// Add aria-describedby to the tracked element for accessibility
44+
if (element) {
45+
element.setAttribute('aria-describedby', tooltipId);
46+
}
47+
return () => {
48+
// Clean up aria-describedby when tooltip unmounts
49+
if (element) {
50+
element.removeAttribute('aria-describedby');
51+
}
52+
};
53+
}, [getTrack, tooltipId]);
3854

3955
if (!trackKey && (typeof content === 'string' || typeof content === 'number')) {
4056
trackKey = content;
@@ -75,6 +91,8 @@ export default function InternalTooltip({
7591
className={clsx(styles.root, baseProps.className)}
7692
data-testid={trackKey}
7793
ref={__internalRootRef}
94+
id={tooltipId}
95+
role="tooltip"
7896
>
7997
<Transition in={true}>
8098
{() => (

0 commit comments

Comments
 (0)