Last active
November 24, 2021 21:01
-
-
Save alexjlockwood/206f52901d089db14bd4dc7f66292dd1 to your computer and use it in GitHub Desktop.
Deletes the selected component if it is private and unused in the file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment