Inertia.js Vue JS Printing prop value as style - javascript

I have the following inertia link component
<inertia-link v-for="project in $page.props.user.favorite_projects" :key="project.id" class="flex items-center px-6 py-2 mb-1 text-sm leading-6 text-gray-300 transition duration-150 ease-in-out group focus:outline-none focus:bg-gray-700 hover:text-white hover:bg-gray-700" :href="route('projects.show',project.slug)">
<div class="w-3 h-3 mr-3 overflow-hidden rounded-full">
<div class="w-full h-full" style="background-color = {{ project.color }};">
</div>
</div>
{{ project.project_name }}
</inertia-link>
Where project.color contains a hex color value like #fc8181, which is used to color the element based on the project.
However, the circle with this color value is not being shown in the dom.
Is there a way to display the project color as background color?

you need to bind the style, try style
:style="{ background-color: project.color }"

Related

I don't see the '...' after setting text-ellipsis on div

I don't see the '...' after setting text-ellipsis, overflow hidden and nowrap on this div.
Here's my code:
import Image from "next/future/image";
import Link from "next/link";
export default function MenuOption({ title, description, icon, route }) {
return (
<Link href={route}>
<div className="flex h-[74px] w-full cursor-pointer gap-4 overflow-hidden text-ellipsis rounded-lg bg-gray-50 p-4 ring-1 ring-gray-300 drop-shadow-sm hover:ring-2">
<Image src={icon} alt={`${title} Icon`} className="mt-1 h-5 w-auto" />
<div className="float-left inline-block overflow-hidden text-ellipsis whitespace-nowrap">
<h2 className="text-md font-medium text-gray-900">{title}</h2>
<p className="text-sm text-gray-500">{description}</p>
</div>
</div>
</Link>
);
}
You have to use all the style on specific element, like <p></p> or <h2></h2> and with fix width like
<h2 className="text-md font-medium text-gray-900 inline-block overflow-hidden text-ellipsis whitespace-nowrap w-100">{title}</h2>
<p className="text-sm text-gray-500 inline-block overflow-hidden text-ellipsis whitespace-nowrap w-100">{description}</p>
here,
.w-100 {
width: 100%
}

How to fix in Tailwindcss div height overflows parent div

My card element has a fixed height of h-80 (in Tailwind). usually I use the card in a grid.
Now, I have a div inside that card, which is bigger than it's parent div, however I want it to scroll vertically.
The problem becomes now, that I can not scroll completly down. The last line is cut off.
Overflow auto, but not completly scrollable:
Full height to see what content is missing:
I am using VueJS as framework, this is my Card component:
<div class="w-full overflow-hidden h-full px-3 py-2">
<div v-if="title" class="block text-gray-700 text-lg font-semibold py-2 px-2">
<i :class="title_icon" />
{{ title }}
</div>
<div class="h-full overflow-y-auto">
<slot>
<-- Here is another component which holds the appointments-->
</slot>
</div>
</div>
How do I fill the remaining space in a div with overflowing content in TailwindCSS?
Use the flex-grow property on the sub-div to allow it to grow.
Set flex on the parent div and flex-col to keep the styling.
Find the documentation here
<div class="flex flex-col w-full overflow-hidden h-full px-3 py-2">
<div v-if="title" class="block text-gray-700 text-lg font-semibold py-2 px-2">
<i :class="title_icon" />
{{ title }}
</div>
<div class="flex-grow h-full overflow-y-auto">
<slot>
<-- Here is another component which holds the appointments-->
</slot>
</div>
</div>

Accessing current data iteration from parent component

