Positioning
Places a popup against an anchor, keeps it inside a boundary, and reports where it ended up: for a popup the kit doesn't have.
Anyone with the link can view it.
import { UiSchema, UiStruct } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Positioner, PositionerPopup } from "#ui/positioning"
const PositioningBasicMessage = UiSchema.TaggedEnum({
PressedShare: {},
PressedDone: {},
ChangedPlacement: { placement: Positioning.Placement }
})
export const PositioningBasic = Html.component({
name: "PositioningBasic",
model: Schema.Struct({ open: Schema.Boolean, placement: Schema.NullOr(Positioning.Placement) }),
message: PositioningBasicMessage,
// A placement is a measurement: after Back the mount reports it again.
restore: (model) => UiStruct.evolve(model, { placement: null })
}).make({
init: () => ({ open: false, placement: null }),
update: {
PressedShare: ($) => $.evolve({ open: (open) => !open }),
PressedDone: ($) => $.evolve({ open: false }),
ChangedPlacement: ($, { placement }) => $.evolve({ placement })
},
view: (h, model) => {
const anchorId = h.id("anchor")
const args = Positioning.trackArgs(
{ align: "start", sideOffset: 8 },
{ side: "bottom", anchorSelector: `[id="${anchorId}"]` }
)
return h.div.children(
Button(h, { variant: "outline", onPress: PositioningBasicMessage.PressedShare() })
.id(anchorId)
.attr("aria-expanded", String(model.open))
.children("Share"),
Positioner(h, {
args,
open: model.open,
placement: model.placement,
onPlacement: (placement) => PositioningBasicMessage.ChangedPlacement({ placement })
}).children(
PositionerPopup(h, { class: "grid w-64 gap-1" }).attrs({ role: "dialog", "aria-label": "Share" }).children(
h.strong.class("font-semibold").children("Share this page"),
h.p.class("mb-2 text-muted-foreground").children("Anyone with the link can view it."),
Button(h, { size: "sm", class: "justify-self-start", onPress: PositioningBasicMessage.PressedDone() })
.children("Done")
)
)
)
}
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.Copy the component into your project
The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
src/ui/positioning.ts import { Mount } from "@effect/ui" import type { Html } from "@effect/ui/html" import { Positioning } from "@effect/ui-kit" import { cn } from "#lib/cn" type ClassOption = { readonly class?: string | undefined } /** * The element `Positioning.TrackPosition` places against its anchor: a * popover in the top layer, or an ordinary element when the args say * `topLayer: false`. It carries the mount while `open`, the inline style the * mount needs, and `data-side`, `data-align` and `data-anchor-hidden` from * the last `Placement`, which the caller keeps in its Model: * * `Positioner(h, { args, open: model.open, placement: model.placement, onPlacement: (placement) => Message.ChangedPlacement({ placement }) })` * * It hides itself while the anchor is scrolled or clipped out of view. */ export const Positioner = <Msg>( h: Html.H<Msg>, options: ClassOption & { /** From `Positioning.trackArgs(props, { side, anchorSelector })`. */ readonly args: Positioning.TrackPositionArgs readonly open: boolean /** The last placement the mount reported, or `null` before the first. */ readonly placement: Positioning.Placement | null readonly onPlacement: (placement: Positioning.Placement) => Msg /** `"auto"` lets the browser close it on Escape or a press outside: follow its `toggle`. */ readonly popover?: "manual" | "auto" | undefined } ) => { const { args, open, placement } = options const element = h.div .class(cn("z-50 w-max max-w-[var(--available-width)] data-[anchor-hidden]:invisible", options.class)) .attrs({ style: Positioning.positionerStyle(args.positionMethod), hidden: args.topLayer === false && !open ? "" : undefined, "data-side": placement?.side ?? args.options.side, "data-align": placement?.align ?? args.options.align, "data-anchor-hidden": placement?.anchorHidden === true ? "" : undefined }) const layered = args.topLayer === false ? element : element.popover(options.popover ?? "manual", open) // The positioner has to be laid out while the mount runs. return open ? layered.onMount(Positioning.TrackPosition(args).pipe(Mount.map(options.onPlacement))) : layered } const sizes = { default: "p-3.5", sm: "px-2.5 py-1.5 text-xs" } as const /** The surface inside a `Positioner`. It fades and grows from the anchor when it first shows. */ export const PositionerPopup = <Msg>( h: Html.H<Msg>, options: ClassOption & { readonly size?: keyof typeof sizes | undefined } = {} ) => h.div.class(cn( "relative origin-[var(--transform-origin)] rounded-lg border bg-popover text-[13px] text-foreground shadow-float", "transition-[opacity,scale] duration-150 starting:scale-95 starting:opacity-0", sizes[options.size ?? "default"], options.class )) const arrowSides: Record<Positioning.Side, string> = { top: "-bottom-[5px] border-t-0 border-l-0", bottom: "-top-[5px] border-r-0 border-b-0", left: "-right-[5px] border-b-0 border-l-0", right: "-left-[5px] border-t-0 border-r-0", "inline-start": "-end-[5px] border-b-0 border-s-0 rtl:-rotate-45", "inline-end": "-start-[5px] border-t-0 border-e-0 rtl:-rotate-45" } /** * An arrow inside a `PositionerPopup`, pointing at the anchor. Pass its `id` * to `trackArgs` as `arrowId`, so the mount measures it, and the side from * the placement. It fades when it can't point at the anchor's middle. */ export const PositionerArrow = <Msg>( h: Html.H<Msg>, options: ClassOption & { readonly id: string readonly side: Positioning.Side readonly uncentered?: boolean | undefined } ) => h.span .id(options.id) .class(cn( "size-2.5 rotate-45 border bg-popover data-[uncentered]:opacity-0", arrowSides[options.side], options.class )) .attrs({ style: Positioning.arrowStyle(options.side), "data-side": options.side, "data-uncentered": options.uncentered === true ? "" : undefined, "aria-hidden": "true" })Import it
import { Positioner, PositionerPopup, PositionerArrow } from "#ui/positioning"
Usage
import { UiSchema, UiStruct } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Positioner, PositionerPopup } from "#ui/positioning"
const PositioningBasicMessage = UiSchema.TaggedEnum({
PressedShare: {},
PressedDone: {},
ChangedPlacement: { placement: Positioning.Placement }
})
export const PositioningBasic = Html.component({
name: "PositioningBasic",
model: Schema.Struct({ open: Schema.Boolean, placement: Schema.NullOr(Positioning.Placement) }),
message: PositioningBasicMessage,
// A placement is a measurement: after Back the mount reports it again.
restore: (model) => UiStruct.evolve(model, { placement: null })
}).make({
init: () => ({ open: false, placement: null }),
update: {
PressedShare: ($) => $.evolve({ open: (open) => !open }),
PressedDone: ($) => $.evolve({ open: false }),
ChangedPlacement: ($, { placement }) => $.evolve({ placement })
},
view: (h, model) => {
const anchorId = h.id("anchor")
const args = Positioning.trackArgs(
{ align: "start", sideOffset: 8 },
{ side: "bottom", anchorSelector: `[id="${anchorId}"]` }
)
return h.div.children(
Button(h, { variant: "outline", onPress: PositioningBasicMessage.PressedShare() })
.id(anchorId)
.attr("aria-expanded", String(model.open))
.children("Share"),
Positioner(h, {
args,
open: model.open,
placement: model.placement,
onPlacement: (placement) => PositioningBasicMessage.ChangedPlacement({ placement })
}).children(
PositionerPopup(h, { class: "grid w-64 gap-1" }).attrs({ role: "dialog", "aria-label": "Share" }).children(
h.strong.class("font-semibold").children("Share this page"),
h.p.class("mb-2 text-muted-foreground").children("Anyone with the link can view it."),
Button(h, { size: "sm", class: "justify-self-start", onPress: PositioningBasicMessage.PressedDone() })
.children("Done")
)
)
)
}
})
Examples
Side and align
Six sides, three alignments and an offset. The popup is drawn in place with topLayer: false.
import { UiSchema, UiStruct } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Positioner, PositionerPopup } from "#ui/positioning"
const PositioningPlaygroundMessage = UiSchema.TaggedEnum({
PressedSide: { side: Positioning.Side },
PressedAlign: { align: Positioning.Align },
UpdatedSideOffset: { value: Schema.String },
ChangedPlacement: { placement: Positioning.Placement }
})
export const PositioningPlayground = Html.component({
name: "PositioningPlayground",
model: Schema.Struct({
side: Positioning.Side,
align: Positioning.Align,
sideOffset: Schema.Number,
placement: Schema.NullOr(Positioning.Placement)
}),
message: PositioningPlaygroundMessage,
restore: (model) => UiStruct.evolve(model, { placement: null })
}).make({
init: () => ({ side: "top", align: "center", sideOffset: 8, placement: null }),
update: {
PressedSide: ($, { side }) => $.evolve({ side }),
PressedAlign: ($, { align }) => $.evolve({ align }),
UpdatedSideOffset: ($, { value }) => $.evolve({ sideOffset: Number(value) }),
ChangedPlacement: ($, { placement }) => $.evolve({ placement })
},
view: (h, model) => {
const anchorId = h.id("anchor")
// With topLayer: false the positioner is an ordinary element, placed
// against the frame, its nearest positioned ancestor.
const args = Positioning.trackArgs(
{ side: model.side, align: model.align, sideOffset: model.sideOffset, topLayer: false },
{ side: "top", anchorSelector: `[id="${anchorId}"]` }
)
return h.div.class("flex w-full max-w-xl flex-col gap-4").children(
h.div.class("flex flex-wrap items-center gap-1").attrs({ role: "group", "aria-label": "Side" }).children(
Positioning.Side.literals.map((side) =>
Button(h, {
variant: side === model.side ? "secondary" : "ghost",
size: "sm",
onPress: PositioningPlaygroundMessage.PressedSide({ side })
}).attr("aria-pressed", String(side === model.side)).children(side)
)
),
h.div.class("flex flex-wrap items-center gap-1").attrs({ role: "group", "aria-label": "Align" }).children(
Positioning.Align.literals.map((align) =>
Button(h, {
variant: align === model.align ? "secondary" : "ghost",
size: "sm",
onPress: PositioningPlaygroundMessage.PressedAlign({ align })
}).attr("aria-pressed", String(align === model.align)).children(align)
),
h.label.class("ml-auto flex items-center gap-2 text-xs text-muted-foreground tabular-nums").children(
`sideOffset ${model.sideOffset}px`,
h.input
.class("w-28 accent-brand")
.attrs({ type: "range", min: "0", max: "24" })
.value(String(model.sideOffset))
.on("input", ({ value }) => PositioningPlaygroundMessage.UpdatedSideOffset({ value }))
)
),
h.div.class("relative flex h-64 items-center justify-center overflow-hidden rounded-lg border bg-muted/40").children(
h.div
.id(anchorId)
.class("flex h-12 w-28 items-center justify-center rounded-md border border-dashed border-input bg-background text-xs text-muted-foreground")
.children("Anchor"),
Positioner(h, {
args,
open: true,
placement: model.placement,
onPlacement: (placement) => PositioningPlaygroundMessage.ChangedPlacement({ placement })
}).children(
PositionerPopup(h, { size: "sm" }).children(
`${model.placement?.side ?? model.side} · ${model.placement?.align ?? model.align}`
)
)
)
)
}
})
Collisions
Scroll the box: the popup flips to the other side, slides over the anchor, or stays and is clipped.
Moves to the opposite side when it would leave the box.
import { UiSchema, UiStruct } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Positioner, PositionerPopup } from "#ui/positioning"
const Avoidance = Schema.Literals(["flip", "shift", "none"])
const PositioningCollisionMessage = UiSchema.TaggedEnum({
PressedAvoidance: { avoidance: Avoidance },
ChangedPlacement: { placement: Positioning.Placement }
})
const notes = {
flip: "Moves to the opposite side when it would leave the box.",
shift: "Keeps its side and slides over the anchor to stay inside.",
none: "Stays where it was asked to be, and is clipped."
} as const
export const PositioningCollision = Html.component({
name: "PositioningCollision",
model: Schema.Struct({ avoidance: Avoidance, placement: Schema.NullOr(Positioning.Placement) }),
message: PositioningCollisionMessage,
restore: (model) => UiStruct.evolve(model, { placement: null })
}).make({
init: () => ({ avoidance: "flip", placement: null }),
update: {
PressedAvoidance: ($, { avoidance }) => $.evolve({ avoidance }),
ChangedPlacement: ($, { placement }) => $.evolve({ placement })
},
view: (h, model) => {
const anchorId = h.id("anchor")
// Drawn in place, "clipping-ancestors" is the part of the scrolling box
// that shows, so the popup collides with the box and not the window.
const args = Positioning.trackArgs(
{
sideOffset: 8,
topLayer: false,
collisionAvoidance: { side: model.avoidance, align: "shift", fallbackAxisSide: "none" }
},
{ side: "bottom", anchorSelector: `[id="${anchorId}"]` }
)
return h.div.class("flex w-full max-w-md flex-col gap-3").children(
h.div
.class("flex flex-wrap items-center gap-1")
.attrs({ role: "group", "aria-label": "When the popup doesn't fit" })
.children(
Avoidance.literals.map((avoidance) =>
Button(h, {
variant: avoidance === model.avoidance ? "secondary" : "ghost",
size: "sm",
onPress: PositioningCollisionMessage.PressedAvoidance({ avoidance })
}).attr("aria-pressed", String(avoidance === model.avoidance)).children(avoidance)
)
),
h.div
.class("relative h-64 overflow-auto rounded-lg border bg-muted/40")
.attrs({ tabindex: "0", role: "group", "aria-label": "Scrolling box" })
.children(
h.div.class("flex h-[26rem] w-[150%] items-center justify-center").children(
h.div
.id(anchorId)
.class("flex h-12 w-28 items-center justify-center rounded-md border border-dashed border-input bg-background text-xs text-muted-foreground")
.children("Scroll me")
),
Positioner(h, {
args,
open: true,
placement: model.placement,
onPlacement: (placement) => PositioningCollisionMessage.ChangedPlacement({ placement })
}).children(
PositionerPopup(h, { size: "sm", class: "grid h-20 w-40 place-items-center" }).children(
`side: ${model.placement?.side ?? args.options.side}`
)
)
),
h.p.class("text-xs text-muted-foreground").children(
model.placement?.anchorHidden === true
? "The anchor is out of view, so the popup hides."
: notes[model.avoidance]
)
)
}
})
With an arrow
The mount measures the arrow by its id and arrowStyle puts it at the anchor's middle.
import { UiSchema, UiStruct } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Positioner, PositionerArrow, PositionerPopup } from "#ui/positioning"
const Side = Schema.Literals(["top", "right", "bottom", "left"])
const PositioningArrowMessage = UiSchema.TaggedEnum({
PressedSide: { side: Side },
PressedAlign: { align: Positioning.Align },
ChangedPlacement: { placement: Positioning.Placement }
})
export const PositioningArrow = Html.component({
name: "PositioningArrow",
model: Schema.Struct({
side: Side,
align: Positioning.Align,
placement: Schema.NullOr(Positioning.Placement)
}),
message: PositioningArrowMessage,
restore: (model) => UiStruct.evolve(model, { placement: null })
}).make({
init: () => ({ side: "top", align: "center", placement: null }),
update: {
PressedSide: ($, { side }) => $.evolve({ side }),
PressedAlign: ($, { align }) => $.evolve({ align }),
ChangedPlacement: ($, { placement }) => $.evolve({ placement })
},
view: (h, model) => {
const anchorId = h.id("anchor")
const arrowId = h.id("arrow")
// The mount measures the arrow by its id and keeps it arrowPadding from the popup's corners.
const args = Positioning.trackArgs(
{ side: model.side, align: model.align, sideOffset: 10, arrowPadding: 10, topLayer: false },
{ side: "top", anchorSelector: `[id="${anchorId}"]`, arrowId }
)
return h.div.class("flex w-full max-w-xl flex-col gap-4").children(
h.div.class("flex flex-wrap items-center gap-1").children(
Side.literals.map((side) =>
Button(h, {
variant: side === model.side ? "secondary" : "ghost",
size: "sm",
onPress: PositioningArrowMessage.PressedSide({ side })
}).attr("aria-pressed", String(side === model.side)).children(side)
),
h.span.class("mx-2 h-4 w-px bg-border"),
Positioning.Align.literals.map((align) =>
Button(h, {
variant: align === model.align ? "secondary" : "ghost",
size: "sm",
onPress: PositioningArrowMessage.PressedAlign({ align })
}).attr("aria-pressed", String(align === model.align)).children(align)
)
),
h.div.class("relative flex h-64 items-center justify-center overflow-hidden rounded-lg border bg-muted/40").children(
h.div
.id(anchorId)
.class("flex size-8 items-center justify-center rounded-full border border-input bg-background text-xs font-medium")
.children("?"),
Positioner(h, {
args,
open: true,
placement: model.placement,
onPlacement: (placement) => PositioningArrowMessage.ChangedPlacement({ placement })
}).children(
PositionerPopup(h, { size: "sm", class: "w-44" }).children(
"The arrow points at the middle of the anchor.",
PositionerArrow(h, {
id: arrowId,
side: model.placement?.side ?? args.options.side,
uncentered: model.placement?.arrowUncentered
})
)
)
)
)
}
})
At the pointer
The anchor is a rectangle instead of an element: a menu where the right-click landed.
Nothing picked yet.
import { UiSchema, UiStruct } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
import { Positioner, PositionerPopup } from "#ui/positioning"
const PositioningPointMessage = UiSchema.TaggedEnum({
RightClickedArea: { x: Schema.Number, y: Schema.Number },
ToggledPopup: { open: Schema.Boolean },
ClickedItem: { item: Schema.String },
ChangedPlacement: { placement: Positioning.Placement }
})
const items = ["Copy", "Paste", "Duplicate", "Delete"]
export const PositioningPoint = Html.component({
name: "PositioningPoint",
model: Schema.Struct({
point: Schema.NullOr(Schema.Struct({ x: Schema.Number, y: Schema.Number })),
picked: Schema.NullOr(Schema.String),
placement: Schema.NullOr(Positioning.Placement)
}),
message: PositioningPointMessage,
restore: (model) => UiStruct.evolve(model, { point: null, placement: null })
}).make({
init: () => ({ point: null, picked: null, placement: null }),
update: {
RightClickedArea: ($, { x, y }) => $.evolve({ point: { x, y } }),
// An auto popover is the browser's to close, on Escape or a press
// outside, and the Model follows its `toggle`.
ToggledPopup: ($, { open }) => (open ? $.model : $.evolve({ point: null })),
ClickedItem: ($, { item }) => $.evolve({ point: null, picked: item }),
ChangedPlacement: ($, { placement }) => $.evolve({ placement })
},
view: (h, model) => {
const areaId = h.id("area")
// The anchor is a rectangle with no size at the pointer. Nothing moves
// it, so the positioner is fixed.
const args = Positioning.trackArgs(
{
side: "bottom",
align: "start",
positionMethod: "fixed",
...(model.point === null ? {} : { anchor: { ...model.point, width: 0, height: 0 } })
},
{ side: "bottom", anchorSelector: `[id="${areaId}"]` }
)
return h.div.class("flex w-full max-w-md flex-col gap-3").children(
h.div
.id(areaId)
.class("flex h-44 items-center justify-center rounded-lg border border-dashed border-input text-[13px] text-muted-foreground select-none")
.on("contextmenu", ({ x, y }) => PositioningPointMessage.RightClickedArea({ x, y }), {
preventDefault: true
})
.children("Right-click anywhere in here"),
h.p.class("text-xs text-muted-foreground").children(
model.picked === null ? "Nothing picked yet." : `Picked ${model.picked}.`
),
Positioner(h, {
args,
open: model.point !== null,
popover: "auto",
placement: model.placement,
onPlacement: (placement) => PositioningPointMessage.ChangedPlacement({ placement })
})
.on("toggle", ({ open }) => PositioningPointMessage.ToggledPopup({ open }))
.children(
PositionerPopup(h, { class: "grid min-w-36 p-1" }).attr("role", "menu").children(
items.map((item) =>
h.button
.class("rounded-sm px-2.5 py-1.5 text-left transition-colors duration-150 hover:bg-accent focus-visible:bg-accent")
.attr("role", "menuitem")
.on("click", PositioningPointMessage.ClickedItem({ item }))
.children(item)
)
)
)
)
}
})
place()
The pure function under the mount: rectangles and options in, a position out, with no DOM.
place() → { x: 110, y: 94, side: "bottom", align: "center" }import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Positioning } from "@effect/ui-kit"
import { Schema } from "effect"
const PositioningPlaceMessage = UiSchema.TaggedEnum({
UpdatedAnchorX: { value: Schema.String },
UpdatedAnchorY: { value: Schema.String }
})
const boundary: Positioning.Rect = { x: 0, y: 0, width: 360, height: 220 }
const anchorSize: Positioning.Size = { width: 64, height: 28 }
const popup: Positioning.Size = { width: 140, height: 64 }
const options = Positioning.resolveOptions({ sideOffset: 6 }, { side: "bottom" })
const box = (rect: Positioning.Rect) => ({
left: `${rect.x}px`,
top: `${rect.y}px`,
width: `${rect.width}px`,
height: `${rect.height}px`
})
export const PositioningPlace = Html.component({
name: "PositioningPlace",
model: Schema.Struct({ x: Schema.Number, y: Schema.Number }),
message: PositioningPlaceMessage
}).make({
init: () => ({ x: 148, y: 60 }),
update: {
UpdatedAnchorX: ($, { value }) => $.evolve({ x: Number(value) }),
UpdatedAnchorY: ($, { value }) => $.evolve({ y: Number(value) })
},
view: (h, model) => {
const anchor = { ...anchorSize, x: model.x, y: model.y }
// `place` reads no DOM: rectangles in, a position out. It runs on the server too.
const result = Positioning.place({
anchor,
popup,
arrow: null,
boundary,
anchorClip: boundary,
rtl: false,
options
})
return h.div.class("flex flex-col items-start gap-4").children(
h.div.class("relative rounded-lg border bg-muted/40 font-mono text-[11px]").style(box(boundary)).children(
h.div
.class("absolute flex items-center justify-center rounded-sm border border-dashed border-input bg-background text-muted-foreground")
.style(box(anchor))
.children("anchor"),
h.div
.class("absolute flex items-center justify-center rounded-md border bg-popover shadow-float")
.style(box({ ...popup, x: result.x, y: result.y }))
.children(`${result.side} · ${result.align}`)
),
h.div.class("grid w-[360px] grid-cols-2 gap-4").children(
h.label.class("grid gap-1.5 text-xs text-muted-foreground tabular-nums").children(
`anchor.x ${model.x}`,
h.input
.class("accent-brand")
.attrs({ type: "range", min: "0", max: String(boundary.width - anchorSize.width) })
.value(String(model.x))
.on("input", ({ value }) => PositioningPlaceMessage.UpdatedAnchorX({ value }))
),
h.label.class("grid gap-1.5 text-xs text-muted-foreground tabular-nums").children(
`anchor.y ${model.y}`,
h.input
.class("accent-brand")
.attrs({ type: "range", min: "0", max: String(boundary.height - anchorSize.height) })
.value(String(model.y))
.on("input", ({ value }) => PositioningPlaceMessage.UpdatedAnchorY({ value }))
)
),
h.pre.class("rounded-md bg-muted px-3 py-2 font-mono text-xs").children(
`place() → { x: ${Math.round(result.x)}, y: ${Math.round(result.y)}, side: "${result.side}", align: "${result.align}" }`
)
)
}
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
place(input) | Pure: anchor, popup, arrow and boundary rectangles and options in, a position, side and align out. |
TrackPosition(args) | A stream mount for the positioner. It writes left and top inline and reports a Placement when it changes. |
trackArgs(props, popup) | Builds the mount's args from positioning props and the popup's own defaults and anchor selector. |
resolveOptions(props, defaults) | Fills in the defaults of the positioning props, for place. |
positionerStyle(method) | The inline style a positioner needs before the mount places it. |
arrowStyle(side) | The inline style that puts an arrow where the mount measured it. |
PositionerFields | The positioning props as Schema fields, to spread into a component's props. |
Placement | Schema of what the mount reports: side, align, anchorHidden, arrowUncentered. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
side | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | The side of the anchor the popup shows on. |
align | "start" | "center" | "end" | How the popup lines up with the anchor along that side. "center" by default. |
sideOffset | number | The gap between the anchor and the popup, in pixels. |
alignOffset | number | Pixels the popup moves along the side. |
collisionAvoidance | { side?, align?, fallbackAxisSide? } | What happens when the popup doesn't fit: "flip", "shift" or "none" for each. |
collisionBoundary | "clipping-ancestors" | { id } | Rect | The area the popup keeps inside. The viewport by default. |
collisionPadding | number | { top?, right?, bottom?, left? } | Pixels kept from the boundary's edges. 5 by default. |
sticky | boolean | The popup stays in view when the anchor scrolls out of it. |
arrowPadding | number | Pixels kept between the arrow and the popup's corners. 5 by default. |
positionMethod | "absolute" | "fixed" | The CSS position of the positioner. |
anchor | string | Rect | The id of an element, or a rectangle in the viewport, to place against instead. |
topLayer | boolean | false draws the positioner in place, so an ancestor's overflow and stacking apply. |
disableAnchorTracking | boolean | Place the popup only when it opens, the window resizes or its size changes. |
Events
| Event | Description |
|---|---|
Placement | Reported by TrackPosition when it starts, then whenever side, align, anchorHidden or arrowUncentered changes. |
Data attributes
| Attribute | Description |
|---|---|
left / top | Written inline on the positioner: the position. |
--anchor-width / --anchor-height | The anchor's size. |
--available-width / --available-height | The space between the anchor and the boundary's edge, for a max-height. |
--transform-origin | The point on the anchor the popup grows from. |
data-side / data-align | Written by you from the Placement the mount reports (the Positioner helper does it). |
data-anchor-hidden | Likewise, while the anchor is scrolled or clipped out of view. |
Related components
The components this page's file and examples import.
- Button: A native button that can stay focusable while disabled.