@@ -18,6 +18,8 @@ var Ripple = {
1818
1919 function rippler ( event , el ) {
2020 var target = el ;
21+ // Get border to avoid offsetting on ripple container position
22+ var targetBorder = parseInt ( ( getComputedStyle ( target ) . borderWidth ) . replace ( 'px' , '' ) ) ;
2123
2224 // Get necessary variables
2325 var rect = target . getBoundingClientRect ( ) ,
@@ -30,11 +32,14 @@ var Ripple = {
3032 maxX = Math . max ( dx , width - dx ) ,
3133 maxY = Math . max ( dy , height - dy ) ,
3234 style = window . getComputedStyle ( target ) ,
33- radius = Math . sqrt ( ( maxX * maxX ) + ( maxY * maxY ) ) ;
35+ radius = Math . sqrt ( ( maxX * maxX ) + ( maxY * maxY ) ) ,
36+ border = ( targetBorder > 0 ) ? targetBorder : 0 ;
3437
3538 // Create the ripple and its container
3639 var ripple = document . createElement ( "div" ) ,
3740 rippleContainer = document . createElement ( "div" ) ;
41+ rippleContainer . className = 'ripple-container' ;
42+ ripple . className = 'ripple' ;
3843
3944 //Styles for ripple
4045 ripple . style . marginTop = '0px' ;
@@ -50,21 +55,29 @@ var Ripple = {
5055
5156 //Styles for rippleContainer
5257 rippleContainer . style . position = 'absolute' ;
53- rippleContainer . style . left = '0 ';
54- rippleContainer . style . top = '0 ';
58+ rippleContainer . style . left = 0 - border + 'px ';
59+ rippleContainer . style . top = 0 - border + 'px ';
5560 rippleContainer . style . height = '0' ;
5661 rippleContainer . style . width = '0' ;
5762 rippleContainer . style . pointerEvents = 'none' ;
5863 rippleContainer . style . overflow = 'hidden' ;
5964
65+ // Store target position to change it after
66+ var storedTargetPosition = ( ( target . style . position ) . length > 0 ) ? target . style . position : getComputedStyle ( target ) . position ;
67+ // Change target position to relative to guarantee ripples correct positioning
68+ if ( storedTargetPosition !== 'relative' ) {
69+ target . style . position = 'relative' ;
70+ }
71+
6072 rippleContainer . appendChild ( ripple ) ;
61- document . body . appendChild ( rippleContainer ) ;
73+ target . appendChild ( rippleContainer ) ;
6274
6375 ripple . style . marginLeft = dx + "px" ;
6476 ripple . style . marginTop = dy + "px" ;
6577
66- rippleContainer . style . left = left + ( ( ( window . pageXOffset || document . scrollLeft ) - ( document . clientLeft || 0 ) ) || 0 ) + "px" ;
67- rippleContainer . style . top = top + ( ( ( window . pageYOffset || document . scrollTop ) - ( document . clientTop || 0 ) ) || 0 ) + "px" ;
78+ // No need to set positioning because ripple should be child of target and to it's relative position.
79+ // rippleContainer.style.left = left + (((window.pageXOffset || document.scrollLeft) - (document.clientLeft || 0)) || 0) + "px";
80+ // rippleContainer.style.top = top + (((window.pageYOffset || document.scrollTop) - (document.clientTop || 0)) || 0) + "px";
6881 rippleContainer . style . width = width + "px" ;
6982 rippleContainer . style . height = height + "px" ;
7083 rippleContainer . style . borderTopLeftRadius = style . borderTopLeftRadius ;
@@ -92,6 +105,27 @@ var Ripple = {
92105 } , 850 ) ;
93106
94107 el . removeEventListener ( 'mouseup' , clearRipple , false ) ;
108+
109+ // After removing event set position to target to it's original one
110+ // Timeout it's needed to avoid jerky effect of ripple jumping out parent target
111+ setTimeout ( function ( ) {
112+
113+ var clearPosition = true ;
114+ for ( var i = 0 ; i < target . childNodes . length ; i ++ ) {
115+ if ( target . childNodes [ i ] . className === 'ripple-container' ) {
116+ clearPosition = false ;
117+ }
118+ }
119+
120+ if ( clearPosition ) {
121+ if ( storedTargetPosition !== 'static' ) {
122+ target . style . position = storedTargetPosition ;
123+ } else {
124+ target . style . position = '' ;
125+ }
126+ }
127+
128+ } , props . transition + 250 )
95129 }
96130
97131 if ( event . type === 'mousedown' ) {
0 commit comments