Closed10
Figma Code Connectやっていき
やってみた時の設定ファイル
figma.config.json
{
"codeConnect": {
"parser": "react",
"include": ["/**/*.{tsx,jsx}"]
}
}
サンプルで作ったComponent

適当にプロパティをつけてみた。
先に自前でコンポーネントを実装して
Button.tsx
import styles from './Button.module.css';
export type Props = {
onClick: () => void;
text: string;
variant: "primary" | "secondary";
Size: "small" | "large";
State: "enabled" | "disabled"
}
const Button = (props: Props) => {
const { onClick, text, variant, Size, State } = props;
const getButtonClasses = () => {
const classes = [styles.btn];
if (variant === 'primary') classes.push(styles.btnPrimary);
if (variant === 'secondary') classes.push(styles.btnSecondary);
if (Size === 'small') classes.push(styles.btnSmall);
if (Size === 'large') classes.push(styles.btnLarge);
if (State === 'disabled') {
classes.push(styles.btnDisabled);
}
return classes.join(' ');
};
return (
<button
onClick={onClick}
className={getButtonClasses()}
disabled={State === 'disabled'}
>
{text}
</button>
);
};
export default Button;
コマンドで生成されたのがこちら
Button.figma.tsx
import React from "react"
import Button from "./Button"
import figma from "@figma/code-connect"
/**
* -- This file was auto-generated by Code Connect --
* `props` includes a mapping from your code props to Figma properties.
* You should check this is correct, and update the `example` function
* to return the code example you'd like to see in Figma
*/
figma.connect(
Button,
"https://www.figma.com/design/ozQcMwI3tRaC83972uL1Nd/Figma-basics?node-id=527%3A6",
{
props: {
// These props were automatically mapped based on your linked code:
text: figma.string("text"),
variant: figma.enum("variant", {
primary: "primary",
secondary: "secondary",
}),
Size: figma.enum("Size", {
small: "small",
large: "large",
}),
State: figma.enum("State", {
enabled: "enabled",
disabled: "disabled",
}),
// No matching props could be found for these Figma properties:
// "text": figma.string('text')
},
example: (props) => (
<Button
onClick={/* TODO */}
text={props.text}
variant={props.variant}
Size={props.Size}
State={props.State}
/>
),
},
)
publishしているとfigma上で見た目をUIから弄りつつ、実際のコードを使う際のスニペットが書かれる感じ。


現状の理解としてはあくまで複雑なデザインシステムがあって、コンポーネントもめっちゃ大量にあって、管理面がカオティックになっているケースにおいて、Figmaからデザインを起点にコードを特定・利用できるようにする感じの利用用途な気がする。
もともとCodeがあって、Designがあって、そこをLinkさせるイメージ。
実装初期のコンポーネント実装を生成で素早くできるようにする、というものではなく、
現状のコードとデザイン資産を活用しやすいように整理する感じかな。
あんまりこのレベルで運用されているケースに当たったことがないので、メリット感は想像でしかかけないが
一旦飽きたのでクローズ。
どちらかというとDesign→Codeのベクトルへの生成に興味があったので
このスクラップは6ヶ月前にクローズされました