使用 Figma&Scripter 构建设计系统库(翻译)

用户头像
深圳/设计爱好者/5年前/577浏览
使用 Figma&Scripter 构建设计系统库(翻译)

如何利用 Figma 的插件 Scripter 自动完成原本需要数小时甚至数天才能完成的任务。

在过去的几个月中,我一直在帮助 Lyft 构建我们的 Figma 设计系统库(Design System Library)的初始版本,该库包含公司中所有设计师使用的所有颜色、文本样式和组件。在进行项目工作时,我们开始注意到耗时的任务模式,例如:

  • 为我们的500多种颜色样式生成有用的描述。

  • 优化我们图标库中的900多个图标。

  • 查找和删除不再使用的私有组件。


最终,我们对是否可以自动化其中一些流程感到好奇。我们发现了一个名为 Scripter 的 Figma 插件,该插件让我们能够使用 Figma Plugin API 在 TypeScript 中编写快捷脚本。从那时起,它大大加快了我们的工作流程。

figma.com/c/plugin/757836922707087381/Scripter


在这里,我想分享一些示例,说明我们如何使用 Scripter 来帮助在 Figma 中构建我们的设计系统库,并自动完成原本需要数小时甚至数天才能完成的任务。


生成颜色样式的描述

Figma 允许用户在其样式(Styles)中添加描述,当设计师将其悬停在样式选择器中时,这些描述以提示(Tooltips)的形式显示。我们要做的一件事是将每种颜色样式的描述更新为该颜色的 RGB 十六进制代码,从而使设计人员可以轻松访问它。(译者注:如下图,这是以前的版本)

在 Figma 中编辑和显示颜色样式说明。


这些描述易于在 Figma 中添加和编辑:只需右键单击样式,选择“Edit Style(编辑样式)”,然后更新描述。但是,我们的设计系统包含500多种颜色样式,而每次单独更新将花费数小时。

我们编写的第一批脚本之一旨在自动生成这些提示。对于每种颜色样式,脚本都会分析样式的颜色,生成其相应的 RGB 十六进制颜色代码,并将其设置为样式的描述。

// Get the list of color styles in the current Figma file.
const colorStyles = figma.getLocalPaintStyles();

const updatedColorStyles = colorStyles.filter(style => {
  const { paints } = style;
  if (paints.length !== 1) {
    // Skip styles containing multiple colors.
    return false;
  }
  const [paint] = paints;
  if (paint.type !== 'SOLID') {
    // Skip styles containing gradients, image fills, etc.
    return false;
  }
  const hexColor = rgbToHex(paint.color);
  if (style.description === hexColor) {
    // Skip styles that already have the correct description.
    return false;
  }
  // Set the style description equal to the color's hex code.
  style.description = hexColor;
  return true;
});

// Show a toast indicating the number of style descriptions that were updated.
figma.notify(`✅ Updated ${updatedColorStyles.length} style description(s)`);

/**
 * Converts an RGB object to a hex color string. For example:
 * { r: 0.5, g: 0.5, b: 0.5 } => '#808080'
 */
function rgbToHex(rgb: RGB) {
  const r = ratioToHex(rgb.r);
  const g = ratioToHex(rgb.g);
  const b = ratioToHex(rgb.b);
  return `#${r}${g}${b}`;
}

/**
 * Converts a ratio between [0,1] to a 2-character hex string. For example:
 * 0.5 => '80'
 */
function ratioToHex(ratio: number) {
  const hex = Math.round(ratio * 255).toString(16).toUpperCase();
  return `${hex.length === 1 ? '0' : ''}${hex}`;
}

该脚本为当前 Figma 文件中的每种颜色样式生成描述。

拼合图标(Flattening icons)

像许多设计工具一样,Figma允许人们通过将图标“拼合”到单个图层来优化图标。我们发现,在发布图标之前先拼合图标是个好习惯,原因如下:

  • 拼合的图标可减少“图层”面板中的噪音。

  • 包含较少图层的图标在 Figma 中渲染的效率更高,尤其是在很大程度上依赖于图标库。

  • 图标在发布(published)之后再拼合的话会破坏设计人员应用的覆盖范围。

    将多图层自行车图标拼合为单独层。


也就是说,我们的图标库目前包含900多个图标,因此通过 Figma 独立地将每个图标拼合是不现实的。幸运的是,Figma Plugin API 提供了 figma.flatten() 可以自动化整个流程!

// Create a list of all component nodes in the Figma file.
const componentNodes = figma.root.children.flatMap(pageNode => {
  return pageNode.findAll(node => node.type === 'COMPONENT');
}) as readonly ComponentNode[];

// Create a list of component nodes that have more than one child node.
const unflattenedComponentNodes = componentNodes.filter(componentNode => {
  const childrenNodes = componentNode.findAll(() => true);
  return childrenNodes.length > 1;
});

// Flatten each component node's children into a single node.
unflattenedComponentNodes.forEach(node => figma.flatten(node.children, node));

figma.notify(`✅ Flattened ${unflattenedComponentNodes.length} icon(s)`);

拼合文件中每个组件的子节点(Children Nodes)的脚本。


请注意,此脚本做出了一些重要的前提条件要注意。具体来说,前提(1)文件中的每个组件都是一个图标,(2)每个图标都包含一种颜色,并且(3)这些图标不使用遮罩。如果不满足以上条件之一,则该 figma.flatten() 功能可能无法正常工作。


