Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
headlessui-float-vue
Advanced tools
English | 繁體中文
Eazy use Headless UI for Vue 3 with Floating UI (Popper.js) to position floating elements.
This package is adapted from headlessui#154 example.
# npm
npm i headlessui-float-vue
# yarn
yarn add headlessui-float-vue
First finding a Headless UI component that needs to positioning element, such as the <Menu>
component here. Import <Float>
component:
<script setup>
import { Float } from 'headlessui-float-vue'
</script>
Then wrap <Float>
around <MenuButton>
and <MenuItems>
:
<Menu>
+ <Float>
<MenuButton class="...">
Options
</MenuButton>
<MenuItems class="...">
...
</MenuItems>
+ </Float>
</Menu>
Note that <Float>
must contain 2 child elements, the first being a reference element, either a Headless UI component or an HTML element, and the second being a float element.
Then remove the "absolute"
, "right-0"
and other positioning class from <MenuItems>
, and add the placement="bottom-end"
attribute:
<Menu>
<Float placement="bottom-end">
...
</Float>
</Menu>
Remove the "mt-2"
class from <MenuItems>
, and add the :offset="4"
attribute:
<Menu>
<Float placement="bottom-end" :offset="4">
...
</Float>
</Menu>
Then <Menu>
can be automatically position the inner <MenuItems>
.
In addition to <Menu>
, the same can be used on <Listbox>
, <Popover>
or <Combobox>
components, and you can use <Float>
on any element that requires floating positioning.
Floating positioning placement:
<Float placement="left-start">
All 12 placement in the Floating UI can be used:
The type of CSS position property, absolute
or fixed
:
<Float strategy="fixed">
Offset of the floating element from the reference element (px):
<Float :offset="8">
More options supported by
offset
, refer to Floating UI'soffset
documentation: https://floating-ui.com/docs/offset
Move the reference elements back into the view:
<Float shift>
Set the offset (px) of the floating element from the view border:
<Float :shift="8">
More options supported by
shift
, refer to Floating UI'sshift
documentation: https://floating-ui.com/docs/shift
Change to the opposite placement to keep it in view:
flip
cannot be used withautoPlacement
<Float flip>
More options supported by
flip
, refer to Floating UI'sflip
documentation: https://floating-ui.com/docs/flip
Floating elements chooses the placement with more space left:
autoPlacement
cannot be used withflip
<Float auto-placement>
More options supported by
autoPlacement
, refer to Floating UI'sautoPlacement
documentation: https://floating-ui.com/docs/autoPlacement
Automatically update floating elements when needed, the default value is true
. Can be set to false
to disable autoUpdate:
<Float :auto-update="false">
More options supported by
autoUpdate
, refer to Floating UI'sautoUpdate
documentation: https://floating-ui.com/docs/autoUpdate
If the above basic settings do not satisfy your needs, you can add the Floating UI middleware yourself:
<Float :middleware="middleware">
<script setup>
import { offset } from '@floating-ui/dom'
const middleware = [
offset({
mainAxis: 10,
crossAxis: 6,
}),
]
</script>
Or pass a function to get reference elements and floating elements in the parameters:
const middleware = ({ referenceEl, floatingEl }) => [
...
]
<Float>
use the <transition>
component, just adds the classes of transition:
<Float
enter="transition duration-200 ease-out"
enter-from="scale-95 opacity-0"
enter-to="scale-100 opacity-100"
leave="transition duration-150 ease-in"
leave-from="scale-100 opacity-100"
leave-to="scale-95 opacity-0"
tailwindcss-origin-class
>
When tailwindcss-origin-class
is enabled, the corresponding Tailwind CSS origin
class will be automatically added according to the placement (e.g. top
corresponds to origin-bottom
class, bottom-start
corresponds to origin-top-left
class).
If use the tailwindcss-origin-class
, also need to add the origin
class to the safelist:
tailwind.config.js
const { tailwindcssOriginSafelist } = require('headlessui-float-vue')
module.exports = {
safelist: [...tailwindcssOriginSafelist],
}
If need to override the origin
class, can use origin-class
.
<Float origin-class="origin-top-left">
First import the <FloatArrow>
component, and palce it inside the floating element, then add the class:
<Popover>
<Float>
...
<PopoverPanel>
<!-- add arrow -->
<FloatArrow class="absolute bg-white w-5 h-5 rotate-45 border border-gray-200" />
<div>
Popover & arrow, content...
</div>
</PopoverPanel>
</Float>
</Popover>
<script setup>
...
import { Float, FloatArrow } from 'headlessui-float-vue'
</script>
Then add the arrow
prop in <Float>
, and add :offset="15"
to keep the arrow away from the reference element:
<Float arrow :offset="15">
Full example of arrow:
<template>
<Popover>
<Float
placement="bottom-start"
:offset="15"
arrow
>
<PopoverButton class="px-5 py-2 bg-rose-50 hover:bg-rose-100 text-rose-500 rounded">
Button
</PopoverButton>
<PopoverPanel class="w-[240px] h-[70px] bg-white border border-gray-200 rounded-md shadow-lg focus:outline-none">
<FloatArrow class="absolute bg-white w-5 h-5 rotate-45 border border-gray-200" />
<div class="relative h-full bg-white p-3 text-rose-500 rounded-md">
Popover & arrow, content...
</div>
</PopoverPanel>
</Float>
</Popover>
</template>
<script setup>
import { Popover, PopoverButton, PopoverPanel } from '@headlessui/vue'
import { Float, FloatArrow } from 'headlessui-float-vue'
</script>
Set z-index
CSS property for the floating element, the default value is 9999, and other numbers can be set:
<Float :z-index="100">
Append the floating element to <body>
:
<Float portal>
Or can select other elements that already exist:
<Float portal="#other-root-element">
High-order component, can be easily applied in projects after custom of <Float>
component:
HighOrderFloat.vue
<template>
<Float
:offset="8"
flip
:shift="6"
portal
enter="transition duration-200 ease-out"
enter-from="scale-95 opacity-0"
enter-to="scale-100 opacity-100"
leave="transition duration-150 ease-in"
leave-from="scale-100 opacity-100"
leave-to="scale-95 opacity-0"
tailwindcss-origin-class
>
<slot></slot>
</Float>
</template>
<script setup>
import { Float } from 'headlessui-float-vue'
</script>
Used in the same way as <Float>
. It can also override the defined prop in high-order component:
<Menu>
<HighOrderFloat placement="bottom-end" :offset="12">
<MenuButton>
Options
</MenuButton>
<MenuItems>
...
</MenuItems>
</HighOrderFloat>
</Menu>
Under the MIT LICENSE
FAQs
Easily use Headless UI for Vue 3 with Floating UI (Popper.js)
The npm package headlessui-float-vue receives a total of 8 weekly downloads. As such, headlessui-float-vue popularity was classified as not popular.
We found that headlessui-float-vue demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.