Skip to content

Instantly share code, notes, and snippets.

@aaroniker
Last active January 15, 2024 21:09
Show Gist options
  • Save aaroniker/418467cccfb23b9e2da9d17f5d5a54a1 to your computer and use it in GitHub Desktop.
Save aaroniker/418467cccfb23b9e2da9d17f5d5a54a1 to your computer and use it in GitHub Desktop.
Figma Plugin resize snippets
// Add element to markup
<div id="resize">
<svg viewBox="0 0 20 20">
<path d="...icon..." />
</svg>
</div>
// Styling
#resize {
position: absolute;
bottom: 0;
right: 0;
cursor: nwse-resize;
}
// Register event listeners
const resize = document.getElementById(“resize”);
resize.addEventListener("pointerdown", (e) => {
resize.addEventListener("pointermove", handleResize);
resize.setPointerCapture(e.pointerId);
});
resize.addEventListener("pointerup", (e) => {
resize.removeEventListener("pointermove", handleResize);
resize.releasePointerCapture(e.pointerId);
});
// Send the size to the code
const handleResize = (e: PointerEvent) => {
const size = {
width: Math.max(50, Math.floor(e.clientX + 5)),
height: Math.max(50, Math.floor(e.clientY + 5)),
};
parent.postMessage({ pluginMessage: { type: "resize", size } }, "*");
};
// Default size
const size = { width: 400, height: 400 };
// Get size if available
async function init() {
const { width, height } =
(await figma.clientStorage.getAsync('size')) ?? size;
figma.showUI(__html__, {
width,
height,
});
}
// Call the function to show the plugin UI
init();
// Update size based on resizing & save it
figma.ui.onmessage = (response) => {
if (response.type === 'resize') {
figma.ui.resize(response.size.width, response.size.height);
figma.clientStorage.setAsync('size', response.size);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment