Skip to content

Commit

Permalink
feat: support 3 options to locale the tooltip container
Browse files Browse the repository at this point in the history
  • Loading branch information
危翰 committed Mar 31, 2023
1 parent e188b20 commit 92bcf21
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/component/tooltip/TooltipHTMLContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,13 @@ function makeStyleCoord(out: number[], zr: ZRenderType, customContainer: HTMLEle

interface TooltipContentOption {
/**
* `false`: the DOM element will be inside the container. Default value.
* `true`: the DOM element will be appended to HTML body, which avoid
* some overflow clip but intrude outside of the container.
* Choose a DOM element which the tooltip element will be located in order to
* avoid some overflow clip but intrude outside of the container.
*
* this config can be either a DomElement, a function to choose a element
* or a selector string used by query delector to local a element
*/
appendToBody: boolean
appendTo: Function | HTMLElement | string
}

class TooltipHTMLContent {
Expand Down Expand Up @@ -292,10 +294,14 @@ class TooltipHTMLContent {
const zr = this._zr = api.getZr();

let container: HTMLElement | null = null;
if (opt && opt.appendToBody) {
container = this._container = document.body
} else if (opt && opt.teleport) {
container = this._customContainer = opt.teleport(api.getDom()) || null;
if (opt && opt.appendTo) {
if(typeof opt.appendTo === 'string') {
container = document.querySelector(opt.appendTo)
} else if (typeof opt.appendTo === 'function') {
container = opt.appendTo(api.getDom())
} else if (opt.appendTo instanceof HTMLElement) {
container = opt.appendTo
}
}

makeStyleCoord(this._styleCoord, zr, container, api.getWidth() / 2, api.getHeight() / 2);
Expand Down
3 changes: 1 addition & 2 deletions src/component/tooltip/TooltipView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ class TooltipView extends ComponentView {
this._tooltipContent = renderMode === 'richText'
? new TooltipRichContent(api)
: new TooltipHTMLContent(api, {
getAppendElement: tooltipModel.get('getAppendElement', true),
appendToBody: tooltipModel.get('appendToBody', true)
appendTo: tooltipModel.get('appendToBody', true) ? 'body' : tooltipModel.get('getAppendElement', true),
});
}

Expand Down

0 comments on commit 92bcf21

Please sign in to comment.