All Projects → cupcakearmy → svelte-cloudinary

cupcakearmy / svelte-cloudinary

Licence: MIT license
Cloudinary SDK for Svelte

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to svelte-cloudinary

svelte3-translation-ru
Russian translation of the official Svelte resources
Stars: ✭ 49 (+276.92%)
Mutual labels:  svelte, sapper
sapper-template-rollup
Starter Rollup template for Sapper apps using postcss, cssnano, tailwindcss, and svelte-preprocess.
Stars: ✭ 32 (+146.15%)
Mutual labels:  svelte, sapper
svelteit
Svelteit is a minimalistic UI/UX component library for Svelte and Sapper projects
Stars: ✭ 64 (+392.31%)
Mutual labels:  svelte, sapper
sapper-typescript-esbuild-template
Sapper template with ESBuild and TypeScript
Stars: ✭ 18 (+38.46%)
Mutual labels:  svelte, sapper
auth
Sapper Authentication Implementation for Wordpress
Stars: ✭ 18 (+38.46%)
Mutual labels:  svelte, sapper
sapper-httpclient
An isomorphic http client for Sapper
Stars: ✭ 48 (+269.23%)
Mutual labels:  svelte, sapper
sapper-postcss-template
A template that includes Sapper for Svelte and PostCSS preprocessing with Tailwind CSS
Stars: ✭ 84 (+546.15%)
Mutual labels:  svelte, sapper
form-validation
FormValidation, the best validation library for JavaScript
Stars: ✭ 137 (+953.85%)
Mutual labels:  svelte, sapper
cloudinary-module
Integration of Cloudinary to Nuxt.js
Stars: ✭ 129 (+892.31%)
Mutual labels:  cloudinary, cloudinary-sdk
cloudinary-api
Shorter and lighter APIs for Cloudinary
Stars: ✭ 41 (+215.38%)
Mutual labels:  cloudinary, cloudinary-sdk
svelte-commerce
Svelte ecommerce - Headless, Authentication, Cart & Checkout, TailwindCSS, Server Rendered, Proxy + API Integrated, Animations, Stores, Lazy Loading, Loading Indicators, Carousel, Instant Search, Faceted Filters, Typescript, Open Source, MIT license. 1 command deploy to your own server, 1 click deploy to netlify.
Stars: ✭ 695 (+5246.15%)
Mutual labels:  svelte, sapper
upswyng
UpSwyng is a mobile-ready, digital directory of resources to assist the unhoused and at-risk communities of Boulder, CO
Stars: ✭ 19 (+46.15%)
Mutual labels:  svelte, sapper
sirix-svelte-frontend
A GUI console for SirixDB, using Svelte/Sapper.
Stars: ✭ 23 (+76.92%)
Mutual labels:  svelte, sapper
status-page
⬆️🌐 Static status page website for Upptime
Stars: ✭ 94 (+623.08%)
Mutual labels:  svelte, sapper
cakcuk
A Command Bot Interface builder, CLI-based to easily create your CLI commands for your Workspace
Stars: ✭ 13 (+0%)
Mutual labels:  svelte, sapper
blog-template
A markdown-style blog template for Sapper
Stars: ✭ 29 (+123.08%)
Mutual labels:  svelte, sapper
SENT-template
Skip setup and start code with SENT (Sapper Express Node Template) and other tools
Stars: ✭ 69 (+430.77%)
Mutual labels:  svelte, sapper
sensor.community
✨ new shiny website built with svelte ❤️ for dust and noise measuring project 👉 https://sensor.community
Stars: ✭ 45 (+246.15%)
Mutual labels:  svelte, sapper
year-in-dev
A web tool tool that displays a summary of your DEV.to blog's stats!
Stars: ✭ 22 (+69.23%)
Mutual labels:  svelte, sapper
tikz
Random collection of standalone TikZ images
Stars: ✭ 87 (+569.23%)
Mutual labels:  svelte

svelte-cloudinary

dependencies downloads badge types badge version badge minzip size badge

This is a little library that aims at integrating and making it easier to use the svelte with Cloudinary. There is an official integration coming, but it's not ready and for not does not work great for now (SSR e.g.).

🌈 Features

  • Fully typed and typescript compatible
  • Tiny: ~30kb gzip (Of which 99% is the cloudinary dep.)
  • Automatic resizing based on the DOM and applied CSS
  • Automatic lazy loading
  • Fully compatible with native cloudinary options
  • Sapper (SSR) compatible

