Skip to content
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

Added support for passing class names to portal-target component. #349

Closed
forejerk opened this issue Aug 3, 2021 · 0 comments
Closed

Added support for passing class names to portal-target component. #349

forejerk opened this issue Aug 3, 2021 · 0 comments

Comments

@forejerk
Copy link

forejerk commented Aug 3, 2021

Hi. Thank you for this great project :-).
This portal-vue project has brought happiness into my life and helped me lose weight.
#101 (Class passed to component is lost in the portal)
I have added some code to solve this issue. I I decided to write it as an issue because not be useful or it might be wrong.
It code is working well on my private project.

src/components/portal.tsx

    sendUpdate() {
      const slotContent = this.normalizeSlots()
      
      // after... inserted.addClass
      this.$nextTick(()=>{
        if (slotContent) {
          const transport: TransportInput = {
            from: this.name,
            to: this.to,
            /*
             * Example
             *
             *   <portal to='example' class='sample-class' v-addclass.sample>
             *  </portal>
             *  
             *    1.this.$vnode.data.staticClass
             *    2.this.$vnode.context.$el.className
             *  
             *   Vue.directive('addclass',{
             *     inserted:function(el,binding,vnode){
             *       $(el).addClass(vnode.context.$options.name)
             *       $(el).addClass(Object.keys(binding.modifiers))
             *     }
             *   })
             * 
             */
            classes: [this.$vnode.context ? this.$vnode.context.$el.className : '',this.$vnode.data ? this.$vnode.data.staticClass : ''].filter(Boolean).join(' ') , // add
            passengers: [...slotContent],
            order: this.order,
          }
          wormhole.open(transport)
        } else {
          this.clear()
        }
      })
    }

src/components/wormhole.ts

export const Wormhole = Vue.extend({
  data: () => ({
    transports,
    targets,
    sources,
    trackInstances: inBrowser,
  }),
  methods: {
    open(transport: TransportInput) {
      if (!inBrowser) return
      const { to, from, classes, passengers, order = Infinity } = transport // add
      if (!to || !from || !passengers) return

      const newTransport = {
        to,
        from,
        classes, // add
        passengers: freeze<object>(passengers),
        order,
      } as Transport

src/components/portal-target.tsx

  computed: {
    ownTransports(): Transport[] {
      const transports: Transport[] = this.transports[this.name] || []
      if (this.multiple) {
        return transports
      }
      return transports.length === 0 ? [] : [transports[transports.length - 1]]
    },
    classes():String[] { // add
      /*
       *
       * Example
       *  
       *  When portaling multiple elements' from.elems -> to.target { multiple:true } 
       * 
       *  from
       *    <portal name="destination" :order="2" class='two'> <p>some content</p> </portal>
       *    <portal name="destination" :order="1" class='one> <p>some other content</p> </portal>
       *  to
       *    <portal-target name="destination" multiple="true">
       * 
       *  Mount with all class names assigned.
       *  
       *    <div class="one two vue-portal-target">
       *      <p>some other content</p>
       *      <p>some content</p>
       *   </div>
       * 
       */
      return this.ownTransports.map(o=>o.classes)
    },

src/types.ts

export interface TransportInput {
  to: string
  from: string
  classes?: string, // add
  order?: number
  passengers: Array<VNode | Function>
}
@LinusBorg LinusBorg closed this as not planned Won't fix, can't repro, duplicate, stale Dec 10, 2022
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