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

perf: Anvil widget name component rendering #33672

Merged
merged 7 commits into from
May 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ export function AnvilWidgetName(props: {

let cleanup = () => {};
useEffect(() => {
if (widgetElement && widgetNameComponent && widgetsEditorElement) {
if (
widgetElement &&
widgetNameComponent &&
widgetsEditorElement &&
// Makes sure we add listeners only if the widget is selected or focused
nameComponentState !== "none"
) {
cleanup = handleWidgetUpdate(
widgetElement,
widgetNameComponent,
Expand All @@ -108,6 +114,8 @@ export function AnvilWidgetName(props: {
return null;
// Don't show widget name component if the widget DOM element isn't found
if (!widgetElement) return null;
// Don't render any DOM nodes if the widget is not selected or focused
if (nameComponentState === "none") return null;

return (
<FloatingPortal>
Expand Down
60 changes: 32 additions & 28 deletions app/client/src/layoutSystems/anvil/editor/AnvilWidgetName/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,42 @@ export function handleWidgetUpdate(
widgetsEditorElement: HTMLDivElement,
nameComponentState: NameComponentStates,
) {
return autoUpdate(widgetElement, widgetNameComponent, () => {
computePosition(widgetElement as HTMLDivElement, widgetNameComponent, {
placement: "top-start",
strategy: "fixed",
middleware: [
flip(),
shift(),
offset({ mainAxis: 0, crossAxis: -5 }),
getOverflowMiddleware(widgetsEditorElement as HTMLDivElement),
hide({ strategy: "referenceHidden" }),
hide({ strategy: "escaped" }),
],
}).then(({ middlewareData, x, y }) => {
let shiftOffset = 0;
if (middlewareData.containWithinCanvas.overflowAmount > 0) {
shiftOffset = middlewareData.containWithinCanvas.overflowAmount + 5;
}
return autoUpdate(
widgetElement,
widgetNameComponent,
() => {
computePosition(widgetElement as HTMLDivElement, widgetNameComponent, {
placement: "top-start",
strategy: "fixed",
middleware: [
flip(),
shift(),
offset({ mainAxis: 0, crossAxis: -5 }),
getOverflowMiddleware(widgetsEditorElement as HTMLDivElement),
hide({ strategy: "referenceHidden" }),
hide({ strategy: "escaped" }),
],
}).then(({ middlewareData, x, y }) => {
let shiftOffset = 0;
if (middlewareData.containWithinCanvas.overflowAmount > 0) {
shiftOffset = middlewareData.containWithinCanvas.overflowAmount + 5;
}

Object.assign(widgetNameComponent.style, {
left: `${x - shiftOffset}px`,
top: `${y}px`,
visibility:
nameComponentState === "none" || middlewareData.hide?.referenceHidden
Object.assign(widgetNameComponent.style, {
left: `${x - shiftOffset}px`,
top: `${y}px`,
visibility: middlewareData.hide?.referenceHidden
? "hidden"
: "visible",
zIndex:
nameComponentState === "focus"
? "calc(var(--on-canvas-ui-zindex) + 1)"
: "var(--on-canvas-ui-zindex)",
zIndex:
nameComponentState === "focus"
? "calc(var(--on-canvas-ui-zindex) + 1)"
: "var(--on-canvas-ui-zindex)",
});
});
});
});
},
{ animationFrame: true },
);
}

export function getWidgetNameComponentStyleProps(
Expand Down
Loading