I want to access current data iteration from template, and also I need certain id's from iterated element.
<template x-for="(datas, idx) in data">
<tr x-bind:class="idx % 2 == 0 ? 'bg-white' : 'bg-green-100'" class="border-4 border-gray-200">
<td class="text-center py-2 w-2">
<span x-text="`${parseInt(idx) + 1}`"></span>
</td>
<td class="text-center py-2">
<strong x-text="datas.name"></strong>
<br />
<i x-text="`Num : ${datas.num}`"></i>
</td>
<td class="text-center py-2" >
<!--I need access datas.num in this area v--->
<div #click.away="open = false" class="relative" x-data="{open : false}">
<button #click="open = !open" class="bg-gray-300 flex flex-row items-center w-full px-4 py-2 mt-2 text-sm font-semibold text-left bg-transparent rounded-lg dark-mode:bg-transparent dark-mode:focus:text-white dark-mode:hover:text-white dark-mode:focus:bg-gray-600 dark-mode:hover:bg-gray-600 md:w-auto md:inline md:mt-0 md:ml-4 hover:text-gray-900 focus:text-gray-900 hover:bg-gray-200 focus:bg-gray-200 focus:outline-none focus:shadow-outline">
<span>Action</span>
<svg fill="currentColor" viewBox="0 0 20 20" :class="{'rotate-180': open, 'rotate-0': !open}" class="inline w-4 h-4 mt-1 ml-1 transition-transform duration-200 transform md:-mt-1"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
<div x-show="open" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="transform opacity-0 scale-95" x-transition:enter-end="transform opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="transform opacity-100 scale-100" x-transition:leave-end="transform opacity-0 scale-95" class="absolute right-0 w-full mt-2 origin-top-right rounded-md shadow-lg md:w-48" style="z-index: 1;">
<div class="px-2 py-2 bg-white rounded-md shadow dark-mode:bg-gray-800">
<a class="block px-4 py-2 mt-2 text-sm font-semibold bg-transparent rounded-lg dark-mode:bg-transparent dark-mode:hover:bg-gray-600 dark-mode:focus:bg-gray-600 dark-mode:focus:text-white dark-mode:hover:text-white dark-mode:text-gray-200 md:mt-0 hover:text-gray-900 focus:text-gray-900 hover:bg-gray-200 focus:bg-gray-200 focus:outline-none focus:shadow-outline" href="#"><i class="fas fa-pencil-alt"></i> Edit</a>
<a #click="$parent.deleteData(datas.num)" class="block px-4 py-2 mt-2 text-sm font-semibold bg-transparent rounded-lg dark-mode:bg-transparent dark-mode:hover:bg-gray-600 dark-mode:focus:bg-gray-600 dark-mode:focus:text-white dark-mode:hover:text-white dark-mode:text-gray-200 md:mt-0 hover:text-white-900 focus:text-white-900 hover:bg-red-200 focus:bg-red-200 focus:outline-none focus:shadow-outline" href="#"><i class="fas fa-trash-alt"></i> Delete</a>
</div>
</div>
</div>
</td>
</tr>
</template>
and here's data definition :
function dataInit(){
return {
data : [
{
num : 10,
name : "josh"
}
],
deleteData(id){
//do something with current id selection
}
}
}
I do can access method of deleteData(id) in parent because using Alpine's magic helper, but I can't access datas.num or $parent.deleteData(datas.num) variable in loop block even they're inside template block. How do I resolve this problem?
Edit :
*Assuming the x-data for data loop is already set in upper element
** What I meant is that, I want to access the x-for in child element ( see comments on html's section). But I can't access it, maybe some scope problems that prevent me to do it. Starting in this area <div #click.away="open = false" class="relative" x-data="{open : false}">
Your deleteData function is within an object, so referring to other elements within that object requires this to refer to itself:
deleteData(id) {
let data = this.data;
}
will return the data variable.
Another thing, you've defined data as an array of objects. So, to access the num variable, you'd have to first access that specific object within the array, and then you can get the num value:
this.data[0].num should do the trick.
P.S. your code snippet is missing one closing bracket (})
Within Alpine, you don't need to refer to the object, since you should be setting it as x-data somewhere, from which you can then do whatever you wish with the data.
<div x-data="dataInit()">
<!-- Loop the array of data -->
<template x-for="singleData in data">
<div>
<span x-text="singleData.num"></span>
</div>
</template>
</div>

Pulling AlpineJS defined data into a LiveWire Component

