You can absolutely do that even with absolute positioning—just combine your responsive utilities (e.g. md:right-0 left-0) with your conditional positioning classes. The idea is to build a computed class string that includes those responsive classes along with your dynamic classes (like top-full mt-2 or bottom-full mb-2 when switching the dropdown’s position).
Below is an updated example based on the previous code snippet but now you can see how to integrate md:right-0 left-0:
<template>
<div class="w-full max-w-4xl mx-auto">
<div class="relative w-full" ref="dropdownWrapper">
<!-- Main dropdown trigger -->
<div
class="w-full bg-white border border-gray-300 rounded-lg px-3 py-[9px] flex items-center justify-between cursor-pointer transition-all duration-200 hover:border-gray-300 focus:outline-none text-gray-500"
:class="{ 'shadow-lg': isOpen }"
@click="toggleDropdown"
>
<span class="text-sm">{{ selectedItem?.title || placeholder }}</span>
<Icon
:icon="isOpen ? 'mdi:chevron-up' : 'mdi:chevron-up-down'"
width="16"
/>
</div>
<!-- Dropdown content -->
<Transition
enter-active-class="transition-all duration-200 ease-out"
enter-from-class="opacity-0 transform -translate-y-2 scale-95"
enter-to-class="opacity-100 transform translate-y-0 scale-100"
leave-active-class="transition-all duration-200 ease-out"
leave-from-class="opacity-100 transform translate-y-0 scale-100"
leave-to-class="opacity-0 transform -translate-y-2 scale-95"
>
<div
v-if="isOpen"
:class="dropdownClasses"
@click.stop
ref="dropdownContent"
>
<div
v-for="(item, index) in items"
:key="index"
class="px-3 sm:px-4 py-2 sm:py-3 text-sm text-gray-500 cursor-pointer transition-colors duration-150 mb-1 hover:bg-gray-100 rounded-xl"
:class="{
'bg-gray-100 text-gray-600': selectedItem === item,
}"
@click="selectItem(item)"
>
{{ item.title }}
</div>
</div>
</Transition>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, nextTick, computed } from "vue";
// Props
const props = defineProps({
placeholder: {
type: String,
default: "Select an option",
},
items: {
type: Array,
default: () => [],
},
modelValue: {
type: [String, Object],
default: "",
},
});
// Emits
const emit = defineEmits(["update:modelValue", "change"]);
// Reactive data
const isOpen = ref(false);
const selectedItem = ref(props.modelValue || props.items[0] || "");
const dropdownDirection = ref("bottom"); // "bottom" or "top"
const dropdownWrapper = ref(null);
const dropdownContent = ref(null);
const toggleDropdown = async (event) => {
event.preventDefault();
event.stopPropagation();
isOpen.value = !isOpen.value;
if (isOpen.value) {
// Wait for the dropdown to be rendered then measure space
await nextTick();
calculateDropdownDirection();
}
};
const selectItem = (item) => {
selectedItem.value = item;
isOpen.value = false;
emit("update:modelValue", item);
emit("change", item);
};
const closeDropdown = (event) => {
// Close dropdown if click is outside the dropdown wrapper
if (!dropdownWrapper.value.contains(event.target)) {
isOpen.value = false;
}
};
const calculateDropdownDirection = () => {
const trigger = dropdownWrapper.value;
const dropdown = dropdownContent.value;
if (!trigger || !dropdown) return;
const triggerRect = trigger.getBoundingClientRect();
const dropdownRect = dropdown.getBoundingClientRect();
const spaceBelow = window.innerHeight - triggerRect.bottom;
const spaceAbove = triggerRect.top;
// If there is less space below than dropdown's height and more space above, open upward
if (spaceBelow < dropdownRect.height && spaceAbove > dropdownRect.height) {
dropdownDirection.value = "top";
} else {
dropdownDirection.value = "bottom";
}
};
const dropdownClasses = computed(() => {
// Base classes including your responsive adjustments
let baseClasses =
"absolute left-0 md:right-0 bg-white border border-gray-300 rounded-xl shadow-xl z-50 overflow-hidden max-h-80 overflow-y-auto px-1 pt-2 pb-1";
if (dropdownDirection.value === "top") {
// Render above the trigger
return `${baseClasses} bottom-full mb-2`;
} else {
// Render below the trigger
return `${baseClasses} top-full mt-2`;
}
});
// Lifecycle hooks
onMounted(() => {
document.addEventListener("click", closeDropdown);
if (props.modelValue) {
selectedItem.value = props.modelValue;
}
window.addEventListener("resize", calculateDropdownDirection);
});
onUnmounted(() => {
document.removeEventListener("click", closeDropdown);
window.removeEventListener("resize", calculateDropdownDirection);
});
</script>