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

Feature request: add ability to concat string props within example snippet #80

Open
glomotion opened this issue Jul 1, 2024 · 7 comments
Labels
under discussion The Code Connect team is currently discussing this request

Comments

@glomotion
Copy link

glomotion commented Jul 1, 2024

Describe the feature you'd like:
We have a Button component in figma, which has a bunch of properties (including specifically "variant" & "type") - some of which are merged into a single "variant" prop inside of the coded Button component eg:

import { figma } from "@figma/code-connect";
import { Button } from "./";

figma.connect(
  Button,
  "https://www.figma.com/design/oHsoXoDsvnhjlbpzSIbHvU/BIOME?node-id=2-3486&m=dev",
  {
    props: {
      children: figma.string("Label"),
      variant: figma.enum("Variant", {
        Primary: "primary",
        Secondary: "secondary",
        Tertiary: "tertiary",
      }),
      type: figma.enum("Type", {
        Fatal: "fatal",
        Inverse: "inverse",
        Default: "default",
      }),
      size: figma.enum("Size", {
        Large: "large",
        Medium: "medium",
        Small: "small",
      }),
    },
    imports: ["import { Button } from '@biom3/react'"],
    example: ({ variant, type, children, size }) => {
      const fullVariant = type === "default" ? variant : `${variant}/${type}`;
      return (
        <Button variant={fullVariant} size={size}>
          {children}
        </Button>
      );
    },
  },
);

this example code-connect snippet above fails to render anything inside of figma:

Screenshot 2024-07-01 at 4 45 11 PM

After reading through your docs, i found some info which tells me why FIGMA might not like this code snippet:

Code Connect files are not executed. While they're written using real components from your codebase, the Figma CLI essentially treats code snippets as strings. ... If something you're trying to do is not possible because of this restriction in the API, we'd love to hear your feedback.

So Im just here wondering, what would be the best way to try and achieve the above?

@glomotion
Copy link
Author

glomotion commented Jul 1, 2024

if i dont try to concat the type and variant into a single string, the code snippet works just fine - eg:

Screenshot 2024-07-01 at 4 58 14 PM
example: ({ variant, type, children, size }) => {
      return (
        <Button variant={variant} size={size}>
          {type}: {children}
        </Button>
      );
    },

But sadly this means code-connect isnt showing us an accurate code snippet. :(

@glomotion
Copy link
Author

just to try and make this crystal clear, the variants that are supported inside the codebase look like:

export type ButtonVariant =
  | "primary"
  | "primary/fatal"
  | "primary/inverse"
  | "secondary"
  | "secondary/fatal"
  | "tertiary"
  | "tertiary/inverse";

@glomotion glomotion changed the title Feature request: add the ability to combine props within the example code Feature request: add the ability to concat string props within the example code Jul 1, 2024
@jyyang0 jyyang0 added the under discussion The Code Connect team is currently discussing this request label Jul 1, 2024
@jyyang0
Copy link

jyyang0 commented Jul 1, 2024

Hey @glomotion, thanks for the feedback -- As you've correctly identified, Code Connect only parses the code so conditionals inside of the template will not render correctly -- The team is currently exploring potential APIs so that we can support scenarios like this where more custom transformations are required to render your components correctly.

In the meantime, if this is blocking you this can technically be achieved using variant restrictions , so you could set up a separate figma.connect call for each

import { figma } from "@figma/code-connect";
import { Button } from "./";

figma.connect(
  Button,
  "https://www.figma.com/design/oHsoXoDsvnhjlbpzSIbHvU/BIOME?node-id=2-3486&m=dev",
  {
    variant: { Variant: "Primary" }, // This code connect will only be shown for nodes with `Variant` = 'primary'
    props: {
      children: figma.string("Label"),
      fullVariant: figma.enum("Type", {
        Fatal: "primary/fatal",
        Inverse: "primary/inverse",
        Default: "primary",
      }),
      ...
    },
    imports: ["import { Button } from '@biom3/react'"],
    example: ({ fullVariant, children, size }) => {
      return (
        <Button variant={fullVariant} size={size}>
          {children}
        </Button>
      );
    },
  },
);

figma.connect(
  Button,
  "https://www.figma.com/design/oHsoXoDsvnhjlbpzSIbHvU/BIOME?node-id=2-3486&m=dev",
  {
    variant: { Variant: "Secondary" }, // This code connect will only be shown for nodes with `Variant` = 'Secondary'
    props: {
      children: figma.string("Label"),
      fullVariant: figma.enum("Type", {
        Fatal: "secondary/fatal",
        Inverse: "secondary/inverse",
        Default: "secondary",
      }),
      ...
    },
    imports: ["import { Button } from '@biom3/react'"],
    example: ({ fullVariant, children, size }) => {
      return (
        <Button variant={fullVariant} size={size}>
          {children}
        </Button>
      );
    },
  },
);

@glomotion
Copy link
Author

Oh thank you kindly for the prompt reply and the code snippet @jyyang0 !!
In this particular case, this should be just what I need! ❤️
Would love to see the ability to use expressions inside of the code snippets in the future. Sadly designers often don't construct variables etc with the same thoughtfulness as engineers, so being able to mix and match figma props into the right API for the component would definitely be very useful for us.

@glomotion
Copy link
Author

glomotion commented Jul 1, 2024

FWIW - i've also found that expressions inside example snippets, could be quite handy to work around type issues when converting props between figma and code (especially given that code-connect files must be TSX).

@glomotion glomotion changed the title Feature request: add the ability to concat string props within the example code Feature request: add ability to concat string props within example snippet Jul 2, 2024
@karlpetersson
Copy link

FWIW - i've also found that expressions inside example snippets, could be quite handy to work around type issues when converting props between figma and code (especially given that code-connect files must be TSX).

Hey @glomotion! This sounds like something we might want to look closer into if you're running into issues here, do you have an example of this?

@glomotion
Copy link
Author

apologies @karlpetersson - I think the issues i mentioned yesterday were actually due to other more complicated issues regarding how to render subcomponents (new issue here: #86). If i think of any other examples were this might be useful, ill come back and update this thread. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
under discussion The Code Connect team is currently discussing this request
Projects
None yet
Development

No branches or pull requests

3 participants