Skip to content

Create a v-if-parent template directive #10078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tofandel opened this issue May 28, 2019 · 4 comments
Closed

Create a v-if-parent template directive #10078

Tofandel opened this issue May 28, 2019 · 4 comments

Comments

@Tofandel
Copy link

Tofandel commented May 28, 2019

What problem does this feature solve?

I would like to have an optional component but with the content being always rendered (so v-if doesn't work with this use case)

It's currently not possible without a lot of code duplication

<my-component v-if="condition">
    <the-content>I'm visible</the-content>
</my-component>
<the-content v-else>I'm visible</the-content>

What does the proposed API look like?

What I would like to be able to write instead is this

<my-component v-if-parent="condition">
    <the-content>I'm visible</the-content>
</my-component>
@posva
Copy link
Member

posva commented May 28, 2019

It looks to me that you want to reuse parts of the template. This is a duplicate of #5401

For the moment, to reuse fragments of a template, you need to either create subcomponents or use JSX/render functions

@posva posva closed this as completed May 28, 2019
@Tofandel
Copy link
Author

Tofandel commented May 28, 2019

The duplicate you provided would only be a bad workaround for the issue and will most likely not be implemented until some years

I'm asking for a different v-if which instead of not rendering the component and all it's children would not render the component but still render it's children.

Please reopen this issue

@posva
Copy link
Member

posva commented May 28, 2019

In that case, that should be implemented in a custom component, in user lad. It's not possible to render the children in every scenario as there could be multiple of them

@Tofandel
Copy link
Author

Tofandel commented Jun 11, 2019

If some people stumble here, this is the component I ended up doing

export default {
    name: "v-if-parent",
    props: {
        condition: {
            type: Boolean,
            required: true
        }
    },
    render(el) {
        if (this.condition) {
            let parent = this.$scopedSlots.parent()[0];
            parent.children = this.$scopedSlots.default();
            return parent;
        } else {
            let children = this.$scopedSlots.default();
            if (children.length > 1) {
                //We can only return a single vnode so if multiple children, wrap them in a div
                return el('div', null, children)
            }
            return children;
        }
    }
}

And then use it like this

<v-if-parent :condition="showparent">
  <template #parent><a href="somelink" /></template>
  This text is always visible <span>You can include absolutely whatever you want in the default slot</span>
</v-if-parent>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants