forked from zhihu/griffith
-
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
Showing
2 changed files
with
26 additions
and
32 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
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 |
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