-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[added] getPointerEventStreamsFromElement(element)
Summary: This is pretty cool, because it lets us support Chrome and Safari; touch and mouse; with a single stream of events and no reliance on polyfills! Closes #212 Reviewers: O2 Material Motion, O3 Material JavaScript platform reviewers, #material_motion, featherless Reviewed By: O2 Material Motion, #material_motion, featherless Subscribers: featherless Tags: #material_motion Differential Revision: http://codereview.cc/D3122
- Loading branch information
1 parent
8d9a3d3
commit 8ed8ac5
Showing
8 changed files
with
266 additions
and
1 deletion.
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
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,135 @@ | ||
/** @license | ||
* Copyright 2016 - present The Material Motion Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
|
||
import { | ||
Draggable, | ||
GestureRecognitionState, | ||
MotionObservable, | ||
Subscription, | ||
dragSystem, | ||
} from 'material-motion'; | ||
|
||
import { | ||
getPointerEventStreamsFromElement, | ||
} from 'material-motion-views-dom'; | ||
|
||
export class QuickiePointerEventTest extends React.Component { | ||
state = { | ||
x: 0, | ||
y: 0, | ||
state: GestureRecognitionState.POSSIBLE, | ||
velocity: { | ||
x: 0, | ||
y: 0, | ||
}, | ||
}; | ||
|
||
testEvents = (element: Element) => { | ||
let subscriptions: Array<Subscription> = []; | ||
|
||
if (element) { | ||
const draggable = new Draggable( | ||
getPointerEventStreamsFromElement(element) | ||
); | ||
|
||
const drag$ = dragSystem(draggable); | ||
const velocity$ = drag$.velocity( | ||
MotionObservable.from(draggable.state)._filter( | ||
state => state === GestureRecognitionState.RECOGNIZED | ||
) | ||
); | ||
|
||
subscriptions = [ | ||
drag$.subscribe(this.setState.bind(this)), | ||
velocity$.subscribe( | ||
(velocity: { x: number, y: number }) => this.setState({ velocity }) | ||
), | ||
draggable.state.subscribe( | ||
(state: GestureRecognitionState) => { | ||
this.setState({ state }); | ||
|
||
if (state === GestureRecognitionState.RECOGNIZED) { | ||
this.setState({ | ||
x: 0, | ||
y: 0, | ||
}); | ||
} | ||
} | ||
), | ||
]; | ||
|
||
} else { | ||
subscriptions.forEach(subscription => subscription.unsubscribe()); | ||
} | ||
} | ||
|
||
render = () => { | ||
const { | ||
x, | ||
y, | ||
velocity, | ||
state, | ||
} = this.state; | ||
|
||
return ( | ||
<div> | ||
<div | ||
style = { | ||
{ | ||
position: 'fixed', | ||
top: 150, | ||
left: 50, | ||
borderRadius: 28, | ||
width: 56, | ||
height: 56, | ||
backgroundColor: 'blue', | ||
transform: `translate(${ x }px, ${ y }px)`, | ||
touchAction: 'none', | ||
} | ||
} | ||
ref = { this.testEvents } | ||
/> | ||
<table | ||
style = { | ||
{ | ||
padding: 16, | ||
} | ||
} | ||
> | ||
<tr> | ||
<td> | ||
State | ||
</td> | ||
<td> | ||
{ GestureRecognitionState[state] } | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
Last recorded velocity | ||
</td> | ||
<td> | ||
{ velocity.x }, { velocity.y } | ||
</td> | ||
</tr> | ||
</table> | ||
</div> | ||
); | ||
} | ||
} | ||
export default QuickiePointerEventTest; |
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,49 @@ | ||
/** @license | ||
* Copyright 2016 - present The Material Motion Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { | ||
MotionObservable, | ||
Observer, | ||
} from 'material-motion'; | ||
|
||
// Passive event feature detection from | ||
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md | ||
let supportsPassiveListeners = false; | ||
try { | ||
const eventOptions: any = Object.defineProperty({}, 'passive', { | ||
get() { | ||
supportsPassiveListeners = true; | ||
} | ||
}); | ||
window.addEventListener("test", () => {}, eventOptions); | ||
} catch (e) {} | ||
|
||
const eventListenerOptions = supportsPassiveListeners | ||
? { passive: true } | ||
: false; | ||
|
||
export function getEventStreamFromElement(type: string, element: Element): MotionObservable<Event> { | ||
return new MotionObservable( | ||
(observer: Observer<Event>) => { | ||
element.addEventListener(type, observer.next, eventListenerOptions); | ||
|
||
return () => { | ||
element.removeEventListener(type, observer.next, eventListenerOptions); | ||
} | ||
} | ||
); | ||
} | ||
export default getEventStreamFromElement; |
63 changes: 63 additions & 0 deletions
63
packages/views-dom/src/getPointerEventStreamsFromElement.ts
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,63 @@ | ||
/** @license | ||
* Copyright 2016 - present The Material Motion Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { | ||
MotionObservable, | ||
PartialPointerEvent, | ||
} from 'material-motion'; | ||
|
||
import { | ||
convertTouchEventsToPointerEvents, | ||
} from './convertTouchEventsToPointerEvents'; | ||
|
||
import { | ||
getEventStreamFromElement, | ||
} from './getEventStreamFromElement'; | ||
|
||
export type PointerEventStreams = { | ||
down$: MotionObservable<PartialPointerEvent>, | ||
move$: MotionObservable<PartialPointerEvent>, | ||
up$: MotionObservable<PartialPointerEvent>, | ||
}; | ||
|
||
export function getPointerEventStreamsFromElement(element: Element): PointerEventStreams { | ||
if (window.PointerEvent) { | ||
return { | ||
down$: getEventStreamFromElement('pointerdown', element), | ||
move$: getEventStreamFromElement('pointermove', element), | ||
up$: getEventStreamFromElement('pointerup', element), | ||
}; | ||
} else { | ||
return { | ||
down$: getEventStreamFromElement('mousedown', element).merge( | ||
convertTouchEventsToPointerEvents( | ||
getEventStreamFromElement('touchstart', element) | ||
) | ||
), | ||
move$: getEventStreamFromElement('mousemove', element).merge( | ||
convertTouchEventsToPointerEvents( | ||
getEventStreamFromElement('touchmove', element) | ||
) | ||
), | ||
up$: getEventStreamFromElement('mouseup', element).merge( | ||
convertTouchEventsToPointerEvents( | ||
getEventStreamFromElement('touchend', element) | ||
) | ||
), | ||
}; | ||
} | ||
} | ||
export default getPointerEventStreamsFromElement; |
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