Vue: Feature : v-show modifier for toggling visibility

Created on 23 Feb 2016  ·  9Comments  ·  Source: vuejs/vue

It would be nice to be able to do:

<div v-show.visible="bool"></div>

... to toggle between visibility:hidden and visibility:visible.

My use case is an element with a transition. When the element transitions out, I'd prefer for the space it took up to not collapse.

Most helpful comment

how does this work in 2.0?

All 9 comments

You could create a custom directive for that and use the instance properties (this.modifiers) to get the v-directive.modifier.

The only thing i can not really make out right now is if you can override the v-showdirective.

Edit: you can override the show directive:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Directive Test</title>
    <script src="/js/vue.js"></script>
</head>
<body>
    <div id="directivetest">

    </div>


    <script>
        Vue.config.debug = true;
        Vue.directive('show', function (value) {
          console.log('overridden directive value: ' + value);
          console.log('overridden directive modifiers:');
          console.log(this.modifiers);
        });

        Vue.component('comp', {
            template: '<div class="my-component" v-show.visible="true" />Component</div>',
        });

        new Vue({
            el: '#directivetest',
            template: '<div class="container"><comp></comp></div>',
        });
    </script>
</body>
</html>

@weotch is opacity: 0; different to visibility: hidden; for your use case?

It's probably better to just create your custom directive like v-visible. Unfortunately the internal transition API is not exposed at the moment, but it will be in the next release and you will be able to achieve what you want using this function exposed as Vue.transition.applyTransition().

@vuchl Thanks for the input, but I don't believe that solution would trigger transitions like normal v-show does.

@simplesmiler no, those would be equivalent for my use case.

@yyx990803 I don't think a custom directive would work because I wouldn't be able to communicate to the transition system to do an enter / leave. But cool to know about that new API, thanks!

@weotch if changing the opacity is what you want, then you can do it with a simple class toggle with CSS transitions instead of using v-show.

Unfortunately the internal transition API is not exposed at the moment

Ha! this got me not long ago, but there is solution: var apply = Vue.directive('show').apply

Edit: but in my case, I just needed to toggle display, just like v-show. It's great that Evan decided to expose transition API, very handy.

@yyx990803 is there API of transitions in the new version? because I want to trigger the transition in my component or with a custom directive.

I'm doing some calculations with the offsetHeight, so I need to use 'visibility' instead of 'display'(v-show)

how does this work in 2.0?

I had the same prob, here is the fix I found. Custom directive in the form of a Node module:
https://www.npmjs.com/package/vue-visible

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franciscolourenco picture franciscolourenco  ·  3Comments

guan6 picture guan6  ·  3Comments

paulpflug picture paulpflug  ·  3Comments

seemsindie picture seemsindie  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments