egoist / tailwindcss-icons

Use any icon (100,000+) from Iconify, for TailwindCSS
MIT License
830 stars 17 forks source link

[Feature Request]: A way to get the types for the provided icon collections #18

Closed AbdallahAlhaddad closed 9 months ago

AbdallahAlhaddad commented 10 months ago

I think it would be very helpful if we can import the types of available icons to use to build a wrapper component which contains more logic to it.

<script setup lang="ts">
const props = defineProps<{
  /**
   * Available `Iconify` icons
   */
  name: string // 👈 Need a type to get autocomplete for provided collections icon names

   //.....
}>()

</script>
<template>
  <span :class="name"></span>
</template>
github-actions[bot] commented 9 months ago

:tada: This issue has been resolved in version 1.1.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

chojnicki commented 4 months ago

@egoist was this 1.1.3 release really related to this issue? If I understand correctly, @AbdallahAlhaddad was asking about type for icons names, not collections names. This is exactly what I'm trying to achieve. I have button component that has optional icon prop for icon name (icon class to be precise), and I also have this as string type, which is fine but not perfect. It would be great if I could somehow get type for my whole collection for warnings when name is wrong and autocomplete functionality.

For example 'i-solar-case-round-linear' | 'i-solar-scooter-linear' etc

chojnicki commented 4 months ago

I quickly made this as a "temporary" (wink wink) solution:

import fs from 'node:fs'
import { icons } from '@iconify-json/solar'

const names = Object.keys(icons.icons)

let result = 'declare global {\n'
result += '  type IconClass =\n'
names.forEach((name) => {
  result += `    | 'i-solar-${name}'\n`
})
result += '}\n'
result += 'export {}\n'

fs.writeFileSync('./src/icons.d.ts', result)

console.log('icons.d.ts has been successfully generated.')

So I'm simply generating types file. Again - not ideal, but it will be probably generated just once anyway so it's good enough. Of course this code will work for single collection (this is what I needed).

chojnicki commented 3 months ago

Updated version for multiple collections:

import fs from 'node:fs'
import { getIconCollections } from '@egoist/tailwindcss-icons'

const collections = getIconCollections([
  'fa6-brands',
  'solar',
])

let result = 'declare global {\n'
result += '  type IconClass =\n'

Object.values(collections).forEach((collection) => {
  Object.keys(collection.icons).forEach((name) => {
    result += `    | 'i-${collection.prefix}-${name}'\n`
  })
})

result += '}\n'
result += 'export {}\n'

fs.writeFileSync('./src/icons.d.ts', result)

console.log('icons.d.ts has been successfully generated.')

Result:

declare global {
  type IconClass =
    | 'i-fa6-brands-42-group'
    | 'i-fa6-brands-500px'
    | 'i-fa6-brands-accessible-icon'
    | 'i-fa6-brands-accusoft'
    | 'i-fa6-brands-adn'
    | 'i-fa6-brands-adversal'
    | 'i-fa6-brands-affiliatetheme'
    | 'i-fa6-brands-airbnb'
    | 'i-fa6-brands-algolia'
    | 'i-fa6-brands-alipay'
    | 'i-fa6-brands-amazon'
    | 'i-fa6-brands-amazon-pay'
    | 'i-fa6-brands-amilia'
    | 'i-fa6-brands-android'
    | 'i-fa6-brands-angellist'
   // and so on....
}
export {}

I have this as global, but it can be changed to export type.

hyoban commented 3 months ago

@chojnicki Thanks for your tip. I just updated README to link this issue.