Git Product home page Git Product logo

svelte-cloudinary's Introduction

DEPRECATED

See https://github.com/cloudinary-community/svelte-cloudinary

The npm package will be transfered to them.

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

svelte-cloudinary's People

Contributors

cobypear avatar cupcakearmy avatar edjw avatar flbn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cobypear

svelte-cloudinary's Issues

Image height and width not applied as transformation

Hello,
I've managed to get this library mostly working, however it seems that passing in height and width as options does not achieve the desired outcome.

Expected: Images are transformed, and the url from cloudinary shows the proper transformation
Actual: No transformation is applied when the image is fetched.

Here is what I'm getting back, note no w_256 in the url:
"http://res.cloudinary.com/{Cloud Name}/image/upload/f_auto,q_auto/{image_name}"

Here's my component:

 <img
    use:image={{
      src: 'image_name',
      options: {
        width: 256,
        height: 250,
      },
    }}
    alt="Logo"
  />

Let me know if you need more information. Thanks for making this btw :)

Unmet peer dependency "lodash@>=4.0".

When building an app with svelte-cloudniary on Vercel. I receive the following warning:

warning "svelte-cloudinary > [email protected]" has unmet peer dependency "lodash@>=4.0".

The build then fails trying to resolve lodash:

> Using @sveltejs/adapter-vercel
--
08:52:04.659 | Error: Cannot find module 'lodash/assign'
08:52:04.659 | Require stack:
08:52:04.659 | - /vercel/path0/node_modules/cloudinary-core/cloudinary-core.js
08:52:04.659 | - /vercel/path0/node_modules/svelte-cloudinary/dist/index.js
08:52:04.660 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
08:52:04.660 | at Function.Module._load (internal/modules/cjs/loader.js:746:27)
08:52:04.660 | at Module.require (internal/modules/cjs/loader.js:974:19)
08:52:04.660 | at require (internal/modules/cjs/helpers.js:93:18)
08:52:04.660 | at webpackUniversalModuleDefinition (/vercel/path0/node_modules/cloudinary-core/cloudinary-core.js:10:28)
08:52:04.660 | at Object.<anonymous> (/vercel/path0/node_modules/cloudinary-core/cloudinary-core.js:17:3)
08:52:04.661 | at Module._compile (internal/modules/cjs/loader.js:1085:14)
08:52:04.661 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
08:52:04.661 | at Module.load (internal/modules/cjs/loader.js:950:32)
08:52:04.661 | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
08:52:04.661 | Error: Cannot find module 'lodash/assign'
08:52:04.662 | Require stack:
08:52:04.662 | - /vercel/path0/node_modules/cloudinary-core/cloudinary-core.js
08:52:04.662 | - /vercel/path0/node_modules/svelte-cloudinary/dist/index.js
08:52:04.663 | > 500 /
08:52:04.664 | Error: 500 /
08:52:04.664 | at file:///vercel/path0/node_modules/@sveltejs/kit/dist/chunks/index5.js:167:11
08:52:04.664 | at visit (file:///vercel/path0/node_modules/@sveltejs/kit/dist/chunks/index5.js:326:5)
08:52:04.692 | error Command failed with exit code 1.
08:52:04.692 | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
08:52:04.703 | Error: Command "yarn run build" exited with 1

Willing to spend some time on this today but I wanted to open an issue in case it's just me :)

TypeError: __vite_ssr_import_11__.initialize is not a function on SvelteKit & fix

Hi there,

I tried to follow the docs (I'm using SvelteKit) but received the following error:

TypeError: __vite_ssr_import_11__.initialize is not a function
    at eval (/src/routes/__layout.svelte:80:25)

I fixed it by wrapping the initialization in a browser check. So instead of

initialize({ cloud_name: 'my-cloud-name' });

I had to use

import { browser } from '$app/env';

if (browser) {
  initialize({ cloud_name: 'my-cloud-name' });
}

This fixed it for me.

Hope this is useful for the docs. Or should I have done something else?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.