Skip to content

Commit

Permalink
fix: adjust tooltip when it is outside (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanho authored Jan 8, 2024
1 parent dd12d83 commit f2fbb86
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('placement', () => {
},
};

global.window = {
global.window ??= {
innerWidth: 500,
innerHeight: 500,
};
Expand Down Expand Up @@ -298,11 +298,11 @@ describe('placement', () => {
expect(r).to.deep.equal({
computedArrowStyle: {
borderWidth: '5px',
left: 'calc(50% - 5px)',
left: 'calc(50% - 28.5px)',
top: '100%',
},
computedTooltipStyle: {
left: '18.5px',
left: '42px',
top: '24px',
transform: 'translate(-50%, -100%) translateY(-5px)',
},
Expand Down
53 changes: 41 additions & 12 deletions packages/picasso.js/src/web/components/tooltip/placement.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,56 @@ function getDockOffset(width, height, offset = 0) {
};
}

function getComputedArrowStyle(offset) {
function getTooltipLeft({ options, docks, dockOffsets, targetBounds, area, width, height }) {
const dock = 'top';
const vx = options.area === 'target' ? docks[dock].x : targetBounds.left + docks[dock].x;
const vy = options.area === 'target' ? docks[dock].y : targetBounds.top + docks[dock].y;
const offset = dockOffsets[dock];
const rect = {
x: vx + offset.x,
y: vy + offset.y,
width,
height,
};

let left = docks.top.x;
if (rect.x < 0 && rect.x + rect.width > area.width) {
return left;
}
if (rect.x < 0) {
left -= rect.x;
} else if (rect.x + rect.width > area.width) {
left -= rect.x + rect.width - area.width;
}
return left;
}

function getComputedArrowStyle(offset, borderWidth) {
const sign = offset > 0 ? '-' : '+';
offset = Math.abs(offset);
if (borderWidth === undefined) {
borderWidth = offset;
}
return {
left: {
left: '100%',
top: `calc(50% - ${offset}px)`,
borderWidth: `${offset}px`,
top: `calc(50% ${sign} ${offset}px)`,
borderWidth: `${borderWidth}px`,
},
right: {
left: `${-offset * 2}px`,
top: `calc(50% - ${offset}px)`,
borderWidth: `${offset}px`,
top: `calc(50% ${sign} ${offset}px)`,
borderWidth: `${borderWidth}px`,
},
top: {
left: `calc(50% - ${offset}px)`,
left: `calc(50% ${sign} ${offset}px)`,
top: '100%',
borderWidth: `${offset}px`,
borderWidth: `${borderWidth}px`,
},
bottom: {
left: `calc(50% - ${offset}px)`,
left: `calc(50% ${sign} ${offset}px)`,
top: `${-offset * 2}px`,
borderWidth: `${offset}px`,
borderWidth: `${borderWidth}px`,
},
inside: {
left: '0px',
Expand Down Expand Up @@ -142,14 +171,14 @@ function alignToBounds({ resources, nodes, pointer, width: elmWidth, height: elm
};
}
}

const left = getTooltipLeft({ options, docks, dockOffsets, targetBounds, area, width: elmWidth, height: elmHeight });
return {
computedTooltipStyle: {
left: `${docks.top.x}px`,
left: `${left}px`,
top: `${docks.top.y}px`,
transform: dockTransforms.top,
},
computedArrowStyle: getComputedArrowStyle(options.offset).top,
computedArrowStyle: getComputedArrowStyle(options.offset + left - docks.top.x, options.offset).top,
dock: 'top',
};
}
Expand Down

0 comments on commit f2fbb86

Please sign in to comment.