Nuxt 3

nuxt-cover
Use TwicPics Nuxt 3 Components to get images and videos integration best practices out-of-the-box.

You can find usage examples in our online demo project.

Installation

Add the @twicpics/components package to your project:

# Using yarn
yarn add @twicpics/components
# Or using npm
npm install @twicpics/components

Setup

Setting-up TwicPics Components into your Nuxt3 project

TwicPics components for Nuxt3 comes as an Nuxt3 Module and is configured as such.

You will need a TwicPics domain to initialize the package. Create an account for free to get your domain.

nuxt.config.ts

Add @twicpics/components/nuxt3 to the modules section

With your twicpics configuration

export default defineNuxtConfig({
  modules: [`@twicpics/components/nuxt3`],
  twicpics: {
    domain: `https://<your-domain>.twic.pics`,
  },
})
// here is an example of a `nuxt.config.ts` configured with TwicPics.
export default defineNuxtConfig({
  modules: [`@twicpics/components/nuxt3`],
  twicpics: {
    domain: `https://<your-domain>.twic.pics`,
    anticipation: 0.5,
    step: 50,
  },
})

Setup Options

OptionDescriptionTypeDefault
anticipationTwicPics will lazy-load images by default. To avoid too abrupt a transition with elements appearing into view and then images very obviously loading afterwards, TwicPics will "anticipate" lazy loading by a factor of the actual viewport. This behavior is controlled by this setting.Number0.2
domainThis is your very own TwicPics domain. Providing it is mandatory.String
envCan be debug, offline or production. When set to debug, a gray lightweight svg placeholder that displays its intrinsic dimensions is displayed in place of all medias targeted by their src value. When set to offline, these medias are replaced by a simple placeholder that allows to visualise their display area.String"production"
handleShadowDomMust be set to true when using TwicComponents within a shadow DOM.booleanfalse
maxDPRTwicPics will take the "Device Pixel Ratio" (DPR) of the current device into consideration when determining the sizes of images to load. By default, it will not take a DPR greater than 2 into consideration. If the DPR of the device is higher than 2, TwicPics will assume it to be 2. Using maxDPR, you can lower this limit down to 1 or be more permissive (for instance by setting it to 3 or 4).Number2
pathPath to prepend to all src attributes. For instance, if path is "some/folder" then a src attribute set to "image.jpg" will be expanded into "some/folder/image.jpg"String
stepTo avoid requesting too may variants of the same image, TwicPics will round the width of images to the closest multiple of step. The height will then be computed in order to respect the original aspect ratio.Integer10

Usage

TwicImg and TwicVideo are available in your templates as long as you have configured the TwicPics Nuxt3 Module.

Just use them in your template files in place of img or video tags (see Components Properties).

<template>
  <main>
    <TwicImg src="path/to/your/image" />
  </main>
</template>

Basic usage

<!-- component.vue -->
<template>
  <main>
    <TwicImg src="path/to/your/image" />
  </main>
</template>

Open in StackBlitz

Bulk loading with TwicView

By default, <TwicImg> and <TwicVideo> will only start loading when they come into the viewport. But sometimes, you may want to load multiple assets in bulk instead of lazy loading them. This is where <TwicView> comes into play.

The <TwicView> components eager loads all of his <TwicImg> and <TwicVideo> children as soon as it comes into the viewport (depending on your anticipation settings.)

For example, if you're building a carousel, you might want to bulk load all images. In the following code, all three images will be loaded when TwicView comes into the viewport:

<TwicView>
  <TwicImg src="image1.jpg" />
  <TwicImg src="image2.jpg" />
  <TwicImg src="image3.jpg" />
</TwicView>

Open in StackBlitz

Image magnifier

The <TwicImg> component allows to display a lazy loaded zoomed version of your image on mouse over.

To activate the zoom feature, simply set the zoom property to a number strictly greater than 1. This number represents the magnification factor of your image.

For example:

<TwicImg src="image1.jpg" zoom="2" /> <TwicImg src="image2.jpg" zoom="2.5" />

The zoom factor can also be configured through the --twic-zoom CSS variable.

To activate the style-driven zoom, simply set zoom property to'css' and add a new rule to your stylesheet.

For example:

