vue-waterfall-plugin-next

repository·master·Indexed 20 days ago

https://github.com/heikaimu/vue3-waterfall-plugin

A Vue 3 waterfall layout plugin supporting PC and mobile. It features built-in animation effects via Animate.css, image lazy-loading via the LazyImg component, and a WaterfallVirtual component for high-performance rendering of large datasets. The plugin provides configurable responsive breakpoints, customizable gutters, and a renderer method for manual layout recalculations.

Tokens
3.6K
Snippets
15
Records
17
Agent score
71%

What's inside vue-waterfall-plugin-next

  1. Configure entry animations

    master

    Animations in this plugin apply when new data is inserted. To use custom animations, you must include animate.css (v4+ works out of the box; for older versions, set animationPrefix to 'animated').

    If you do not want to include animate.css and only want the default fadeIn effect, add this to your CSS:

    .animate__animated {
      animation-fill-mode: both;
      animation-duration: 1s;
    }

    Note: animation-duration in your CSS controls the speed of the 'fly-in' animation.

  2. Configure Waterfall component props

    master

    The Waterfall component accepts several props to control layout and behavior:

    PropTypeDefaultDescription
    listArray[]The list of data items
    rowKeyString'id'Unique identifier for each item (required for deletions)
    imgSelectorString'src'Path to the image field in the data object (e.g., info.img.url)
    widthNumber200Card width on PC (overridden by breakpoints)
    breakpointsObject{...}Responsive configuration for different container widths
    gutterNumber10Spacing between cards
    spaceNumber10Row spacing (if not set, uses gutter for both row and column)
    hasAroundGutterBooleantrueWhether to include gutter around the container edges
    posDurationNumber300Animation duration for cards moving to their position
    animationPrefixString'animate__animated'Prefix for animation classes
    animationEffectString'fadeIn'Entry animation effect
    animationDurationNumber1000Entry animation duration (ms)
    animationDelayNumber300Entry animation delay (ms)
    animationCancelBooleanfalseIf true, disables all animations
    backgroundColorString'#ffffff'Background color
    loadPropsObjectloadPropsConfiguration for the LazyImg component
    lazyloadBooleantrueEnable/disable lazy loading
    crossOriginBooleantrueEnable/disable cross-origin image loading
    delayNumber300Debounce time for layout refresh (ms)
    alignString'center'Card alignment: 'left', 'center', or 'right'
    horizontalOrderBooleanfalseIf true, cards are ordered left-to-right; if false, they fill the shortest column first
    heightDifferenceNumber0When horizontalOrder is false, used to pick the next column if height difference is within this value.
  3. Configure responsive breakpoints

    master

    The breakpoints prop allows you to define how many cards appear per row based on the container width, similar to CSS media queries. When breakpoints are active, the width prop is ignored.

    Example configuration:

    breakpoints: {
      1200: { rowPerView: 3 }, // When width < 1200
      800: { rowPerView: 2 }, // When width < 800
      500: { rowPerView: 1 }, // When width < 500
    }
    breakpoints: {
      1200: {
        rowPerView: 3,
      },
      800: {
        rowPerView: 2,
      },
      500: {
        rowPerView: 1,
      },
    }
  4. Configure LazyImg properties and ratio calculation

    master

    The loadProps object passed to Waterfall configures the LazyImg component. You can provide custom loading and error images, and a ratioCalculator to control the aspect ratio of the image placeholder before the actual image loads.

    ratioCalculator receives (width, height) and should return the desired ratio. This helps prevent layout shifts by fixing the image area size.

    import loading from 'assets/loading.png'
    import error from 'assets/error.png'
    
    const loadProps = {
      loading,
      error,
      ratioCalculator: (width, height) => {
        const minRatio = 3 / 4;
        const maxRatio = 4 / 3;
        const curRatio = width / height;
        if (curRatio < minRatio) return minRatio;
        if (curRatio > maxRatio) return maxRatio;
        return curRatio;
      }
    }
  5. Define responsive Breakpoints

    master

    Breakpoints allow you to define different layout configurations based on the container width. The Breakpoints type is a record where the key is the screen width (number) and the value is a Point object containing layout settings like rowPerView.

    Example structure:

    const breakpoints: Breakpoints = {
      768: { rowPerView: 2 },
      1200: { rowPerView: 4 }
    };
    export type Breakpoints = Record<number, Point>
    
    interface Point {
      rowPerView: number
    }
  6. Basic usage of the Waterfall component

    master

    To implement a waterfall layout, import Waterfall and LazyImg from vue-waterfall-plugin-next. You must also import the component's CSS. Use the #default slot to define how each item in your list is rendered. The slot provides item (the data object), url (the image source), and index.

    <script setup>
    import { LazyImg, Waterfall } from 'vue-waterfall-plugin-next'
    import 'vue-waterfall-plugin-next/dist/style.css'
    
    const list = [
      { id: 1, src: 'xxxx.jpg' },
      // ...
    ]
    </script>
    
    <template>
    <Waterfall :list="list">
      <template #default="{ item, url, index }">
        <div class="card">
          <LazyImg :url="url" />
          <p class="text">{{ item.text }}</p>
        </div>
      </template>
    </Waterfall>
    </template>
  7. LazyImg API and attributes

    master

    The LazyImg component is used within the Waterfall slot to handle image loading.

    Props:

    • ratio (Number, default 1): The default aspect ratio. Providing this allows the component to reserve the correct space before the image loads.

    Events/Methods:

    • load: Triggered by the img tag's load event.
    • success: Triggered when the image loads successfully.
    • error: Triggered when the image fails to load.
  8. Manually trigger Waterfall re-render

    master

    The Waterfall component exposes a renderer() method. You can use a template ref to access the component instance and call renderer() to manually force a layout recalculation. This is useful when using external lazy-loading callbacks that need to trigger a refresh.

    <Waterfall ref="waterfall"></Waterfall>
    
    <script setup>
    import { ref } from 'vue'
    const waterfall = ref(null)
    
    // Call this to force a redraw
    const refresh = () => {
      waterfall.value.renderer()
    }
    </script>
  9. Configure LazyImg options

    master

    When using the LazyImg component, you can provide a LazyOptions object to control its behavior. This includes setting placeholder strings for loading and error states, configuring the IntersectionObserver via observerOptions, and providing a custom ratio calculator.

    Key properties:

    • error: A string to display or use when an image fails to load.
    • loading: A string to display or use while the image is loading.
    • observerOptions: An object of type IntersectionObserverInit to configure the intersection observer (e.g., root, rootMargin, threshold).
    • log: A boolean to enable or disable logging.
    • ratioCalculator: A function (width: number, height: number) => number used to calculate the aspect ratio of the image.
    const options: LazyOptions = {
      error: 'failed-to-load-image-url',
      loading: 'loading-placeholder-url',
      observerOptions: {
        rootMargin: '0px 0px 200px 0px',
        threshold: 0.01
      },
      log: false,
      ratioCalculator: (width, height) => width / height
    };
  10. Import the Waterfall components

    master

    The vue3-waterfall-plugin package provides three main components for implementing waterfall layouts in Vue 3: Waterfall for standard layouts, WaterfallVirtual for virtualized large lists, and LazyImg for optimized image loading within the waterfall.

    import { Waterfall, WaterfallVirtual, LazyImg } from 'vue3-waterfall-plugin-next';