@awcodes/alpine-floating-ui

中文文档
3.6.4发布于 2025/10/05ES Module零依赖近 30 天下载 2.1万

Add Floating UI functionality to Alpine 3.x components.

原始使用文档
文档目录19 节
  1. About
  2. Installation
  3. CDN
  4. NPM
  5. CSS
  6. Usage
  7. $float Magic
  8. x-float Directive
  9. Floating UI configuration
  10. With $float Magic
  11. With x-float Directive
  12. Arrow Middleware
  13. With $float Magic
  14. With x-float Directive
  15. X-Trap
  16. With $float Magic
  17. With x-float Directive
  18. Versioning
  19. License

Alpine Floating UI

Add Floating UI functionality to Alpine 3.x components.

This package only supports Alpine ^3.x.

About

This plugin adds a $float magic and an x-float directive to Alpine, with modifiers for Floating UI functionality.

Installation

CDN

Include the following <script> tag in the <head> of your document, just before Alpine. This plugin has the option to use x-trap, but you must also include the Alpine Focus plugin as well to make use of it.

<script src="https://cdn.jsdelivr.net/npm/@awcodes/[email protected]/dist/cdn.min.js" defer></script>

<!-- optional unless you want to use x-trap -->
<script defer src="https://unpkg.com/@alpinejs/[email protected]/dist/cdn.min.js"></script>

NPM

npm install @awcodes/alpine-floating-ui

This plugin has the option to use x-trap, but you must also include the Alpine Focus plugin as well to make use of it.

import Alpine from "alpinejs";
import Focus from "@alpinejs/focus"; // optional unless you want to use x-trap
import AlpineFloatingUI from "@awcodes/alpine-floating-ui";

Alpine.plugin(Focus); // optional unless you want to use x-trap
Alpine.plugin(AlpineFloatingUI);

window.Alpine = Alpine;
window.Alpine.start();

CSS

See the Floating UI Tutorial for infomation about styling.

Usage

$float Magic

To create a Floating UI component, use the $float magic on the trigger element and apply an x-ref to your 'panel'. Don't forget to have x-data on the root element of your component.

<style>
  .panel { display: none; }
</style>

<div class="component" x-data>
  <button @click="$float()">I have a floating panel. Woot!</button>
  <div x-ref="panel" class="panel">I'm floating</div>
</div>

x-float Directive

To create a floating UI component with x-float, add the directive to your floating element with an x-ref to name the name. You can then trigger the panel with a click event using $refs to target the panel.

Available methods on the trigger are toggle, open, and close.

To use the middleware with x-float simply chain on the middleware you need to use.

<div class="component" x-data>
  <button @click="$refs.panel.toggle">Trigger</button>
  <div x-ref="panel" class="panel" x-float.placement.bottom-start.flip.offset>
    <p>I'm floating</p>
  </div>
</div>

Floating UI configuration

The first argument of $float should be an object.

This object currently accepts the 'position', 'flip', 'offset', 'shift', 'arrow', and 'hide' middleware for Floating UI. See the Floating UI Tutorial for infomation about these options.

Default options are:

{
    placement: 'bottom',
    middleware: [autoPlacement()],
}

With $float Magic

To use the default options for each middleware pass an empty object as the value.

<button @click="$float({
  offset: 10,
  flip: {},
  hide: {
    strategy: 'referenceHidden'
  }
})"></button>

With x-float Directive

All settings should be configured within the expression of the x-float directive with the exception of placement which should be used directly on the directive itself. See https://floating-ui.com/docs/computePosition#placement placement options.

<div
  x-ref="panel"
  class="panel"
  x-float.placement.top.flip.offset.hide="{
    offset: 30,
    hide: {
      strategy: 'referenceHidden'
    }
  }"
>

Arrow Middleware

To use the arrow middleware you must provide an element to use as the arrow, placed inside your panel in the HTML and passed into the arrow middleware as a $ref to the element.

With $float Magic

<div class="component" x-data>
  <button @click="$float({
    offset: 10,
    arrow: {
      element: $refs.arrow
    }
  })">I'm a fancy button with a fancy arrow panel</button>
  <div x-ref="panel" class="panel">
    I'm floating 2!
    <div x-ref="arrow" class="arrow"></div>
  </div>
</div>

With x-float Directive

<div class="component" x-data>
  <button @click="$refs.panel.toggle">I'm a fancy button with a fancy arrow panel</button>
  <div
    x-ref="panel"
    class="panel"
    x-float.flip.offset.trap.arrow="{
    offset: 30,
    arrow: {
      element: $refs.arrow
    }
  }"
  >
    <p>I'm floating 2!</p>
    <div x-ref="arrow" class="arrow"></div>
  </div>
</div>

Styling the arrow (this is just an example, you are free to style it anyway you choose):

.arrow {
  position: absolute;
  background-color: inherit;
  width: 8px;
  height: 8px;
  transform: rotate(45deg);
}

X-Trap

The second argument that can be passed to $float is an object of plugin options for each float-ui element.

With $float Magic

<button @click="$float({
  offset: 10,
  placement: 'bottom-start',
},{
  trap: true
})"></button>

With x-float Directive

<div
  x-ref="panel"
  class="panel"
  x-float.trap
>

Versioning

This projects follow the Semantic Versioning guidelines.

License

Copyright (c) 2022 Adam Weston and contributors

Licensed under the MIT license, see LICENSE.md for details.

版本与发布记录

@awcodes/alpine-floating-ui 共发布 23 个版本,首次发布于 2022/04/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.6.4latest2025/10/051.1 年前
3.6.32024/03/222 年前
3.6.22024/03/152 年前
3.6.12024/03/152 年前
3.6.02024/03/102 年前
3.5.02022/12/303 年前

关于 @awcodes/alpine-floating-ui 的常见问题

@awcodes/alpine-floating-ui 是什么?

Add Floating UI functionality to Alpine 3.x components.

如何安装 @awcodes/alpine-floating-ui?

在项目目录运行 npm install @awcodes/alpine-floating-ui,或使用 pnpm add / yarn add / bun add。

@awcodes/alpine-floating-ui 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/awcodes__alpine-floating-ui 是否存在。

@awcodes/alpine-floating-ui 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@awcodes/alpine-floating-ui 可以商用吗?

@awcodes/alpine-floating-ui 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@awcodes/alpine-floating-ui 的下载量有多少?

根据 npm 官方下载统计,@awcodes/alpine-floating-ui 在 2026/10/07 – 2026/11/05 这 30 天内被下载 20,755 次,日均约 692 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@awcodes/alpine-floating-ui 还在维护吗?

最新版本 3.6.4 发布于 2025/10/05(1.1 年前);至今共发布 23 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/04/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。