<TwicImg src="image3.jpg" zoom="css" class=".zoom-3" />
.zoom-3 {
  --twic-zoom: 3;
}

It applies only to TwicImg component in cover mode.

Open in StackBlitz

Lifecycle

Passing a callback function to stateChange emitter gives access to the loading state of your image or video.

Here are the values the Component will emit (see State Type definition) :

  • new: when the img or video source has not started loading
  • loading: when the img or video source is loading
  • done: when the img or video source has successfully loaded
  • error: when an error occurred while loading the img or video source
<!-- component.vue -->
<template>
  <main>
    <div class="style-driven-responsive">
      <TwicImg src="path/to/your/image" @stateChange="handleStateChange" />
    </div>
  </main>
</template>

<script>
  export default {
    name: 'App',
    data() {
      state: undefined
    },
    methods: {
      handleStateChange(stateEvent) {
        // Implement the logic here
        const { state } = stateEvent
        this.state = state
        console.log(`TwicComponent emits a new state`, this.state)
      },
    },
  }
</script>

Open in StackBlitz

Refit Example

The <TwicImg> component allows to reframe your image on the main subject(s) it contains.

In cover mode, the resulting image will respectratio while maximizing the area occupied by the main subject(s).

In contain mode, the image will be cropped as close as possible to the main subject(s).

To activate automatic cropping, simply add the refit property to your component.

By default, the subject will be placed at the center of the resulting image but it is possible to align the subject with a given border by specifying an anchor.

Also by default, the subject will touch the borders of the resulting image. This can be avoided by setting refit with a comma-separated length value defining padding.

For example:

  <!-- default refit: centered object(s), no padding around -->
  <TwicImg src="image1.jpg" refit />

  <!-- main subject(s) will be left aligned -->
  <TwicImg src="image3.jpg" anchor="left" refit/>

  <!-- a 5% padding will be applied around main subject(s) -->
  <TwicImg src="image2.jpg" refit="5p" />

  <!-- a 5% padding will be applied vertically, a 10% padding will be applied horizontally -->
  <TwicImg src="image3.jpg" refit="5p,10p" />

Open in StackBlitz

Style-Driven Approach

You can set up components using pure CSS and the power of CSS variables

<!-- component.vue -->
<template>
  <main>
    <div class="twic-item landscape">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
    <div class="twic-item square">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
    <div class="twic-item portrait">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
    <div class="twic-item contain left">
      <TwicImg src="path/to/your/image" ratio="16/9"></TwicImg>
    </div>
    <div class="twic-item contain right">
      <TwicImg src="path/to/your/image" ratio="16/9"></TwicImg>
    </div>
    <div class="twic-item lg">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
    <div class="twic-item md">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
    <div class="twic-item sm">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
    <!---
    Attributes take precedence over CSS.
    In the next example, ratio will 16/9 AND NOT 1 as defined in square css class 
--->
    <div class="cover square">
      <TwicImg src="path/to/your/image" ratio="16/9"></TwicImg>
    </div>
  </main>
</template>

<script>
  export default {
    name: 'App',
  }
</script>

<style>
  .landscape {
    --twic-ratio: calc(4 / 3);
  }
  .portrait {
    --twic-ratio: calc(3 / 4);
  }
  .square {
    --twic-ratio: calc(1);
  }

  .lg {
    width: 300px;
  }

  .md {
    width: 150px;
  }

  .sm {
    width: 100px;
  }
</style>

Open in StackBlitz

Responsive Example

Setting up components using CSS and CSS variables enables hassle-free responsive designs.

<!-- component.vue -->
<template>
  <main>
    <div class="style-driven-responsive">
      <TwicImg src="path/to/your/image"></TwicImg>
    </div>
  </main>
</template>

<script>
  export default {
    name: 'App',
  }
</script>

<style>
  .style-driven-responsive {
    --twic-ratio: calc(2 / 3);
    --twic-mode: cover;
    margin: auto;
  }

  @media (min-width: 640px) {
    .style-driven-responsive {
      --twic-ratio: calc(1);
    }
  }

  @media (min-width: 768px) {
    .style-driven-responsive {
      --twic-ratio: calc(4 / 3);
    }
  }

  @media (min-width: 1024px) {
    .style-driven-responsive {
      --twic-ratio: calc(16 / 9);
    }
  }

  @media (min-width: 1280px) {
    .style-driven-responsive {
      --twic-ratio: calc(1.85);
    }
  }

  @media (min-width: 1536px) {
    .style-driven-responsive {
      --twic-ratio: calc(21 / 9);
    }
  }
