Giter Site home page Giter Site logo

vue-size-provider's Introduction

vue-size-provider

Declarative element size observer and provider.

Motivation

Sometimes you may want to animate an element height when its content is changed. In that case, you need to directly read height value from DOM because Virtual DOM cannot acquire element size. Since it is low-level manipulation, the code would become messier.

vue-size-provider solves this problem by hiding low-level code with abstract helper components - <SizeProvider> and <SizeObserver>. The following gif is an example to show how vue-size-provider works:

Simple demo of vue-size-provider

Install

Install it via npm:

$ npm install vue-size-provider

Then, notify Vue to use it:

import Vue from 'vue'
import VueSizeProvider from 'vue-size-provider'

Vue.use(VueSizeProvider)

Or you can directly use the components:

<script>
import { SizeProvider, SizeObserver } from 'vue-size-provider'

export default {
  components: {
    SizeProvider,
    SizeObserver
  }
}
</script>

Usage

First, wrap elements that you would like to observe their size by <SizeObserver>.

<template>
  <!-- observe content size -->
  <SizeObserver>

    <!-- arbitrary contents that you want to observe their size -->

  </SizeObserver>
</template>

<script>
import { SizeProvider, SizeObserver } from 'vue-size-provider'

export default {
  components: {
    SizeProvider,
    SizeObserver
  }
}
</script>

Then, wrap them by <SizeProvider> and any element that you want to animate its size when the contents size is changed.

<template>
  <!-- provide observed content size via scoped slot -->
  <SizeProvider v-slot="{ width, height }">
    <div class="wrapper" :style="{ height: height + 'px' }">

      <!-- observe content size -->
      <SizeObserver>

        <!-- arbitrary contents that you want to observe their size -->

      </SizeObserver>

    </div>
  </SizeProvider>
</template>

<script>
import { SizeProvider, SizeObserver } from 'vue-size-provider'

export default {
  components: {
    SizeProvider,
    SizeObserver
  }
}
</script>

Finally, you need to write some animation code. In this example, we simply use CSS transition:

<template>
  <!-- provide observed content size via scoped slot -->
  <SizeProvider v-slot="{ width, height }">
    <div class="wrapper" :style="{ height: height + 'px' }">

      <!-- observe content size -->
      <SizeObserver>

        <!-- arbitrary contents that you want to observe their size -->

      </SizeObserver>

    </div>
  </SizeProvider>
</template>

<script>
import { SizeProvider, SizeObserver } from 'vue-size-provider'

export default {
  components: {
    SizeProvider,
    SizeObserver
  }
}
</script>

<style scoped>
.wrapper {
  box-sizing: content-box;
  border: 1px solid #000;
  
  /* animate content height smoothly */
  transition: height 300ms ease-out;
}
</style>

Note that you may want to specify box-sizing: content-box; to the animating element because the provided width and height are content size of the observed element. i.e. They do not include padding and border size.

License

MIT

vue-size-provider's People

Contributors

dependabot-preview[bot] avatar ktsn avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

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.