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

Reference instance swap components by name #13

Open
andreiduca opened this issue Apr 17, 2024 · 2 comments
Open

Reference instance swap components by name #13

andreiduca opened this issue Apr 17, 2024 · 2 comments

Comments

@andreiduca
Copy link

Currently, "instance swap" properties are referenced as "links" to other components. It would be great to be able to reference them by name, and get a string value when mapping back to the React component. Ideally, we should also be able to manipulate the strings when mapping (for example, to convert from snake_case to camelCase).


For icons for example, we don't have a React component for each individual icon. Instead, we use a single <Icon> component, and we use a string prop to reference icons by id.

<Icon id="icChef" />

When connecting this to the Figma component, we get a "link" to the instance-swap icon, but we don't have a connected React component for every icon in Figma.

figma.connect(
  Icon,
  "https://www.figma.com/file/...[Icon component with an instance swap]",
  {
    props: {
      iconId: figma.instance("Icon ID"),
    },
    example: (props) => <Icon id={props.iconId} />
  },
);

So ideally, we should be able to do something like this, where the instance has a name attribute we can access.

figma.connect(
  Icon,
  "https://www.figma.com/file/...[Icon component]",
  {
    props: {
      iconId: figma.instance("Icon ID"),
    },
    example: (props) => <Icon id={props.iconId.name} />
  },
);

Additionally, with a custom mapper function, we could also manipulate the instance at will:

    props: {
      // just get the `name` as a string
      iconId: figma.instance("Icon ID", (instance) => instance.name),
    },
    example: (props) => <Icon id={props.iconId} />

or even change it how we see fit:

  iconId: figma.instance("Icon ID", (instance) => toCamelCase(instance.name)),
@tomduncalf-figma
Copy link

tomduncalf-figma commented Apr 18, 2024

Hey @andreiduca, so right now the way we suggest you solve this is that you create a connection for every icon. You can have multiple connections for the same React component, so you could have a file icons.figma.tsx with something like:

// Chef icon
figma.connect(Icon, "https://figma.com/file/123?node_id=1:1", {
  example: () => <Icon id="icChef" />
}

// Plate icon
figma.connect(Icon, "https://figma.com/file/123?node_id=1:2", {
  example: () => <Icon id="icPlate" />
}

// Fork icon
figma.connect(Icon, "https://figma.com/file/123?node_id=1:3", {
  example: () => <Icon id="icFork" />
}

// etc...

We talk a bit about this in the README, although I think we could probably make this clearer and show some examples.

There's a template script which you can use a basis for automatically generating such a file from Figma. If you have any questions or comments about implementing this, please let us know and we'd be happy to help out.

That said, we have also talked about concepts like you describe, where e.g. strings can be manipulated. Right now, we don't have concrete plans on whether we'll support this or when, but your input is really helpful and I'll share it with the team for discussion. We might follow up with further questions if we do look at implementing something like this.

@andreiduca
Copy link
Author

Connecting all individual icon components in Figma is exactly what I am trying to avoid. It's a scalability issue to have to "connect" every time a new icon gets added to the library.

In Figma, we have 250+ icons as individual components. So we created a separate component named Icon as a wrapper around them, which uses an instance swap named Icon ID to swap the actual icon component being rendered inside.

Similarly, in React, we only have a single <Icon> component, and we reference individual icons by a string id prop.

We can easily connect these two, only once, since the APIs are identical, and then it shouldn't matter how many new icons get added to the library – the "connection" remains the same: an Icon component on both sides with an icon-id property, which is an instance swap in Figma, but a string in React.

So mapping the swap instance to a string, like I tried to explain in my original comment, and referencing the swapped instance by its name should solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants