@vx/tooltip
Advanced tools
Comparing version
@@ -18,3 +18,3 @@ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
renderContainer = function renderContainer(children, props) { | ||
return React.createElement("div", props, children); | ||
return /*#__PURE__*/React.createElement("div", props, children); | ||
}; | ||
@@ -25,3 +25,3 @@ } | ||
var tooltipProps = useTooltip(); | ||
return renderContainer(React.createElement(BaseComponent, _extends({}, tooltipProps, props)), containerProps); | ||
return renderContainer( /*#__PURE__*/React.createElement(BaseComponent, _extends({}, tooltipProps, props)), containerProps); | ||
}; | ||
@@ -28,0 +28,0 @@ |
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
import React, { useState } from 'react'; | ||
import React, { useState, useCallback } from 'react'; | ||
export default function useTooltip() { | ||
var _useState = useState({ | ||
tooltipOpen: false, | ||
tooltipLeft: undefined, | ||
tooltipTop: undefined, | ||
tooltipData: undefined | ||
tooltipOpen: false | ||
}), | ||
@@ -14,3 +11,3 @@ tooltipState = _useState[0], | ||
var updateTooltip = function updateTooltip(_ref) { | ||
var updateTooltip = useCallback(function (_ref) { | ||
var tooltipOpen = _ref.tooltipOpen, | ||
@@ -28,5 +25,4 @@ tooltipLeft = _ref.tooltipLeft, | ||
}); | ||
}; | ||
var showTooltip = function showTooltip(_ref2) { | ||
}, []); | ||
var showTooltip = useCallback(function (_ref2) { | ||
var tooltipLeft = _ref2.tooltipLeft, | ||
@@ -41,5 +37,4 @@ tooltipTop = _ref2.tooltipTop, | ||
}); | ||
}; | ||
var hideTooltip = function hideTooltip() { | ||
}, [updateTooltip]); | ||
var hideTooltip = useCallback(function () { | ||
return updateTooltip({ | ||
@@ -51,4 +46,3 @@ tooltipOpen: false, | ||
}); | ||
}; | ||
}, [updateTooltip]); | ||
return { | ||
@@ -55,0 +49,0 @@ tooltipOpen: tooltipState.tooltipOpen, |
@@ -31,3 +31,3 @@ import _pt from "prop-types"; | ||
return React.createElement("div", _extends({ | ||
return /*#__PURE__*/React.createElement("div", _extends({ | ||
className: cx('vx-tooltip-portal', className), | ||
@@ -34,0 +34,0 @@ style: _extends({ |
@@ -9,3 +9,3 @@ import _pt from "prop-types"; | ||
import { withBoundingRects } from '@vx/bounds'; | ||
import Tooltip from './Tooltip'; | ||
import Tooltip, { defaultStyles } from './Tooltip'; | ||
@@ -25,4 +25,7 @@ function TooltipWithBounds(_ref) { | ||
children = _ref.children, | ||
style = _ref.style, | ||
otherProps = _objectWithoutPropertiesLoose(_ref, ["left", "top", "offsetLeft", "offsetTop", "rect", "parentRect", "getRects", "children", "style"]); | ||
_ref$style = _ref.style, | ||
style = _ref$style === void 0 ? defaultStyles : _ref$style, | ||
_ref$unstyled = _ref.unstyled, | ||
unstyled = _ref$unstyled === void 0 ? false : _ref$unstyled, | ||
otherProps = _objectWithoutPropertiesLoose(_ref, ["left", "top", "offsetLeft", "offsetTop", "rect", "parentRect", "getRects", "children", "style", "unstyled"]); | ||
@@ -39,7 +42,8 @@ var left = initialLeft; | ||
top = Math.round(top); | ||
return React.createElement(Tooltip, _extends({ | ||
return /*#__PURE__*/React.createElement(Tooltip, _extends({ | ||
style: _extends({ | ||
top: 0, | ||
transform: "translate(" + left + "px, " + top + "px)" | ||
}, style) | ||
}, !unstyled && style), | ||
unstyled: unstyled | ||
}, otherProps), children); | ||
@@ -51,6 +55,2 @@ } | ||
offsetTop: _pt.number, | ||
left: _pt.number, | ||
top: _pt.number, | ||
className: _pt.string, | ||
children: _pt.node, | ||
getRects: _pt.func, | ||
@@ -57,0 +57,0 @@ rect: _pt.shape({ |
@@ -27,3 +27,3 @@ "use strict"; | ||
renderContainer = function renderContainer(children, props) { | ||
return _react.default.createElement("div", props, children); | ||
return /*#__PURE__*/_react.default.createElement("div", props, children); | ||
}; | ||
@@ -34,3 +34,3 @@ } | ||
var tooltipProps = (0, _useTooltip.default)(); | ||
return renderContainer(_react.default.createElement(BaseComponent, _extends({}, tooltipProps, props)), containerProps); | ||
return renderContainer( /*#__PURE__*/_react.default.createElement(BaseComponent, _extends({}, tooltipProps, props)), containerProps); | ||
}; | ||
@@ -37,0 +37,0 @@ |
@@ -16,6 +16,3 @@ "use strict"; | ||
var _useState = (0, _react.useState)({ | ||
tooltipOpen: false, | ||
tooltipLeft: undefined, | ||
tooltipTop: undefined, | ||
tooltipData: undefined | ||
tooltipOpen: false | ||
}), | ||
@@ -25,3 +22,3 @@ tooltipState = _useState[0], | ||
var updateTooltip = function updateTooltip(_ref) { | ||
var updateTooltip = (0, _react.useCallback)(function (_ref) { | ||
var tooltipOpen = _ref.tooltipOpen, | ||
@@ -39,5 +36,4 @@ tooltipLeft = _ref.tooltipLeft, | ||
}); | ||
}; | ||
var showTooltip = function showTooltip(_ref2) { | ||
}, []); | ||
var showTooltip = (0, _react.useCallback)(function (_ref2) { | ||
var tooltipLeft = _ref2.tooltipLeft, | ||
@@ -52,5 +48,4 @@ tooltipTop = _ref2.tooltipTop, | ||
}); | ||
}; | ||
var hideTooltip = function hideTooltip() { | ||
}, [updateTooltip]); | ||
var hideTooltip = (0, _react.useCallback)(function () { | ||
return updateTooltip({ | ||
@@ -62,4 +57,3 @@ tooltipOpen: false, | ||
}); | ||
}; | ||
}, [updateTooltip]); | ||
return { | ||
@@ -66,0 +60,0 @@ tooltipOpen: tooltipState.tooltipOpen, |
@@ -43,3 +43,3 @@ "use strict"; | ||
return _react.default.createElement("div", _extends({ | ||
return /*#__PURE__*/_react.default.createElement("div", _extends({ | ||
className: (0, _classnames.default)('vx-tooltip-portal', className), | ||
@@ -46,0 +46,0 @@ style: _extends({ |
import React from 'react'; | ||
import { TooltipProps } from './Tooltip'; | ||
declare type RectShape = { | ||
@@ -15,9 +16,2 @@ top: number; | ||
}; | ||
declare type TooltipProps = { | ||
left?: number; | ||
top?: number; | ||
className?: string; | ||
style?: React.CSSProperties; | ||
children?: React.ReactNode; | ||
}; | ||
declare type Props = { | ||
@@ -34,3 +28,3 @@ offsetLeft?: number; | ||
context: any; | ||
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<Props>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void; | ||
setState<K extends never>(state: {} | Pick<{}, K> | ((prevState: Readonly<{}>, props: Readonly<Props>) => {} | Pick<{}, K> | null) | null, callback?: (() => void) | undefined): void; | ||
forceUpdate(callback?: (() => void) | undefined): void; | ||
@@ -37,0 +31,0 @@ readonly props: Readonly<Props> & Readonly<{ |
@@ -12,4 +12,8 @@ "use strict"; | ||
var _Tooltip = _interopRequireDefault(require("./Tooltip")); | ||
var _Tooltip = _interopRequireWildcard(require("./Tooltip")); | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -34,4 +38,7 @@ | ||
children = _ref.children, | ||
style = _ref.style, | ||
otherProps = _objectWithoutPropertiesLoose(_ref, ["left", "top", "offsetLeft", "offsetTop", "rect", "parentRect", "getRects", "children", "style"]); | ||
_ref$style = _ref.style, | ||
style = _ref$style === void 0 ? _Tooltip.defaultStyles : _ref$style, | ||
_ref$unstyled = _ref.unstyled, | ||
unstyled = _ref$unstyled === void 0 ? false : _ref$unstyled, | ||
otherProps = _objectWithoutPropertiesLoose(_ref, ["left", "top", "offsetLeft", "offsetTop", "rect", "parentRect", "getRects", "children", "style", "unstyled"]); | ||
@@ -48,7 +55,8 @@ var left = initialLeft; | ||
top = Math.round(top); | ||
return _react.default.createElement(_Tooltip.default, _extends({ | ||
return /*#__PURE__*/_react.default.createElement(_Tooltip.default, _extends({ | ||
style: _extends({ | ||
top: 0, | ||
transform: "translate(" + left + "px, " + top + "px)" | ||
}, style) | ||
}, !unstyled && style), | ||
unstyled: unstyled | ||
}, otherProps), children); | ||
@@ -60,6 +68,2 @@ } | ||
offsetTop: _propTypes.default.number, | ||
left: _propTypes.default.number, | ||
top: _propTypes.default.number, | ||
className: _propTypes.default.string, | ||
children: _propTypes.default.node, | ||
getRects: _propTypes.default.func, | ||
@@ -66,0 +70,0 @@ rect: _propTypes.default.shape({ |
{ | ||
"name": "@vx/tooltip", | ||
"version": "0.0.196", | ||
"version": "0.0.197", | ||
"description": "vx tooltip", | ||
@@ -33,3 +33,3 @@ "sideEffects": false, | ||
"@types/react": "*", | ||
"@vx/bounds": "0.0.196", | ||
"@vx/bounds": "0.0.197", | ||
"classnames": "^2.2.5", | ||
@@ -44,3 +44,3 @@ "prop-types": "^15.5.10" | ||
}, | ||
"gitHead": "0b9ff48c735be9a9273f38750120e7fe2fb2caaf" | ||
"gitHead": "c56b64bfbedde9ca06255b2eeff84b52c49d57ea" | ||
} |
# @vx/tooltip | ||
<a title="@vx/tooltip npm downloads" href="https://www.npmjs.com/package/@vx/tooltip"> | ||
<img src="https://img.shields.io/npm/dm/@vx/tooltip.svg?style=flat-square" /> | ||
</a> | ||
The `@vx/tooltip` package provides utilities for making it easy to add `Tooltip`s to a visualization | ||
and includes hooks, higher-order component (HOC) enhancers and Tooltip components. | ||
### Installation | ||
``` | ||
@@ -7,4 +16,2 @@ npm install --save @vx/tooltip | ||
The `@vx/tooltip` package provides utilities for making it easy to add `Tooltip`s to a visualization and includes hooks, higher-order component (HOC) enhancers and Tooltip components. | ||
### Hooks and Enhancers | ||
@@ -14,6 +21,8 @@ | ||
- a hook: `useTooltip()` | ||
- a hook: `useTooltip()` | ||
- a higher order component (HOC): `withTooltip()` | ||
The `useTooltip` hook is the recommended way to add tooltip state logic to your components, but can only be used in functional components. The `withTooltip` HOC can be used with both functional and class components, and is the recommended way to add tooltip state logic to class components. | ||
The `useTooltip` hook is the recommended way to add tooltip state logic to your components, but can | ||
only be used in functional components. The `withTooltip` HOC can be used with both functional and | ||
class components, and is the recommended way to add tooltip state logic to class components. | ||
@@ -32,20 +41,33 @@ Both `useTooltip` and `withTooltip` expose the same values and functions for use in your component: | ||
In the case of `useTooltip`, these will be returned from the `useTooltip()` call in your component. In the case of `withTooltip`, they will be passed as props to your wrapped component. Refer to the [Examples](#examples) section for a basic demo of each approach. | ||
In the case of `useTooltip`, these will be returned from the `useTooltip()` call in your component. | ||
In the case of `withTooltip`, they will be passed as props to your wrapped component. Refer to the | ||
[Examples](#examples) section for a basic demo of each approach. | ||
#### useTooltip() | ||
If you would like to add tooltip state logic to a functional component, you may use the `useTooltip()` hook which will return an object with several properties that you can use to manage the tooltip state of your component. | ||
If you would like to add tooltip state logic to a functional component, you may use the | ||
`useTooltip()` hook which will return an object with several properties that you can use to manage | ||
the tooltip state of your component. **For correct tooltip positioning, it is important to wrap your | ||
component in an element (e.g., `div`) with `relative` positioning**. This is handled for you by the | ||
`withTooltip` HOC, but not with the `useTooltip()` hook. | ||
#### withTooltip(BaseComponent [, containerProps [, renderContainer]]) | ||
If you would like to add tooltip state logic to a class component, you may wrap it in `withTooltip(BaseComponent [, containerProps [, renderContainer])`. | ||
If you would like to add tooltip state logic to a class component, you may wrap it in | ||
`withTooltip(BaseComponent [, containerProps [, renderContainer])`. | ||
The HOC will wrap your component in a `div` with `relative` positioning by default and handle state for tooltip positioning, visibility, and content by injecting the following props into your `BaseComponent`: | ||
The HOC will wrap your component in a `div` with `relative` positioning by default and handle state | ||
for tooltip positioning, visibility, and content by injecting the following props into your | ||
`BaseComponent`: | ||
You may override the container by specifying `containerProps` as the second argument to `withTooltip`, or by specifying `renderContainer` as the third argument to `withTooltip`. | ||
You may override the container by specifying `containerProps` as the second argument to | ||
`withTooltip`, or by specifying `renderContainer` as the third argument to `withTooltip`. | ||
### Components | ||
#### Tooltip | ||
This is a simple Tooltip container component meant to be used to actually render a Tooltip. It accepts the following props, and will spread any additional props on the tooltip container div (i.e., ...restProps): | ||
This is a simple Tooltip container component meant to be used to actually render a Tooltip. It | ||
accepts the following props, and will spread any additional props on the tooltip container div | ||
(i.e., ...restProps): | ||
@@ -59,7 +81,10 @@ | Name | Type | Default | Description | | ||
| children | node | -- | Sets the children of the tooltip, i.e., the actual content | | ||
| unstyled | bool | true | Whether the tooltip use styles from the style prop or not | | ||
| unstyled | bool | true | Whether the tooltip should use styles from the style prop or not | | ||
#### TooltipWithBounds | ||
This tooltip component is exactly the same as `Tooltip` above, but it is aware of its boundaries meaning that it will flip left/right and bottom/top based on whether it would overflow its parent's boundaries. It accepts the following props, and will spread any additional props on the Tooltip component (i.e., ...restProps): | ||
This tooltip component is exactly the same as `Tooltip` above, but it is aware of its boundaries | ||
meaning that it will flip left/right and bottom/top based on whether it would overflow its parent's | ||
boundaries. It accepts the following props, and will spread any additional props on the Tooltip | ||
component (i.e., ...restProps): | ||
@@ -74,6 +99,9 @@ | Name | Type | Default | Description | | ||
| children | node | -- | Sets the children of the tooltip, i.e., the actual content | | ||
| unstyled | bool | true | Whether the tooltip should use styles from the style prop or not | | ||
Note that this component is positioned using a `transform`, so overriding `left` and `top` via styles may have no effect. | ||
Note that this component is positioned using a `transform`, so overriding `left` and `top` via | ||
styles may have no effect. | ||
### Examples | ||
#### useTooltip For Functional Components | ||
@@ -80,0 +108,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
40373
3.93%208
15.56%493
-1.6%1
Infinity%+ Added
- Removed
Updated