Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resizable: Fix content shrink on resize #2281

Merged
merged 9 commits into from
Sep 9, 2024
53 changes: 53 additions & 0 deletions tests/unit/resizable/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,57 @@ QUnit.test( "nested resizable", function( assert ) {
outer.remove();
} );

QUnit.test( "Resizable with scrollbars and box-sizing: border-box", function( assert ) {
Daniel-Garmig marked this conversation as resolved.
Show resolved Hide resolved
assert.expect( 2 );

var elementContent = $( "<div>" )
.css( {
width: 50,
height: 200,
padding: 10,
Daniel-Garmig marked this conversation as resolved.
Show resolved Hide resolved
border: 5,
borderStyle: "solid"
} )
.appendTo( "#resizable1" ),
element = $( "#resizable1" ).css( { overflow: "auto" } ).resizable(),
Daniel-Garmig marked this conversation as resolved.
Show resolved Hide resolved
handle = ".ui-resizable-se";

$( "*" ).css( "box-sizing", "border-box" );
Daniel-Garmig marked this conversation as resolved.
Show resolved Hide resolved

testHelper.drag( handle, 10, 10 );
assert.equal( element.width(), 110, "element width" );
assert.equal( element.height(), 110, "element height" );

elementContent.remove();
} );

QUnit.test( "Resizable with scrollbars and box-sizing: content-box", function( assert ) {
assert.expect( 2 );

var elementContent = $( "<div>" )
.css( {
width: 50,
height: 200,
padding: 10,
border: 5,
borderStyle: "solid"
} )
.appendTo( "#resizable1" ),
element = $( "#resizable1" ).css( { overflow: "auto" } ).resizable(),
handle = ".ui-resizable-se";

$( "*" ).css( "box-sizing", "content-box" );

// In some browsers scrollbar may change element size (when "box-sizing: content-box")
var widthBefore = element.innerWidth();
var heightBefore = element.innerHeight();

testHelper.drag( handle, 10, 10 );

assert.equal( parseFloat( element.innerWidth() ), widthBefore + 10, "element width" );
assert.equal( parseFloat( element.innerHeight() ), heightBefore + 10, "element height" );

elementContent.remove();
} );

} );
58 changes: 58 additions & 0 deletions tests/unit/resizable/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,62 @@ QUnit.test( "alsoResize with box-sizing: border-box", function( assert ) {
assert.equal( parseFloat( other.css( "height" ) ), 130, "alsoResize height" );
} );

QUnit.test( "alsoResize with scrollbars and box-sizing: border-box", function( assert ) {
assert.expect( 4 );

var other = $( "<div>" )
.css( {
width: 150,
height: 150,
padding: 10,
border: 5,
borderStyle: "solid",
overflow: "scroll"
} )
.appendTo( "body" ),
element = $( "#resizable1" ).resizable( {
alsoResize: other
} ),
handle = ".ui-resizable-se";


$( "*" ).css( "box-sizing", "border-box" );
testHelper.drag( handle, 80, 80 );

assert.equal( element.width(), 180, "resizable width" );
assert.equal( parseFloat( other.css( "width" ) ), 230, "alsoResize width" );
assert.equal( element.height(), 180, "resizable height" );
assert.equal( parseFloat( other.css( "height" ) ), 230, "alsoResize height" );
} );

QUnit.test( "alsoResize with scrollbars and box-sizing: content-box", function( assert ) {
assert.expect( 4 );

var other = $( "<div>" )
.css( {
width: 150,
height: 150,
overflow: "scroll"
} )
.appendTo( "body" ),
element = $( "#resizable1" ).resizable( {
alsoResize: other
} ),
handle = ".ui-resizable-se";

$( "*" ).css( "box-sizing", "content-box" );

// In some browsers scrollbar may change element size.
var widthBefore = other.innerWidth();
var heightBefore = other.innerHeight();

testHelper.drag( handle, 80, 80 );

assert.equal( element.width(), 180, "resizable width" );
assert.equal( parseFloat( other.innerWidth() ), widthBefore + 80, "alsoResize width" );
assert.equal( element.height(), 180, "resizable height" );
assert.equal( parseFloat( other.innerHeight() ), heightBefore + 80, "alsoResize height" );
} );


} );
30 changes: 21 additions & 9 deletions ui/widgets/resizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,12 @@ $.widget( "ui.resizable", $.ui.mouse, {
this.size = this._helper ? {
width: this.helper.width(),
height: this.helper.height()
} : {
width: el.width(),
height: el.height()
};
} : this._calculateAdjustedElementDimensions( el );

this.originalSize = this._helper ? {
width: el.outerWidth(),
height: el.outerHeight()
} : {
width: el.width(),
height: el.height()
};
} : this._calculateAdjustedElementDimensions( el );

this.sizeDiff = {
width: el.outerWidth() - el.width(),
Expand Down Expand Up @@ -690,6 +684,21 @@ $.widget( "ui.resizable", $.ui.mouse, {
};
},

_calculateAdjustedElementDimensions: function( element ) {
if ( !( /content-box/ ).test( element.css( "box-sizing" ) ) ) {
Daniel-Garmig marked this conversation as resolved.
Show resolved Hide resolved
return {
height: parseFloat( element.css( "height" ) ),
width: parseFloat( element.css( "width" ) )
};
}

var outerDimensions = this._getPaddingPlusBorderDimensions( element );
return {
height: element[ 0 ].getBoundingClientRect().height - outerDimensions.height,
width: element[ 0 ].getBoundingClientRect().width - outerDimensions.width
Daniel-Garmig marked this conversation as resolved.
Show resolved Hide resolved
};
},

_proportionallyResize: function() {

if ( !this._proportionallyResizeElements.length ) {
Expand Down Expand Up @@ -1045,8 +1054,11 @@ $.ui.plugin.add( "resizable", "alsoResize", {

$( o.alsoResize ).each( function() {
var el = $( this );

var elSize = that._calculateAdjustedElementDimensions( el );

el.data( "ui-resizable-alsoresize", {
width: parseFloat( el.css( "width" ) ), height: parseFloat( el.css( "height" ) ),
width: elSize.width, height: elSize.height,
left: parseFloat( el.css( "left" ) ), top: parseFloat( el.css( "top" ) )
} );
} );
Expand Down