PricingBlog

Getting entries from svg body for dynamically creating icon

  • tomthebigtree-1176817157565337620

    Tom Ireland

    2 years ago

    I'm experimenting with how I might use the Iconify API to get Tabler icon data to dynamically create svg icons and even use the API to create some form of icon component for searching and rendering icons.

    The data comes back as a JSON object (see example):

    {"prefix":"tabler","lastModified":1700636897,"aliases":{},"width":24,"height":24,"icons":{"circle-x":{"body":"<path fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M3 12a9 9 0 1 0 18 0a9 9 0 1 0-18 0m7-2l4 4m0-4l-4 4\"/>"}}}

    My challenge us how I break up the body to set certain attributes on an svg tag via a formula. I have tried using split (space as the delimiter), which generates an array but getting stuck there in terms of how I might create an object and map each value to its respective key. Any pointers?
    1
  • erikbeus-1176817811851575367

    Erik Beuschau

    2 years ago

    I believe we have a Parse JSON formula that you might be able to use to break up the object? Afterwards you might be able to Encode JSON again and use the output?
  • tomthebigtree-1176846642121543740

    Tom Ireland

    2 years ago

    @Erik Beuschau - Thanks for getting back to me. Sadly, Parse JSON does not appear to work for the body. I did try that this morning (see example screenshot).

    The string output is <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12a9 9 0 1 0 18 0a9 9 0 1 0-18 0m7-2l4 4m0-4l-4 4"/> and displays null as it appears to already be parsed?

    Am I getting confused with what you mean? In this case, I'm looking to split it up to get fill, stroke, etc., into an object to apply that to svg attributes.
    1176846641941205042-iconify-json.png
  • tomthebigtree-1176849211208245319

    Tom Ireland

    2 years ago

    It looks as though body is the key and the string output is the value but it's the value I want to work with. 🙂
  • tomthebigtree-1177133218303058000

    Tom Ireland

    2 years ago

    Okay, so had another go this morning and managed (by using Matches) to use a regex to get the string reduced to an array with the values but now stuck on how I create an object from that. I read that parsing HTML is tricky because input could be incorrect, etc., but given the source data is consistently correct and formatted in a specific way, this reduces risk of problems.

    Any pointers on how to get this last bit working so I can set each array item to be key/value pair in object without the ="? Converting to a string after match and then splitting by =" does not work because the string conversion separates each item with a comma.
    1177133218076573748-regex-svg.png
  • erikbeus-1177162251048845332

    Erik Beuschau

    2 years ago

    Perhaps you could use a Map function where you use Split to split each item on the = character and then finally use Flat to get a flat array structure? Perhaps if you could post an example of how you would like the structure to look like I could come up with a better suggestion? 🤞
  • tomthebigtree-1177166938091307048

    Tom Ireland

    2 years ago

    @Erik Beuschau - Thanks, pal. Funnily enough, I was looking at Map before seeing your message and that's kinda heading in the right direction. Basically, I want my svg path to look like this:

    {
    "fill": "none",
    "stroke": "currentColor",
    "stroke-linecap": "round",
    "stroke-linejoin": "round",
    "stroke-width": "2",
    "d": "M3 12a9 9 0 1 0 18 0a9 9 0 1 0-18 0m7-2l4 4m0-4l-4 4"
    }
  • tomthebigtree-1177167989984993280

    Tom Ireland

    2 years ago

    So far, from doing the initial regex, I have an array to work with and with Map and Split I have achieved an array with an array of each item.
    1177167989758492692-mapped-array.png
  • erikbeus-1177171040103845978

    Erik Beuschau

    2 years ago

    Alright. Almost there 😅 If you put in an Object formula between your Map and Split formulas, and add 2 entries in your object: key being item->Split->Get[0] and value being item->Split->Get[1] and then finally using From entries after your Map, then I think it should work 🤞
  • tomthebigtree-1177247110165446686

    Tom Ireland

    2 years ago

    @Erik Beuschau "The Legend" Beuschau strikes again! That worked! Interestingly, I didn't know it was possible to specify the index as the path using Get so learned something new there. Now just to tie it all together and get it to change dependent on chosen icon. I'm excited by this because it could mean an icon component that allows lookup of tabler icons via Iconify API and application to svg with all the appropriate attributes! Very handy!
    🙌1
  • erikbeus-1177247571668906005

    Erik Beuschau

    2 years ago

    Sounds awesome @Tom Ireland Happy to help 🙂
    @Andreas Møller has an in-depth video on data structures coming up which might be helpful in terms of understanding what's possible when working with arrays/objects 🍿
  • tomthebigtree-1177247900170993674

    Tom Ireland

    2 years ago

    That's awesome. Look forward to seeing that. 🤘
  • andreasmoller-1177248268615426110

    Andreas Møller

    2 years ago

    Basic data structures
    https://youtu.be/8gOxQhhVohA
    🤯1
  • tomthebigtree-1177248611227156600

    Tom Ireland

    2 years ago

    That was fast!!! 🏎️
    😅1
  • andreasmoller-1177265866556063864

    Andreas Møller

    2 years ago

    You asked
    😂1
  • tomthebigtree-1177356570267488266

    Tom Ireland

    2 years ago

    First pass parsing svg data from Iconify API for Tabler icons is up and running. At the moment, I have to reset the text colour to something different for it to update in preview (not sure why 🤔 ) but it works! I set a couple of buttons up to update the API but the goal is to be able to search for icons, have them be displayed and then pick one. This is all in a component, so could be fashioned into some kind of icon picker for various things. Thanks to @Erik Beuschau for the pointers on parsing the svg <path .. /> data - that was hard to work out!
  • tomthebigtree-1177356966998327396

    Tom Ireland

    2 years ago

    Also, need to rename the component. 😄
  • andreasmoller-1177359488588722176

    Andreas Møller

    2 years ago

    I realize this might be a stupid question, but any reason why you do not just use the image directly?
    1177359488395771914-CleanShot_2023-11-23_at_22.23.43.png
  • You can also use a data URI list this: data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="24" height="24" viewBox="0 0 24 24"%3E%3Cg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"%3E%3Cpath d="m8 9l3 3l-3 3m5 0h3"%2F%3E%3Cpath d="M4 8a4 4 0 0 1 4-4h8a4 4 0 0 1 4 4v8a4 4 0 0 1-4 4H8a4 4 0 0 1-4-4z"%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E
  • tomthebigtree-1177480632444649503

    Tom Ireland

    2 years ago

    It’s not a stupid question. A couple of reasons:

    * I’m stupid because I didn’t read all the docs and didn’t realise I could just get the data URI (I was thinking about trying to get JSON) 😂
    * I assumed less customisation potential with img but reflecting on the scenario, the customisation is not required. 🫣

    However, the bonus is that I learned a bunch of stuff!
  • andreasmoller-1177480915396599868

    Andreas Møller

    2 years ago

    You do get less customisation, but the iconify does offer a lot
  • tomthebigtree-1177481396340674570

    Tom Ireland

    2 years ago

    Indeed. I’m going to take a look at that later today.