Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit d63abae

Browse files
authored
Merge pull request #158 from ckeditor/t/157
Other: `getOptimalPosition` utility should accept the target option defined as a function. Closes #157.
2 parents aa3e35d + 01b8684 commit d63abae

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/dom/position.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ import getPositionedAncestor from './getpositionedancestor';
7676
* @returns {module:utils/dom/position~Position}
7777
*/
7878
export function getOptimalPosition( { element, target, positions, limiter, fitInViewport } ) {
79+
// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
80+
// https://github.com/ckeditor/ckeditor5-utils/issues/157
81+
if ( typeof target == 'function' ) {
82+
target = target();
83+
}
84+
7985
const positionedElementAncestor = getPositionedAncestor( element.parentElement );
8086
const elementRect = new Rect( element );
8187
const targetRect = new Rect( target );
@@ -255,7 +261,7 @@ function getAbsoluteRectCoordinates( { left, top } ) {
255261
/**
256262
* Target with respect to which the `element` is to be positioned.
257263
*
258-
* @member {HTMLElement|Range|ClientRect} #target
264+
* @member {HTMLElement|Range|ClientRect|Function} #target
259265
*/
260266

261267
/**

tests/dom/position.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ describe( 'getOptimalPosition()', () => {
5858
} );
5959
} );
6060

61+
it( 'should work when the target is a Function', () => {
62+
setElementTargetPlayground();
63+
64+
assertPosition( {
65+
element,
66+
target: () => target,
67+
positions: [ attachLeft ]
68+
}, {
69+
top: 100,
70+
left: 80,
71+
name: 'left'
72+
} );
73+
} );
74+
6175
describe( 'for single position', () => {
6276
beforeEach( setElementTargetPlayground );
6377

0 commit comments

Comments
 (0)