From 7e7e4c9a46a17fb549dadcfe0933aa1c6ff6619f Mon Sep 17 00:00:00 2001 From: Ben Keen Date: Wed, 20 Feb 2019 20:58:29 -0800 Subject: [PATCH 1/5] added className prop support --- demo/index.js | 15 ++++++++++++++- index.d.ts | 3 ++- src/Tippy.js | 22 ++++++++++++++++++++-- src/utils.js | 10 +++++++++- test/Tippy.test.js | 23 +++++++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/demo/index.js b/demo/index.js index aa629fc..f8dbb3c 100644 --- a/demo/index.js +++ b/demo/index.js @@ -117,14 +117,21 @@ class ComponentChild extends React.Component { class App extends React.Component { state = { arrow: false, + customClass: '' } toggleArrow = () => { this.setState(state => ({ - arrow: !state.arrow, + arrow: !state.arrow })) } + updateCustomClass = (e) => { + this.setState({ + customClass: e.target.value + }) + } + render() { return (
@@ -165,6 +172,12 @@ class App extends React.Component { + +

Other

+ + + +
) } diff --git a/index.d.ts b/index.d.ts index c9ad244..e3cfd9e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,7 +8,8 @@ export interface TippyProps extends Omit { children: React.ReactElement onCreate?: (tip: Instance) => void isVisible?: boolean - isEnabled?: boolean + isEnabled?: boolean, + className?: string, } declare const Tippy: React.ForwardRefExoticComponent diff --git a/src/Tippy.js b/src/Tippy.js index 8ae580d..af6ee48 100644 --- a/src/Tippy.js +++ b/src/Tippy.js @@ -13,6 +13,7 @@ import { hasOwnProperty, ssrSafeCreateDiv, preserveRef, + updateClassNames, } from './utils' function Tippy(props) { @@ -33,12 +34,17 @@ function Tippy(props) { useEffect(() => { instanceRef.current = tippy(targetRef.current, options) - const { onCreate, isEnabled, isVisible } = props + const { onCreate, isEnabled, isVisible, className } = props if (onCreate) { onCreate(instanceRef.current) } + if (className) { + const { tooltip } = instanceRef.current.popperChildren + updateClassNames(props.className, 'add', tooltip) + } + if (isEnabled === false) { instanceRef.current.disable() } @@ -70,7 +76,6 @@ function Tippy(props) { if (isEnabled === false) { instanceRef.current.disable() } - if (isVisible === true) { instanceRef.current.show() } @@ -79,6 +84,18 @@ function Tippy(props) { } }) + useEffect(() => { + if (!isMounted) { + return + } + const { tooltip } = instanceRef.current.popperChildren + updateClassNames(props.className, 'add', tooltip) + + return () => { + updateClassNames(props.className, 'remove', tooltip) + } + }, [props.className]) + return ( <> {cloneElement(props.children, { @@ -99,6 +116,7 @@ Tippy.propTypes = { onCreate: PropTypes.func, isVisible: PropTypes.bool, isEnabled: PropTypes.bool, + className: PropTypes.string, } Tippy.defaultProps = { diff --git a/src/utils.js b/src/utils.js index 2f531db..437e0cc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,6 @@ export function getNativeTippyProps(props) { // eslint-disable-next-line no-unused-vars - const { children, onCreate, isVisible, isEnabled, ...nativeProps } = props + const { children, onCreate, isVisible, isEnabled, className, ...nativeProps } = props return nativeProps } @@ -22,3 +22,11 @@ export function preserveRef(ref, node) { export function ssrSafeCreateDiv() { return typeof document !== 'undefined' && document.createElement('div') } + +export function updateClassNames(classNames, action, tooltip) { + classNames.split(' ').forEach(name => { + if (name) { + tooltip.classList[action](name) + } + }) +} diff --git a/test/Tippy.test.js b/test/Tippy.test.js index 8d276f1..70b12f2 100644 --- a/test/Tippy.test.js +++ b/test/Tippy.test.js @@ -53,6 +53,29 @@ describe('', () => { expect(tip.popper.querySelector('strong')).not.toBeNull() }) + test('custom class name get added to DOM', () => { + const className = 'hello' + const { container } = render( + +