</style>

Your template features a single component that will follow your CSS directives and behave responsively.

Open in StackBlitz

Working with ratio="none"

Particularly useful when creating hero banner, you can specify the height of your image while respecting its natural aspect ratio and optimizing your Cumulative Layout Shift (CLS) metric.

<!-- component.vue -->
<template>
  <TwicImg src="path/to/your/image" class="hero-image" ratio="none"></TwicImg>
</template>

<script>
  export default {
    name: 'App',
  }
</script>

<style>
  /* You are responsible for properly sizing the component. */
  .hero-image {
    height: 500px;
  }

  @media (min-width: 1024px) {
    .hero-image {
      height: 300px;
      width: 100%;
    }
  }
</style>

Open in StackBlitz

Components Properties

TwicImg

This component can be used in place of an img element.

<TwicImg
  src="<path>"
  alt="<String>"
  anchor="<String>"
  bot="<String>"
  eager="<boolean>"
  focus="<auto|coordinates>"
  intrinsic="<String>"
  mode="<contain|cover>"
  onStateChange="<function>"
  position="<css position>"
  placeholder="<preview|maincolor|meancolor|none>"
  preTransform="<String>"
  ratio="<ratio>"
  refit="<boolean|String>"
  step="<integer>"
  title="<String>"
  transition="<fade|zoom|none>"
  transitionDelay="<String>"
  transitionDuration="<String>"
  transitionTimingFunction="<String>"
  zoom="<String | Number>"
/>
AttributeDescriptionTypeDefault
altalt attribute contentString
anchorPositions the image in both contain and cover mode. Accepted values are top, bottom, left, right, top-left, top-right, bottom-left, bottom-right and center. position and focus take precedence in contain and cover mode respectively. Please note that anchor is applied after an eventual preTransform.String
botA slash-separated list of TwicPics API transformations to be performed for search engine bots. This overrides all other transformations when provided, even if empty (i.e bot=""). See the TwicPics bot attribute documentation for more information.String
eagerLoad the image as soon as the component is mounted. This effectively means disabling lazy loading for this image.booleanfalse
focusSets the focus point in cover mode. focus takes precedence over anchor when both are provided. See the TwicPics focus attribute documentation for more information. Only use this attribute if you need a specific focus point or if you want to leverage smart cropping with focus="auto": if you only need border-based positionning (top, bottom, left, right, etc), use anchor instead.String
onStateChangeA callback function triggered each time the asset loading state is updated. State can be new, loading, done or error.( stateEvent: StateEvent ) => void
intrinsicDimensions in pixels of the original image, formatted <width>x<height> (eg. 1920x1080). It prevents image upscaling and limits the number of generated variants. If using preTransform, you should specify the intrinsic dimensions of the resulting image. Using incorrect values can lead to display issues, see the intrinsic attribute documentation.String
modeCan be contain or cover and determines if the image fills the area and is cropped accordingly (cover) or if the image will sit inside the area with no cropping (contain).Stringcover
placeholderCan be preview, meancolor, maincolor or none. See the TwicPics output transformation documentation for more information. Setting will be overridden to none when using zoom transition.Stringpreview
positionPositions the image in contain mode. position takes precedence over anchor when both are provided. Syntax is the same as for CSS position properties background-position and object-position. Only use this attribute if you need precise positionning: if you only need border-based positionning (top, bottom, left, right, etc), use anchor instead.Stringcenter
preTransformA slash-separated list of TwicPics API transformations to be performed before resizing the image (see the TwicPics Manipulation documentation). Note that anchor and focus are applied after preTransform: if you need to specify a specific focus point for your preTransform then it needs to be part of the expression (like preTransform="focus=auto/crop=50px50p" for instance). Be aware that using this option can lead to unexpected results so use with caution!String
ratioA unitless width/height or width:height value pair (as in 4/3 or 4:3) that defines the aspect ratio of the display area. If height is not specified, it is assumed to be 1. A square area will be created by default. When set to none, ratio is determined based on width and height as computed by the browser following your CSS definitions. The --twic-ratio CSS variable is ignored in this instance. You are responsible for properly sizing the component when ratio="none".String or number1
refitReframes the image to maximize the area occupied by the main object(s) while respecting ratio in cover mode. Crops the image as close as possible to the main object(s) in contain mode. Can be true, false or a list of comma-separated length defining padding. See the TwicPics refit documentation for more information.boolean or String false
srcPath to the image. When not provided, a red lightweight svg placeholder that displays its intrinsic dimensions is displayed in place of the absent image. When env is set to offline, that red lightweight svg is replaced by a simple red placeholder.String
stepSee the TwicPics step attribute documentation for more information.Integer10
titletitle representing information related to the image. See global attribute title.String
transitionDetermines how the image will be revealed once loaded. With a fade in effect (fade), a zoom effect (zoom) or without any transition (none). Unsupported values are handled as fade.Stringfade
transitionDurationDuration of the transition effect.String400ms
transitionTimingFunctionCSS timing function applied to the transition effect.Stringease
transitionDelayTransition delay of the transition effect.String0ms
zoomEnables zoom feature and sets the magnification factor. Must be a number strictly greater than 1 as in "1.5" or 1.5. When set to 'css' magnification factor is defined through the CSS variable --twic-zoom. Should only be applied to images in cover mode. See Image magnifier.`Stringnumber`

