How to position a dropdown relatively to body, not parent, with popper js
Asked Answered
S

1

2

Basically I have a table that is scrollable, and in the last column is an ellipsis which opens a dropdown, but because the table is scrollable dropdown is being cut off.

If you look at the bootstrap's implementation of dropdowns with popper js they have a boundary option that you can pass.

This is how it looks like right now:

enter image description here

And this is my code so far:

<template>
    <div class="relative">
        <div @click="toggleDropdown" ref="trigger">
            <slot name="trigger"></slot>
        </div>

        <div :class="[widthClass]"
             @click="open = false"
             class="rounded-md shadow-lg bg-white border border-gray-100 dark:border-gray-700 dark:text-gray-300 dark:bg-gray-700 text-gray-600 dark:text-gray-300"
             ref="menu"
             v-show="open">
            <div :class="contentClasses" class="rounded-md shadow-xs">
                <slot name="content"></slot>
            </div>
        </div>
    </div>
</template>

<script>
    import {createPopper} from '@popperjs/core';

    export default {

        props: {
            width         : {
                default: '48',
            },
            contentClasses: {
                default: () => ['py-1'],
            },
        },

        computed: {
            widthClass() {
                return {
                    '48': 'w-48',
                    '56': 'w-56',
                    '64': 'w-64',
                }[this.width.toString()]
            },
        },

        data() {
            return {
                open: false
            }
        },

        methods: {
            toggleDropdown() {
                if (this.open) {
                    this.open = false;
                } else {
                    this.open = true;
                    createPopper(this.$refs.trigger, this.$refs.menu, {
                        placement: 'bottom-start',
                        container: 'body',
                        modifiers: [
                            {
                                name   : 'preventOverflow',
                                enabled: false,
                                options: {
                                    boundary    : document.body,
                                    altBoundary : true,
                                    rootBoundary: 'window',
                                }
                            }
                        ]
                    });
                }
            },
        },
    }
</script>

Honestly, I have no idea what I'm doing, the popper js docs provide no examples, just diagrams. Help will be appreciated.

Sugar answered 7/12, 2020 at 21:37 Comment(0)
P
4

Are you using bootstrap v5 o v4.5? It works to me with v4.5 with html attribute data-boundary="window". But I couldn't make it work with v5 so I finally found another solution:

return new bootstrap.Dropdown(dropdownTriggerEl, {
            popperConfig: {
                strategy: "fixed"
            }
        });

without strategy option:

enter image description here

with strategy option:

enter image description here

By the way. Still have no idea how to make it works declaratively with data-bs-... html attributes.

Pentecostal answered 11/12, 2020 at 23:14 Comment(2)
Im using TailwindCSS, I just refered Bootstrap implementation of Popper JSSugar
Hi @Pentecostal This method is working but it has an issue. I answered here and mentioned your solution tooVarus

© 2022 - 2024 — McMap. All rights reserved.