Socket
Book a DemoInstallSign in
Socket

@locmod/icon-name-type

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@locmod/icon-name-type

This is a script to generate TS file with IconName type

1.0.6
latest
Source
npmnpm
Version published
Weekly downloads
91
22.97%
Maintainers
2
Weekly downloads
 
Created
Source

icon-name-type

CLI to generate TS file with IconName type.

Intro

Image a React (NextJS) project with following structure.

Icons stored by groups in folders named by icon size.

public/
  images/
    icons/
      16/
        chevron.svg
        clockwise.svg
      24/
        arrow-left.svg
        arrow-right.svg  

There is an Icon component which accepts icon name property.

type IconName = '16/chevron' | '16/clockwise' | '24/arrow-left' | '24/arrow-right'

type IconProps = {
  name: IconName
}

const Icon: React.FC<IconProps> = ({ name }) => (
  <img src={`/images/svg/${name}`} />
)

To automate process and avoid handwriting IconName type this package exists.

icon-name-type call will generate icons.d.ts file with content

export type IconName = '16/chevron' | '16/clockwise' | '24/arrow-left' | '24/arrow-right'

The Icon component's code can be simplified

import type { IconType } from './icons'

type IconProps = {
  name: IconName
}

const Icon: React.FC<IconProps> = ({ name }) => (
  <img src={`/images/svg/${name}`} />
)

Generate types

icon-name-type --i ./public/images/svg --o ./src/components/Icon

Watch for input changes and generate types

icon-name-type --i .. --o .. --watch

Helpful scripts

Add this to your package.json scripts

"build-icons": "icon-name-type --i ./public/images/svg --o ./src/components/ui/Icon",
"watch-icons": "npm run build-icons -- --watch",

FAQs

Package last updated on 29 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.