I'm having some issues with how to pull data from Alpine to work in LiveWire.
Code Example
<div x-data #click="$dispatch('popup', { name: 'Hello World', drip: 'yes' })" class="border-2 border-white flex font-semibold hover:bg-yellow-400 hover:border-yellow-200 items-center justify-center p-1.5 rounded-md shadow-sm text-base text-black transition-all md:w-8/12 lg:w-6/12 bg-green-300 cursor-pointer">Click Me</div>
<div x-data="{ popupinfo: false, drip: null }" x-on:popup.window="{ popupinfo = true }" #popup.window="{ name = $event.detail.name, drip = $event.detail.drip }" x-show="popupinfo" x-cloak>
<h3 class="text-lg leading-6 font-bold text-gray-900" id="modal-title" x-text="name"></h3>
<div class="flex items-center justify-start">
<template x-if="drip == 'yes'">
<div>True</div>
</template>
<template x-if="drip == 'no'">
<div>False</div>
</template>
</div>
<div class="bg-yellow-400 flex items-center justify-between px-3 py-2 rounded-xl my-2 cursor-pointer transition-colors tracking-tight"
wire:click="$emit('addToBasket', {{ $drip }})"
#click="$dispatch('addtobasket')">
<div class="text-sm">
<span class="font-bold">2 Uploads</span> a day
</div>
<div class="bg-white font-semibold px-2 py-1 rounded-md text-sm tracking-tighter"> Monthly</div>
</div>
</div>
Look on line 15 (wire:click="$emit('addToBasket', {{ $drip }})"). How can I add the drip data to the emit. Right now I've wrapped it in a {{ }} (Which is on purpose). I don't know how to call it for the emit.
You can see how it runs here
https://codepen.io/bitvalentine/pen/wvJEYYX
You can use the livewire #entangle() feature to share property state between alpine and livewire.
just define your alpine drip property like below and set its value on your livewire property
<div x-data="{ popupinfo: false, #entangle('drip') }" x-on:popup.window="{ popupinfo = true }" #popup.window="{ name = $event.detail.name, drip = $event.detail.drip }" x-show="popupinfo" x-cloak>
on your livewire component
public $drip = null;

Fix dropdown auto open in tailwind ui navbar component with vue.js

