Skip to content

Commit b56ed40

Browse files
authored
[♻️]: Refactor to cleanup unnecessary errors (#98)
* Refactor to move the aria-describedby attribute attachment logic to tooltip * Refactor to remove unnecessary error
1 parent d424c4a commit b56ed40

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

lib/Popper/Popper.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
componentWithForwardedRef,
1111
useDeterministicId,
1212
useForkedRefs,
13-
useIsomorphicLayoutEffect,
1413
useIsomorphicValue,
1514
} from "../utils";
1615
import * as Slots from "./slots";
@@ -227,14 +226,6 @@ const PopperBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
227226
[open],
228227
);
229228

230-
useIsomorphicLayoutEffect(() => {
231-
const anchor = resolveAnchor();
232-
233-
if (id && anchor instanceof HTMLElement) {
234-
anchor.setAttribute("aria-describedby", id);
235-
}
236-
});
237-
238229
const renderProps: RenderProps = {
239230
placement,
240231
open,

lib/TabGroup/TabGroup.tsx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from "react";
2-
import { SystemError } from "../internals";
32
import type { MergeElementProps } from "../types";
43
import {
54
componentWithForwardedRef,
@@ -86,29 +85,6 @@ const TabGroupBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
8685
onActiveTabChange?.(tabValue);
8786
};
8887

89-
React.useEffect(() => {
90-
if (!rootRef.current) return;
91-
92-
const tabs = Array.from(
93-
rootRef.current.querySelectorAll<HTMLButtonElement>('[role="tab"]'),
94-
);
95-
96-
const activeTabElement = tabs.find(
97-
tab => tab.getAttribute("data-entity") === activeTab,
98-
);
99-
100-
if (!activeTabElement) return;
101-
102-
const isActiveTabDisabled =
103-
activeTabElement.hasAttribute("disabled") ||
104-
activeTabElement.getAttribute("aria-disabled") === "true";
105-
106-
if (isActiveTabDisabled) {
107-
throw new SystemError("The selected tab is `disabled`.", "TabGroup.Root");
108-
}
109-
// eslint-disable-next-line react-hooks/exhaustive-deps
110-
}, []);
111-
11288
React.useEffect(() => {
11389
if (!rootRef.current) return;
11490

lib/Tooltip/Tooltip.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ type OwnProps = {
5555
*/
5656
autoPlacement?: PopperProps["autoPlacement"];
5757
/**
58-
* The tooltip will be triggered by this event.\
59-
* **Note**: choosing `"follow-mouse"` will disable `autoPlacement` property.
58+
* The tooltip will be triggered by this event.
6059
*
6160
* @default "open-on-click"
6261
*/
@@ -137,6 +136,32 @@ const TooltipBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
137136
onOpenChange?.(newOpenState);
138137
};
139138

139+
React.useEffect(() => {
140+
if (!open && !keepMounted) return;
141+
142+
const anchor = resolveAnchor();
143+
144+
if (!anchor || !(anchor instanceof HTMLElement)) return;
145+
146+
let describedBy = anchor.getAttribute("aria-describedby");
147+
148+
if (describedBy) {
149+
if (describedBy.includes(id)) return;
150+
151+
describedBy += ` ${id}`;
152+
} else describedBy = id;
153+
154+
anchor.setAttribute("aria-describedby", describedBy);
155+
156+
return () => {
157+
anchor.setAttribute(
158+
"aria-describedby",
159+
describedBy.replace(id, "").trim(),
160+
);
161+
};
162+
// eslint-disable-next-line react-hooks/exhaustive-deps
163+
}, [id, open, keepMounted]);
164+
140165
if (typeof document !== "undefined") {
141166
/* eslint-disable react-hooks/rules-of-hooks */
142167
const anchor = resolveAnchor();
@@ -165,8 +190,7 @@ const TooltipBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
165190
emitOpenChange(true);
166191
}),
167192
},
168-
isHTMLElement(anchor) &&
169-
["open-on-hover", "follow-mouse"].includes(behavior),
193+
isHTMLElement(anchor) && behavior === "open-on-hover",
170194
);
171195

172196
useEventListener(
@@ -177,8 +201,7 @@ const TooltipBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
177201
emitOpenChange(false);
178202
}),
179203
},
180-
isHTMLElement(anchor) &&
181-
["open-on-hover", "follow-mouse"].includes(behavior),
204+
isHTMLElement(anchor) && behavior === "open-on-hover",
182205
);
183206

184207
useEventListener(

0 commit comments

Comments
 (0)