Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-tooltip-for-buttons
Browse files Browse the repository at this point in the history
Co-authored-by: Rafael Toledo <87545086+Toledodev@users.noreply.github.com>
Co-authored-by: Nitesh Singh <nitesh.singh@gitstart.dev>
Co-authored-by: Rubens Rafael <70234898+RubensRafael@users.noreply.github.com>
  • Loading branch information
4 people committed Jun 12, 2023
1 parent 1f51771 commit 7c19d9e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/components/input-elements/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
import React from "react";
import { tremorTwMerge } from "lib";
import { Transition } from "react-transition-group";
import Tooltip, { useTooltip } from "components/util-elements/Tooltip/Tooltip";

import {
BaseColors,

Check warning on line 8 in src/components/input-elements/Button/Button.tsx

View workflow job for this annotation

GitHub Actions / Build dist

'BaseColors' is defined but never used
HorizontalPositions,
Sizes,
border,
borderRadius,

Check warning on line 12 in src/components/input-elements/Button/Button.tsx

View workflow job for this annotation

GitHub Actions / Build dist

'borderRadius' is defined but never used
boxShadow,

Check warning on line 13 in src/components/input-elements/Button/Button.tsx

View workflow job for this annotation

GitHub Actions / Build dist

'boxShadow' is defined but never used
fontWeight,

Check warning on line 14 in src/components/input-elements/Button/Button.tsx

View workflow job for this annotation

GitHub Actions / Build dist

'fontWeight' is defined but never used
makeClassName,
mergeRefs,
sizing,
spacing,
} from "lib";

import { HorizontalPositions, Sizes, border, makeClassName, sizing, spacing } from "lib";
import { Color, HorizontalPosition, ButtonVariant, Size } from "../../../lib";
import { getButtonColors, getButtonProportions, iconSizes } from "./styles";
import { LoadingSpinner } from "assets";
Expand Down Expand Up @@ -66,6 +80,7 @@ export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElemen
disabled?: boolean;
loading?: boolean;
loadingText?: string;
tooltip?: string;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
Expand All @@ -80,6 +95,7 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) =>
loadingText,
children,
className,
tooltip,
...other
} = props;

Expand All @@ -104,12 +120,14 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) =>
: "";
const buttonColorStyles = getButtonColors(variant, color);
const buttonProportionStyles = getButtonProportions(variant)[size];
const delay = 300;
const { tooltipProps, getReferenceProps } = useTooltip(delay);

return (
<Transition in={loading} timeout={50}>
{(state) => (
<button
ref={ref}
ref={mergeRefs([ref, tooltipProps.refs.setReference])}
className={tremorTwMerge(
makeButtonClassName("root"),
// common
Expand All @@ -132,8 +150,10 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) =>
className,
)}
disabled={isDisabled}
{...getReferenceProps}
{...other}
>
<Tooltip text={tooltip} {...tooltipProps} />
{showButtonIconOrSpinner && iconPosition !== HorizontalPositions.Right ? (
<ButtonIconOrSpinner
loading={loading}
Expand Down
17 changes: 15 additions & 2 deletions src/components/util-elements/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ import {
import { tremorTwMerge } from "lib";
import { spacing } from "lib";

export const useTooltip = () => {
export const useTooltip = (delay?: number) => {
const [open, setOpen] = useState(false);
const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout>();

const handleOpenChange = (isOpen: boolean) => {
if (isOpen && delay) {
const timer = setTimeout(() => {
setOpen(isOpen);
}, delay);
setTimeoutId(timer);
return;
}
clearTimeout(timeoutId);
setOpen(isOpen);
};

const { x, y, refs, strategy, context } = useFloating({
open,
onOpenChange: setOpen,
onOpenChange: handleOpenChange,
placement: "top",
whileElementsMounted: autoUpdate,
middleware: [
Expand Down
3 changes: 3 additions & 0 deletions src/stories/input-elements/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,13 @@ const LoadingStateTemplate: ComponentStory<typeof Button> = (args) => {
);
};

const tooltip = "Tooltip";

export const Sizes = SizesTemplate.bind({});
Sizes.args = {
onClick: () => alert(2),
className: "max-w-fit",
tooltip,
};

export const Colors = ColorsTemplate.bind({});
Expand Down

0 comments on commit 7c19d9e

Please sign in to comment.