Skip to content

Commit 7b2f626

Browse files
authored
feat: Support padding in transitionEnd (#96)
* feat: Support padding in transitionEnd * fix tests
1 parent b37aa27 commit 7b2f626

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ require('dom-helpers/css')(node, { width: '40px' })
5959
- `style(element, propName)` or `style(element, objectOfPropValues)`
6060
- `getComputedStyle(element)` -> `getPropertyValue(name)`
6161
- `animate(node, properties, duration, easing, callback)` programmatically start css transitions
62-
- `transitionEnd(node, handler, [duration])` listens for transition end, and ensures that the handler if called even if the transition fails to fire its end event. Will attempt to read duration from the element, otherwise one can be provided
62+
- `transitionEnd(node, handler, [duration], [padding])` listens for transition end, and ensures that the handler if called even if the transition fails to fire its end event. Will attempt to read duration from the element, otherwise one can be provided
6363
- `addEventListener(node, eventName, handler, [options])`:
6464
- `removeEventListener(node, eventName, handler, [options])`:
6565
- `listen(node, eventName, handler, [options])`: wraps `addEventlistener` and returns a function that calls `removeEventListener` for you

src/transitionEnd.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import canUseDOM from './canUseDOM'
21
import css from './css'
32
import listen from './listen'
43

54
export type Listener = (this: HTMLElement, ev: TransitionEvent) => any
65

7-
export const TRANSITION_SUPPORTED = canUseDOM && 'ontransitionend' in window
8-
9-
export function parseDuration(node: HTMLElement) {
6+
function parseDuration(node: HTMLElement) {
107
const str = css(node, 'transitionDuration') || ''
118

129
const mult = str.indexOf('ms') === -1 ? 1000 : 1
@@ -45,20 +42,19 @@ function emulateTransitionEnd(
4542
}
4643
}
4744

48-
function transitionEnd(
45+
export default function transitionEnd(
4946
element: HTMLElement,
5047
handler: Listener,
51-
duration?: number
48+
duration?: number | null,
49+
padding?: number
5250
) {
5351
if (duration == null) duration = parseDuration(element) || 0
54-
const removeEmulate = emulateTransitionEnd(element, duration)
52+
const removeEmulate = emulateTransitionEnd(element, duration, padding)
5553

56-
const remove = listen(element, 'transitionend', handler);
54+
const remove = listen(element, 'transitionend', handler)
5755

5856
return () => {
5957
removeEmulate()
6058
remove()
6159
}
6260
}
63-
64-
export default transitionEnd

test/transition.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
const { parseDuration } = require('../src/transitionEnd')
1+
const transitionEnd = require('../src/transitionEnd')
2+
3+
describe('transitionEnd', () => {
4+
let clock
5+
6+
beforeEach(() => {
7+
clock = sinon.useFakeTimers()
8+
})
9+
10+
afterEach(() => {
11+
clock.restore()
12+
})
213

3-
describe('Transition helpers', () => {
414
it('should parse duration from node property', () => {
515
const el = document.createElement('div')
616

717
el.style.transitionDuration = '1.4s'
18+
const handler1 = sinon.spy()
19+
transitionEnd(el, handler1)
20+
21+
clock.tick(1300)
22+
expect(handler1.callCount).to.equal(0)
23+
expect(handler1).to.not.be.called
824

9-
expect(parseDuration(el)).to.equal(1400)
25+
clock.tick(200)
26+
expect(handler1.callCount).to.equal(1)
1027

1128
el.style.transitionDuration = '500ms'
29+
const handler2 = sinon.spy()
30+
transitionEnd(el, handler2)
31+
32+
clock.tick(400)
33+
expect(handler2.callCount).to.equal(0)
1234

13-
expect(parseDuration(el)).to.equal(500)
35+
clock.tick(200)
36+
expect(handler2.callCount).to.equal(1)
1437
})
1538
})

0 commit comments

Comments
 (0)