Possible to turn an image into a prop in svelte?

Messing around with Svelte and have setup a gallery using the same image but will css/svelte it differently for each image (is this possible). Wondered if it was possible to save that image as a prop, so instead of having to do img src="" .... I can do <div>{img}</div> etc... I wanted to mess around with each image differently, would that then be possible with the single prop, assuming what I'm messing with is the div containing that image, and not the propped image itself?
70 Replies
b1mind
b1mind4mo ago
What is the end result you want, I'm not clear on what you mean.
CDL
CDL4mo ago
So take this hideous picture right now
No description
b1mind
b1mind4mo ago
Because you are in vite ecosystem you want to import the asset anyway so vite will handle cache for you
CDL
CDL4mo ago
ah, hmm basically, is there a better way than doing
<div><img src="link here" alt="pic"></div>
<div><img src="link here" alt="pic"></div>
for repetitive use of the same image
b1mind
b1mind4mo ago
Right but you said you want to "mess with each one differently" So are you wanting to make a <Image> component that you pass a asset into? https://vitejs.dev/guide/assets
CDL
CDL4mo ago
I was looking to css each image, so animation/transition etc etc.. I'm just winging it man, lmao
b1mind
b1mind4mo ago
import imgUrl from './img.png' is how you should use the img then you would have src={imgURL} You can also glob them in an array and use {#each images as img} or what not. https://vitejs.dev/guide/features.html#glob-import also have https://vitejs.dev/guide/features.html#dynamic-import This is basically how you would want it unless you were handling cache on the proxy server. or if your host handles it for you. Nothing wrong for using the /static/ folder for assets either just realize that they won't get the cache headers from Vite.
CDL
CDL4mo ago
slow down there cowboy, im still trying to import the fucking image LOL
b1mind
b1mind4mo ago
I'm just showing you the methods vite uses You only have to know they exist not how to use them xD
CDL
CDL4mo ago
gotcha
b1mind
b1mind4mo ago
SvelteKit goes into this too on their docs https://kit.svelte.dev/docs/images#vite-s-built-in-handling Because its basically just a vite plugin so even if you are using it same thing.
CDL
CDL4mo ago
Understood, I'll try read, I'm struggling with the most basic shit atm, thanks for the links!
b1mind
b1mind4mo ago
I have not used <enhanced:img> yet but looks legit 😄
CDL
CDL4mo ago
I mean, I thought let src = '../assets/images/image.jpg'; would work, but apparently not
b1mind
b1mind4mo ago
mmm I wouldn't use src name it something better 😄 never use namespaces that are used by html/css/js bad habit to get into and just spells disaster
CDL
CDL4mo ago
i can change the name when the damn thing works lol
b1mind
b1mind4mo ago
but if its not an allowed namespace it will not work sir
CDL
CDL4mo ago
(i changed the name, it doesnt work)
b1mind
b1mind4mo ago
so best to just not in the first place right 😄 share the full code or demo
CDL
CDL4mo ago
I'm gonna have to ask you here because I'm clearly missing something simple here
CDL
CDL4mo ago
No description
b1mind
b1mind4mo ago
Ok pretty clear mistake ya? You really gotta think about this one cause its important you understand what you are doing here. what does let fleur return?
CDL
CDL4mo ago
uhh... a link, but I know it's wrong, I just can't remember how I'm supposed to do it well, it's supposed to be a link, it's not
b1mind
b1mind4mo ago
From proper naming you have what import imgUrl from './img.png' a link or ... what? like what is it at its core in JS
CDL
CDL4mo ago
I created a variable
b1mind
b1mind4mo ago
typeOf(fleur) So two things going wrong here you are not using the right syntax as above for a Vite import Either way you are still just returning a string So <div> '../folder/path'</div> would be the result
CDL
CDL4mo ago
I mean I tried this but it also didnt work
import fleur from '../assets/fleur.jpg';
import fleur from '../assets/fleur.jpg';
b1mind
b1mind4mo ago
So how would you use the string in html? not alone but in a <img src={imgUrl} />
CDL
CDL4mo ago
Not a clue mate, literally not a clue
b1mind
b1mind4mo ago
Also.. you img is not in that path? but in /public? you don't have /assets/ exapanded so I can't tell if its duplicated there.
CDL
CDL4mo ago
oh sorry i just moved it there now so it's in assets/fleur.jpg right now
missymae#2783
missymae#27834mo ago
Does vite allow for syntax like this?
import { ReactComponent as Selfie } from "./Assets/Selfie_BW_1F40B.svg"
import { ReactComponent as Selfie } from "./Assets/Selfie_BW_1F40B.svg"
in React it then allows <div><Selfie/></div>
b1mind
b1mind4mo ago
What the cursed .... 😄 So you are making a component from a svg? But why 🤷‍♂️
missymae#2783
missymae#27834mo ago
use directly as a named component, no mix ups, no src. I just stumbled upon it recently, and it does seem like what the original question is hinting at how to do imports number 11,536
b1mind
b1mind4mo ago
That seems like such a bad pattern to me so it creates a <img src> for you? what props does it take as a ReactComponent by default? does it pull height/width for you?
CDL
CDL4mo ago
I mean, I literally just want to import an image to use as {fleur} that's it
b1mind
b1mind4mo ago
https://kit.svelte.dev/docs/images#vite-s-built-in-handling Linking it again xD You can make that a <Img> component and just pass in the import url too from the base component. You would then use vites dynamic import (with the prop name in the url)
<script>
export let webp = ''
export let jpg = ''
export let width = ''
export let height = ''
export let alt = ''
</script>

<picture>
<source srcset={webp} type="image/webp" />
<source srcset={jpg} type="image/jpeg" />
<img src={jpg} {alt} {width} {height} loading="lazy" />
</picture>
<script>
export let webp = ''
export let jpg = ''
export let width = ''
export let height = ''
export let alt = ''
</script>

<picture>
<source srcset={webp} type="image/webp" />
<source srcset={jpg} type="image/jpeg" />
<img src={jpg} {alt} {width} {height} loading="lazy" />
</picture>
not this ugly probably but lol that is a <Picture> comp I made quickly just to srcset easily in a project. Oh I kinda break my own rule here to because Svelte shorthand... If you do name the prop the same as the attribute you get to use the shorthand >.>;; vs writing out height={height} etc
CDL
CDL4mo ago
what in the heck is going on here /me reads
b1mind
b1mind4mo ago
You got me really curious now do you have any documentation to link about this?
CDL
CDL4mo ago
is SFO lying to me?
No description
b1mind
b1mind4mo ago
Oh I wonder if its only svg's cause its returning the actual .svg code as a componentn 🤔 I linked you actual docs why are you on SO ...
CDL
CDL4mo ago
this was before your link
b1mind
b1mind4mo ago
well xD All that is doing is saving a string to a variable then reusing the string You get me?
CDL
CDL4mo ago
I get that
b1mind
b1mind4mo ago
Where as importing via Vite is creating an asset it will handle as such.
CDL
CDL4mo ago
For some reason I thought it was that simple to do this, but your answer says it's a little more to it my little autistic brain is trying to wrap it's dumbass mind around this lmao
b1mind
b1mind4mo ago
You can do it either way just understand what you are doing via each. React typically does it the same way too (specially in vite) you want to import imgURL from 'path'
CDL
CDL4mo ago
ok so I have the variable way working.. I just now need to get my head around your way
b1mind
b1mind4mo ago
Also why one you need to have in a static/public path and the other you are importing from src 😉 Otherwise you might have issues during build 🤷‍♂️
CDL
CDL4mo ago
hmm, true Well, sounds like I need to go back to learning basic html
missymae#2783
missymae#27834mo ago
imports and paths are just a bit messy at first.
b1mind
b1mind4mo ago
the fact you were just putting a string into a div maybe so 😄 na you just gotta think about what you are doing more
CDL
CDL4mo ago
bit worrying no? 3 years of "studying" and I can't even do this shit 😄 anyway, thanks
b1mind
b1mind4mo ago
Look you keep leaving it... and learning all this other wack o mole shit? Can you even say 3 years if you don't actually focus on the web most the time 😄 I've been learning CSS/HTML for 3 years, more so than JS But its all I really focus on... nothing else. My end goal is building websites... I build websites and learn how to build them better as I go 😄 but its ALL I DO Party why I suck at design anymore... cause I don't practice it hardly at all. Where 20 years ago it was what I did more, but I was not as good as coding. Feelme?
CDL
CDL4mo ago
I mean, I just have no path of where I'm going, so I literally just make shit up every day I start studying, so it's a repetitive cycle of bollocks, anyway we're straying off-topic here, import my damn image 😄
b1mind
b1mind4mo ago
Srry I just hate when you start stank thinking (stap it)
CDL
CDL4mo ago
I've become MD the Scottish version
b1mind
b1mind4mo ago
Na not even close but don't get lost in what you wanna do
CDL
CDL4mo ago
I don't say this in like a "woah pitty me" way btw
b1mind
b1mind4mo ago
lol feels
CDL
CDL4mo ago
I say it in a way like "shit, do I have the fucking time and mental capacity to actually keep this up" lol
b1mind
b1mind4mo ago
If it makes you feel better I don't import images proper on my folio 😄 cause I'm pulling from a .json file as "database" Took me awhile to realize they were not cached because of this 😂 but I've had lots of prior knowhow to even notice that right? lol
CDL
CDL4mo ago
I have no idea why I'm even doing this hahahaha
CDL
CDL4mo ago
What's the plan CDL, where ar eyou going with this
No description
b1mind
b1mind4mo ago
🤷‍♂️ only you know that answer, I'd hope you enjoy it though. 🤘
CDL
CDL4mo ago
This is slightly on topic but hey i imagine i cant fill this div with the image without it skewing the image right because it's not a square image
b1mind
b1mind4mo ago
You would want to use object-fit probably or more to my style you let your content determine size as much as possible 😄
CDL
CDL4mo ago
Yeah fair, I think I was originally trying to copy the youtube layout which has everything the same sized box
b1mind
b1mind4mo ago
yup in that case you would want to fit
CDL
CDL4mo ago
aight il try get the hang of it, doesnt work initially, but i think its due to targetting infact, it definitely is, I was doing object-fit on the div, not the image