TwicVideo

This component can be used in place of a video element.

<TwicVideo
  src="<path>"
  anchor="<String>"
  bot="<String>"
  duration="<String|number>"
  eager="<boolean>"
  from="<String|number>"
  focus="<auto|coordinates>"
  intrinsic="<String>"
  mode="<contain|cover>"
  position="<css position>"
  posterFrom="<String|number>"
  placeholder="<preview|maincolor|meancolor|none>"
  preTransform="<String>"
  ratio="<ratio>"
  (stateChangeEvent)="<function>"
  step="<integer>"
  title="<String>"
  to="<String|number>"
  transition="<fade|zoom|none>"
  transitionDelay="<String>"
  transitionDuration="<String>"
  transitionTimingFunction="<String>"
/>
AttributeDescriptionTypeDefault
anchorPositions the video in both contain and cover mode. Accepted values are top, bottom, left, right, top-left, top-right, bottom-left, bottom-right and center. position and focus take precedence in contain and cover mode respectively. Please note that anchor is applied after an eventual preTransform.String
botA slash-separated list of TwicPics API transformations to be performed for search engine bots. This overrides all other transformations when provided, even if empty (i.e bot=""). See the TwicPics bot attribute documentation for more information.String
durationLimits the duration of the video. duration is expressed in seconds and must be a positive number. duration will not move the starting point of the video: to do so, you'll have to use the from property. See duration documentation.String or number
eagerLoad the video as soon as the component is mounted. This effectively means disabling lazy loading for this video.booleanfalse
focusSets the focus point in cover mode. focus takes precedence over anchor when both are provided. See the TwicPics focus attribute documentation for more information. Only use this attribute if you need a specific focus point or if you want to leverage smart cropping with focus="auto": if you only need border-based positionning (top, bottom, left, right, etc), use anchor instead.String
fromMoves the starting point of the video. from is expressed in seconds and must be a positive number. from will not move the end point of the video: to do so, you'll have to use the duration or to properties. See from documentation. See from documentation.String or number
intrinsicDimensions in pixels of the original video, formatted <width>x<height> (eg. 1920x1080). It prevents video upscaling and limits the number of generated variants. If using preTransform, you should specify the intrinsic dimensions of the resulting video. Using incorrect values can lead to display issues, see the intrinsic attribute documentation.String
modeCan be contain or cover and determines if the video fills the area and is cropped accordingly (cover) or if the video will sit inside the area with no cropping (contain).Stringcover
placeholderCan be preview, meancolor, maincolor or none. See the TwicPics output transformation documentation for more information. Setting will be overridden to none when using zoom transition.Stringpreview
positionPositions the video in contain mode. position takes precedence over anchor when both are provided. Syntax is the same as for CSS position properties background-position and object-position. Only use this attribute if you need precise positionning: if you only need border-based positionning (top, bottom, left, right, etc), use anchor instead.Stringcenter
posterFromDetermines which frame of the source video should be used as a poster / preview. posterFrom is expressed in seconds and must be a positive number. By default posterFrom is equal to 0, meaning the very first frame of the video is used. posterFrom will not modify the video in any way: to do so, you'll have to use the duration, from or to properties.String or number
preTransformA slash-separated list of TwicPics API transformations to be performed before resizing the video (see the TwicPics Manipulation documentation). Note that anchor and focus are applied after preTransform: if you need to specify a specific focus point for your preTransform then it needs to be part of the expression (like preTransform="focus=auto/crop=50px50p" for instance). Be aware that using this option can lead to unexpected results so use with caution!String
ratioA unitless width/height or width:height value pair (as in 4/3 or 4:3) that defines the aspect ratio of the display area. If height is not specified, it is assumed to be 1. A square area will be created by default. When set to none, ratio is determined based on width and height as computed by the browser following your CSS definitions. The --twic-ratio CSS variable is ignored in this instance. You are responsible for properly sizing the component when ratio="none".String or number1
srcPath to the video. When not provided, a red lightweight svg placeholder that displays its intrinsic dimensions is displayed in place of the absent video. When env is set to offline, that red lightweight svg is replaced by a simple red placeholder.String
stateChangeEventAn event emitter triggered each time the asset loading state is updated. State can be new, loading, done or error.EventEmitter<StateEvent>
stepSee the TwicPics step attribute documentation for more information.Integer10
titletitle representing information related to the video. See global attribute title.String
toMoves the end point of the video. to is expressed in seconds and must be a positive number. to will not move the starting point of the video: to do so, you'll have to use the from property. See to documentation.String or number
transitionDetermines how the video will be revealed once loaded. With a fade in effect (fade), a zoom effect (zoom) or without any transition (none). Unsupported values are handled as fade.Stringfade
transitionDurationDuration of the transition effect.String400ms
transitionTimingFunctionCSS timing function applied to the transition effect.Stringease
transitionDelayTransition delay of the transition effect.String0ms

