Skip to content

Commit

Permalink
feat(line): forward ref to the canvas element
Browse files Browse the repository at this point in the history
  • Loading branch information
bobylito authored and plouc committed Nov 12, 2020
1 parent d66acd6 commit 4be9c8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions packages/line/src/LineCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React, { useRef, useEffect, useState, useCallback } from 'react'
import React, { useRef, useEffect, useState, useCallback, forwardRef } from 'react'
import {
withContainer,
useDimensions,
Expand Down Expand Up @@ -67,6 +67,8 @@ const LineCanvas = ({
onMouseLeave,
onClick,
tooltip,

canvasRef,
}) => {
const canvasEl = useRef(null)
const { margin, innerWidth, innerHeight, outerWidth, outerHeight } = useDimensions(
Expand Down Expand Up @@ -100,6 +102,10 @@ const LineCanvas = ({
})

useEffect(() => {
if (canvasRef) {
canvasRef.current = canvasEl.current
}

canvasEl.current.width = outerWidth * pixelRatio
canvasEl.current.height = outerHeight * pixelRatio

Expand Down Expand Up @@ -326,4 +332,6 @@ const LineCanvas = ({
LineCanvas.propTypes = LineCanvasPropTypes
LineCanvas.defaultProps = LineCanvasDefaultProps

export default withContainer(LineCanvas)
const LineCanvasWithContainer = withContainer(LineCanvas)

export default forwardRef((props, ref) => <LineCanvasWithContainer {...props} canvasRef={ref} />)
8 changes: 4 additions & 4 deletions packages/line/src/ResponsiveLineCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React from 'react'
import React, { forwardRef } from 'react'
import { ResponsiveWrapper } from '@nivo/core'
import LineCanvas from './LineCanvas'

const ResponsiveLineCanvas = props => (
const ResponsiveLineCanvas = (props, ref) => (
<ResponsiveWrapper>
{({ width, height }) => <LineCanvas width={width} height={height} {...props} />}
{({ width, height }) => <LineCanvas width={width} height={height} {...props} ref={ref} />}
</ResponsiveWrapper>
)

export default ResponsiveLineCanvas
export default forwardRef(ResponsiveLineCanvas)

0 comments on commit 4be9c8a

Please sign in to comment.