From 4b9ce0c95d19b4786736c7595ef73404c2788e32 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 23 Feb 2019 11:54:32 +1100 Subject: [PATCH 1/4] Fix className prop --- .eslintrc | 4 ++-- demo/index.js | 22 ++++++++++++++++------ src/Tippy.js | 25 ++++++++----------------- src/utils.js | 14 +++++++++----- test/Tippy.test.js | 17 +++++++++++++++++ 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/.eslintrc b/.eslintrc index 158f293..dd54aa6 100644 --- a/.eslintrc +++ b/.eslintrc @@ -19,7 +19,7 @@ }, "extends": ["eslint:recommended", "plugin:react/recommended", "prettier"], "rules": { - "react/no-find-dom-node": "off", - "react/prop-types": "off" + "react/prop-types": "off", + "no-unused-vars": ["error", { "ignoreRestSiblings": true }] } } diff --git a/demo/index.js b/demo/index.js index f8dbb3c..6a58cb5 100644 --- a/demo/index.js +++ b/demo/index.js @@ -117,21 +117,27 @@ class ComponentChild extends React.Component { class App extends React.Component { state = { arrow: false, - customClass: '' + customClass: 'hello', } toggleArrow = () => { this.setState(state => ({ - arrow: !state.arrow + arrow: !state.arrow, })) } - updateCustomClass = (e) => { + updateCustomClass = e => { this.setState({ - customClass: e.target.value + customClass: e.target.value, }) } + componentDidMount() { + setTimeout(() => { + this.setState({ customClass: 'bye' }) + }, 500) + } + render() { return (
@@ -174,8 +180,12 @@ class App extends React.Component {

Other

- - + +
diff --git a/src/Tippy.js b/src/Tippy.js index 41c88f2..e380138 100644 --- a/src/Tippy.js +++ b/src/Tippy.js @@ -3,7 +3,6 @@ import React, { cloneElement, useState, useRef, - useEffect, useLayoutEffect, } from 'react' import { createPortal } from 'react-dom' @@ -32,20 +31,15 @@ function Tippy(props) { options.trigger = 'manual' } - useEffect(() => { + useLayoutEffect(() => { instanceRef.current = tippy(targetRef.current, options) - const { onCreate, isEnabled, isVisible, className } = props + const { onCreate, isEnabled, isVisible } = props if (onCreate) { onCreate(instanceRef.current) } - if (className) { - const { tooltip } = instanceRef.current.popperChildren - updateClassName(tooltip, 'add', props.className) - } - if (isEnabled === false) { instanceRef.current.disable() } @@ -86,15 +80,12 @@ function Tippy(props) { }) useLayoutEffect(() => { - if (!isMounted) { - return - } - - const { tooltip } = instanceRef.current.popperChildren - updateClassName(tooltip, 'add', props.className) - - return () => { - updateClassName(tooltip, 'remove', props.className) + if (props.className) { + const { tooltip } = instanceRef.current.popperChildren + updateClassName(tooltip, 'add', props.className) + return () => { + updateClassName(tooltip, 'remove', props.className) + } } }, [props.className]) diff --git a/src/utils.js b/src/utils.js index 22d8818..e1b236c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,12 @@ export function getNativeTippyProps(props) { - // eslint-disable-next-line no-unused-vars - const { children, onCreate, isVisible, isEnabled, className, ...nativeProps } = props + const { + children, + onCreate, + isVisible, + isEnabled, + className, + ...nativeProps + } = props return nativeProps } @@ -25,8 +31,6 @@ export function ssrSafeCreateDiv() { export function updateClassName(tooltip, action, classNames) { classNames.split(/\s+/).forEach(name => { - if (name) { - tooltip.classList[action](name) - } + tooltip.classList[action](name) }) } diff --git a/test/Tippy.test.js b/test/Tippy.test.js index e95d784..0084e31 100644 --- a/test/Tippy.test.js +++ b/test/Tippy.test.js @@ -75,6 +75,23 @@ describe('', () => { expect(tip.popper.querySelector('.world')).not.toBeNull() }) + test('updating className does not leave stale className behind', () => { + const { container, rerender } = render( + + From 6b70c38babe14f4535ae970093763a567749478d Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 23 Feb 2019 13:15:22 +1100 Subject: [PATCH 3/4] Consistency --- test/Tippy.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/Tippy.test.js b/test/Tippy.test.js index 0084e31..bd9a46f 100644 --- a/test/Tippy.test.js +++ b/test/Tippy.test.js @@ -55,24 +55,24 @@ describe('', () => { test('custom class name get added to DOM', () => { const className = 'hello' const { container } = render( - +