Skip to content

Commit acf26ba

Browse files
authored
Fix tooltip in button looking weird in groups (#95)
Noticed UI issue with button inside tooltips on filter groups, fixed based on chakra-ui/chakra-ui#9294 (comment). Before ![image](https://github.com/user-attachments/assets/5d7204ba-ed59-4657-afa0-47da0e4d8bd1) After ![image](https://github.com/user-attachments/assets/5e9065bb-5d08-49c4-a6ea-8524778c4d54)
1 parent 0bde927 commit acf26ba

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/components/ClassTable.tsx

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import React, { useEffect, useMemo, useRef, useState } from "react";
77
import { LuPlus, LuMinus, LuSearch } from "react-icons/lu";
88

99
import { InputGroup } from "./ui/input-group";
10-
import { Tooltip } from "./ui/tooltip";
11-
import { Button } from "./ui/button";
10+
import { Button, LabelledButton } from "./ui/button";
1211
import { useColorMode } from "./ui/color-mode";
1312

1413
import { Class, DARK_IMAGES, Flags, getFlagImg } from "../lib/class";
@@ -222,34 +221,33 @@ function ClassFlags(props: {
222221
<Group attached colorPalette="orange" wrap="wrap">
223222
{group.map(([flag, label, image]) => {
224223
const checked = flags.get(flag);
225-
const content = (
224+
return image ? (
225+
<LabelledButton
226+
key={flag}
227+
onClick={() => onChange(flag, !checked)}
228+
title={label}
229+
variant={checked ? "solid" : "outline"}
230+
portalled
231+
>
232+
<Image
233+
src={image}
234+
alt={label}
235+
filter={
236+
colorMode === "dark" && DARK_IMAGES.includes(flag ?? "")
237+
? "invert()"
238+
: ""
239+
}
240+
/>
241+
</LabelledButton>
242+
) : (
226243
<Button
227244
key={flag}
228245
onClick={() => onChange(flag, !checked)}
229246
variant={checked ? "solid" : "outline"}
230247
>
231-
{image ? (
232-
<Image
233-
src={image}
234-
alt={label}
235-
filter={
236-
colorMode === "dark" && DARK_IMAGES.includes(flag ?? "")
237-
? "invert()"
238-
: ""
239-
}
240-
/>
241-
) : (
242-
label
243-
)}
248+
{label}
244249
</Button>
245250
);
246-
return image ? (
247-
<Tooltip content={label} key={flag} portalled>
248-
{content}
249-
</Tooltip>
250-
) : (
251-
content
252-
);
253251
})}
254252
</Group>
255253
);

src/components/ui/button.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
Button as ChakraButton,
55
Span,
66
Spinner,
7+
Tooltip as ChakraTooltip,
78
} from "@chakra-ui/react";
89
import * as React from "react";
10+
import { Tooltip } from "./tooltip";
911

1012
interface ButtonLoadingProps {
1113
loading?: boolean;
@@ -45,3 +47,27 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
4547
);
4648
},
4749
);
50+
51+
export interface LabelledButtonProps extends ButtonProps {
52+
showArrow?: boolean;
53+
portalled?: boolean;
54+
portalRef?: React.RefObject<HTMLElement>;
55+
titleProps?: ChakraTooltip.ContentProps;
56+
disabled?: boolean;
57+
}
58+
59+
export const LabelledButton = (props: LabelledButtonProps) => {
60+
const { showArrow, title, titleProps, portalled, disabled, ...rest } = props;
61+
if (!title) return <Button {...rest} />;
62+
return (
63+
<Tooltip
64+
content={title}
65+
contentProps={titleProps}
66+
portalled={portalled}
67+
showArrow={showArrow}
68+
disabled={disabled}
69+
>
70+
<Button {...rest} />
71+
</Tooltip>
72+
);
73+
};

0 commit comments

Comments
 (0)