Anchored region
Anchored regions are containers which position content relative to the anchor element. The anchored region can react to the available space between the anchor and a parent "viewport" element such that the region is placed on the side of the anchor with the most available space, or even resize itself based on that space.
Use cases:
- Creating a layout which is reactive to layout changes
- Creating a layout where elements are not viewed in DOM order (1)
(1) Elements which are viewed differently to the DOM order may cause accessibility issues with screen readers.
Example
Content after button in DOM
The live example includes css styling to ensure the components fit into the example container.
- Genesis
- React
- Angular
Declaration
<rapid-anchored-region></rapid-anchored-region>
Usage
@customElement({
name: 'my-element',
template: html`
<div id="viewport">
<button id="anchor">
Button is an anchor, defined first in the DOM
</button>
<rapid-anchored-region
anchor="anchor"
vertical-positioning-mode="locktodefault"
vertical-default-position="top">
This shows up above the button, even though it's after it in the DOM
</rapid-anchored-region>
</div>
`,
})
export class MyElement extends GenesisElement { }
Declaration
<rapid-anchored-region></rapid-anchored-region>
Usage
export function MyComponent() {
return (
<div id="viewport">
<button id="anchor">
Button is an anchor, defined first in the DOM
</button>
<rapid-anchored-region
anchor="anchor"
vertical-positioning-mode="locktodefault"
vertical-default-position="top">
This shows up above the button, even though it's after it in the DOM
</rapid-anchored-region>
</div>
);
}
Declaration
<rapid-anchored-region></rapid-anchored-region>
Usage
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-root',
template: `
<div id="viewport">
<button id="anchor">
Button is an anchor, defined first in the DOM
</button>
<rapid-anchored-region
anchor="anchor"
vertical-positioning-mode="locktodefault"
vertical-default-position="top">
This shows up above the button, even though it's after it in the DOM
</rapid-anchored-region>
</div>
`,
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [FormsModule],
})
export class AppComponent { }
API
Attributes
Attribute | Type | Description | Example |
---|---|---|---|
anchor | string | The HTML ID of the anchor element this region is positioned relative to. Default: "" |
|
viewport | string | The HTML ID of the viewport element this region is positioned relative to. Default: "" |
|
horizontal-positioning-mode | "uncontrolled" | "locktodefault" | "dynamic" | Sets logic for horizontal placement. Default: "uncontrolled". locktodefault forces the default position, dynamic decides placement based on available space, and uncontrolled does not control placement on the horizontal axis |
|
horizontal-default-position | "start" | "end" | "left" | "right" | "center" | "unset" | Default horizontal position relative to anchor. Default: "unset" |
|
horizontal-viewport-lock | boolean | Whether region remains in viewport on horizontal axis. Default: false |
|
horizontal-inset | boolean | Whether region overlaps anchor on horizontal axis. Default: false |
|
horizontal-threshold | number | How narrow the space allocated to the default position has to be before the widest area is selected for the layout. |
|
horizontal-scaling | "anchor" | "fill" | "content" | How width is calculated. Default: "content" |
|
vertical-positioning-mode | "uncontrolled" | "locktodefault" | "dynamic" | Sets logic for vertical placement. Default: "uncontrolled". locktodefault forces the default position, dynamic decides placement based on available space, and uncontrolled does not control placement on the horizontal axis |
|
vertical-default-position | "top" | "bottom" | "center" | "unset" | Default vertical position relative to anchor. Default: "unset" |
|
vertical-viewport-lock | boolean | Whether region remains in viewport on vertical axis (i.e. detatches from the anchor). Default: false |
|
vertical-inset | boolean | Whether the region overlaps the anchor on the vertical axis |
|
vertical-threshold | number | Space threshold before tallest area is selected for layout |
|
vertical-scaling | "anchor" | "fill" | "content" | How height is calculated. Default: "content" |
|
fixed-placement | boolean | Whether region uses fixed positioning instead of absolute. Default: false |
|
auto-update-mode | "anchor" | "auto" | Defines what triggers position reevaluation. Default: "anchor". anchor the component only updates its position when the anchor resizes, auto the component updates its position when: the anchor, window, or viewport resizes, any scroll in the document. Both update when update() is called. |
|
Properties
You should only be reading these properties via the code, rather than setting them on the template.
Property | Type | Description |
---|---|---|
initialLayoutComplete | boolean | Indicates that an initial positioning pass on layout has completed |
verticalPosition | "start" | "insetStart" | "insetEnd" | "end" | "center" | Indicates the current vertical position of the region. Depending on the axis start = left/top, end = right/bottom |
horizontalPosition | "start" | "insetStart" | "insetEnd" | "end" | "center" | Indicates the current horizontal position of the region. Depending on the axis start = left/top, end = right/bottom |
Slots
Slot | Description |
---|---|
Default | This elements which are contained in the anchored region. |
Parts
This component doesn't have any css parts. Style the content in the default slot directly.
Events fired
Event | Type | Description | Example |
---|---|---|---|
loaded | this | Fires when the region is loaded and visible |
|
positionchange | this | Fires when the position has changed |
|
Event type this
means the event detail is a reference to the element itself.
Events listened to
This component doesn't listen to any events.