npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-teleporter

v1.0.1

Published

一种 Vue 组件函数式渲染方案,允许开发者在调用/使用阶段,函数式地传送/渲染/挂载 Vue 组件。A functional rendering scheme for Vue components, allowing developers to teleport/render/mount Vue components in a functional way during the call/use phase.

Downloads

1

Readme

vue-teleporter

简体中文 | English

Introduction

Vue-teleporter is a functional rendering scheme for Vue components, allowing developers to teleport/render/mount Vue components in a functional way during the call/use phase.

Cooperate with await/promise to code in a process oriented manner, reducing developers' burden of maintaining the mount points, visibility, data flow and other pain points of traditional modal boxes (declaratively write down pop-up components in advance).

Interface

The package vue-teleporter exposes a container component and two functions:

  • teleport is consistent with the input parameters of h, teleport can be understood as the alias of h;
  • teleport will directly render the teleported component into the container and return to the manual unmount function;
  • Emitting a return event on the teleported component will cause it to be unmounted;
  • If the return event listener is defined, it will be called when the teleported component is unmounted.

If you are completely unfamiliar with the h function, you can first read the corresponding documents of Vue2 or Vue3 versions to have a general impression of its input data structure, but this is not necessary.

Life Cycle

  • Convention: when the teleported component emits a return event, the vue-teleporter considers that it has completed the task of collecting data, and automatically unmounts/destroys it.

  • The teleport function returns an unmount function. Calling it will forcibly unmount the previously teleported single component, and the incoming parameters will also be forwarded to the return event listener.

  • vue-teleporter also exposes the unmountAllTeleportedComponents method, which can be called when the project route changes to unmount all teleported components.

  • Internally, it is guaranteed that the return event listener will be called only once (it is necessary and sufficient for the teleported component to be unmounted and the return event listener to be called).

Get Started Quickly

install:

npm i vue-teleporter

In App.vue or other suitable place to insert the Teleported Component Container:

<!-- Vue2/Vue3 -->
<template>
  <div id="app">
    ...
    <TeleportedComponentContainer></TeleportedComponentContainer>
  </div>
</template>

<!-- Vue3 only -->
<template>
  <div id="app">
    ...
  </div>
  <teleport>
    <TeleportedComponentContainer></TeleportedComponentContainer>
  </teleport>
</template>

<script>
import {TeleportedComponentContainer} from 'vue-teleporter';

export default {
  name: 'App',
  components: {
    TeleportedComponentContainer,
  }
}
</script>

use the teleport method to teleport/render/mount a component (to the Teleported Component Container):

import MyConfirm from '... MyConfirm.vue';
import {teleport} from 'vue-teleporter';

async function my_Vue2_confirm(message): Promise<boolean> {
    return new Promise(function (resolve) {
        var manualUnmount = teleport(MyConfirm, {
            props: {
                message,
            },
            on: {
                return($event) {
                    resolve($event);
                },
            },
        });
    });
}

async function my_Vue3_confirm(message): Promise<boolean> {
    return new Promise(function (resolve) {
        var manualUnmount = teleport(MyConfirm, {
            message,
            onReturn($event) {
                resolve($event);
            }
        });
    });
}

Emitting a return event on the teleported MyConfirm.vue component:


<template>
  <div style="
    position: fixed; top: 50vh; left: 50vw; translate: -50% -50%;
    padding: 1em; box-shadow: 0 0 .5em #aaa;"
  >
    <p>
      {{ message }}
    </p>
    <div>
      <button @click="$emit('return', false);">Cancel</button>
      <button @click="$emit('return', true);">Ok</button>
    </div>
  </div>
</template>
<script>
export default {
  props: ['message']
}
</script>

After the event listener in my_Vue_confirm function is triggered (gets the business result), you can continue to execute the subsequent logic. When the teleported component emits a return event, it will be unmounted automatically by vue-teleporter.

Best Practices

teleport function returns a new function, which can be called to unmount the teleported component:

async function my_Vue3_confirm(message): Promise<boolean> {
    return new Promise(function (resolve) {
        var manualUnmount = teleport(MyConfirm, {
            message,
            onReturn($event) {
                resolve($event);
            }
        });

        // Manually unmount the teleported component without operation within 5 seconds.
        setTimeout(function () {
            manualUnmount(false); // It is deemed to be canceled.
        }, 5_000);

    });
}

When the route changes, call the unmountAllTeleportedComponents function to unmount all teleported components:

import {unmountAllTeleportedComponents} from 'vue-teleporter';

router.beforeEach(function (to, from, next) {

    setTimeout(unmountAllTeleportedComponents);

    next();
});

In this case, when the return event listener is called back, there is no input parameter.

Use JSX syntax:

setTimeout(teleport(
    <div
        style="
            position: fixed; top: 1em; right: 1em;
            z-index: 9999; background-color: #eee;
        "
    >
        Prompt information, automatically close after 3 seconds.
    </div>
), 3000);

You may think that you can continue to encapsulate a set of UI frameworks.

It is unnecessary. Please consider directly reusing third-party modal box components:


<template>
  <el-dialog :visible="true" @close="$emit('return', false);">
    <p>
      {{ message }}
    </p>
    <div>
      <button @click="$emit('return', false);">Cancel</button>
      <button @click="$emit('return', true);">Ok</button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  props: ['message']
}
</script>

Now we don't need to pay attention to the visible attribute, It is always true during the life cycle of teleported components.