I need list of all class name of Font-Awesome

前端 未结 18 3045
傲寒
傲寒 2020-12-12 13:44

I build app that will present a lot of icons so I need list of all Font-Awesome class name like [\"fa-dropbox\",\"fa-rocket\",\"fa-globe\", ....] so on is ther

18条回答
  •  情歌与酒
    2020-12-12 14:32

    A very, very easy and handy way to dynamically get all FA icon classes, if you are using npm and @fortawesome/fontawesome-svg-core npm package (tested at least with version v1.2.x), is the following:

    import { library } from '@fortawesome/fontawesome-svg-core'
    import { fas } from '@fortawesome/free-solid-svg-icons'
    
    // You need to add the entire icon set
    library.add(fas)
    
    let fasArray = Object.keys(library.definitions.fas)
    

    With just few lines of code, you'll get an array of all icon classes in solid library, with values like:

    [ "ad", "address-book", "address-card", "adjust", "air-freshener", "align-center", "align-justify", "align-left", "align-right", "allergies", "ambulance", "american-sign-language-interpreting", "anchor" ....]
    

    For all (solid + regular + brand) icon libraries, you can instead use:

    import { library } from '@fortawesome/fontawesome-svg-core'
    import { fas } from '@fortawesome/free-solid-svg-icons'
    import { far } from '@fortawesome/free-regular-svg-icons'
    import { fab } from '@fortawesome/free-brands-svg-icons'
    
    // Add both three icon sets
    library.add(fas, far, fab)
    
    let fasArray = Object.keys(library.definitions.fas)
    let farArray = Object.keys(library.definitions.far)
    let fabArray = Object.keys(library.definitions.fab)
    

    With these arrays, you can build whatever JSON or object structure you want to fit your purpose.

    One note to mention, library.definitions property seems to not be an official way to get access to all icons in the package, so maybe in future package versions, this snippet might not work; however a quick look at the package's source, it seems somehow unlikely they might replace it, at least without a major restructuring and different versioning.

    A second note is that, if you want the array to be dynamic, you have to import the entire icon sets, which might not be something you'd like to do. Otherwise, you can run it once, get the results and use them statically.

提交回复
热议问题