Skip to content

Commit

Permalink
Fix tooltip in button looking weird in groups (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtemkin1 authored Dec 7, 2024
1 parent 0bde927 commit acf26ba
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/components/ClassTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import React, { useEffect, useMemo, useRef, useState } from "react";
import { LuPlus, LuMinus, LuSearch } from "react-icons/lu";

import { InputGroup } from "./ui/input-group";
import { Tooltip } from "./ui/tooltip";
import { Button } from "./ui/button";
import { Button, LabelledButton } from "./ui/button";
import { useColorMode } from "./ui/color-mode";

import { Class, DARK_IMAGES, Flags, getFlagImg } from "../lib/class";
Expand Down Expand Up @@ -222,34 +221,33 @@ function ClassFlags(props: {
<Group attached colorPalette="orange" wrap="wrap">
{group.map(([flag, label, image]) => {
const checked = flags.get(flag);
const content = (
return image ? (
<LabelledButton
key={flag}
onClick={() => onChange(flag, !checked)}
title={label}
variant={checked ? "solid" : "outline"}
portalled
>
<Image
src={image}
alt={label}
filter={
colorMode === "dark" && DARK_IMAGES.includes(flag ?? "")
? "invert()"
: ""
}
/>
</LabelledButton>
) : (
<Button
key={flag}
onClick={() => onChange(flag, !checked)}
variant={checked ? "solid" : "outline"}
>
{image ? (
<Image
src={image}
alt={label}
filter={
colorMode === "dark" && DARK_IMAGES.includes(flag ?? "")
? "invert()"
: ""
}
/>
) : (
label
)}
{label}
</Button>
);
return image ? (
<Tooltip content={label} key={flag} portalled>
{content}
</Tooltip>
) : (
content
);
})}
</Group>
);
Expand Down
26 changes: 26 additions & 0 deletions src/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
Button as ChakraButton,
Span,
Spinner,
Tooltip as ChakraTooltip,
} from "@chakra-ui/react";
import * as React from "react";
import { Tooltip } from "./tooltip";

interface ButtonLoadingProps {
loading?: boolean;
Expand Down Expand Up @@ -45,3 +47,27 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
);
},
);

export interface LabelledButtonProps extends ButtonProps {
showArrow?: boolean;
portalled?: boolean;
portalRef?: React.RefObject<HTMLElement>;
titleProps?: ChakraTooltip.ContentProps;
disabled?: boolean;
}

export const LabelledButton = (props: LabelledButtonProps) => {
const { showArrow, title, titleProps, portalled, disabled, ...rest } = props;
if (!title) return <Button {...rest} />;
return (
<Tooltip
content={title}
contentProps={titleProps}
portalled={portalled}
showArrow={showArrow}
disabled={disabled}
>
<Button {...rest} />
</Tooltip>
);
};

0 comments on commit acf26ba

Please sign in to comment.