Loading State Values

Union type for all possible image or video loading state.

type State = `error` | `done` | `loading` | `new`
  • new: when the img or video source has not started loading
  • loading: when the img or video source is loading
  • done: when the img or video source has successfully loaded
  • error: when an error occurred while loading the img or video source

State Change Event

Data type passed as parameter to stateChange emitter.

export type StateEvent = {
  target: TwicImg | TwicVideo
  state: State
}

CSS Variables

List of variables that can be used to configure your components using pure CSS.

<selector > {
  --twic-ratio: <ratio>;
  --twic-mode: <contain|cover>;
  --twic-position: <css position>;
  --twic-transition-delay: <string>;
  --twic-transition-duration: <string>;
  --twic-transition-timing-function: <string>;
  --twic-zoom: <number>;
}

Each CSS variable corresponds to one of the components attributes listed in the Components Properties section. If present, the attribute takes precedence over the corresponding CSS variable.

VariableDescriptionHTML AttributeDefault
--twic-modeCan be contain or cover and determines if the image fills the area and is cropped accordingly (cover) or if the image will sit inside the area with no cropping (contain).modecover
--twic-positionOnly useful in contain mode. Locates the image inside the area. Syntax is the same as for CSS position properties like background-position or object-position. Useful values are top, bottom, left, right, left top, left bottom and so on.positioncenter
--twic-ratioFloating point value corresponding to a unitless width/height ratio (as in calc(4/3) or 1.333). Ratio will correspond to a square area by default.ratio1
--twic-transition-delayTransition delay of the transition effect.transitionDelay0ms
--twic-transition-durationDuration of the transition effect.transitionDuration400ms
--twic-transition-timing-functionCSS timing function applied to the transition effect.transitionTimingFunctionease
--twic-zoomStrictly greater than 1 floating point value corresponding to the zoom factor to be applied. Only applies to TwicImg with zoom property set to 'css'.number

Feedback and support

TwicPics Components are open-source on GitHub. For features requests and bugs, open an issue. The TwicPics team also remains available for support via the live chat (at the bottom right.)

responsive images

The All-In-One Toolbox For Your Medias

The simplest, most powerful way to tackle responsive images & videos