I'll help you create a starter code for a Figma plugin. Here's a step-by-step setup:
- First, create a new directory and initialize the project:
mkdir my-figma-plugin
cd my-figma-plugin
npm init -y
- Create the following file structure:
my-figma-plugin/
├── code.ts
├── ui.html
├── manifest.json
└── package.json
- Here's the basic
manifest.json:
{
"name": "My Figma Plugin",
"id": "your-plugin-id",
"api": "1.0.0",
"main": "code.js",
"ui": "ui.html",
"editorType": ["figma"],
"networkAccess": {
"allowedDomains": ["none"]
}
}
- Basic
code.ts:
figma.showUI(__html__);
figma.ui.onmessage = msg => {
if (msg.type === 'create-shapes') {
const nodes: SceneNode[] = [];
const rect = figma.createRectangle();
rect.x = 150;
rect.y = 150;
rect.fills = [{type: 'SOLID', color: {r: 1, g: 0, b: 0}}];
nodes.push(rect);
figma.currentPage.selection = nodes;
figma.viewport.scrollAndZoomIntoView(nodes);
}
figma.closePlugin();
};
- Basic
ui.html:
<html>
<head>
<style>
body {
font-family: sans-serif;
padding: 20px;
}
button {
padding: 8px 16px;
border-radius: 4px;
border: none;
background: #18A0FB;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<button id="create">Create Shape</button>
<script>
document.getElementById('create').onclick = () => {
parent.postMessage({ pluginMessage: { type: 'create-shapes' } }, '*')
}
</script>
</body>
</html>
- Add TypeScript support by installing necessary dependencies:
npm install --save-dev typescript @figma/plugin-typings
- Create a
tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"lib": ["es6", "dom"],
"typeRoots": ["./node_modules/@types", "./node_modules/@figma"],
"moduleResolution": "node"
}
}
To use this plugin:
- In Figma, go to Plugins > Development > New Plugin
- Choose "Link existing plugin"
- Select the manifest.json file from your project
- Run the plugin from Figma's Plugins menu
To build the TypeScript code:
tsc -w
This is a basic setup that creates a simple plugin with a UI button that creates a red rectangle when clicked. You can expand upon this structure to build more complex functionality.
Would you like me to explain any part of this setup in more detail or help you add specific functionality to your plugin?