-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 move extra mouse functionality into useMouseHovered
- Loading branch information
Showing
6 changed files
with
89 additions
and
30 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,4 +1,4 @@ | ||
import '@storybook/addon-knobs/register'; | ||
import '@storybook/addon-options/register'; | ||
import '@storybook/addon-actions/register'; | ||
import '@storybook/addon-knobs/register'; | ||
import '@storybook/addon-notes/register'; |
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,46 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {withKnobs, boolean} from '@storybook/addon-knobs'; | ||
import {useMouseHovered} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo: React.FC<any> = ({whenHovered, bound}) => { | ||
const ref = React.useRef(null); | ||
const state = useMouseHovered(ref, {whenHovered, bound}) | ||
|
||
return ( | ||
<> | ||
<pre> | ||
{JSON.stringify(state, null, 2)} | ||
</pre> | ||
<br /> | ||
<br /> | ||
<div | ||
ref={ref} | ||
style={{ | ||
position: 'relative', | ||
width: '400px', | ||
height: '400px', | ||
backgroundColor: 'whitesmoke', | ||
}}> | ||
<span style={{ | ||
position: 'absolute', | ||
left: `${state.elX}px`, | ||
top: `${state.elY}px`, | ||
pointerEvents: 'none', | ||
transform: 'scale(4)'}}> | ||
🐭 | ||
</span> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
storiesOf('Sensors|useMouseHovered', module) | ||
.addDecorator(withKnobs) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useMouse.md')} />) | ||
.add('Demo', () => { | ||
const bound = boolean('bound', false); | ||
const whenHovered = boolean('whenHovered', false); | ||
return <Demo whenHovered={whenHovered} bound={bound} />; | ||
}) |
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,27 @@ | ||
import {RefObject} from 'react'; | ||
import useHoverDirty from './useHoverDirty'; | ||
import useMouse, {State} from './useMouse'; | ||
|
||
export interface UseMouseHoveredOptions { | ||
whenHovered?: boolean; | ||
bound?: boolean; | ||
} | ||
|
||
const nullRef = {current: null}; | ||
|
||
const useMouseHovered = (ref: RefObject<HTMLElement>, options: UseMouseHoveredOptions = {}): State => { | ||
const whenHovered = !!options.whenHovered; | ||
const bound = !!options.bound; | ||
|
||
const isHovered = useHoverDirty(ref, whenHovered); | ||
const state = useMouse(whenHovered && !isHovered ? nullRef : ref); | ||
|
||
if (bound) { | ||
state.elX = Math.max(0, Math.min(state.elX, state.elW)); | ||
state.elY = Math.max(0, Math.min(state.elY, state.elH)); | ||
} | ||
|
||
return state; | ||
} | ||
|
||
export default useMouseHovered; |