Skip to content

Commit

Permalink
Tooltip: add placement prop to replace position
Browse files Browse the repository at this point in the history
  • Loading branch information
brookewp committed Sep 7, 2023
1 parent 69132c4 commit 7e12bac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/components/src/tooltip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ The amount of time in milliseconds to wait before showing the tooltip.
- Required: No
- Default: `700`

#### `placement`: `'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end'`

Used to specify the tooltip's position with respect to its anchor.

- Required: No
- Default: `'bottom'`

#### `position`: `string`

The direction in which the tooltip should open relative to its parent node. Specify y- and x-axis as a space-separated string. Supports `"top"`, `"middle"`, `"bottom"` y axis, and `"left"`, `"center"`, `"right"` x axis.
_Note: use the `placement` prop instead when possible._

Legacy way to specify the popover's position with respect to its anchor. Specify y- and x-axis as a space-separated string. Supports `'top'`, `'middle'`, `'bottom'` y axis, and `'left'`, `'center'`, `'right'` x axis.

- Required: No
- Default: `"bottom"`
- Default: `'bottom'`

#### `shortcut`: `string` | `object`

Expand Down
27 changes: 25 additions & 2 deletions packages/components/src/tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as Ariakit from '@ariakit/react/tooltip';
*/
import { useInstanceId } from '@wordpress/compose';
import { Children } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand All @@ -25,7 +26,8 @@ function Tooltip( props: TooltipProps ) {
const {
children,
delay = TOOLTIP_DELAY,
position = 'bottom',
placement,
position,
shortcut,
text,
} = props;
Expand All @@ -44,8 +46,29 @@ function Tooltip( props: TooltipProps ) {
}
}

const DEFAULT_PLACEMENT = 'bottom';

// Compute tooltip's placement:
// - give priority to `placement` prop, if defined
// - otherwise, compute it from the legacy `position` prop (if defined)
// - finally, fallback to the DEFAULT_PLACEMENT.
let computedPlacement;
if ( placement !== undefined ) {
computedPlacement = placement;
} else if ( position !== undefined ) {
computedPlacement = positionToPlacement( position );
}
computedPlacement = computedPlacement || DEFAULT_PLACEMENT;

if ( position !== undefined ) {
deprecated( '`position` prop in wp.components.tooltip', {
since: '6.4',
alternative: '`placement` prop',
} );
}

const tooltipStore = Ariakit.useTooltipStore( {
placement: positionToPlacement( position ),
placement: computedPlacement,
timeout: delay,
} );

Expand Down
17 changes: 16 additions & 1 deletion packages/components/src/tooltip/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import type { Placement } from '@floating-ui/react-dom';

/**
* Internal dependencies
*/
Expand All @@ -18,9 +23,19 @@ export type TooltipProps = {
*/
delay?: number;
/**
* The direction in which the tooltip should open relative to its parent node.
* Where the tooltip should be positioned relative to its parent.
*
* @default bottom
*/
placement?: Placement;
/**
* _Note: this prop is deprecated. Please use the `placement` prop instead._
*
* Legacy way of specifying the tooltip's position relative to its parent.
*
* Specify y- and x-axis as a space-separated string. Supports `"top"`,
* `"bottom"` y axis, and `"left"`, `"center"`, `"right"` x axis.
* Where the tooltip should be positioned relative to its parent.
*
* @default bottom
*/
Expand Down

0 comments on commit 7e12bac

Please sign in to comment.