press.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { press, frame } from 'motion-dom';
  2. import { extractEventInfo } from '../events/event-info.mjs';
  3. import { Feature } from '../motion/features/Feature.mjs';
  4. function handlePressEvent(node, event, lifecycle) {
  5. const { props } = node;
  6. if (node.current instanceof HTMLButtonElement && node.current.disabled) {
  7. return;
  8. }
  9. if (node.animationState && props.whileTap) {
  10. node.animationState.setActive("whileTap", lifecycle === "Start");
  11. }
  12. const eventName = ("onTap" + (lifecycle === "End" ? "" : lifecycle));
  13. const callback = props[eventName];
  14. if (callback) {
  15. frame.postRender(() => callback(event, extractEventInfo(event)));
  16. }
  17. }
  18. class PressGesture extends Feature {
  19. mount() {
  20. const { current } = this.node;
  21. if (!current)
  22. return;
  23. this.unmount = press(current, (_element, startEvent) => {
  24. handlePressEvent(this.node, startEvent, "Start");
  25. return (endEvent, { success }) => handlePressEvent(this.node, endEvent, success ? "End" : "Cancel");
  26. }, { useGlobalTarget: this.node.props.globalTapTarget });
  27. }
  28. unmount() { }
  29. }
  30. export { PressGesture };