Skip to content

Commit

Permalink
refactor: use hooks in Hover
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Jan 26, 2022
1 parent 56db346 commit 74f9400
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
56 changes: 25 additions & 31 deletions packages/griffith/src/components/Hover.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import React from 'react'
import {sequence} from 'griffith-utils'
import noop from 'lodash/noop'
import useBoolean from '../hooks/useBoolean'

type Props = React.HTMLAttributes<HTMLDivElement> & {
children: (isHover: boolean) => React.ReactElement
}

type State = {
isHovered: boolean
}

export default class Hover extends React.Component<Props, State> {
state = {
isHovered: false,
}

handlePointerEnter = () => {
this.setState({isHovered: true})
}
const Hover: React.FC<Props> = ({
children,
onMouseEnter,
onMouseLeave,
...rest
}) => {
const [isHovered, isHoveredSwitch] = useBoolean()

handlePointerLeave = () => {
this.setState({isHovered: false})
}

render() {
const {onMouseEnter, onMouseLeave, children, ...rest} = this.props
const {isHovered} = this.state
return (
<div
{...rest}
onMouseEnter={sequence(this.handlePointerEnter, onMouseEnter || noop)}
onMouseLeave={sequence(this.handlePointerLeave, onMouseLeave || noop)}
>
{children(isHovered)}
</div>
)
}
return (
<div
{...rest}
onMouseEnter={(e) => {
isHoveredSwitch.on()
onMouseEnter?.(e)
}}
onMouseLeave={(e) => {
isHoveredSwitch.off()
onMouseLeave?.(e)
}}
>
{children(isHovered)}
</div>
)
}

export default Hover
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const FullScreenButtonItem: React.FC<{
onClick: React.HTMLAttributes<HTMLButtonElement>['onClick']
}> = ({isFullScreen, onClick}) => (
<Hover className={css(styles.menuContainer)}>
{(isFullScreenHovered: any) => (
{(isFullScreenHovered) => (
<>
<ControllerButton
icon={isFullScreen ? icons.smallscreen : icons.fullscreen}
Expand Down

0 comments on commit 74f9400

Please sign in to comment.