I use navbar component from tailwind ui. It's looks like something like this:
<!-- Profile dropdown -->
<div class="ml-3 relative">
<div>
<button
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
id="user-menu"
aria-label="User menu"
aria-haspopup="true"
>
<img
class="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt
/>
</button>
</div>
<!--
Profile dropdown panel, show/hide based on dropdown state.
Entering: "transition ease-out duration-100"
From: "transform opacity-0 scale-95"
To: "transform opacity-100 scale-100"
Leaving: "transition ease-in duration-75"
From: "transform opacity-100 scale-100"
To: "transform opacity-0 scale-95"
-->
<div class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg">
<div
class="py-1 rounded-md bg-white shadow-xs"
role="menu"
aria-orientation="vertical"
aria-labelledby="user-menu"
>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Your Profile</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Settings</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Sign out</a>
</div>
</div>
</div>
In this case when I run this code in vue.js navbar dropdown menu status is open by default. How can set status closed by defaul?
Here is preview:
I'm not sure if anyone is following this question right now but I'm sharing my solution.
In the snippet to the dropdown code there was a comment saying:
<!--
Profile dropdown panel, show/hide based on dropdown state.
Entering: "transition ease-out duration-100"
From: "transform opacity-0 scale-95"
To: "transform opacity-100 scale-100"
Leaving: "transition ease-in duration-75"
From: "transform opacity-100 scale-100"
To: "transform opacity-0 scale-95"
-->
It's basically telling the state of dropdown is changing based on classes names so you'd have to make them dynamic like this:
<div :class="`origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg transition ease-${dropdown ? 'out' : 'in'} duration-${dropdown ? '100' : '75'} transform opacity-${dropdown ? '100' : '0'} scale-${dropdown ? '100' : '95'}`">
now the classes will depend on the dropdown value which is just a property of your component that can be changed via a click event like this:
export default {
name: 'TheNavBar',
data() {
return {
dropdown: false,
}
},
}
<div>
<button
id="user-menu"
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
aria-label="User menu"
aria-haspopup="true"
#click="dropdown = !dropdown"
>
</button>
</div>
I was using alpinejs and included it using CDN.
It worked for me when I put the following in the div that was wrapping the whole component:
x-data="{ open: false }"
The below went into the (User profile image) button div
#click="open = true"
And finally, this went into the drop-down div
x-show="open" #click.away="open = false"
I'm exactly using the same component and came here to find an answer :(
Since nobody answered it, here is where I've been:
It's explicitly said that you'll need to use Javascript with some Tailwind UI component like this one.
But I've done with no JS, only a CSS tricks and a slightly different markup and a simpler animation (but still smooth! You can see the fiddle on the bottom of this answer).
The markup: I've just removed the div wraper arround the avatar button to benefits from the ~ CSS selector, and I've added an id #user-menu-dropdown:
<!-- Profile dropdown -->
<div class="ml-3 relative">
<button
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
id="user-menu" aria-label="User menu" aria-haspopup="true">
<img class="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt=""/>
</button>
<div id="user-menu-dropdown" class="menu-hidden origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg">
<div
class="py-1 rounded-md bg-white shadow-xs"
role="menu"
aria-orientation="vertical"
aria-labelledby="user-menu"
>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Your Profile</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Settings</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Sign out</a>
</div>
</div>
</div>
And here is my LESS
#user-menu ~ #user-menu-dropdown
#apply transform
#apply ease-in duration-75 opacity-0 scale-0
#user-menu ~ #user-menu-dropdown:focus-within, #user-menu:focus ~ #user-menu-dropdown
#apply ease-out duration-100 opacity-100 scale-100
And here is the results, using the generated CSS
Hope it will help you
If the Alpine JS is working fine. Just Copy-paste this code and you will be good to go.
<!-- Profile dropdown -->
<div class="ml-3 relative" x-data="{ dropdown: false }">
<div>
<button x-on:click="dropdown = ! dropdown" type="button" class="max-w-xs bg-gray-800 rounded-full flex items-center text-sm focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white" id="user-menu-button" aria-expanded="false" aria-haspopup="false">
<span class="sr-only">Open user menu</span>
<img class="h-8 w-8 rounded-full" src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" alt="">
</button>
</div>
<div x-show="dropdown" class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg py-1 bg-white ring-1 ring-black ring-opacity-5 focus:outline-none" role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">
<!-- Active: "bg-gray-100", Not Active: "" -->
Your Profile
Settings
Sign out
</div>
</div>
Explanation of what I do
I just simply added x-data="{ dropdown: false }" in the parent div to hide the menu at the initial state.
Then, I added x-on:click="dropdown = ! dropdown" on the button to toggle the menu.
And lastly, I added x-show="dropdown" in the menu div. to show or hide the dropdown based on its value.
You can use any text at the spot of dropdown. Because its works like a variable.
As pointed out in previous answers one can use Vue.js or some smart CSS to solve the problem. If you don't want to make your page heavy by using Vue or use CSS which has limitations then you can use alpinejs. This is what Tailwindui is using in their demo.
You can use alpinejs either by installing it via yarn or npm or simply install it from CDN.
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine#v2.7.0/dist/alpine.min.js" defer></script>
You don't need to write even one line of javascript. If you installed via npm or yarn then import it to your project.
import "alpinejs";
Open the HTML with navigation code.
Add x-data directive.
<!-- Profile dropdown -->
<div class="ml-3 relative" x-data="open = false">
Now add x-click directive to the element that is clicked to reveal the dropdown.
<button
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
id="user-menu"
aria-label="User menu"
aria-haspopup="true"
x-on:click="open=true"
>
<img
class="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt
/>
</button>
Finally modify the div that nests the dropdown elements.
<div class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg" x-show="open" x-on:click.away="open = false">
x-on:click.away directive will close the dropdown when mouse is clicked anywhere else.
To polish it off you can use Alpine transition directives to the previous div.
<div class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg" x-show="open" x-on:click.away="open = false" x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0 -translate-y-1" x-transition:enter-end="opacity-100 translate-y-0" x-transition:leave="transition ease-in duration-150" x-transition:leave-start="opacity-100 translate-y-0" x-transition:leave-end="opacity-0 -translate-y-1">
If you have more than two dropdowns then use data directive as under
<div x-data="{openDropdown1: false, openDropdown2: false}"
Tailwind UI only provides example static html. You'll need to use your JS framework (Vue.js) to dynamically generate similar html that varies based on the state of your app. In general you need to:
1- add a boolean state variable that controls whether the menu is currently displayed or not. Initialize it to false so the menu is hidden by default.
data() {
return {
show: false,
}
}
2- Add a click handler to the menu button to toggle this variable.
<button #click="show = !show">Menu</button>
3- Only render the menu when show is true. You can do this with v-if
<div v-if="show" class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg">
//...menu content
</div>
4- Animate the menu when it gets displayed or hidden. You can do this by wrapping the menu in a Vue.js <transition> component.
<transition
enter-active-class="transition ease-out duration-100"
enter-from-class="transform opacity-0 scale-95"
enter-to-class="transform opacity-100 scale-100"
leave-active-class="transition ease-in duration-75"
leave-from-class="transform opacity-100 scale-100"
leave-to-class="transform opacity-0 scale-95"
>
<div v-if="show" class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg">
//...menu content
</div>
</transition>
Here is a full working example in Vue 3:
https://codepen.io/mpvosseller/pen/RwoNaVz
Notes:
In Vue 2 you should use enter-class and leave-class instead of enter-from-class and leave-from-class
In React you can use Tailwind's Headless UI React Transition component for similar functionality.
use v-on:click and v-show
<!-- ProfileNavDropdown.vue -->
<template>
<div class="ml-3 relative">
<div v-on:click="isActive = !isActive">
<button
class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-white transition duration-150 ease-in-out"
id="user-menu"
aria-label="User menu"
aria-haspopup="true"
>
<img
class="h-8 w-8 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt
/>
</button>
</div>
<!--
Profile dropdown panel, show/hide based on dropdown state.
Entering: "transition ease-out duration-100"
From: "transform opacity-0 scale-95"
To: "transform opacity-100 scale-100"
Leaving: "transition ease-in duration-75"
From: "transform opacity-100 scale-100"
To: "transform opacity-0 scale-95"
-->
<div v-show="isActive" class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg">
<div
class="py-1 rounded-md bg-white shadow-xs"
role="menu"
aria-orientation="vertical"
aria-labelledby="user-menu"
>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Your Profile</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Settings</a>
<a
href="#"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 transition duration-150 ease-in-out"
role="menuitem"
>Sign out</a>
</div>
</div>
</div>
</template>
<script>
export default {
data: function () {
return {
isActive: false,
}
},
}
</script>
I had this same issue, here is a solution I discovered from my little research:
<!-- vue template -->
<template>
<div class="relative">
<button #click="isOpen = !isOpen" class="relative z-10 block h-8 w-8 rounded-full overflow-hidden border-2 border-gray-600 focus:outline-none focus:border-white">
<img class="h-full w-full object-cover" src="https://images.unsplash.com/photo-1487412720507-e7ab37603c6f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=256&q=80" alt="Your avatar">
</button>
<button v-if="isOpen" #click="isOpen = false" tabindex="-1" class="fixed inset-0 h-full w-full bg-black opacity-50 cursor-default"></button>
<div v-if="isOpen" class="absolute right-0 mt-2 py-2 w-48 bg-white rounded-lg shadow-xl">
Account settings
Support
Sign out
</div>
</div>
</template>
<script>
//javascript
export default {
data() {
return {
isOpen: false
}
},
created() {
const handleEscape = (e) => {
if (e.key === 'Esc' || e.key === 'Escape') {
this.isOpen = false
}
}
document.addEventListener('keydown', handleEscape)
this.$once('hook:beforeDestroy', () => {
document.removeEventListener('keydown', handleEscape)
})
}
}
</script>
I hope this helps, you can see more here
Adding to #Andreas Hunter's solution, you may use the more convenient headless-ui (Tailwind CSS) for Vue (click here). It is also available for React. Headless UI is designed to integrate beautifully with Tailwind CSS.
Use Menu component for dropdowns:
<template>
<Menu>
<MenuButton> More </MenuButton>
<MenuItems>
<MenuItem v-slot="{ active }">
<a :class="{ 'bg-blue-500': active }" href="/account-settings"> Account settings </a>
</MenuItem>
<MenuItem v-slot="{ active }">
<a :class="{ 'bg-blue-500': active }" href="/account-settings"> Documentation </a>
</MenuItem>
<MenuItem v-slot="{ active }" disabled>
<span :class="{ 'bg-blue-500': active }"> Invite a friend (coming soon!) </span>
</MenuItem>
</MenuItems>
</Menu>
</template>
<script>
import { Menu, MenuButton, MenuItems, MenuItem } from '#headlessui/vue'
export default {
components: {
Menu,
MenuButton,
MenuItems,
MenuItem,
},
}
</script>
You don't have to worry about its state anymore.
Since tailwind by default handles the dropdown (open and close, even when you click outside the dropdown). It just programmatically hides the dropdown.
I used a javascript one-line solution by simulating a click on the document body. That way, the dropdown automatically close while you still maintain its animations. And you could simulate a click event on the button/element to open the dropdown.
document.querySelector('body').click()
For anyone struggling with this, the solution is much simpler than all the replies above.
Add this class to the div (so it is invisible when the page first loads): "invisible".
Add another class called "profile-menu".
<div class="invisible profile-menu" role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">
In Javascript at the bottom of your page add this:
const navbarBtn = document.querySelector(".close-profile-bar");
const profileMenu = document.querySelector(".profile-menu");
navbarBtn.addEventListener("click", () => {
let results = profileMenu.classList.contains('invisible');
if(results){
profileMenu.classList.remove('invisible');
profileMenu.classList.add('visible');
} else {
profileMenu.classList.remove('visible');
profileMenu.classList.add('invisible');
}
});
The above will just remove or add the invisible or invisible class to the profile div.

Categories