From 72f2f751318323f2431641c970680db5d9c8f251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Benitte?= Date: Thu, 17 Aug 2017 17:59:55 +0900 Subject: [PATCH] feat(tooltip): add support for tooltip theming --- src/Nivo.js | 14 ++++++++++++++ src/components/charts/Container.js | 14 +++++--------- src/components/charts/bar/Bar.js | 2 +- src/components/charts/bubble/BubblePlaceholders.js | 9 ++++++--- src/components/charts/bubble/BubbleProps.js | 1 + src/components/charts/chord/Chord.js | 7 ++++--- src/components/charts/line/Line.js | 3 ++- src/components/charts/line/LineSlices.js | 4 +++- src/components/charts/line/LineSlicesItem.js | 8 ++++++-- src/components/charts/pie/Pie.js | 2 +- src/components/charts/stream/Stream.js | 3 ++- src/components/charts/stream/StreamSlices.js | 4 +++- src/components/charts/stream/StreamSlicesItem.js | 8 ++++++-- src/components/charts/sunburst/Sunburst.js | 5 ++++- src/components/tooltip/TableTooltip.js | 12 ++++-------- 15 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/Nivo.js b/src/Nivo.js index c807f8073..08ca608dc 100644 --- a/src/Nivo.js +++ b/src/Nivo.js @@ -41,6 +41,20 @@ export const defaultTheme = { textColor: '#000', fontSize: '11px', }, + tooltip: { + background: 'white', + color: 'inherit', + fontSize: 'inherit', + borderRadius: '2px', + boxShadow: '0 1px 2px rgba(0, 0, 0, 0.25)', + padding: '5px 9px', + table: { + fontSize: 'inherit', + }, + tableCell: { + padding: '3px 5px', + }, + }, } export default { diff --git a/src/components/charts/Container.js b/src/components/charts/Container.js index 82e67590f..33d3cdc7d 100644 --- a/src/components/charts/Container.js +++ b/src/components/charts/Container.js @@ -15,18 +15,13 @@ const containerStyle = { const tooltipStyle = { position: 'absolute', - background: '#FFF', zIndex: 10, top: 0, left: 0, - borderRadius: '2px', - boxShadow: '0 1px 2px rgba(0, 0, 0, 0.25)', - padding: '5px 9px', - fontSize: '14px', } -const Tooltip = ({ x, y, children }) => -
+const Tooltip = ({ x, y, children, theme }) => +
{children}
@@ -39,6 +34,7 @@ export default class Container extends Component { static propTypes = { children: PropTypes.func.isRequired, isInteractive: PropTypes.bool.isRequired, + theme: PropTypes.object.isRequired, } static defaultProps = { @@ -69,7 +65,7 @@ export default class Container extends Component { } render() { - const { children, isInteractive } = this.props + const { children, isInteractive, theme } = this.props const { isTooltipVisible, tooltipContent, tooltipX, tooltipY } = this.state if (!isInteractive) return children(noop) @@ -86,7 +82,7 @@ export default class Container extends Component { hideTooltip: this.hideTooltip, })} {isTooltipVisible && - + {tooltipContent} }
diff --git a/src/components/charts/bar/Bar.js b/src/components/charts/bar/Bar.js index d69626170..714cba923 100644 --- a/src/components/charts/bar/Bar.js +++ b/src/components/charts/bar/Bar.js @@ -83,7 +83,7 @@ const Bar = ({ } return ( - + {({ showTooltip, hideTooltip }) => { let bars if (animate === true) { diff --git a/src/components/charts/bubble/BubblePlaceholders.js b/src/components/charts/bubble/BubblePlaceholders.js index 62a3fdb26..9e6b2f29a 100644 --- a/src/components/charts/bubble/BubblePlaceholders.js +++ b/src/components/charts/bubble/BubblePlaceholders.js @@ -7,9 +7,10 @@ * file that was distributed with this source code. */ import React, { Component } from 'react' +import { merge } from 'lodash' import { TransitionMotion, spring } from 'react-motion' import _ from 'lodash' -import Nivo from '../../../Nivo' +import Nivo, { defaultTheme } from '../../../Nivo' import { getColorsGenerator, extractRGB } from '../../../lib/colorUtils' import Container from '../Container' import BubbleHelper from '../../../lib/charts/bubble/BubbleHelper' @@ -65,6 +66,7 @@ export default class BubblePlaceholders extends Component { identity: _identity, value: _value, padding, + theme: _theme, colors, colorBy, animate, @@ -77,6 +79,7 @@ export default class BubblePlaceholders extends Component { const identity = convertGetter(_identity) const value = convertGetter(_value) const color = getColorsGenerator(colors, colorBy) + const theme = merge({}, defaultTheme, _theme) const margin = Object.assign({}, Nivo.defaults.margin, this.props.margin) const width = _width - margin.left - margin.right @@ -124,7 +127,7 @@ export default class BubblePlaceholders extends Component { if (!animate) { return ( - + {({ showTooltip, hideTooltip }) => React.createElement( wrapperTag, @@ -154,7 +157,7 @@ export default class BubblePlaceholders extends Component { } return ( - + {({ showTooltip, hideTooltip }) => React.createElement( wrapperTag, diff --git a/src/components/charts/bubble/BubbleProps.js b/src/components/charts/bubble/BubbleProps.js index 857724fa9..94e7e5892 100644 --- a/src/components/charts/bubble/BubbleProps.js +++ b/src/components/charts/bubble/BubbleProps.js @@ -74,6 +74,7 @@ export const bubbleDefaultProps = { padding: 1, // theming + theme: {}, colors: 'nivo', colorBy: 'depth', diff --git a/src/components/charts/chord/Chord.js b/src/components/charts/chord/Chord.js index 7629ee16e..fbab55e80 100644 --- a/src/components/charts/chord/Chord.js +++ b/src/components/charts/chord/Chord.js @@ -14,7 +14,7 @@ import pure from 'recompose/pure' import { chord as d3Chord, ribbon as Ribbon } from 'd3-chord' import { arc as Arc } from 'd3-shape' import { rgb } from 'd3-color' -import { withDimensions } from '../../../hocs' +import { withTheme, withDimensions } from '../../../hocs' import { getColorRange } from '../../../lib/colorUtils' import Container from '../Container' import SvgWrapper from '../SvgWrapper' @@ -38,6 +38,7 @@ const Chord = ({ arcBorderWidth, // theming + theme, colors, // interactivity @@ -62,7 +63,7 @@ const Chord = ({ const arcs = ribbons.groups return ( - + {({ showTooltip, hideTooltip }) => { return ( @@ -144,6 +145,6 @@ export const ChordDefaultProps = { isInteractive: true, } -const enhance = compose(defaultProps(ChordDefaultProps), withDimensions(), pure) +const enhance = compose(defaultProps(ChordDefaultProps), withTheme(), withDimensions(), pure) export default enhance(Chord) diff --git a/src/components/charts/line/Line.js b/src/components/charts/line/Line.js index 36fdf922a..7c49f87d4 100644 --- a/src/components/charts/line/Line.js +++ b/src/components/charts/line/Line.js @@ -85,7 +85,7 @@ const Line = ({ } return ( - + {({ showTooltip, hideTooltip }) => } {enableMarkers && +const LineSlices = ({ slices, height, showTooltip, hideTooltip, theme }) => {slices.map(slice => height={height} showTooltip={showTooltip} hideTooltip={hideTooltip} + theme={theme} /> )} @@ -41,6 +42,7 @@ LineSlices.propTypes = { height: PropTypes.number.isRequired, showTooltip: PropTypes.func.isRequired, hideTooltip: PropTypes.func.isRequired, + theme: PropTypes.object.isRequired, } export default pure(LineSlices) diff --git a/src/components/charts/line/LineSlicesItem.js b/src/components/charts/line/LineSlicesItem.js index 7b7793990..f2d7bdff4 100644 --- a/src/components/charts/line/LineSlicesItem.js +++ b/src/components/charts/line/LineSlicesItem.js @@ -48,13 +48,17 @@ LineSlicesItem.propTypes = { showTooltip: PropTypes.func.isRequired, hideTooltip: PropTypes.func.isRequired, isHover: PropTypes.bool.isRequired, + theme: PropTypes.object.isRequired, } const enhance = compose( withState('isHover', 'setIsHover', false), - withPropsOnChange(['slice'], ({ slice }) => ({ + withPropsOnChange(['slice', 'theme'], ({ slice, theme }) => ({ tooltip: ( - [, p.id, p.value])} /> + [, p.id, p.value])} + /> ), })), withHandlers({ diff --git a/src/components/charts/pie/Pie.js b/src/components/charts/pie/Pie.js index 8852a0cfc..0a60b6373 100644 --- a/src/components/charts/pie/Pie.js +++ b/src/components/charts/pie/Pie.js @@ -113,7 +113,7 @@ const Pie = ({ arc.outerRadius(radius) return ( - + {({ showTooltip, hideTooltip }) => + {({ showTooltip, hideTooltip }) => } } diff --git a/src/components/charts/stream/StreamSlices.js b/src/components/charts/stream/StreamSlices.js index 62cb52350..0abcc37fe 100644 --- a/src/components/charts/stream/StreamSlices.js +++ b/src/components/charts/stream/StreamSlices.js @@ -11,7 +11,7 @@ import PropTypes from 'prop-types' import pure from 'recompose/pure' import StreamSlicesItem from './StreamSlicesItem' -const StreamSlices = ({ slices, height, showTooltip, hideTooltip }) => +const StreamSlices = ({ slices, height, showTooltip, hideTooltip, theme }) => {slices.map(slice => height={height} showTooltip={showTooltip} hideTooltip={hideTooltip} + theme={theme} /> )} @@ -41,6 +42,7 @@ StreamSlices.propTypes = { height: PropTypes.number.isRequired, showTooltip: PropTypes.func.isRequired, hideTooltip: PropTypes.func.isRequired, + theme: PropTypes.object.isRequired, } export default pure(StreamSlices) diff --git a/src/components/charts/stream/StreamSlicesItem.js b/src/components/charts/stream/StreamSlicesItem.js index 4b776765e..b86d8cb6c 100644 --- a/src/components/charts/stream/StreamSlicesItem.js +++ b/src/components/charts/stream/StreamSlicesItem.js @@ -48,13 +48,17 @@ StreamSlicesItem.propTypes = { showTooltip: PropTypes.func.isRequired, hideTooltip: PropTypes.func.isRequired, isHover: PropTypes.bool.isRequired, + theme: PropTypes.object.isRequired, } const enhance = compose( withState('isHover', 'setIsHover', false), - withPropsOnChange(['slice'], ({ slice }) => ({ + withPropsOnChange(['slice', 'theme'], ({ slice, theme }) => ({ tooltip: ( - [, p.id, p.value])} /> + [, p.id, p.value])} + /> ), })), withHandlers({ diff --git a/src/components/charts/sunburst/Sunburst.js b/src/components/charts/sunburst/Sunburst.js index 6d73f7281..f3a8c7b72 100644 --- a/src/components/charts/sunburst/Sunburst.js +++ b/src/components/charts/sunburst/Sunburst.js @@ -46,11 +46,14 @@ const Sunburst = ({ borderWidth, borderColor, + // theming + theme, + // interactivity isInteractive, }) => { return ( - + {({ showTooltip, hideTooltip }) => diff --git a/src/components/tooltip/TableTooltip.js b/src/components/tooltip/TableTooltip.js index d6771642e..9c9eb2c0f 100644 --- a/src/components/tooltip/TableTooltip.js +++ b/src/components/tooltip/TableTooltip.js @@ -11,25 +11,20 @@ import PropTypes from 'prop-types' import pure from 'recompose/pure' const tableStyle = { - fontSize: '14px', width: '100%', borderCollapse: 'collapse', } -const cellStyle = { - padding: '3px 5px', -} - -const TableTooltip = ({ rows }) => { +const TableTooltip = ({ rows, theme }) => { if (!rows.length) return null return ( - +
{rows.map((row, i) => {row.map((column, j) => - )} @@ -42,6 +37,7 @@ const TableTooltip = ({ rows }) => { TableTooltip.propTypes = { rows: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.node)).isRequired, + theme: PropTypes.object.isRequired, } TableTooltip.defaultProps = {}
+ {column}