forked from plouc/nivo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Raphaël Benitte
authored and
Raphaël Benitte
committed
Mar 27, 2019
1 parent
a78b293
commit 9c5f187
Showing
39 changed files
with
4,356 additions
and
11,900 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import React, { Component, useRef, useState, useCallback } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { themeContext } from '../theming' | ||
import { tooltipContext } from '../tooltip' | ||
import { usePartialTheme } from '../hooks' | ||
|
||
const containerStyle = { | ||
position: 'relative', | ||
} | ||
|
||
const tooltipStyle = { | ||
pointerEvents: 'none', | ||
position: 'absolute', | ||
zIndex: 10, | ||
} | ||
|
||
const Container = ({ theme: partialTheme = {}, children }) => { | ||
const containerEl = useRef(null) | ||
const [state, setState] = useState({ | ||
isTooltipVisible: false, | ||
tooltipContent: null, | ||
position: {}, | ||
}) | ||
const showTooltip = useCallback( | ||
(content, event) => { | ||
if (!containerEl) return | ||
|
||
const bounds = containerEl.current.getBoundingClientRect() | ||
|
||
const { clientX, clientY } = event | ||
|
||
const x = clientX - bounds.left | ||
const y = clientY - bounds.top | ||
|
||
const position = {} | ||
|
||
if (x < bounds.width / 2) position.left = x + 20 | ||
else position.right = bounds.width - x + 20 | ||
|
||
if (y < bounds.height / 2) position.top = y - 12 | ||
else position.bottom = bounds.height - y - 12 | ||
|
||
setState({ | ||
isTooltipVisible: true, | ||
tooltipContent: content, | ||
position, | ||
}) | ||
}, | ||
[containerEl] | ||
) | ||
const hideTooltip = useCallback(() => { | ||
setState({ isTooltipVisible: false, tooltipContent: null }) | ||
}) | ||
const { isTooltipVisible, tooltipContent, position } = state | ||
const theme = usePartialTheme(partialTheme) | ||
|
||
return ( | ||
<themeContext.Provider value={theme}> | ||
<tooltipContext.Provider value={[showTooltip, hideTooltip]}> | ||
<div style={containerStyle} ref={containerEl}> | ||
{children} | ||
{isTooltipVisible && ( | ||
<div | ||
style={{ | ||
...tooltipStyle, | ||
...position, | ||
...theme.tooltip, | ||
}} | ||
> | ||
{tooltipContent} | ||
</div> | ||
)} | ||
</div> | ||
</tooltipContext.Provider> | ||
</themeContext.Provider> | ||
) | ||
} | ||
|
||
Container.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
theme: PropTypes.object, | ||
} | ||
|
||
export const withContainer = WrappedComponent => { | ||
return class extends Component { | ||
render() { | ||
const { theme, ...rest } = this.props | ||
|
||
return ( | ||
<Container theme={theme}> | ||
<WrappedComponent {...rest} /> | ||
</Container> | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
export * from './useDimensions' | ||
export * from './usePartialTheme' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { useMemo } from 'react' | ||
import { defaultMargin } from '../defaults' | ||
|
||
export const useDimensions = (width, height, partialMargin = {}) => | ||
useMemo(() => { | ||
const margin = { | ||
...defaultMargin, | ||
...partialMargin, | ||
} | ||
|
||
return { | ||
margin, | ||
innerWidth: width - margin.left - margin.right, | ||
innerHeight: height - margin.top - margin.bottom, | ||
outerWidth: width, | ||
outerHeight: height, | ||
} | ||
}, [ | ||
width, | ||
height, | ||
partialMargin.top, | ||
partialMargin.right, | ||
partialMargin.bottom, | ||
partialMargin.left, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { useMemo } from 'react' | ||
import { defaultTheme, extendDefaultTheme } from '../theming' | ||
|
||
export const usePartialTheme = partialTheme => | ||
useMemo(() => extendDefaultTheme(defaultTheme, partialTheme), [partialTheme]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { createContext, useContext } from 'react' | ||
|
||
export const themeContext = createContext() | ||
|
||
export const useTheme = () => useContext(themeContext) |
Oops, something went wrong.