Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
alexjlockwood / delete-unused-component.ts
Last active November 24, 2021 21:01
Deletes the selected component if it is private and unused in the file.
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;
}
@alexjlockwood
alexjlockwood / flatten-icons.ts
Last active September 12, 2023 12:49
Flattens all components in a Figma file. Note that this script assumes that (1) every component in the file is an icon, (2) each icon contains a single color, and (3) the icons don't use masks. The `figma.flatten` function may not work as you expect if one of these conditions aren't met.
// 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;
});
@alexjlockwood
alexjlockwood / cleanup-figma-names.ts
Last active November 24, 2021 21:02
Cleans up Figma component and style names
const components = figma.root.children.flatMap(pageNode => {
return pageNode.findAll(node => node.type === 'COMPONENT');
});
const styles = [
...figma.getLocalEffectStyles(),
...figma.getLocalGridStyles(),
...figma.getLocalPaintStyles(),
...figma.getLocalTextStyles(),
];
@alexjlockwood
alexjlockwood / generate-style-descriptions.ts
Last active September 12, 2023 12:54
Generates style descriptions for each color style in the current Figma file.
// 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;
//
// The Scripter environment.
//
// What's declared in there is available to scripts in their global namespace.
//
// symbolic type aliases
type int = number
type float = number
type byte = number
@alexjlockwood
alexjlockwood / TypedArrayUtils.kt
Created August 13, 2019 16:51
Kotlin helper functions for extracting ColorStateLists and Drawables from a TypedArray using AppCompatResources.
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import androidx.annotation.StyleableRes
import androidx.appcompat.content.res.AppCompatResources
/**
* Utility methods for extracting [ColorStateList]s and [Drawable]s from a [TypedArray].
@alexjlockwood
alexjlockwood / AppCompatViewConstructorDetector.kt
Created August 10, 2019 23:36
Custom lint rule that prohibits requires instantiating AppCompat views when possible.
package com.lyft.android.lint.checks
import com.android.SdkConstants
import com.android.tools.lint.detector.api.*
import com.intellij.psi.PsiMethod
import org.jetbrains.uast.UCallExpression
class AppCompatViewConstructorDetector : Detector(), Detector.UastScanner {
companion object {
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
<vector
android:name="heartbreak"
android:width="56dp"
android:height="56dp"
android:viewportWidth="56"
android:viewportHeight="56">
@alexjlockwood
alexjlockwood / CircleSquareView.kt
Created April 8, 2019 05:59
Inspired by @beesandbombs (twitter.com/beesandbombs)
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import java.lang.Math.pow
import kotlin.math.cos