I am trying to use bootstrap 5 with Vuejs and shadow dom (Vue custom element). The standard bootstrap css classes work, but the bootstrap components do not seem to work.
I have tried the solution suggested in https://ilikekillnerds.com/2022/08/how-to-use-bootstrap-javascript-components-inside-of-shadow-dom-web-components/ which initializes the bootstrap 5 component properly but all the event handlers are not bound.
I have tried implementing the dropdown component by initializing the Dropdown component from bootstrap myself.
<template>
<div class="dropdown" ref="dropdown">
<button
class="btn btn-secondary dropdown-toggle"
type="button"
id="dropdownMenuButton1"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Dropdown button
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</template>
<script setup lang="ts">
import { Dropdown } from 'bootstrap'
import { ref } from 'vue'
const dropdown = ref()
let dropdownEl: Dropdown
onMounted(() => {
if (dropdown.value) {
dropdownEl = new Dropdown(dropdown.value)
}
})
The dropdown does not work(I'm assuming due to the shadow dom, this works on non-shadow dom vuejs project). Do all events like closing/opening the dropdown have to be manually implemented?
Related
Is it possible to use the Bootstrap 5 dropdown component in a Nuxt 3 app?
My attempt failed with the following error:
[nuxt] [request error] document is not defined
Thus, I tried to wrap the dropdown component in a <ClientOnly> component but that did not help.
Anyone got a suggestion on how I can get the dropdown to work?
Here's my code:
https://stackblitz.com/edit/nuxt-starter-4jphlx?file=components/Dropdown.vue
App.vue:
<template>
<div>
<ClientOnly>
<Dropdown />
</ClientOnly>
</div>
</template>
Dropdown.vue:
<template>
<div class="dropdown">
<button
class="btn btn-secondary dropdown-toggle"
type="button"
id="dropdownMenuButton1"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Dropdown button
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</template>
<script setup>
import Dropdown from 'bootstrap/js/dist/dropdown.js';
</script>
I am trying to create a dropdown list in the navbar that matches the style of the rest of the navbar. I have not overwritten any of the bootstrap classes to my knowledge and I am wondering if there are any classes that have the same styling for a dropdown list.
I have been looking through other articles and most seem to just use a random class and it works correctly. I tried to make a custom CSS class for it but I was unable to successfully match the header.
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Management
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#Url.Action("Index", "Management")">Statistics</a>
<br />
<a class="dropdown-item" href="#Url.Action("Index", "Production")">Production Lines</a>
<br />
<a class="dropdown-item" href="#Url.Action("Index", "Announcement")">Announcements</a>
<br />
<a class="dropdown-item" href="#Url.Action("Index", "TestStation")">Test Stations</a>
</div>
</li>
When creating a dropdown list, is there a special class to use that matches the navbar or do I have to create a custom one?
dropdown-item background color in bootstrap is white and if you want change it you can change it in your page style. you can add this code to your head tag:
.dropdown-item{
background-color: #2e89e5;
color: #fff;
}
Assuming your nav-bar uses the primary color you can give the dropdown the bg-primary class to give it the primary color background (https://getbootstrap.com/docs/4.0/utilities/colors/#background-color)
It's possible your going to have to assign this class to all the ul items individually
Change the styling of the .dropdown-menu class in your style sheet in bootstrap 4.1.3.
i'm create a leftbar using aside and bootstrap , can i show dropdown-menu to outside aside?
my current code in :
https://jsfiddle.net/q79w5zbj/3/
<aside>(this button dropdown)<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
i want dropdown-menu in outside aside , whats wrong with my code?
dropdown-menu have position: absolute base on class dropdown with position: relative. So if you want dropdown-menu show outside aside, just remove overflow-y:hidden in aside tag.
Example : https://jsfiddle.net/1hrvx9L3/
I'm working with Bootstrap 4 dropdown and there are about 18 dropdown items.
Because the height is too much, popper.js automatically moves the dropdown away from its original position to the top of the screen. How do I prevent this?
I always want the dropdown to appear right below the button that toggles it.
Thanks
Posting code as requested - ( I'm using C# but the code should convey the point I'm hoping)
<div class="dropdown">
<span class="p-2 text-uppercase font-weight-semi-bold pointer dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#topMenu.Name
</span>
<div class="dropdown-menu" style="font-size:0.9rem" aria-labelledby="dropdownMenuButton">
#foreach (var subMenu in topMenu.SubMenu)
{
<a class="dropdown-item" href="#Url.Content("~/" + subMenu.Url)">#subMenu.Name</a>
}
</div>
</div>
Bootstrap 4.1
There is a new "display" option that disables popper.js dropdown positioning. Use data-display="static" to prevent popper.js from dynamically changing the dropdown position...
Bootstrap 4.0
There are some issues with popper.js and positioning.
I've found the solution is to position-relative the .dropdown, and set it as the data-boundary="" option in the dropdown toggle:
https://www.codeply.com/go/zZJwAuwC5s
<div class="dropdown position-relative" id="dd">
<button type="button" data-boundary="dd" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Dropdown
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 1</a>
...
</div>
</div>
The boundary is set to the id of the dropdown. Read more about data-boundary.
Related questions:
Bootstrap 4: Why popover inside scrollable dropdown doesn't show?
Scrolling a dropdown in a modal in Bootstrap4
I achieved this in Bootstrap 4.3.1 by adding data-flip="false" to the dropdown element.
ex:
dropdown
ZimSystems got it almost right in Bootstrap 4.1. To disable the dropdown changing direction while open, aka. it's x-placement="bottom-end", you should use the "flip" option, instead of the "display" option.
Display static disables the positioning entirely, while flip only disables the LIVE checks, that flip the dropdown upside down, when you scroll towards the top of the screen.
In options, you can try to change dropupAuto : true to dropupAuto : false.
https://developer.snapappointments.com/bootstrap-select/options/#bootstrap-version
Im using the bootstrap navbar and its working fine, on mobile devices the toggle button appears and when this button is clicked it appears a dropdown with the correct menu items, just like the bootstrap default.
But i want also to have a menu item with the username that appears when a user has a session started, and when this menu item is clicked it appears also a dropdown menu with the items available to a user with a session initiated.
The menu items available to a user with a session initiated are:
<ul>
<li>Edit Profile</li>
<li>Logout</li>
</ul>
Do you know how to use the bootstrap to create that dropdown menu with the above items for the user with a session start? So that when the menu item "Jan" is clicked apperas a dropdown menu with the items above using the bootstrap styles just like when the toggle button in mobile device is clicked it appears the dropdown with the menu items?
Working example wihout that part of show the dropdown menu items available for a user with a session initiated working properly: https://jsfiddle.net/eeeoft8n/3/
Please see the bootstrap docs, there are some great examples there.
you'll want to add the add the div with the dropdown-menu class and the necessary aria-toggle on the link to activate them.
.menu_container {
position: relative;
width: 100%;
}
div.dropdown-menu{
left:auto;
right:0;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
<div class="container-fluid px-0">
<nav class="navbar navbar-expand-md navbar-light">
Logo
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#main_nav" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="menu_container">
<div class="collapse navbar-collapse" id="main_nav">
<ul class="navbar-nav ml-auto d-flex align-items-lg-center">
<li class="nav-item">
<a class="nav-link" href="#">Item 0</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item 1</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-user" aria-hidden="true"></i> Jan
</a>
<div class="dropdown-menu" aria-labelledby="userDropdown">
<a class="dropdown-item" href="#">Edit Profile</a>
<a class="dropdown-item" href="#">Delete brains</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">logout</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
</div>
you might need to go fullscreen in order to get the full-width view instead of the mobile view