Skip to content

Commit

Permalink
feat(components): add new tooltip component
Browse files Browse the repository at this point in the history
  • Loading branch information
ruru-m07 committed Jul 27, 2024
1 parent 6d31382 commit 3dee192
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 0 deletions.
22 changes: 22 additions & 0 deletions apps/www/components/preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { Button } from "ruru-ui/components/button";
import { Spinner } from "ruru-ui/components/spinner";
import { Avatar } from "ruru-ui/components/avatar";
import { Checkbox } from "ruru-ui/components/checkbox";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "ruru-ui/components/tooltip";

import { BadgePreview } from "../badgePreview";

Expand Down Expand Up @@ -58,4 +64,20 @@ export default {
</div>
</Wrapper>
),
tooltip: (
<Wrapper>
<div className="flex items-center justify-center gap-4">
<TooltipProvider>
<Tooltip>
<TooltipTrigger className="underline">@ruru</TooltipTrigger>
<TooltipContent
style={{ padding: "0px", paddingRight: "10px", paddingLeft: "10px", height: "35px", display: "flex", justifyContent: "center", alignItems: "center" }}
>
<p> author of ruru-UI </p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
</Wrapper>
),
} as Record<string, ReactNode>;
92 changes: 92 additions & 0 deletions apps/www/content/docs/components/tooltip.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: Tooltip
description: This set of components provides a tooltip implementation using the `@radix-ui/react-tooltip package`. It includes components for the tooltip provider, root, trigger, and content.
preview: tooltip
---

import { Tab, Tabs } from "fumadocs-ui/components/tabs";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "ruru-ui/components/tooltip";
import { Button } from "ruru-ui/components/button";

## Usage

---

<Tabs items={["Preview", "Code"]}>
<Tab className={"flex justify-center"} value="Preview" >
<TooltipProvider>
<Tooltip>
<TooltipTrigger
asChild>
<Button>Hover or focus me</Button>
</TooltipTrigger>
<TooltipContent
style={{ padding: "0px", paddingRight: "10px", paddingLeft: "10px", height: "35px", display: "flex", justifyContent: "center", alignItems: "center" }}
>
Tooltip content here
</TooltipContent>
</Tooltip>
</TooltipProvider>
</Tab>
<Tab className={"-mt-8"} value="Code">
```tsx
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "ruru-ui/components/tooltip";
import { Button } from "ruru-ui/components/button";

export function TooltipDemo() {
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Button>Hover or focus me</Button>
</TooltipTrigger>
<TooltipContent className="px-[15px] py-[px]">
Tooltip content here
</TooltipContent>
</Tooltip>
</TooltipProvider>
)
}
```
</Tab>

</Tabs>

## Components

### TooltipProvider

The `TooltipProvider` component is a context provider that wraps the tooltip components. It is required to be used in order to provide the context to the tooltip components.

### Tooltip

The `Tooltip` component is a container component that wraps the `TooltipTrigger` and `TooltipContent` components. It is used to group the trigger and content components together.

### TooltipTrigger

The `TooltipTrigger` component is a component that triggers the tooltip to be shown. It can be used as a child component or as a trigger for another component.

### TooltipContent

The `TooltipContent` component is a component that contains the content of the tooltip. It is shown when the trigger component is hovered or focused.

## props

---

| Prop name | Type | Default value | Description |
| ---------- | --------------- | ------------- | ------------------------------------------------ |
| className | string | | Additional classes to style the tooltip content. |
| sideOffset | number | 8 | Offset from the trigger element. |
| ref | React.Ref | | Forwarded ref to the content element. |
| children | React.ReactNode | | Children to be rendered inside the tooltip. |
1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-direction": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.2",
"@swc/core": "^1.6.13",
"@tailwindcss/typography": "^0.5.13",
"class-variance-authority": "^0.7.0",
Expand Down
89 changes: 89 additions & 0 deletions packages/ui/src/components/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"use client";

import * as React from "react";
import * as TooltipPrimitive from "@radix-ui/react-tooltip";

import { cn } from "@/utils/cn";

/**
* TooltipProvider component.
* Provides context for all tooltip components.
*
* @component
* @example
* <TooltipProvider>
* <Tooltip>
* <TooltipTrigger asChild>
* <button>Hover or focus me</button>
* </TooltipTrigger>
* <TooltipContent>
* Tooltip content here
* </TooltipContent>
* </Tooltip>
* </TooltipProvider>
*/
const TooltipProvider = TooltipPrimitive.Provider;

/**
* Tooltip component.
* The root component for the tooltip.
*
* @component
* @example
* <Tooltip>
* <TooltipTrigger asChild>
* <button>Hover or focus me</button>
* </TooltipTrigger>
* <TooltipContent>
* Tooltip content here
* </TooltipContent>
* </Tooltip>
*/
const Tooltip = TooltipPrimitive.Root;

/**
* TooltipTrigger component.
* The element that triggers the display of the tooltip.
*
* @component
* @example
* <TooltipTrigger asChild>
* <button>Hover or focus me</button>
* </TooltipTrigger>
*/
const TooltipTrigger = TooltipPrimitive.Trigger;

/**
* TooltipContent component.
* The content of the tooltip that appears when the trigger is activated.
*
* @component
* @param {string} className - Additional classes to style the tooltip content.
* @param {number} sideOffset - Offset from the trigger element.
* @param {React.Ref} ref - Forwarded ref to the content element.
* @param {React.ReactNode} props.children - Children to be rendered inside the tooltip.
* @example
* <TooltipContent>
* Tooltip content here
* </TooltipContent>
*/
const TooltipContent = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
>(({ className, sideOffset = 5, ...props }, ref) => (
<TooltipPrimitive.Content
ref={ref}
className={cn(
"h-fit data-[state=delayed-open]:data-[side=top]:animate-slideDownAndFade data-[state=delayed-open]:data-[side=right]:animate-slideLeftAndFade data-[state=delayed-open]:data-[side=left]:animate-slideRightAndFade data-[state=delayed-open]:data-[side=bottom]:animate-slideUpAndFade text-primary-foreground select-none rounded-[4px] bg-primary px-[15px] py-[10px] text-[15px] leading-none shadow-[hsl(206_22%_7%_/_35%)_0px_10px_38px_-10px,_hsl(206_22%_7%_/_20%)_0px_10px_20px_-15px] will-change-[transform,opacity]",
className
)}
sideOffset={sideOffset}
{...props}
>
{props.children}
<TooltipPrimitive.Arrow className="fill-primary" />
</TooltipPrimitive.Content>
));
TooltipContent.displayName = TooltipPrimitive.Content.displayName;

export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider };
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3dee192

Please sign in to comment.