@ -96,6 +96,10 @@ export function animation(element, get_fn, get_params) {
/** @type {null | { position: string, width: string, height: string, transform: string }} */
/** @type {null | { position: string, width: string, height: string, transform: string }} */
var original _styles = null ;
var original _styles = null ;
/** Captured pre-absolute size, set by `fix_size`, consumed by `fix_position`. */
var frozen _width = '' ;
/** Captured pre-absolute size, set by `fix_size`, consumed by `fix_position`. */
var frozen _height = '' ;
nodes . a ? ? = {
nodes . a ? ? = {
element ,
element ,
@ -128,41 +132,57 @@ export function animation(element, get_fn, get_params) {
) ;
) ;
}
}
} ,
} ,
fix ( ) {
fix _size ( ) {
original _styles = null ;
// If an animation is already running, transforming the element is likely to fail,
// If an animation is already running, transforming the element is likely to fail,
// because the styles applied by the animation take precedence. In the case of crossfade,
// because the styles applied by the animation take precedence. In the case of crossfade,
// that means the `translate(...)` of the crossfade transition overrules the `translate(...)`
// that means the `translate(...)` of the crossfade transition overrules the `translate(...)`
// we would apply below, leading to the element jumping somewhere to the top left.
// we would apply below, leading to the element jumping somewhere to the top left.
if ( element . getAnimations ( ) . length ) return ;
if ( this . element . getAnimations ( ) . length ) return ;
// It's important to destructure these to get fixed values - the object itself has getters,
// It's important to destructure these to get fixed values - the object itself has getters,
// and changing the style to 'absolute' can for example influence the width.
// and changing the style to 'absolute' can for example influence the width. We also have
var { position , width , height } = getComputedStyle ( element ) ;
// to capture this BEFORE any sibling has been mutated, so that the recorded dimensions
// reflect the original layout (matters in flex/grid containers).
if ( position !== 'absolute' && position !== 'fixed' ) {
var { position , width , height } = getComputedStyle ( this . element ) ;
var style = /** @type {HTMLElement | SVGElement} */ ( element ) . style ;
if ( position === 'absolute' || position === 'fixed' ) return ;
original _styles = {
position : style . position ,
var style = /** @type {HTMLElement | SVGElement} */ ( this . element ) . style ;
width : style . width ,
height : style . height ,
original _styles = {
transform : style . transform
position : style . position ,
} ;
width : style . width ,
height : style . height ,
style . position = 'absolute' ;
transform : style . transform
style . width = width ;
} ;
style . height = height ;
frozen _width = width ;
var to = element . getBoundingClientRect ( ) ;
frozen _height = height ;
} ,
if ( from . left !== to . left || from . top !== to . top ) {
fix _position ( ) {
var transform = ` translate( ${ from . left - to . left } px, ${ from . top - to . top } px) ` ;
if ( original _styles === null ) return ;
style . transform = style . transform ? ` ${ style . transform } ${ transform } ` : transform ;
}
var style = /** @type {HTMLElement | SVGElement} */ ( this . element ) . style ;
style . position = 'absolute' ;
style . width = frozen _width ;
style . height = frozen _height ;
} ,
fix _transform ( ) {
if ( original _styles === null ) return ;
var to = this . element . getBoundingClientRect ( ) ;
if ( from . left !== to . left || from . top !== to . top ) {
var style = /** @type {HTMLElement | SVGElement} */ ( this . element ) . style ;
var translate = ` translate( ${ from . left - to . left } px, ${ from . top - to . top } px) ` ;
style . transform = style . transform ? ` ${ style . transform } ${ translate } ` : translate ;
}
}
} ,
} ,
unfix ( ) {
unfix ( ) {
if ( original _styles ) {
if ( original _styles ) {
var style = /** @type {HTMLElement | SVGElement} */ ( element ) . style ;
var style = /** @type {HTMLElement | SVGElement} */ ( this . element ) . style ;
style . position = original _styles . position ;
style . position = original _styles . position ;
style . width = original _styles . width ;
style . width = original _styles . width ;