删除未使用的私有组件

设计系统库中的大多数组件都是使用原子构造原理(Atomic Building Block Components)创建的。举例,我们的每个按钮组件都是共享相同的内部背景组件,这是文件专用的,也使得我们的按钮更易于更新和维护。当 Figma 发布“自动布局(Auto Layout)”时,我们从头开始重建了几个组件,而许多原子组件则变得过时且不需要了。


不幸的是,Figma 没有提供用于搜索给定主组件的本地实例(Instances)的内置机制,这使我们很难确认文件中是否仍在使用这些组件。为了解决这个问题,我们写了一个脚本!它允许我们选择要删除的组件,并且分辨出组件私有且未在文件中使用的情况下删除组件。

if (figma.currentPage.selection.length !== 1) {
  figma.notify("🚫 Select a component");
  return;
}

const [componentNode] = figma.currentPage.selection;
if (componentNode.type !== "COMPONENT") {
  figma.notify("🚫 Select a component");
  return;
}

if (componentNode.key !== '') {
  // If the key is non-empty, then that means the component has
  // been published as part of the team library. Deleting published
  // components isn't safe because they may be used outside of the file.
  figma.notify("🚫 Selected component has been published");
  return;
}

// Create a list of all instances of the selected component node.
const instanceNodes = figma.root.children.flatMap(pageNode => {
  return pageNode.findAll(node => {
    if (node.type !== 'INSTANCE') {
      // Ignore non-instance nodes.
      return false;
    }
    // The node is an instance of the selected component node
    // if its master component ID matches the component node's ID.
    return node.masterComponent.id === componentNode.id;
  });
}) as readonly InstanceNode[];

if (instanceNodes.length === 0) {
  // The component is unused, so delete it.
  componentNode.remove();
  figma.notify(`🗑️ Deleted unused component`);
} else {
  // The component is still in use, so don't delete it.
  figma.notify(`✅ Found ${instanceNodes.length} instance(s) of the selected component`);
}

如果所选组件是私有的并且在文件中未使用,则脚本将删除所选组件。


我们编写的其他脚本

这些只是我们为构建 Figma 设计系统库而编写的许多脚本中的几个。以下是一些其他示例:


  • 生成颜色样式。我们编写了一个脚本,该脚本解析一个 JSON 文件,其中包含我们色谱中的所有颜色,并为每个使用 figma.createPaintStyle() 创建对应的 Figma 颜色样式 。这为我们节省了工作时间,而这些工作原本需要通过 Figma 手动创建数百种颜色样式。

  • 搜索未使用我们的颜色样式的图层。我们希望所有组件都使用我们的色谱样式,而不是硬编码(Hardcoded)的十六进制颜色。为了验证这种情况,我们编写了一个脚本,该脚本在文件中搜索未链接到 Figma 颜色样式的颜色用例。我们通过分析图层的  fills  属性来确定图层是否具有填充颜色,然后通过检查图层的 fillStyleId 属性是否为非空来验证该填充颜色是否与 Figma 颜色样式相关联。查看此脚本作为示例。

  • 建立自定义选择。Figma提供了用于一次选择多个图层的众多工具。但是,有时我们想创建一个满足我们自己的自定义标准的选择(例如,选择当前所选主组件的所有实例)。幸运的是,构建选择相对容易在脚本中自动化实现。我们通过遍历当前页面的图层层次结构,创建要选择的图层列表并将该列表分配给 figma.currentPage.selection 属性来实现。

  • 清理图层名称。我们每隔一段时间就会运行一个脚本,该脚本通过删除结尾的空格,纠正大小写错误,在'/'字符之前和之后添加缺失的空格等来清理图层名称。这大大减少了我们产生的意外拼写错误的次数,并确保在我们的库中命名保持一致。


最后的想法

编写脚本对于加速我们的 Figma 设计系统库的开发至关重要,并且使该项目的工作变得更加有趣!我们希望在下面的评论中听到有关其他人如何自动化其工作流程的想法和故事!而且,如果你想与我合作,或者想和我们 Lyft 合作的任何出色设计师和工程师工作-我们正在招聘!

3
阅读原文
|
举报
|
1
分享
相关推荐
评论
用户头像
评论你的想法~
表情
喜欢TA的作品吗?喜欢就快来夸夸TA吧!
推荐素材
手表表盘UI系列
UI通用设计素材1
【新年UI图标】会员icon
智能家居中心 简约 UI设计组件库
APP/小程序商业项目UI模板|99个高保真UI+79个原型
新拟态风格 UI设计组件库
拟物风质感写实UI卡片合集源文件
3D渐变流体抽象矢量UI背景图
原创UIUX交互橙红渐变炫酷视觉平面设计作品集模板PSD
【新年UI图标】旅行icon
3D卡通UI界面图标可爱插画免扣素
我的钱包-UI界面设计-app
新能源APP应用UIKit
【新年UI图标】30个图标
盲盒APP UI设计
Security Camera UI kit
科技医疗透明柜UI界面设计
钱包ui模板
高级表盘系列UI源文件
UI界面 组件
【新年UI图标】汽车icon
【新年UI图标】钱包icon
UI 登录界面设计模板包
【新年UI图标】秒杀icon
你可能喜欢
相关收藏夹
大家都在看
登录注册