Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
alexjlockwood / flatten-icons.ts
Last active Apr 2, 2021
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.
View flatten-icons.ts
// 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 Apr 2, 2021
Cleans up Figma component and style names
View cleanup-figma-names.ts
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 Oct 2, 2021
Generates style descriptions for each color style in the current Figma file.
View generate-style-descriptions.ts
// 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;
View scripter.ts
//
// 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 Aug 13, 2019
Kotlin helper functions for extracting ColorStateLists and Drawables from a TypedArray using AppCompatResources.
View TypedArrayUtils.kt
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 Aug 10, 2019
Custom lint rule that prohibits requires instantiating AppCompat views when possible.
View AppCompatViewConstructorDetector.kt
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 {
View my_sample_avd.xml
<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 Apr 8, 2019
Inspired by @beesandbombs (twitter.com/beesandbombs)
View CircleSquareView.kt
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
View CrossesSquaresView.kt
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
@alexjlockwood
alexjlockwood / animated_cat.xml
Last active Apr 22, 2019
Android implementation of an animated cat loading spinner. Inspired by https://twitter.com/marcedwards/status/1109431862030524418
View animated_cat.xml
<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="vector"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<group