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:
Install
Install it via npm:
Then, notify Vue to use it:
Or you can directly use the components:
Usage
First, wrap elements that you would like to observe their size by <SizeObserver>
.
Then, wrap them by <SizeProvider>
and any element that you want to animate its size when the contents size is changed.
Finally, you need to write some animation code. In this example, we simply use CSS transition:
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.