Skip to content

Commit

Permalink
feat: expose onMouseMove event on <MouseSensor>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 31, 2018
1 parent 8e23e1c commit d22dc80
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/en/MouseSensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ interface IMouseSensorProps {
, where

- `bond` &mdash; optional, boolean or string, specifying bondig spread object. If string, it specifies the name of the bonding object injected into state, if boolean and true the bonding object will have its default name `bond`.
- `whenHovered` - optional, boolean, when true, will track mouse position only when target element is hovered, defaults to `false`.
- `whenHovered` &mdash; optional, boolean, when true, will track mouse position only when target element is hovered, defaults to `false`.
- `onMouseMove` &mdash; optional, callback called on every mouse move, receives state as a single argument.

### Example

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libreact",
"version": "1.2.1",
"version": "1.3.0",
"description": "React standard library",
"main": "lib/index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/MouseSensor/__story__/story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Decorator extends Component<any, any> {

storiesOf('Sensors/MouseSensor', module)
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/MouseSensor.md')}))
.add('FaCC', () => <MouseSensor>{Demo}</MouseSensor>)
.add('FaCC', () => <MouseSensor onMouseMove={action('onMouseMove')}>{Demo}</MouseSensor>)
.add('FaCC with bond', () => <MouseSensor bond>{DemoWithBond}</MouseSensor>)
.add('Render prop', () => <MouseSensor render={Demo} />)
.add('HOC 1', () => <Hoc1 />)
Expand Down
9 changes: 6 additions & 3 deletions src/MouseSensor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface IMouseSensorProps {
children?: (state: IMouseSensorState) => React.ReactElement<any>;
render?: (state: IMouseSensorState) => React.ReactElement<any>;
whenHovered?: boolean;
onMouseMove?: (state: IMouseSensorState) => void;
}

export interface IMouseSensorState {
Expand Down Expand Up @@ -71,8 +72,7 @@ export class MouseSensor extends Component<IMouseSensorProps, IMouseSensorState>
const {left, top} = el.getBoundingClientRect();
const posX = left + window.scrollX;
const posY = top + window.scrollY;

this.setState({
const state = {
docX: event.pageX,
docY: event.pageY,
posX,
Expand All @@ -81,7 +81,10 @@ export class MouseSensor extends Component<IMouseSensorProps, IMouseSensorState>
elW: el.offsetWidth,
elX: event.pageX - posX,
elY: event.pageY - posY
});
};

this.setState(state);
(this.props.onMouseMove || noop)(state);
});
};

Expand Down

0 comments on commit d22dc80

Please sign in to comment.