🗂 Docs

Questions & More -> Discussions

🚀 Quickstart

npm install svelte-cloudinary
<script>

  import { image, initialize } from 'svelte-cloudinary'

  initialize({ cloud_name: 'myCloudinaryCloud' })

  const src = 'my/cloudinary/asset'
</script>

<style>
  img {
    width: 50vw;
    height: 50vh;
    object-fit: cover;
  }
</style>

<img
  use:image={{ src, bind: true, lazy: true }}
  class="home-img"
  alt="background" />

This will formulate the Cloudinary url and insert it into the img.src property. Also it will resize to the img object itself because we set bind: true.

⚠️ In firefox if you want to use the bind option to automatically compute sizes you need to set img { display: inline-block or block; } otherwise there can be problems with getComputedStyle.

☁️ Cloudinary Setup

If you want the use super handfull auto upload function (which I think will apply to everyone) you have to set configure a few settings first.

  • Settings -> Security -> Allowed fetch domains: Add your domain(s) from which the images are fetched from.
  • Settings -> Upload -> Auto upload mapping: Set the mapping for your domain and/or path
Example

Immagine you want to serve an image with the url of: https://api.example.org/images/elephants.png

  1. Settings -> Security -> Allowed fetch domains: Add api.example.org to the list.
  2. Settings -> Upload -> Auto upload mapping:
    Folder: myimages
    URL prefix: https://api.example.org/images/

Now you can use the auto upload funtion like this:

<img
  use:image={{ src: 'myimages/elephants.png' }}
  class="home-img"
  alt="background" />

🤔 Why an action and not component?

Well components are great of course, but when we only need to set a src tags we can leverage the upsides of a svelte action

What are benefits?

  • Native styling (Svelte for now does not allow easy styling of child components)
    With an action we can easily use local styling beacuse we still have access to the <img /> element
  • Lightweight
  • Reusable and composable

Downsides:

  • The src will not be set serverside, so also not in the initial server response. Which I believe is not that bad though for images.

👷 Sapper

If you are using sapper you can initialize it once in your root _layout.svelte.

<script lang="ts">
	import { initialize } from 'svelte-cloudinary'

	initialize({ cloud_name: 'mycloud' })

  // ...
</script>

🤓 Key Concepts

lazy

default true

If images should use the Intersection Observer API to lazy load.

step

default 200

The step property helps reducing the amounts of transformations cloudinary need to perform and less credits being billed to you.

How? Basically whenever we calculate the dynamic size of the image rendered on the DOM we will get very specific numbers depending on the device.

With step we approximate the size needed to the nearset bigger multiple of step.

Example

Imagine the same <img /> has the width of 420,470 and 1080 on an iPhone, Android and Laptop respectively.

With a step size of e.g. 100 (<img use:image={{ ..., step: 100 }} />) they will become 500, 500, and 1100. This will limit the ammount of transformations needed.

bind

With bind we can dynamically set the width and/or height of the transformed image depending of the rendered size.

  • bind: this The size of the <img /> element
  • bind: el The computed size of another element in the dom (useful for a carousel e.g.)
  • bind: { width: this } Only bind the width, leaving the height free. Also works with height of course
  • bind: { width: '.wrapper' } Finds the closest element that matches the selector and uses it for width.
Note

If you provide a bind options (<img use:image={{..., bind: true }} />) but no crop option, we will automatically add crop: 'fill' otherwise the image will not be resized by cloudinary.

TLDR;
<img use:image={{ src, bind: true }} />
<!-- Is internally equivalent to the following -->
<img use:image={{ src, bind: true, options: { crop: 'fill' } }} />

⌨️ Examples

Fixed size

<img
  use:image={{ src, options: { width: 200, height: 100, crop: 'fill' } }}
/>

bind

<!-- Simple -->
<img
  use:image={{ src, bind: true, }}
/>
<!-- Bind size to parent with selectors -->
<div class="wrapper"
  <img
    use:image={{ src, bind: '.wrapper', }}
  />
</div>
<!-- Bind width to parent with element -->
<div class="wrapper"
  <img
    use:image={{ src, bind: { width: '.wrapper' }, }}
  />
</div>

options

Native cloudinary options. See here for official docs For a quick glance

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].