question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prefixing event handlers (a-la onWebkitAnimationEnd)

See original GitHub issue

The following works great for browsers that don’t need a prefix, but fails for browsers that do.

<div onAnimationEnd={this.onAnimationEnd} />

What is the best way to handle this for browsers that need the webkit prefix for this event?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
marvinhagemeistercommented, Dec 5, 2018

You could use a simple helper function to add prefixes:

function addPrefix(name, fn) {
  const renamed = name.charAt(0).toUpperCase() + name.slice(1);
  return {
    [name]: fn,
    ["webkit" + renamed]: fn,
  }
}

// Usage
<div {...addPrefix("onAnimationEnd", this.onAnimationEnd)} />

// output:
<div onAnimationEnd={this.onAnimationEnd} webkitOnAnimationEnd={this.onAnimationEnd} />
1reaction
bartlewiscommented, Dec 7, 2018

Thanks a ton @developit . This is working well.

My final solution looks something like this:

function getAnimationEndEventName() {
  const animations = {
    animation: 'animationend',
    WebkitAnimation: 'webkitAnimationEnd',
  };

  for (let key in animations) {
    if (document.body.style[key] !== undefined) {
      return animations[key];
    }
  }

  return false;
}

const animationEndEventName = getAnimationEndEventName();
if (animationEndEventName !== 'animationend') {
  window.addEventListener(animationEndEventName, e => {
    const event = new Event('animationend', { bubbles: true });
    e.target.dispatchEvent(event);
  });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prefixing event handlers (a-la onWebkitAnimationEnd) #1263
I played with this a little bit tonight, but not seeing any of the prefix extensions fire. Tried all of the following variations....
Read more >
Element: animationend event - Web APIs | MDN
The animationend event is fired when a CSS Animation has completed. ... The same, but using the onanimationend event handler property:.
Read more >
How to handle vendor prefixing for event handlers in Scala.js?
The simplest solution is to polyfill the API with its spec'ed name, redirecting to the vendor-prefixed versions. For example, in a ...
Read more >
XML Events - W3C
Abstract. The XML Events module defined in this specification provides XML languages with the ability to uniformly integrate event listeners ...
Read more >
206170 – onwebkit{animation, transition}XX handlers missing ...
The event handlers are: * onwebkitanimationend * onwebkitanimationiteration ... And does this affect only the prefixed handlers?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found