Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: popup: add more props to style and global css vars #592

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/Popup/Popup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export const Popups = Popup_Story.bind({});
Popups.args = {
offset: 8,
theme: PopupTheme.light,
animate: true,
bordered: false,
dropShadow: true,
content: (
<>
<Stack
Expand Down
15 changes: 14 additions & 1 deletion src/components/Popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC } from 'react';
import { PopupProps, PopupRef, PopupSize, PopupTheme } from './Popup.types';
import { Tooltip, TooltipSize, TooltipType } from '../Tooltip';
import { Tooltip, TooltipSize, TooltipTheme, TooltipType } from '../Tooltip';
import { useCanvasDirection } from '../../hooks/useCanvasDirection';
import { mergeClasses, uniqueId } from '../../shared/utilities';

Expand All @@ -9,16 +9,20 @@ import styles from './popup.module.scss';
export const Popup: FC<PopupProps> = React.forwardRef<PopupRef, PopupProps>(
(
{
animate = true,
bordered = false,
classNames,
closeOnPopupClick = false,
closeOnReferenceClick = true,
dropShadow = true,
id,
popupOnKeydown,
referenceOnClick,
referenceOnKeydown,
showPopup,
size = PopupSize.Medium,
tabIndex = 0,
theme = PopupTheme.light,
trigger = 'click',
popupStyle,
...rest
Expand All @@ -42,19 +46,28 @@ export const Popup: FC<PopupProps> = React.forwardRef<PopupRef, PopupProps>(
[PopupSize.Small, TooltipSize.Small],
]);

const popupThemeToTooltipThemeMap = new Map<PopupTheme, TooltipTheme>([
[PopupTheme.dark, TooltipTheme.dark],
[PopupTheme.light, TooltipTheme.light],
]);

return (
<Tooltip
{...rest}
animate={animate}
bordered={bordered}
classNames={popupClassNames}
closeOnTooltipClick={closeOnPopupClick}
closeOnReferenceClick={closeOnReferenceClick}
dropShadow={dropShadow}
id={popupId}
ref={ref}
referenceOnClick={referenceOnClick}
referenceOnKeydown={referenceOnKeydown}
showTooltip={showPopup}
size={popupSizeToTooltipSizeMap.get(size)}
tabIndex={tabIndex}
theme={popupThemeToTooltipThemeMap.get(theme)}
tooltipOnKeydown={popupOnKeydown}
tooltipStyle={popupStyle}
trigger={trigger}
Expand Down
6 changes: 6 additions & 0 deletions src/components/Popup/Popup.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface PopupProps
| 'closeOnTooltipClick'
| 'showTooltip'
| 'size'
| 'theme'
| 'tooltipOnKeydown'
| 'tooltipStyle'
| 'type'
Expand Down Expand Up @@ -48,6 +49,11 @@ export interface PopupProps
* @default PopupSize.Medium
*/
size?: PopupSize;
/**
* Theme of the Popup.
* @default light
*/
theme?: PopupTheme;
}

export type PopupRef = {
Expand Down
3 changes: 3 additions & 0 deletions src/components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ Tooltips.args = {
placement: 'bottom',
disabled: false,
visibleArrow: true,
animate: true,
bordered: false,
dropShadow: true,
classNames: 'my-tooltip-class',
openDelay: 0,
hideAfter: 200,
Expand Down
43 changes: 43 additions & 0 deletions src/components/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ describe('Tooltip', () => {
);
});

test('Tooltip is bordered with no drop shadow', async () => {
const { container } = render(
<Tooltip
bordered
content={<div data-testid="tooltipBordered">This is a tooltip.</div>}
dropShadow={false}
>
<div className="test-div">test</div>
</Tooltip>
);
fireEvent.mouseOver(container.querySelector('.test-div'));
await waitFor(() => screen.getByTestId('tooltipBordered'));
expect(container.querySelector('.bordered')).toBeTruthy();
expect(container.querySelector('.drop-shadow')).toBeFalsy();
});

test('Tooltip is animated by default', async () => {
const { container } = render(
<Tooltip
content={<div data-testid="tooltipAnimated">This is a tooltip.</div>}
>
<div className="test-div">test</div>
</Tooltip>
);
fireEvent.mouseOver(container.querySelector('.test-div'));
await waitFor(() => screen.getByTestId('tooltipAnimated'));
expect(container.querySelector('.animate')).toBeTruthy();
});

test('Tooltip not animated', async () => {
const { container } = render(
<Tooltip
animate={false}
content={<div data-testid="tooltipNotAnimated">This is a tooltip.</div>}
>
<div className="test-div">test</div>
</Tooltip>
);
fireEvent.mouseOver(container.querySelector('.test-div'));
await waitFor(() => screen.getByTestId('tooltipNotAnimated'));
expect(container.querySelector('.animate')).toBeFalsy();
});

test('Tooltip is large', async () => {
const { container } = render(
<Tooltip
Expand Down
6 changes: 6 additions & 0 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ export const Tooltip: FC<TooltipProps> = React.memo(
React.forwardRef<TooltipRef, TooltipProps>(
(
{
animate = true,
bordered = false,
children,
classNames,
closeOnOutsideClick = true,
closeOnReferenceClick = true,
closeOnTooltipClick = false,
content,
disabled,
dropShadow = true,
height,
hideAfter = ANIMATION_DURATION,
id,
Expand Down Expand Up @@ -256,6 +259,9 @@ export const Tooltip: FC<TooltipProps> = React.memo(
styles.tooltip,
{ [styles.popup]: type === TooltipType.Popup },
classNames,
{ [styles.animate]: !!animate },
{ [styles.bordered]: !!bordered },
{ [styles.dropShadow]: !!dropShadow },
{ [styles.visibleArrow]: !!visibleArrow },
{ [styles.visible]: mergedVisible },
{ [styles.hiding]: hiding },
Expand Down
15 changes: 15 additions & 0 deletions src/components/Tooltip/Tooltip.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export enum TooltipType {
}

export interface TooltipProps extends Omit<OcBaseProps<HTMLDivElement>, 'ref'> {
/**
* Should animate the Tooltip transitions.
* @default true
*/
animate?: boolean;
/**
* Whether the Tooltip has a border.
* @default false
*/
bordered?: boolean;
/**
* Should close Tooltip on click outside.
* @default true
Expand All @@ -59,6 +69,11 @@ export interface TooltipProps extends Omit<OcBaseProps<HTMLDivElement>, 'ref'> {
* @default false
*/
disabled?: boolean;
/**
* Whether the Tooltip has a drop shadow.
* @default true
*/
dropShadow?: boolean;
/**
* Timeout in milliseconds to hide the Tooltip.
* @default 0
Expand Down
51 changes: 40 additions & 11 deletions src/components/Tooltip/tooltip.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ $tooltip-arrow-shadow: 0px 1px 2px rgba(15, 20, 31, 0.12);
}

.tooltip {
--bg: var(--background-color);
--text-color: var(--text-primary-color);
--bg: var(--tooltip-background-color-light);
--text-color: var(--tooltip-text-color-light);
background: var(--bg);
border-radius: $border-radius-s;
box-shadow: $shadow-object-s;
border-radius: var(--tooltip-border-radius);
color: var(--text-color);
font-family: $octuple-font-family;
font-size: $text-font-size-1;
Expand All @@ -39,6 +38,14 @@ $tooltip-arrow-shadow: 0px 1px 2px rgba(15, 20, 31, 0.12);
text-align: center;
z-index: $z-index-400;

&.bordered {
border: $tooltip-border-light;
}

&.drop-shadow {
box-shadow: $shadow-object-s;
}

// When Popup, adds a pixel buffer equal to the width of the arrow to prevent
// floating ui dismiss when hovering from reference to floating element.
// Especially helpful when trigger is hover.
Expand All @@ -65,18 +72,34 @@ $tooltip-arrow-shadow: 0px 1px 2px rgba(15, 20, 31, 0.12);
}

&.dark {
--bg: var(--grey-color-80);
--text-color: var(--text-inverse-color);
--bg: var(--tooltip-background-color-dark);
--text-color: var(--tooltip-text-color-dark);

&.bordered {
border: $tooltip-border-dark;
}
}

.arrow {
background: inherit;
box-shadow: $tooltip-arrow-shadow;
height: $space-xs;
width: $space-xs;
z-index: -1;
}

&.bordered {
.arrow {
border-bottom: $tooltip-border-light;
border-right: $tooltip-border-light;
}
}

&.drop-shadow {
.arrow {
box-shadow: $tooltip-arrow-shadow;
}
}

&.bottom {
transform-origin: top center;

Expand Down Expand Up @@ -175,14 +198,20 @@ $tooltip-arrow-shadow: 0px 1px 2px rgba(15, 20, 31, 0.12);

&.visible {
display: block;
animation: scaleTooltipIn $motion-duration-extra-fast
$motion-easing-easeinout 0s forwards;

&.animate {
animation: scaleTooltipIn $motion-duration-extra-fast
$motion-easing-easeinout 0s forwards;
}
}

&.hiding {
animation: scaleTooltipOut $motion-duration-extra-fast
$motion-easing-easeinout 0s forwards;
pointer-events: none;

&.animate {
animation: scaleTooltipOut $motion-duration-extra-fast
$motion-easing-easeinout 0s forwards;
}
}

&.hidden {
Expand Down
11 changes: 11 additions & 0 deletions src/styles/themes/_default-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -750,4 +750,15 @@
--slider-track-error-border-color: var(--red-color-60);
--slider-value-text-color: var(--text-secondary-color);
// ------ Slider theme ------

// ------ Tooltip theme ------
--tooltip-background-color-dark: var(--grey-color-80);
--tooltip-background-color-light: var(--background-color);
--tooltip-border-color-dark: var(--grey-color-40);
--tooltip-border-color-light: var(--grey-color-60);
--tooltip-border-radius: var(--border-radius-s);
--tooltip-border-style: solid;
--tooltip-border-width: 1px;
--tooltip-text-color-dark: var(--text-inverse-color);
--tooltip-text-color-light: var(--text-primary-color);
}
10 changes: 10 additions & 0 deletions src/styles/themes/_definitions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ $picker-input-foreground-color: var(--picker-input-foreground-color);
$picker-input-placeholder-color: var(--picker-input-placeholder-color);
$picker-outline-color: var(--picker-outline-color);

// Tooltip
$tooltip-border-color-dark: var(--tooltip-border-color-dark);
$tooltip-border-color-light: var(--tooltip-border-color-light);
$tooltip-border-width: var(--tooltip-border-width);
$tooltip-border-style: var(--tooltip-border-style);
dkilgore-eightfold marked this conversation as resolved.
Show resolved Hide resolved
$tooltip-border-dark: $tooltip-border-width $tooltip-border-style
$tooltip-border-color-dark;
$tooltip-border-light: $tooltip-border-width $tooltip-border-style
$tooltip-border-color-light;
dkilgore-eightfold marked this conversation as resolved.
Show resolved Hide resolved

// END: VARIABLE COLOR DEFINITIONS

// BEGIN: NON-COLOR DEFINITIONS
Expand Down