npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

figma-to-code

v1.0.8

Published

A CLI tool for accessing Figma Designs and turning them into React components via the terminal.

Downloads

17

Readme

figma-to-code

'figma-to-code' is a CLI tool that allows you to generate React components from your Figma designs via your terminal.

Installation

You can install figma-to-code globally using npm:

npm install -g figma-to-code

Usage

After installing figma-to-code, you can run the CLI using the ftc command inside the root of your React/Next project. To learn how to create a React/Next project please visit https://react.dev/learn/start-a-new-react-project

Initialization

Before you can use figma-to-code, you need to initialize the project. Run below prompt in the terminal.

ftc init

The terminal will ask you for your Figma Access Token, Document ID as well as what type of a project you are building.

Your Figma Access Token can be found via these steps:

  1. Login to your Figma account.
  2. Head to the account settings from the top-left menu inside Figma.
  3. Find the personal access tokens section.
  4. Click Create new token.
  5. A token will be generated. Copy the token (this will only be possible once so make sure that you keep it stored somewhere where you can return to it later)

Your Document ID can be read from any Figma file URL. For example in: https://www.figma.com/file/12345/my-design, the Document ID is 12345.

The choice between whether you are in a React or Next project will decide whether files are generated inside ./src/components (React) or ./components (Next).

Getting your designs

To see a list of your design, run:

ftc list

This will list all the top-level components of your Figma document.

Choose the component you want to generate, and figma-to-code will automatically create a new jsx-component in your project as well as a styles module.

If you choose a component that itself contains child-components you will be asked whether to generate code for the top level component (parent) or to see a list of its children.

Get a specific design

To generate a specific design, you can search by entering a string that should be included in your design name. Run:

ftc get <string>

This will list all the Figma frames where the name includes your search-string. Select the design you want to use.

Regenerating Components

If you make changes to your Figma designs, you can update your project files using figma-to-code.

To regenerate a component, run:

ftc refresh

This will list all of the components that have already been generated. Choose the component you want to regenerate, and figma-to-code will update the component in your project.

Changing document

If you want to import a component from another Figma design you will need to change the project Document ID. Run:

ftc change-doc

The terminal will ask you to enter the new Document ID:

Remove access to your Figma

If you want to remove project setup (access token, document ID, etc), run:

ftc clear

Best practice

Component size

figma-to-code works best with smaller components/frames. For example, instead of generating a JSX file for your entire landing page, you may want to generate a JSX file for your header, your footer, your main etc separately.

Naming & Semantic HTML

It is important that you give names to your frames in Figma as that will decide the name of the component as well as the class for which the element will be styled.

The element type will be a div unless otherwise stated in the name. To define the type, use $ followed by the element type you want generate (this will not be taken into consideration when naming the component)

For example, lets say you have a Figma design like the one below:

This will generate a JSX file like this

import styles from "./styles.module.css";

const ActionButton = () => {
    return (
        <button className={styles.ActionButton}>
            <span className={styled.ButtonText}>Click Here</span>
        </button>
    )
}

export default ActionButton

And a CSS-module style sheet like this.

.ActionButton {
    background-color: rgba(79, 139, 255, 1);
    padding: 8px 16px 8px 16px;
    border-radius: 12px;
}

.ButtonText {
    font-size: 14px;
    color: rgba(255, 255, 255, 1)
}

Global CSS

To get the best effect out of figma-to-code I suggest adding a global.css like below:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.App {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

button {
  border: none;
  cursor: pointer;
  width: fit-content;
}

Limitations

Positioning

figma-to-code uses Figma's auto-layout and turns in into flex display CSS values. Neither grid nor absolute positioning is currently supported.

Gradients

figma-to-code can currently handle gradients between two colors where the colors are positioned at the far end of each gradient handle bar.

Fonts & images

Font styles and images are not supported and therefore need to be separately imported into your code.