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

feat(tooltip): add appendTo option to allow customizing tooltip container #18436

Merged
merged 19 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/component/tooltip/TooltipHTMLContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ function assembleCssText(tooltipModel: Model<TooltipOption>, enableTransition?:
}

// If not able to make, do not modify the input `out`.
function makeStyleCoord(out: number[], zr: ZRenderType, appendToBody: boolean, zrX: number, zrY: number) {
function makeStyleCoord(out: number[], zr: ZRenderType, customContainer: HTMLElement | null, zrX: number, zrY: number) {
const zrPainter = zr && zr.painter;

if (appendToBody) {
if (customContainer) {
const zrViewportRoot = zrPainter && zrPainter.getViewportRoot();
if (zrViewportRoot) {
// Some APPs might use scale on body, so we support CSS transform here.
transformLocalCoord(out, zrViewportRoot, document.body, zrX, zrY);
transformLocalCoord(out, zrViewportRoot, customContainer, zrX, zrY);
}
}
else {
Expand Down Expand Up @@ -257,7 +257,7 @@ class TooltipHTMLContent {
private _show: boolean = false;

private _styleCoord: [number, number, number, number] = [0, 0, 0, 0];
private _appendToBody: boolean;
private _customContainer: HTMLElement | null;

private _enterable = true;
private _zr: ZRenderType;
Expand Down Expand Up @@ -291,14 +291,21 @@ class TooltipHTMLContent {
(el as any).domBelongToZr = true;
this.el = el;
const zr = this._zr = api.getZr();
const appendToBody = this._appendToBody = opt && opt.appendToBody;

makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2);

if (appendToBody) {
document.body.appendChild(el);
let customContainer: HTMLElement | null
if (opt && opt.appendToBody) {
customContainer = this._customContainer = document.body
} else if (opt && opt.getAppendElement) {
viking7982 marked this conversation as resolved.
Show resolved Hide resolved
customContainer = this._customContainer = opt.getAppendElement(container) || null;
} else {
customContainer = this._customContainer = null
}
else {

makeStyleCoord(this._styleCoord, zr, customContainer, api.getWidth() / 2, api.getHeight() / 2);

if (customContainer) {
customContainer.appendChild(el);
} else {
container.appendChild(el);
}

Expand Down Expand Up @@ -456,7 +463,7 @@ class TooltipHTMLContent {

moveTo(zrX: number, zrY: number) {
const styleCoord = this._styleCoord;
makeStyleCoord(styleCoord, this._zr, this._appendToBody, zrX, zrY);
makeStyleCoord(styleCoord, this._zr, this._customContainer, zrX, zrY);

if (styleCoord[0] != null && styleCoord[1] != null) {
const style = this.el.style;
Expand Down
3 changes: 2 additions & 1 deletion src/component/tooltip/TooltipView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ class TooltipView extends ComponentView {
this._tooltipContent = renderMode === 'richText'
? new TooltipRichContent(api)
: new TooltipHTMLContent(api.getDom(), api, {
appendToBody: tooltipModel.get('appendToBody', true)
getAppendElement: tooltipModel.get('getAppendElement', true),
appendToBody: tooltipModel.get('appendToBody', true)
});
}

Expand Down