Skip to content

Commit 14013ef

Browse files
authored
fix: do not override position in the controller (#4654)
1 parent 7d29e8b commit 14013ef

6 files changed

Lines changed: 53 additions & 25 deletions

File tree

‎integration/tests/component-tooltip.test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ import { mouseenter, mouseleave } from '@vaadin/tooltip/test/helpers.js';
9292
expect(tooltip.target).to.equal(target);
9393
});
9494

95-
it('should set tooltip position', () => {
96-
expect(tooltip.position).to.equal(position || 'bottom');
95+
it('should set tooltip overlay position', () => {
96+
expect(tooltipOverlay.position).to.equal(position || 'bottom');
9797
});
9898

9999
it('should or should not show tooltip', () => {

‎packages/component-base/src/tooltip-controller.d.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export class TooltipController extends SlotController {
6767
setOpened(opened: boolean): void;
6868

6969
/**
70-
* Set position on the slotted tooltip.
70+
* Set default position for the slotted tooltip.
71+
* This can be overridden by setting the position
72+
* using corresponding property or attribute.
7173
*/
7274
setPosition(position: TooltipPosition): void;
7375

‎packages/component-base/src/tooltip-controller.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class TooltipController extends SlotController {
3939
}
4040

4141
if (this.position !== undefined) {
42-
this.__updatePosition(tooltipNode, this.position);
42+
tooltipNode._position = this.position;
4343
}
4444

4545
if (this.shouldShow !== undefined) {
@@ -87,13 +87,18 @@ export class TooltipController extends SlotController {
8787
}
8888

8989
/**
90-
* Set position on the slotted tooltip.
90+
* Set default position for the slotted tooltip.
91+
* This can be overridden by setting the position
92+
* using corresponding property or attribute.
9193
* @param {string} position
9294
*/
9395
setPosition(position) {
9496
this.position = position;
9597

96-
this.__updatePosition(this.node, position);
98+
const tooltipNode = this.node;
99+
if (tooltipNode) {
100+
tooltipNode._position = position;
101+
}
97102
}
98103

99104
/**
@@ -122,11 +127,4 @@ export class TooltipController extends SlotController {
122127
tooltipNode.target = target;
123128
}
124129
}
125-
126-
/** @private */
127-
__updatePosition(node, position) {
128-
if (node && !node.hasAttribute('position')) {
129-
node.position = position;
130-
}
131-
}
132130
}

‎packages/component-base/test/tooltip-controller.test.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ describe('TooltipController', () => {
7070
expect(tooltip.shouldShow).to.be.eql(shouldShow);
7171
});
7272

73-
it('should update tooltip position using controller setPosition method', () => {
73+
it('should update tooltip _position using controller setPosition method', () => {
7474
controller.setPosition('top-start');
75-
expect(tooltip.position).to.eql('top-start');
75+
expect(tooltip._position).to.eql('top-start');
7676
});
7777

78-
it('should not override position attribute using controller setPosition method', () => {
79-
tooltip.setAttribute('position', 'end');
78+
it('should not set position property using controller setPosition method', () => {
8079
controller.setPosition('top-start');
8180
expect(tooltip.position).to.not.eql('top-start');
8281
});
@@ -156,13 +155,13 @@ describe('TooltipController', () => {
156155
expect(tooltip.shouldShow).to.be.eql(shouldShow);
157156
});
158157

159-
it('should update lazy tooltip position using controller setPosition method', async () => {
158+
it('should update lazy tooltip _position using controller setPosition method', async () => {
160159
controller.setPosition('top-start');
161160

162161
host.appendChild(tooltip);
163162
await nextFrame();
164163

165-
expect(tooltip.position).to.eql('top-start');
164+
expect(tooltip._position).to.eql('top-start');
166165
});
167166
});
168167
});

‎packages/tooltip/src/vaadin-tooltip.js‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ class Tooltip extends ThemePropertyMixin(ElementMixin(PolymerElement)) {
8484
theme$="[[_theme]]"
8585
opened="[[__computeOpened(manual, opened, _autoOpened, _isConnected)]]"
8686
position-target="[[target]]"
87-
position="[[position]]"
88-
no-horizontal-overlap$="[[__computeNoHorizontalOverlap(position)]]"
89-
no-vertical-overlap$="[[__computeNoVerticalOverlap(position)]]"
90-
horizontal-align="[[__computeHorizontalAlign(position)]]"
91-
vertical-align="[[__computeVerticalAlign(position)]]"
87+
position="[[__effectivePosition]]"
88+
no-horizontal-overlap$="[[__computeNoHorizontalOverlap(__effectivePosition)]]"
89+
no-vertical-overlap$="[[__computeNoVerticalOverlap(__effectivePosition)]]"
90+
horizontal-align="[[__computeHorizontalAlign(__effectivePosition)]]"
91+
vertical-align="[[__computeVerticalAlign(__effectivePosition)]]"
9292
on-mouseleave="__onOverlayMouseLeave"
9393
modeless
9494
></vaadin-tooltip-overlay>
@@ -173,7 +173,6 @@ class Tooltip extends ThemePropertyMixin(ElementMixin(PolymerElement)) {
173173
*/
174174
position: {
175175
type: String,
176-
value: 'bottom',
177176
},
178177

179178
/**
@@ -228,6 +227,21 @@ class Tooltip extends ThemePropertyMixin(ElementMixin(PolymerElement)) {
228227
observer: '__autoOpenedChanged',
229228
},
230229

230+
/**
231+
* Default value used when `position` property is not set.
232+
* @protected
233+
*/
234+
_position: {
235+
type: String,
236+
value: 'bottom',
237+
},
238+
239+
/** @private */
240+
__effectivePosition: {
241+
type: String,
242+
computed: '__computePosition(position, _position)',
243+
},
244+
231245
/** @protected */
232246
_overlayElement: Object,
233247

@@ -341,6 +355,11 @@ class Tooltip extends ThemePropertyMixin(ElementMixin(PolymerElement)) {
341355
return connected && (manual ? opened : autoOpened);
342356
}
343357

358+
/** @private */
359+
__computePosition(position, defaultPosition) {
360+
return position || defaultPosition;
361+
}
362+
344363
/** @private */
345364
__tooltipRenderer(root) {
346365
root.textContent = typeof this.generator === 'function' ? this.generator(this.context) : this.text;

‎packages/tooltip/test/tooltip-position.test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ describe('position', () => {
8484
expect(overlayRect.top).to.be.equal(targetRect.top + offset);
8585
}
8686

87+
describe('default', () => {
88+
it('should not set position property value by default', () => {
89+
expect(tooltip.position).to.be.undefined;
90+
});
91+
92+
it('should set overlay position to bottom by default', () => {
93+
expect(overlay.position).to.be.equal('bottom');
94+
});
95+
});
96+
8797
describe('top-start', () => {
8898
beforeEach(() => {
8999
tooltip.position = 'top-start';

0 commit comments

Comments
 (0)