Skip to content

Commit 7a15cac

Browse files
committed
✨ Add sticky joystick
The joystick will now stay in place if sticky is set to true
1 parent e23bd69 commit 7a15cac

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Joystick } from 'react-joystick-component';
1414

1515

1616
```React
17-
<Joystick size={100} baseColor="red" stickColor="blue" move={handleMove} stop={handleStop}></Joystick>
17+
<Joystick size={100} sticky={true} baseColor="red" stickColor="blue" move={handleMove} stop={handleStop}></Joystick>
1818
```
1919

2020
Component Props - as described by IJoystickProps
@@ -25,6 +25,7 @@ Component Props - as described by IJoystickProps
2525
| baseColor | string | The color of the Joystick base |
2626
| stickColor | string | The color of the Stick |
2727
| throttle | number | The [throttling](https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf) rate of the move callback |
28+
| sticky | Boolean | Should the joystick stay where it is when the interaction ends |
2829
| move | Function | Callback fired on every mouse move, not throttled unless a throttling rate is provided as above |
2930
| stop | Function | Callback fired when the user releases the joystick |
3031
| start | Function | Callback fired when the user starts moving the Joystick |
@@ -37,6 +38,7 @@ interface IJoystickProps {
3738
stickColor?: string;
3839
disabled?: boolean;
3940
throttle?: number;
41+
sticky?: boolean;
4042
move?: (event: IJoystickUpdateEvent) => void;
4143
stop?: (event: IJoystickUpdateEvent) => void;
4244
start?: (event: IJoystickUpdateEvent) => void;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"deploy-storybook": "storybook-to-ghpages"
2222
},
2323
"repository": "https://github.com/elmarti/react-joystick-component",
24-
"author": "elmarti info@elmarti.tech",
24+
"author": "elmarti elmarti.tech@gmail.com",
2525
"license": "MIT",
2626
"devDependencies": {
2727
"@storybook/addon-actions": "^6.4.9",

src/Joystick.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ joystickStories.add("200ms throttled joystick", ()=> <Joystick start={action("St
2323

2424
joystickStories.add("500ms throttled joystick", ()=> <Joystick start={action("Started")} throttle={500} move={action("Moved")} stop={action("Stopped")} />);
2525

26+
joystickStories.add("Sticky joystick", ()=> <Joystick sticky={true} start={action("Started")} throttle={50} move={action("Moved")} stop={action("Stopped")} />);
27+
28+
2629

2730
joystickStories.add("HUGE joystick", ()=> <Joystick start={action("Started")}move={action("Moved")} stop={action("Stopped")} size={500}/>);
2831
joystickStories.add("Tiny joystick", ()=> <Joystick start={action("Started")}move={action("Moved")} stop={action("Stopped")} size={50}/>);

src/Joystick.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface IJoystickProps {
66
stickColor?: string;
77
throttle?: number;
88
disabled?: boolean;
9+
sticky?: boolean;
910
move?: (event: IJoystickUpdateEvent) => void;
1011
stop?: (event: IJoystickUpdateEvent) => void;
1112
start?: (event: IJoystickUpdateEvent) => void;
@@ -219,19 +220,25 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
219220
* @private
220221
*/
221222
private _mouseUp() {
222-
this.setState({
223+
const stateUpdate = {
223224
dragging: false,
224-
coordinates: undefined
225-
});
225+
};
226+
if(!this.props.sticky){
227+
stateUpdate['coordinates'] = undefined;
228+
}
229+
this.setState(stateUpdate);
226230
window.removeEventListener("mouseup", this._boundMouseUp);
227231
window.removeEventListener("mousemove", this._boundMouseMove);
228232

229233
if (this.props.stop) {
230234
this.props.stop({
231235
type: "stop",
232-
x: null,
233-
y: null,
234-
direction: null
236+
//@ts-ignore
237+
x: this.props.sticky ? this.state.coordinates.relativeX : null,
238+
//@ts-ignore
239+
y: this.props.sticky ? this.state.coordinates.relativeY : null,
240+
//@ts-ignore
241+
direction: this.props.sticky ? this.state.coordinates.direction : null
235242
});
236243
}
237244

@@ -275,7 +282,7 @@ class Joystick extends React.Component<IJoystickProps, IJoystickState> {
275282
flexShrink: 0
276283
};
277284

278-
if (this.state.dragging && this.state.coordinates !== undefined) {
285+
if (this.state.coordinates !== undefined) {
279286
stickStyle = Object.assign({}, stickStyle, {
280287
position: 'absolute',
281288
transform: `translate3d(${this.state.coordinates.relativeX}px, ${this.state.coordinates.relativeY}px, 0)`

0 commit comments

Comments
 (0)