Anchored Region

<anchored-region> is a declarative way of declarign floating elements. It is an alternative until anchored-positioning in CSS gets better support.

API Reference

Imports

Script
HTML
<!-- Auto registers as <role-anchored-region> -->
<script type="module" src="https://cdn.jsdelivr.net/npm/role-components/exports/components/anchored-region/anchored-region-register.js"></script>
CDN
HTML
<script type="module">
  // Auto registers as <role-anchored-region>
  import "https://cdn.jsdelivr.net/npm/role-components/exports/components/anchored-region/anchored-region-register.js"

  // Manual Register
  import RoleAnchoredRegion from "https://cdn.jsdelivr.net/npm/role-components/exports/components/anchored-region/anchored-region.js"
  RoleAnchoredRegion.define()
  // => Registers as <role-anchored-region>
</script>
Bundler
JavaScript
// Auto registers as <role-anchored-region>
import "role-components/exports/components/anchored-region/anchored-region-register.js"

// Manual Register
import RoleAnchoredRegion "role-components/exports/components/anchored-region/anchored-region.js"
RoleAnchoredRegion.define()
// => Registers as <role-anchored-region>

Slots

Name Description

The popover’s content.

anchor

The element the popover will be anchored to. If the anchor lives outside of the popover, you can use the anchor attribute or property instead.

Attributes

Name Description Reflects Type Default
[Attribute + Property]
placement

The preferred placement of the popover. Note that the actual placement will vary as configured to keep the panel inside of the viewport.

'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'right' | 'right-start' | 'right-end' | 'left' | 'left-start' | 'left-end'
-
[Attribute + Property]
current-placement

The currentPlacement property / current-placement attribute are where Floating UI actually positions the popup.

'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'right' | 'right-start' | 'right-end' | 'left' | 'left-start' | 'left-end' | null
-
[Attribute + Property]
strategy

Determines how the popover is positioned. Because you native “popover” API uses a fixed strategy, we use it as the default.

Strategy
-
[Attribute + Property]
distance

The distance in pixels from which to offset along the “main axis”. Usually its equivalent to offsetY

number
-
[Attribute + Property]
skidding

The distance in pixels from which to offset along the “cross axis”. Usually its equivalent to offsetX.

number
-
[Attribute + Property]
arrow

Attaches an arrow to the popover. The arrow’s size and color can be customized using the --arrow-size and --background-color custom properties. For additional customizations, you can also target the arrow using ::part(arrow) in your stylesheet.

boolean
-
[Attribute + Property]
arrow-placement

The placement of the arrow. The default is anchor, which will align the arrow as close to the center of the anchor as possible, considering available space and arrow-padding. A value of start, end, or center will align the arrow to the start, end, or center of the popover instead.

'start' | 'end' | 'center' | 'anchor'
-
[Attribute + Property]
arrow-padding

The amount of padding between the arrow and the edges of the popover. If the popover has a border-radius, for example, this will prevent it from overflowing the corners.

number
-
[Attribute + Property]
flip

When set, placement of the popover will flip to the opposite site to keep it in view. You can use flipFallbackPlacements to further configure how the fallback placement is determined.

boolean
-
[Attribute + Property]
flip-fallback-placements

If the preferred placement doesn’t fit, popover will be tested in these fallback placements until one fits. Must be a string of any number of placements separated by a space, e.g. “top bottom left”. If no placement fits, the flip fallback strategy will be used instead.

string
-
[Attribute + Property]
flip-fallback-strategy

When neither the preferred placement nor the fallback placements fit, this value will be used to determine whether the popover should be positioned using the best available fit based on available space or as it was initially preferred.

'best-fit' | 'initial'
-
[Attribute + Property]
flip-padding

The amount of padding, in pixels, to exceed before the flip behavior will occur.

number
-
[Attribute + Property]
shift

Moves the popover along the axis to keep it in view when clipped.

boolean
-
[Attribute + Property]
shift-padding

The amount of padding, in pixels, to exceed before the shift behavior will occur.

number
-
[Attribute + Property]
auto-size

When set, this will cause the popover to automatically resize itself to prevent it from overflowing.

null | 'horizontal' | 'vertical' | 'both'
-
[Attribute + Property]
sync

Syncs the popover’s width or height to that of the anchor element.

null | 'width' | 'height' | 'both'
-
[Attribute + Property]
auto-size-padding

The amount of padding, in pixels, to exceed before the auto-size behavior will occur.

number
-
[Attribute + Property]
hover-bridge

When a gap exists between the anchor and the popover element, this option will add a “hover bridge” that fills the gap using an invisible element. This makes listening for events such as mouseenter and mouseleave more sane because the pointer never technically leaves the element. The hover bridge will only be drawn when the popover is active.

boolean
-

Events

Name Description
role-reposition

Emitted when the popover is repositioned. This event can fire a lot, so avoid putting expensive operations in your listener or consider debouncing it.

Functions

Name Description Parameters
__togglePopover__()
-
-
reposition()

Forces the popover to recalculate and reposition itself.

-
getOrAssignId()

Assign a random UUID with a prefix if no id is found. Will return the “id” of the element if its already assigned.

prefix: string, force: boolean
getTextDirection()
-
-
debounce()
-
callback: (...args: any[]) => any, options: { key: any, wait: number }
setAria()
-
key: T, value: ARIAMixin[T]

Parts

Name Description
arrow

The arrow’s container. Avoid setting top|bottom|left|right properties, as these values are assigned dynamically as the popover moves. This is most useful for applying a background color to match the popover, and maybe a border or box shadow.

popover

The popover’s container. Useful for setting a background color, box shadow, etc.

hover-bridge

The hover bridge element. Only available when the hover-bridge option is enabled.