Skip to content

Commit

Permalink
feat: add arrows towards the trigger and add isCompact prop for a sma… (
Browse files Browse the repository at this point in the history
#2134)

feat: add compact version and fix arrow color
  • Loading branch information
r-mulder committed May 16, 2024
1 parent 3ff0152 commit 9d7545a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-comics-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/react-ui": patch
---

Add indication arrow and add an compact version of the tooltip
29 changes: 19 additions & 10 deletions packages/libs/react-ui/src/components/Tooltip/Tooltip.css.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { style, styleVariants } from '@vanilla-extract/css';
import { atoms } from '../../styles/atoms.css';
import { tokens } from '../../styles/index';
import { token } from '../../styles/index';

export const base = style([
atoms({
position: 'absolute',
fontSize: 'sm',
paddingBlock: 'sm',
paddingInline: 'md',
borderRadius: 'md',
borderRadius: 'xs',
color: 'text.base.inverse.default',
pointerEvents: 'none',
backgroundColor: 'base.inverse.default',
Expand All @@ -21,18 +19,18 @@ export const base = style([
position: 'absolute',
borderTop: '6px solid transparent',
borderRight: '6px solid transparent',
borderBottom: `6px solid ${tokens.kda.foundation.color.background.layer.default}`,
borderBottom: `6px solid ${token('color.background.base.inverse.default')}`,
borderLeft: '6px solid transparent',
},
zIndex: tokens.kda.foundation.zIndex.overlay,
zIndex: token('zIndex.overlay'),
},
]);

export const tooltipPositionVariants = styleVariants({
bottom: [
base,
{
marginTop: tokens.kda.foundation.spacing.md,
marginTop: token('spacing.md'),
top: '100%',
left: '50%',
transform: 'translateX(-50%)',
Expand All @@ -46,7 +44,7 @@ export const tooltipPositionVariants = styleVariants({
top: [
base,
{
marginBottom: tokens.kda.foundation.spacing.md,
marginBottom: token('spacing.md'),
bottom: '100%',
left: '50%',
transform: 'translateX(-50%)',
Expand All @@ -60,7 +58,7 @@ export const tooltipPositionVariants = styleVariants({
right: [
base,
{
marginLeft: tokens.kda.foundation.spacing.md,
marginLeft: token('spacing.md'),
left: '100%',
top: '50%',
transform: 'translateY(-50%)',
Expand All @@ -74,7 +72,7 @@ export const tooltipPositionVariants = styleVariants({
left: [
base,
{
marginRight: tokens.kda.foundation.spacing.md,
marginRight: token('spacing.md'),
right: '100%',
top: '50%',
transform: 'translateY(-50%)',
Expand All @@ -86,3 +84,14 @@ export const tooltipPositionVariants = styleVariants({
},
],
});

export const tooltipSizes = styleVariants({
default: {
paddingInline: token('spacing.n4'),
paddingBlock: token('spacing.n3'),
},
compact: {
paddingInline: token('spacing.n3'),
paddingBlock: token('spacing.n1'),
},
});
106 changes: 96 additions & 10 deletions packages/libs/react-ui/src/components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const meta: Meta<ITooltipProps> = {
decorators: [onLayer1],
parameters: {
status: {
type: ['releaseCandidate'],
type: ['stable'],
},
docs: {
description: {
Expand All @@ -34,6 +34,10 @@ const meta: Meta<ITooltipProps> = {
position: {
description:
'The position of the tooltip relative to the element that triggers it.',
control: {
type: 'select',
},
options: ['top', 'right', 'bottom', 'left'],
table: {
defaultValue: { summary: 'right' },
},
Expand All @@ -60,6 +64,18 @@ const meta: Meta<ITooltipProps> = {
},
isOpen: {
description: 'Allows the user to control the open state of the tooltip.',
control: {
type: 'boolean',
},
table: {
defaultValue: { summary: false },
},
},
isCompact: {
description: 'Shows a more compact version of the tooltip',
control: {
type: 'boolean',
},
table: {
defaultValue: { summary: false },
},
Expand All @@ -84,16 +100,11 @@ export const Dynamic: Story = {
isDisabled: false,
delay: 500,
closeDelay: 300,
defaultOpen: true,
},
render: ({ content, position, isDisabled, delay, closeDelay }) => {
render: (props) => {
return (
<Tooltip
content={content}
position={position}
isDisabled={isDisabled}
delay={delay}
closeDelay={closeDelay}
>
<Tooltip {...props}>
<Button>Trigger</Button>
</Tooltip>
);
Expand Down Expand Up @@ -128,7 +139,7 @@ export const TooltipReactNode: Story = {
},
};

export const DefaultOpen: Story = {
export const DefaultOpenRight: Story = {
name: 'Tooltip that is set to defaultOpen',
args: {
content: "I'm a tooltip, look at me!",
Expand All @@ -153,6 +164,81 @@ export const DefaultOpen: Story = {
},
};

export const DefaultOpenLeft: Story = {
name: 'Tooltip that is set to defaultOpen',
args: {
content: "I'm a tooltip, look at me!",
position: 'left',
isDisabled: false,
delay: 500,
closeDelay: 300,
},
render: ({ content, position, isDisabled, delay, closeDelay }) => {
return (
<Tooltip
content={content}
position={position}
isDisabled={isDisabled}
delay={delay}
closeDelay={closeDelay}
defaultOpen={true}
>
<Button>Trigger</Button>
</Tooltip>
);
},
};

export const DefaultOpenTop: Story = {
name: 'Tooltip that is set to defaultOpen',
args: {
content: "I'm a tooltip, look at me!",
position: 'top',
isDisabled: false,
delay: 500,
closeDelay: 300,
},
render: ({ content, position, isDisabled, delay, closeDelay }) => {
return (
<Tooltip
content={content}
position={position}
isDisabled={isDisabled}
delay={delay}
closeDelay={closeDelay}
defaultOpen={true}
>
<Button>Trigger</Button>
</Tooltip>
);
},
};

export const DefaultOpenBottom: Story = {
name: 'Tooltip that is set to defaultOpen',
args: {
content: "I'm a tooltip, look at me!",
position: 'bottom',
isDisabled: false,
delay: 500,
closeDelay: 300,
},
render: ({ content, position, isDisabled, delay, closeDelay }) => {
return (
<Tooltip
content={content}
position={position}
isDisabled={isDisabled}
delay={delay}
closeDelay={closeDelay}
defaultOpen={true}
>
<Button>Trigger</Button>
</Tooltip>
);
},
};

export const Controlled: Story = {
name: 'Tooltip that is controlled by a button',
args: {
Expand Down
13 changes: 11 additions & 2 deletions packages/libs/react-ui/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { FocusableProvider } from '@react-aria/focus';
import classNames from 'classnames';
import type { FC, ReactElement, ReactNode } from 'react';
import React, { cloneElement, useRef } from 'react';
import { useTooltip, useTooltipTrigger } from 'react-aria';
import type { TooltipTriggerProps } from 'react-stately';
import { useTooltipTriggerState } from 'react-stately';
import { Box } from '../Layout';
import { tooltipPositionVariants } from './Tooltip.css';
import { tooltipPositionVariants, tooltipSizes } from './Tooltip.css';
export interface ITooltipProps
extends Omit<TooltipTriggerProps, 'trigger' | 'onOpenChange'> {
children: ReactElement;
content: ReactNode;
isCompact?: boolean;
position?: keyof typeof tooltipPositionVariants;
}

export const Tooltip: FC<ITooltipProps> = ({
children,
content,
position = 'right',
isCompact = false,
...props
}) => {
const config = {
Expand Down Expand Up @@ -44,7 +47,13 @@ export const Tooltip: FC<ITooltipProps> = ({
</FocusableProvider>

{state.isOpen && (
<span className={tooltipPositionVariants[position]} {...tooltipProps}>
<span
className={classNames(
tooltipPositionVariants[position],
tooltipSizes[isCompact ? 'compact' : 'default'],
)}
{...tooltipProps}
>
{content}
</span>
)}
Expand Down

0 comments on commit 9d7545a

Please sign in to comment.