Skip to content

[♻️]: Refactor to cleanup unnecessary errors #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/Popper/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
componentWithForwardedRef,
useDeterministicId,
useForkedRefs,
useIsomorphicLayoutEffect,
useIsomorphicValue,
} from "../utils";
import * as Slots from "./slots";
Expand Down Expand Up @@ -227,14 +226,6 @@ const PopperBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
[open],
);

useIsomorphicLayoutEffect(() => {
const anchor = resolveAnchor();

if (id && anchor instanceof HTMLElement) {
anchor.setAttribute("aria-describedby", id);
}
});

const renderProps: RenderProps = {
placement,
open,
Expand Down
24 changes: 0 additions & 24 deletions lib/TabGroup/TabGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from "react";
import { SystemError } from "../internals";
import type { MergeElementProps } from "../types";
import {
componentWithForwardedRef,
Expand Down Expand Up @@ -86,29 +85,6 @@ const TabGroupBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
onActiveTabChange?.(tabValue);
};

React.useEffect(() => {
if (!rootRef.current) return;

const tabs = Array.from(
rootRef.current.querySelectorAll<HTMLButtonElement>('[role="tab"]'),
);

const activeTabElement = tabs.find(
tab => tab.getAttribute("data-entity") === activeTab,
);

if (!activeTabElement) return;

const isActiveTabDisabled =
activeTabElement.hasAttribute("disabled") ||
activeTabElement.getAttribute("aria-disabled") === "true";

if (isActiveTabDisabled) {
throw new SystemError("The selected tab is `disabled`.", "TabGroup.Root");
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

React.useEffect(() => {
if (!rootRef.current) return;

Expand Down
35 changes: 29 additions & 6 deletions lib/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ type OwnProps = {
*/
autoPlacement?: PopperProps["autoPlacement"];
/**
* The tooltip will be triggered by this event.\
* **Note**: choosing `"follow-mouse"` will disable `autoPlacement` property.
* The tooltip will be triggered by this event.
*
* @default "open-on-click"
*/
Expand Down Expand Up @@ -137,6 +136,32 @@ const TooltipBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
onOpenChange?.(newOpenState);
};

React.useEffect(() => {
if (!open && !keepMounted) return;

const anchor = resolveAnchor();

if (!anchor || !(anchor instanceof HTMLElement)) return;

let describedBy = anchor.getAttribute("aria-describedby");

if (describedBy) {
if (describedBy.includes(id)) return;

describedBy += ` ${id}`;
} else describedBy = id;

anchor.setAttribute("aria-describedby", describedBy);

return () => {
anchor.setAttribute(
"aria-describedby",
describedBy.replace(id, "").trim(),
);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id, open, keepMounted]);

if (typeof document !== "undefined") {
/* eslint-disable react-hooks/rules-of-hooks */
const anchor = resolveAnchor();
Expand Down Expand Up @@ -165,8 +190,7 @@ const TooltipBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
emitOpenChange(true);
}),
},
isHTMLElement(anchor) &&
["open-on-hover", "follow-mouse"].includes(behavior),
isHTMLElement(anchor) && behavior === "open-on-hover",
);

useEventListener(
Expand All @@ -177,8 +201,7 @@ const TooltipBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
emitOpenChange(false);
}),
},
isHTMLElement(anchor) &&
["open-on-hover", "follow-mouse"].includes(behavior),
isHTMLElement(anchor) && behavior === "open-on-hover",
);

useEventListener(
Expand Down