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

fix(tooltip): add boundary padding #1065

Merged
merged 4 commits into from
Mar 9, 2021
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
2 changes: 2 additions & 0 deletions api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,8 @@ export interface TooltipInfo {
// @public
export interface TooltipPortalSettings<B = never> {
boundary?: HTMLElement | B;
// Warning: (ae-forgotten-export) The symbol "Padding" needs to be exported by the entry point index.d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to expose also the Padding type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 8d9f3f6

boundaryPadding?: Partial<Padding_2> | number;
fallbackPlacements?: Placement[];
offset?: number;
placement?: Placement;
Expand Down
20 changes: 18 additions & 2 deletions src/components/portal/tooltip_portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useRef, useEffect, useCallback, ReactNode, useMemo } from 'react';
import { createPortal } from 'react-dom';

import { mergePartial, isDefined } from '../../utils/common';
import { Padding } from '../../utils/dimensions';
import { TooltipPortalSettings, PortalAnchorRef } from './types';
import { DEFAULT_POPPER_SETTINGS, getOrCreateNode, isHTMLElement } from './utils';

Expand Down Expand Up @@ -56,6 +57,20 @@ type PortalTooltipProps = {
chartId: string;
};

function addToPadding(padding?: Partial<Padding> | number, extra: number = 0): Padding | number | undefined {
if (!padding) return undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if padding is 0, is it OK to return undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I don't think this is critical but it should return just the extra value when/if padding is undefined. I'll open a pr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monfera fixed here #1088

if (typeof padding === 'number') return padding + extra;

const { top = 0, right = 0, bottom = 0, left = 0 } = padding;

return {
top: top + extra,
right: right + extra,
bottom: bottom + extra,
left: left + extra,
};
}

const TooltipPortalComponent = ({
anchor,
scope,
Expand Down Expand Up @@ -113,7 +128,7 @@ const TooltipPortalComponent = ({
return;
}

const { fallbackPlacements, placement, boundary, offset } = popperSettings;
const { fallbackPlacements, placement, boundary, offset, boundaryPadding } = popperSettings;
popper.current = createPopper(anchorNode.current, portalNode.current, {
strategy: 'absolute',
placement,
Expand All @@ -128,6 +143,7 @@ const TooltipPortalComponent = ({
name: 'preventOverflow',
options: {
boundary,
padding: boundaryPadding,
},
},
{
Expand All @@ -138,7 +154,7 @@ const TooltipPortalComponent = ({
boundary,
// checks main axis overflow before trying to flip
altAxis: false,
padding: offset || 10,
padding: addToPadding(boundaryPadding, offset || 10),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with that offset || 10 are we saying that we always want a padding of 10 and the user cannot set 0 here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why I had 10 there. Maybe just to flip away 10px from the edge.

Anyways I removed this and only add the offset value. See 8d9f3f6

},
},
],
Expand Down
9 changes: 9 additions & 0 deletions src/components/portal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import { $Values } from 'utility-types';

import { Padding } from '../../utils/dimensions';

/**
* Placement used in positioning tooltip
* @public
Expand Down Expand Up @@ -100,6 +102,13 @@ export interface TooltipPortalSettings<B = never> {
* @defaultValue parent scroll container
*/
boundary?: HTMLElement | B;
/**
* Boundary element padding.
* Used to reduce extents of boundary placement when magins or paddings are used on boundary
*
* @defaultValue 0
*/
boundaryPadding?: Partial<Padding> | number;
/**
* Custom tooltip offset
*/
Expand Down
12 changes: 9 additions & 3 deletions stories/bar/55_tooltip_boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const Example = () => {
const red = useRef<HTMLDivElement | null>(null);
const white = useRef<HTMLDivElement | null>(null);
const blue = useRef<HTMLDivElement | null>(null);
const boundaryMap: Record<string, TooltipProps['boundary'] | null> = {
const getBoundary: Record<string, TooltipProps['boundary'] | null> = {
default: undefined,
red: red.current,
white: white.current,
Expand All @@ -62,14 +62,20 @@ export const Example = () => {
},
'default',
);
const boundary = boundaryMap[boundarySting] ?? undefined;
const boundary = getBoundary[boundarySting] ?? undefined;
const boundaryPadding = {
top: number('Boundary top padding', 0, { min: 0 }),
right: number('Boundary right padding', 0, { min: 0 }),
bottom: number('Boundary bottom padding', 0, { min: 0 }),
left: number('Boundary left padding', 0, { min: 0 }),
};

return (
<div ref={red} style={{ backgroundColor: 'red', padding: 30, height: '100%' }}>
<div ref={white} style={{ backgroundColor: 'white', padding: 30, height: '100%' }}>
<div ref={blue} style={{ backgroundColor: 'blue', padding: 30, height: '100%' }}>
<Chart className="story-chart">
<Settings tooltip={{ boundary }} />
<Settings tooltip={{ boundary, boundaryPadding }} />
<Axis id="bottom" hide={!showAxes} position={Position.Bottom} title="Bottom axis" showOverlappingTicks />
<Axis
id="left"
Expand Down