Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to render prop function pattern #29

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ import ReactCursorPosition from 'react-cursor-position';
...

<ReactCursorPosition>
<YourComponentOne/>
<YourComponentTwo/>
{(cursorProps) => (
<div>
<YourComponentOne {...cursorProps} />
<YourComponentTwo {...cursorProps} />
</div>
)}
</ReactCursorPosition>
```

react-cursor-position wraps its children in a div, which mouse and touch position
are plotted relative to.

Each child component will receive the following props:
The function provided will receive the following object:

```JavaScript
{
Expand All @@ -64,8 +68,6 @@ Each child component will receive the following props:
}
}
```
This structure may be customized by implementing `mapChildProps` API feature.

The information in `detectedEnvironment` is acquired from interaction with this component and will be unset until the first interaction.

## Props API
Expand All @@ -84,12 +86,6 @@ All props are optional.

**isEnabled** : Boolean - Enable or disable cursor position monitoring without remounting. Defaults to true.

**mapChildProps** : Function - Model child component props to your custom shape.
Function receives one parameter with the signature
`{ isActive: Boolean, isPositionOutside: Boolean, position: { x: Number, y: Number } }`.
It should return an object that is compatible with the props interface of your child components.
See [example demo](https://ethanselzer.github.io/react-cursor-position/#/map-child-props).

**onActivationChanged** : Function - Called when the component is active.
Function receives one parameter with the signature `{ isActive: Boolean }`.

Expand All @@ -103,9 +99,6 @@ Function receives one parameter with the signature `{ isMouseDetected: Boolean,

**pressMoveThreshold** : Number - Amount of movement, in pixels, allowed during press gesture detection. Defaults to 5.

**shouldDecorateChildren** : Boolean - Suppress decoration of child components by
setting this prop false. Defaults to true.

**shouldStopTouchMovePropagation** : Boolean - Stop touchmove event bubbling when react-cursor-position is active. Defaults to false.

**style** : Object - Style to be applied to the div rendered by react-cursor-position.
Expand Down
8 changes: 6 additions & 2 deletions example/public/activation-click.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
&lt;ReactCursorPosition
activationInteractionMouse={INTERACTIONS.CLICK}
&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
</code>
Expand Down
8 changes: 6 additions & 2 deletions example/public/activation-hover.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
hoverDelayInMs={250} //default: 0
hoverOffDelayInMs={150} //default: 0
&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
</code>
Expand Down
8 changes: 6 additions & 2 deletions example/public/activation-press.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
pressDurationInMs={500} //default
pressMoveThreshold={5} //default
&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
</code>
Expand Down
8 changes: 6 additions & 2 deletions example/public/activation-tap.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
&lt;ReactCursorPosition
activationInteractionTouch={INTERACTIONS.TAP}
&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
</code>
Expand Down
8 changes: 6 additions & 2 deletions example/public/activation-touch.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
&lt;ReactCursorPosition
activationInteractionTouch={INTERACTIONS.TOUCH}
&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
</code>
Expand Down
8 changes: 6 additions & 2 deletions example/public/class-name.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@

export default () => (
&lt;ReactCursorPosition className="example__target"&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
</code>
Expand Down
44 changes: 0 additions & 44 deletions example/public/map-child-props.html

This file was deleted.

4 changes: 3 additions & 1 deletion example/public/on-activation-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
this.setState({ isActive });
}
}}&gt;
&lt;InstructionsLabel /&gt;
{() => (
&lt;InstructionsLabel /&gt;
)}
&lt;/ReactCursorPosition&gt;
&lt;ActivationChangedLabel {...this.state} /&gt;
&lt;/div&gt;
Expand Down
4 changes: 3 additions & 1 deletion example/public/on-detected-environment-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
this.setState({ detectedEnvironment });
}
}}&gt;
&lt;InstructionsLabel /&gt;
{() => (
&lt;InstructionsLabel /&gt;
)}
&lt;/ReactCursorPosition&gt;
&lt;DetectedEnvironmentChangeLabel {...this.state} /&gt;
&lt;/div&gt;
Expand Down
4 changes: 3 additions & 1 deletion example/public/on-position-changed.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
className: 'example__target',
onPositionChanged: props =&gt; this.setState(props)
}}&gt;
&lt;InstructionsLabel /&gt;
{() => (
&lt;InstructionsLabel /&gt;
)}
&lt;/ReactCursorPosition&gt;
&lt;PositionChangedLabel {...this.state} /&gt;
&lt;/div&gt;
Expand Down
59 changes: 0 additions & 59 deletions example/public/should-decorate-children.html

This file was deleted.

8 changes: 6 additions & 2 deletions example/public/style.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@

return (
&lt;ReactCursorPosition {...{ style }}&gt;
&lt;PositionLabel /&gt;
&lt;InstructionsLabel /&gt;
{(cursorProps) => (
&lt;div&gt;
&lt;PositionLabel {...cursorProps} /&gt;
&lt;InstructionsLabel /&gt;
&lt;/div&gt;
)}
&lt;/ReactCursorPosition&gt;
);
}
Expand Down
13 changes: 8 additions & 5 deletions example/src/components/ActivationByClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export default () => (
className="example__target"
activationInteractionMouse={INTERACTIONS.CLICK}
>
<PositionLabel />
<div className="example__instructions">
Click to activate. Hover. Then click again to deactivate.
</div>
{(cursorProps) => (
<div>
<PositionLabel {...cursorProps}/>
<div className="example__instructions">
Click to activate. Hover. Then click again to deactivate.
</div>
</div>
)}
</ReactCursorPosition>
);

12 changes: 8 additions & 4 deletions example/src/components/ActivationByHover.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ export default () => (
hoverDelayInMs={250} //default 0
hoverOffDelayInMs={150} //default 0
>
<PositionLabel />
<div className="example__instructions">
<InstructionsLabel />
</div>
{(cursorProps) => (
<div>
<PositionLabel {...cursorProps} />
<div className="example__instructions">
<InstructionsLabel />
</div>
</div>
)}
</ReactCursorPosition>
);

12 changes: 8 additions & 4 deletions example/src/components/ActivationByPress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ export default () => (
pressDurationInMs={500} //default
pressMoveThreshold={5} //default
>
<PositionLabel />
<div className="example__instructions">
Press and hold to activate, then drag
</div>
{(cursorProps) => (
<div>
<PositionLabel {...cursorProps} />
<div className="example__instructions">
Press and hold to activate, then drag
</div>
</div>
)}
</ReactCursorPosition>
);

12 changes: 8 additions & 4 deletions example/src/components/ActivationByTap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ export default () => (
className="example__target"
activationInteractionTouch={INTERACTIONS.TAP}
>
<PositionLabel />
<div className="example__instructions">
Tap to activate. Drag. Tap again to deactivate.
</div>
{(cursorProps) => (
<div>
<PositionLabel {...cursorProps} />
<div className="example__instructions">
Tap to activate. Drag. Tap again to deactivate.
</div>
</div>
)}
</ReactCursorPosition>
);

12 changes: 8 additions & 4 deletions example/src/components/ActivationByTouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ export default () => (
className="example__target"
activationInteractionTouch={INTERACTIONS.TOUCH}
>
<PositionLabel />
<div className="example__instructions">
Touch and Drag
</div>
{(cursorProps) => (
<div>
<PositionLabel {...cursorProps} />
<div className="example__instructions">
Touch and Drag
</div>
</div>
)}
</ReactCursorPosition>
);

Loading