Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #30 from Laaouatni/bugs-UI-tooltip
Browse files Browse the repository at this point in the history
fix: #29 popup zIndex bug, and it go outside parentDiv
  • Loading branch information
Laaouatni authored Aug 21, 2022
2 parents d6768a3 + 27236ea commit e00cf6b
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 91 deletions.
82 changes: 41 additions & 41 deletions gcode/src/API/GcodeApi/Gsimulator/Gcanvas/Gcanvas.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import Gsimulator from "../Gsimulator.js";
import GcodeAPI from "../../GcodeAPI_main/GcodeAPI.js";
// import Gsimulator from "../Gsimulator.js";
// import GcodeAPI from "../../GcodeAPI_main/GcodeAPI.js";

export default class Gcanvas extends Gsimulator {
constructor(_GcanvasObj) {
super();
// in this case we use || instead of ?? to exclude also 0 from being a size
this.width = _GcanvasObj.width || _GcanvasObj.height || 500;
this.height = _GcanvasObj.height || _GcanvasObj.width || 500;
this.parentHtmlContainer =
_GcanvasObj.parentHtmlContainer ??
document.querySelector("#app") ??
document.body;
this.create();
}
// export default class Gcanvas extends Gsimulator {
// constructor(_GcanvasObj) {
// super();
// // in this case we use || instead of ?? to exclude also 0 from being a size
// this.width = _GcanvasObj.width || _GcanvasObj.height || 500;
// this.height = _GcanvasObj.height || _GcanvasObj.width || 500;
// this.parentHtmlContainer =
// _GcanvasObj.parentHtmlContainer ??
// document.querySelector("#app") ??
// document.body;
// this.create();
// }

create() {
this.canvasElement = document.createElement("canvas");
this.appendCanvasToParent();
this.changeSize();
this.reset();
}
// create() {
// this.canvasElement = document.createElement("canvas");
// this.appendCanvasToParent();
// this.changeSize();
// this.reset();
// }

appendCanvasToParent() {
this.parentHtmlContainer.appendChild(this.canvasElement);
}
// appendCanvasToParent() {
// this.parentHtmlContainer.appendChild(this.canvasElement);
// }

changeSize() {
this.canvasElement.width = this.width;
this.canvasElement.height = this.height;
}
// changeSize() {
// this.canvasElement.width = this.width;
// this.canvasElement.height = this.height;
// }

reset() {
this.ctx = this.canvasElement.getContext("2d");
this.ctx.beginPath();
}
// reset() {
// this.ctx = this.canvasElement.getContext("2d");
// this.ctx.beginPath();
// }

drawLine(_obj) {
this.ctx.lineTo(_obj.x, _obj.y);
this.ctx.stroke();
}
// drawLine(_obj) {
// this.ctx.lineTo(_obj.x, _obj.y);
// this.ctx.stroke();
// }

generate() {
GcodeAPI.array.forEach((pos) => {
this.drawLine(pos);
});
}
}
// generate() {
// GcodeAPI.array.forEach((pos) => {
// this.drawLine(pos);
// });
// }
// }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import updatePopup from "./other/Methods/updatePopup/updatePopup.js";
import UpdatePopup from "./other/Methods/updatePopup/updatePopup.js";
import getPopupSpanHtml from "./other/Methods/getPopupSpanHtml/getPopupSpanHtml.js";
export default class CssFloatPopup {
static popup;
Expand All @@ -18,7 +18,7 @@ export default class CssFloatPopup {
CssFloatPopup.popup.appendChild(CssFloatPopup.arrow);
}

constructor(_obj) {
constructor(_obj, _this) {
this.obj = _obj;

this.button = this.obj.button;
Expand All @@ -27,6 +27,8 @@ export default class CssFloatPopup {
this.arrow = CssFloatPopup.arrow;
this.span = CssFloatPopup.span;

this.parentElement = _this.parentElement;

this.addDefaultStyles();
this.addEvents();
}
Expand All @@ -38,7 +40,7 @@ export default class CssFloatPopup {
showPopup() {
this.popup.style.display = "block";
this.span.innerHTML = getPopupSpanHtml(this);
updatePopup(this);
new UpdatePopup(this).update();
}

addEvents() {
Expand Down Expand Up @@ -70,8 +72,8 @@ export default class CssFloatPopup {
styleArrow() {
this.arrowStylesToAdd = {
position: "absolute",
width: "0.5rem",
height: "0.5rem",
width: "1rem",
height: "1rem",
transform: "rotate(45deg)",
"z-index": -1,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default function calcolatePopupArrowXposition(_x, _this) {
if (_x > _this.parentPosition.x) {
if (
_this.parentPosition.x + _this.parentElement.offsetWidth <
_x + _this.popup.offsetWidth
) {
return {
popup:
_this.parentPosition.x +
_this.parentElement.offsetWidth -
_this.popup.offsetWidth -
_this.padding,
arrow: _this.arrowX,
};
}

return {
popup: _x,
arrow: _this.arrowX,
};
} else if (_x <= _this.parentPosition.x) {
return {
popup: _this.parentPosition.x + _this.padding,
arrow:
_this.arrowX -
(_this.parentPosition.x - _x) +
_this.padding +
_this.radiusPopup,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default function calcolatePopupArrowYposition(_y, _this) {
if (_y > _this.parentPosition.y) {
if (
_this.parentPosition.y + _this.parentElement.offsetHeight <
_y + _this.popup.offsetHeight
) {
return {
popup:
_this.parentPosition.y +
_this.parentElement.offsetHeight -
_this.popup.offsetHeight -
_this.padding,
arrow: _this.arrowY,
};
}

return {
popup: _y,
arrow: _this.arrowY,
};
} else if (_y < _this.parentPosition.y) {
return {
popup: _this.parentPosition.y + _this.padding,
arrow:
_this.arrowY -
(_this.parentPosition.y - _y) +
_this.padding +
_this.radiusPopup
};
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,75 @@
import { computePosition, shift, offset, arrow, autoPlacement } from "@floating-ui/dom";

export default function updatePopup(_this) {
computePosition(_this.button, _this.popup, {
placement: "bottom",
middleware: [
autoPlacement(),
shift({ padding: 8 }),
offset(8),
arrow({ element: _this.arrow }),
],
}).then(({ x, y, placement, middlewareData }) => {
Object.assign(_this.popup.style, {
left: `${x}px`,
top: `${y}px`,
});
import {
computePosition,
shift,
offset,
arrow,
autoPlacement,
} from "@floating-ui/dom";

const { x: arrowX, y: arrowY } = middlewareData.arrow;

const staticSide = {
top: "bottom",
right: "left",
bottom: "top",
left: "right",
}[placement.split("-")[0]];

Object.assign(_this.arrow.style, {
left: arrowX != null ? `${arrowX}px` : "",
top: arrowY != null ? `${arrowY}px` : "",
right: "",
bottom: "",
[staticSide]: "-4px",
});
});
}
import calcolatePopupArrowXposition from "./other/Methods/calcolatePopupArrowPos/x/calcolatePopupArrowXposition.js";
import calcolatePopupArrowYposition from "./other/Methods/calcolatePopupArrowPos/y/calcolatePopupArrowYposition.js";
export default class UpdatePopup {
constructor(_this) {
this.button = _this.button;
this.popup = _this.popup;
this.arrow = _this.arrow;

this.parentElement = _this.parentElement;

this.parentPosition = {
x: this.parentElement.getBoundingClientRect().x,
y: this.parentElement.getBoundingClientRect().y,
};

this.padding = 8;
this.radiusPopup = window.getComputedStyle(this.popup).borderRadius.split("px")[0] ?? 0;
}

update() {
computePosition(this.button, this.popup, {
middleware: [
autoPlacement(),
shift({ padding: this.padding }),
offset(this.padding),
arrow({ element: this.arrow }),
],
}).then(({ x, y, placement, middlewareData }) => {
({ x: this.arrowX, y: this.arrowY } = middlewareData.arrow);

this.choosedPos = this.calcolatePosition(x, y);

Object.assign(this.popup.style, {
left: `${this.choosedPos.popup.x}px`,
top: `${this.choosedPos.popup.y}px`,
});

this.staticSide = {
top: "bottom",
right: "left",
bottom: "top",
left: "right",
}[placement.split("-")[0]];

Object.assign(this.arrow.style, {
left: this.arrowX != null ? `${this.choosedPos.arrow.x}px` : "",
top: this.arrowY != null ? `${this.choosedPos.arrow.y}px` : "",
right: "",
bottom: "",
[this.staticSide]: `${this.padding * -1}px`,
});
});
}

calcolatePosition(_x, _y) {
return {
popup: {
x: calcolatePopupArrowXposition(_x, this).popup,
y: calcolatePopupArrowYposition(_y, this).popup,
},
arrow: {
x: calcolatePopupArrowXposition(_x, this).arrow,
y: calcolatePopupArrowYposition(_y, this).arrow,
},
};
}
}
9 changes: 5 additions & 4 deletions gcode/src/API/GcodeApi/Gsimulator/Gcss/CssLine/CssLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CssLineLength from "../other/Methods/CssLineLength/CssLineLength.js";
import CssLineAngle from "../other/Methods/CssLineAngle/CssLineAngle.js";
import CssFloatPopup from "../CssFloatPopup/CssFloatPopup.js";
export default class CssLine {
constructor(_CurrentObj, _index) {
constructor(_CurrentObj, _index, _this) {
this.index = _index;
this.lineElement = document.createElement("div");

Expand All @@ -26,15 +26,16 @@ export default class CssLine {
};

this.lineLength = this.calcolateLength();
this.lineHeight = this.calcolateHeight(0.2); // 0.2rem
this.lineHeight = this.calcolateHeight(0.4); // 0.2rem
this.lineAngle = this.calcolateAngle();

this.styleLine();

this.parentElement = _this.parentHtmlContainer;

this.createPopup({
button: this.lineElement,
});

}

calcolateLength() {
Expand Down Expand Up @@ -84,6 +85,6 @@ export default class CssLine {
}

createPopup(_obj) {
this.popupClass = new CssFloatPopup(_obj);
this.popupClass = new CssFloatPopup(_obj, this);
}
}
2 changes: 1 addition & 1 deletion gcode/src/API/GcodeApi/Gsimulator/Gcss/Gcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Gcss extends Gsimulator {
}

drawLine(_CurrentObj, _index) {
this.CssLineClass = new CssLine(_CurrentObj, _index);
this.CssLineClass = new CssLine(_CurrentObj, _index, this);

this.cssContainer.appendChild(this.CssLineClass.lineElement);
}
Expand Down
1 change: 0 additions & 1 deletion gcode/src/CSS/FloatingUI/arrow/arrow.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#arrow {
border: 0.1rem solid var(--sky-200);
background-color: var(--sky-50);
border-radius: 0.2rem;
}
3 changes: 2 additions & 1 deletion gcode/src/CSS/FloatingUI/tooltip/tooltip.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
border: 0.1rem solid var(--sky-200);
background-color: var(--sky-50);
border-radius: 0.5rem;
box-shadow: 0 0.5rem 1rem var(--sky-200);
box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.2);
z-index: 9999;
}
4 changes: 2 additions & 2 deletions gcode/src/CSS/Gline/Gline_before/Gline_before.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
}

.Gline::before {
font-size: calc(var(--line-height) * 2.5);
font-size: calc(var(--line-height) * 1.5);
font-family: monospace;
}

.Gline::before {
--before-width: calc(var(--line-height) * 3.5);
--before-width: calc(var(--line-height) * 2.5);
width: var(--before-width);
height: var(--before-width);
border-radius: 50%;
Expand Down
2 changes: 1 addition & 1 deletion gcode/src/CSS/Gline/Gline_hover/Gline_hover.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.Gline:hover::before {
transform: scale(2) translateY(-25%);
zoom: 1.5;
}

.Gline:hover {
Expand Down
2 changes: 1 addition & 1 deletion gcode/src/CSS/Gline/Gline_main/Gline_main.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}

.Gline {
transform-origin: top left;
transform-origin: bottom left;
top: var(--line-top);
left: var(--line-left);
transform: rotate(var(--line-rotate));
Expand Down

1 comment on commit e00cf6b

@vercel
Copy link

@vercel vercel bot commented on e00cf6b Aug 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gcode-js – ./

gcode-js.vercel.app
gcode-js-git-main-laaouatni.vercel.app
gcode-js-laaouatni.vercel.app

Please sign in to comment.