PreviewCard unstable
A card that shows a preview of what a link leads to while the link is hovered or focused with the keyboard.
PreviewCard is headless: it owns the hover and focus timing, the data
attributes, the popup's place in the top layer and its position, and the
consumer draws the markup. The consumer owns whether the card is open,
passes it as open, and stores what the RequestedOpen and RequestedClose
events ask for. The card stays as it is until open changes, so a
consumer can refuse a close.
The link stays the only way in for assistive technology: the trigger gets no ARIA from the card, the popup has no role, and focus never moves into it. Put nothing in the card that the link's own page doesn't also offer.
Anatomy
| Part | Element |
|---|---|
trigger |
<a> the card previews |
portal |
<div> in the top layer, around the parts below |
backdrop |
<div> that covers the page |
positioner |
<div> placed next to the trigger |
popup |
<div> with the card's content |
viewport |
<div> around content that changes with the trigger |
arrow |
<div> that points at the trigger |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children:
previewCard.portal(h.div)?.children(previewCard.positioner(h.div).children(previewCard.popup(h.div).children("โฆ"))).
The trigger takes an options object, previewCard.trigger({ element: h.a }),
since each trigger has its own delays. The portal returns undefined
while the card is closed, unless keepMounted. backdrop, viewport
and arrow are optional.
The portal is shown as a manual popover, so the card sits in the top layer.
The kit resets its box inline to an empty one with nothing visible of its
own, at the document's origin, or at the viewport's corner with
positionMethod: "fixed". The positioner is placed inside it, so style the
popup and the arrow, and a backdrop as a fixed layer:
.backdrop { position: fixed; inset: 0 }. The backdrop takes no presses.
With topLayer: false, the portal renders in place as a <div> with no
box of its own, hidden while closed, and the preview is placed and
clipped inside the positioner's containing block, as Positioning
describes.
The positioner's inline style carries left and top, and the variables
--anchor-width, --anchor-height, --available-width,
--available-height and --transform-origin. sideOffset is padding on
the positioner's side facing the trigger, so the pointer can cross the gap
without leaving the card.
A link that wraps over several lines is measured line by line: the card
is placed against the line the pointer rested on, which it keeps when the
lines move before the card opens. Opened by focus, it is
placed against the last line for side: "bottom", the first for "top",
and the lines that reach furthest toward the side for the other two.
Data attributes
Each present with an empty value or absent, except the ones with a value:
trigger:data-popup-openwhile the card is open for itbackdrop:data-open/data-closed,data-starting-style,data-ending-stylepositioner:data-open/data-closed,data-side,data-align,data-anchor-hidden,data-instantpopup: the backdrop's, anddata-side,data-align,data-instantviewport:data-transitioning,data-activation-direction,data-instantarrow:data-open/data-closed,data-side,data-align,data-uncentered
data-side and data-align name where the popup ended up, after it
flipped or shifted to fit. data-instant names a change that should skip
its transition: focus while the card is open by keyboard focus, and
dismiss once Escape closed it. data-starting-style is present for the
first frame after the card opens and data-ending-style while it closes.
The card stays shown until the popup's own transitions and animations
finish, then closes and emits Settled.
Keyboard
Focusing the trigger with the keyboard opens the card after the same
delay as a hover, and focus leaving the trigger and the card closes it.
Only focus that shows a focus ring, as :focus-visible tells, opens it,
so focus from a press, on the trigger or on a button that moves focus to
it, doesn't. Escape closes an open card wherever focus is. With focus in
the card, focus goes back to the trigger as the close starts, without
opening the card again, and the card is inert through its exit. The
card's own links follow the trigger in the tab order when the portal is
drawn right after it. A card opened from a detached trigger isn't next to
the link in the page, so Tab from the link skips the card's content and
the card closes. Anything interactive in such a card must also be
reachable from the link's own page.
Hover
The card asks to open once the mouse has rested on a trigger for its
delay, 600 milliseconds by default, and to close closeDelay
milliseconds, 300 by default, after the pointer has left the trigger and
the card. On its way from the trigger to the card, the pointer can cross
the triangle between where it left the trigger and the card's near edge
without starting the close, so a short link and a wide card can be crossed
diagonally. Keyboard focus on the trigger or in the card holds it open. A
touch on the trigger opens nothing, and a click on it doesn't close the
card. A press outside the trigger and the card closes it. A layer that
covers the trigger and gives the pointer back where it rested is no new
hover. If the trigger the card is open for leaves the page, the card asks
to close.
Several triggers
Every trigger reports its id in RequestedOpen, the consumer's or a
generated one. Store it and pass it as triggerId: the popup is placed
against that trigger, and the consumer draws the content for it. Hovering
another trigger while the card is open moves it there at once.
To animate the content as the card moves from one trigger to another,
draw it inside previewCard.viewport(h.div), in the popup. The viewport puts its
children in a <div data-current>. While the card moves, it keeps what the
last trigger showed in a <div data-previous> before it, inert and out of
the flow, has data-transitioning, and names where the new trigger is from
the last one in data-activation-direction: left or right, a space,
then up or down, each left out within 5 pixels. The new content has
data-starting-style for the first frame and the old one
data-ending-style after it, and the old content is removed once the new
content's transitions finish.
With a viewport, the positioner's inline style also carries
--positioner-width and --positioner-height, the popup's size for the
current content, and the popup's --popup-width and --popup-height.
Those two are auto, except while the card moves: they then go from the
old content's size to the new one's, so
.popup { width: var(--popup-width, auto); transition: width 200ms }
animates the resize.
A PreviewCardTrigger opens a card from another part of the page. Give the
card an id and the trigger the same popupId. Its hovers and focus reach
the card by themselves, so they arrive as the card's own RequestedOpen and
RequestedClose.
Limits
- There is no
payload, handle ordefaultOpen:openandtriggerIdlive in the consumer's Model, which also decides the content. - A pointer that stops in the triangle between the trigger and the card keeps the card open until it moves out.
Example A link with a preview whose open state lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { PreviewCard } from "effect/ui/kit"import { Effect, Schema } from "effect"
const ArticleMessage = UiSchema.TaggedEnum({ RequestedOpenPreview: {}, RequestedClosePreview: {} })
const Article = Html.component({ name: "Article", model: Schema.Struct({ preview: Schema.Boolean }), message: ArticleMessage}).make({ init: () => ({ preview: true }), update: { RequestedOpenPreview: ($) => $.evolve({ preview: true }), RequestedClosePreview: ($) => $.evolve({ preview: false }) }, view: (h, model) => PreviewCard(h, { props: { open: model.preview, sideOffset: 8 }, onEmit: { RequestedOpen: () => ArticleMessage.RequestedOpenPreview(), RequestedClose: () => ArticleMessage.RequestedClosePreview(), Settled: () => undefined }, view: (previewCard) => h.div( h.p( "Good ", previewCard.trigger({ element: h.a.attr("href", "/typography") }).children("typography"), " makes text easy to read." ), previewCard.portal(h.div)?.children( previewCard.positioner(h.div).children( previewCard.popup(h.div.class("card")).children("Typography is the art of arranging type.") ) ) ) })})
// The trigger has no ARIA of its own, and the popup takes no focus from Tab.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Article, {}))if (html.includes("aria-expanded")) throw new Error("the link announces a popup")if (!/<div class="card"[^>]* tabindex="-1"/.test(html)) throw new Error("the popup is in the tab order")Components
PreviewCard
unstableA headless preview card. Call it with the consumer's h and a view
closure that draws the link with previewCard.trigger and the card with
previewCard.portal and the parts inside it.
Details
The parts also return plain values for markup that depends on them:
open, mounted (the card shows, through its exit transition),
transitionStatus, triggerId, the id of the trigger the card is open
for or last was, openReason, side and align, where the popup is,
and instant.
The trigger part takes { element, delay, closeDelay }. It reports the
id the consumer gave the element in RequestedOpen, and gives a trigger
without one a generated id.
Each part's attributes win over the ones the consumer set on the builder
it passes in, including the ones a state leaves absent: id,
data-popup-open and data-preview-card-trigger on the trigger; id and
popover on the portal; role on the backdrop; id and role on the
positioner; id and tabindex on the popup; id and aria-hidden on
the arrow; and the data attributes on every part. The viewport's children
are drawn inside its data-current container. Consumer inline styles override
the portal's box defaults. The portal's document-origin placement, the
positioner's and arrow's position and the backdrop's pointer-events
are added after consumer styles.
A handler the consumer adds to a part runs after the card's own. The
trigger takes the element's mount.
It emits RequestedOpen, RequestedClose and Settled.
On the server and on a host that doesn't run mounts, the card renders closed and isn't placed.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const PreviewCard: Headless<{ readonly align?: "start" | "end" | "center"; readonly alignOffset?: number; readonly anchor?: string | { readonly height: number; readonly width: number; readonly x: number; readonly y: number; }; readonly arrowPadding?: number; readonly collisionAvoidance?: { readonly align?: "shift" | "none" | "flip"; readonly fallbackAxisSide?: "start" | "none" | "end"; readonly side?: "shift" | "none" | "flip"; }; readonly collisionBoundary?: "clipping-ancestors" | { readonly height: number; readonly width: number; readonly x: number; readonly y: number; } | { readonly id: string; }; readonly collisionPadding?: number | { readonly bottom?: number; readonly left?: number; readonly right?: number; readonly top?: number; }; readonly disableAnchorTracking?: boolean; readonly holdExit?: boolean; readonly id?: string; readonly keepMounted?: boolean; readonly open: boolean; readonly positionMethod?: "fixed" | "absolute"; readonly side?: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; readonly sideOffset?: number; readonly sticky?: boolean; readonly topLayer?: boolean; readonly triggerId?: string | null;}, { readonly _tag: "Settled"; readonly open: boolean;} | { readonly _tag: "RequestedOpen"; readonly reason: "trigger-hover" | "trigger-focus"; readonly triggerId: string | null;} | { readonly _tag: "RequestedClose"; readonly reason: "outside-press" | "escape-key" | "trigger-hover" | "trigger-focus" | "trigger-removed";}, { align: "start" | "end" | "center"; arrow: Part<"div", [], never>; backdrop: <B extends Keyed<"div">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"div", [], never>, "~R">; instant: PreviewCardInstant | undefined; mounted: boolean; open: boolean; openReason: "trigger-hover" | "trigger-focus" | null; popup: Part<"div", [], never>; portal: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; positioner: <B extends Keyed<"div">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"div", [], never>, "~R">; side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; transitionStatus: TransitionStatus; trigger: <B extends PartTarget<"a">>(options: { readonly closeDelay?: number; readonly delay?: number; readonly element: B; }) => ReturnType<B["attr"]> & Pick<Part<"a", [key: string, delays: { readonly closeDelay: number; readonly delay: number; }], never>, "~R">; triggerId: string | null; viewport: Part<"div", [], never>;}, never, never, never, never, never, never, { readonly anchorHit: { readonly line: number | null; readonly x: number; readonly y: number; } | null; readonly asked: boolean; readonly away: boolean; readonly closeDelay: number; readonly closeReason: "outside-press" | "escape-key" | "trigger-hover" | "trigger-focus" | "trigger-removed" | null; readonly focused: boolean; readonly focusInside: boolean; readonly focusTrigger: string | null; readonly hoverTrigger: string | null; readonly mounted: boolean; readonly onTrigger: boolean; readonly pending: "trigger-hover" | "trigger-focus" | null; readonly pendingTrigger: string | null; readonly placement: { readonly align: "start" | "end" | "center"; readonly anchorHidden: boolean; readonly arrowUncentered: boolean; readonly side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; } | null; readonly point: { readonly line: number | null; readonly x: number; readonly y: number; } | null; readonly reason: "trigger-hover" | "trigger-focus" | null; readonly rest: { readonly triggerId: string | null; readonly x: number; readonly y: number; } | null; readonly started: boolean; readonly transition: { readonly direction: string | null; readonly entered: boolean; } | null; readonly trigger: string | null;}, { readonly _tag: "RenderedFirstFrame";} | { readonly _tag: "PressedEscape";} | { readonly _tag: "FocusedOutside";} | { readonly _tag: "ShowedPortal";} | { readonly _tag: "BlurredTrigger";} | { readonly _tag: "LeftHoverArea";} | { readonly _tag: "ReturnedToHoverArea";} | { readonly _tag: "PressedOutside";} | { readonly _tag: "CompletedWaitBeforeClosing";} | { readonly _tag: "CompletedWaitBeforeOpening";} | { readonly _tag: "FinishedTransition"; readonly open: boolean;} | { readonly _tag: "ChangedPlacement"; readonly placement: { readonly align: "start" | "end" | "center"; readonly anchorHidden: boolean; readonly arrowUncentered: boolean; readonly side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; };} | { readonly _tag: "EnteredTrigger"; readonly at: { readonly x: number; readonly y: number; } | null; readonly closeDelay: number; readonly delay: number; readonly triggerId: string | null;} | { readonly _tag: "LeftTrigger"; readonly at: { readonly x: number; readonly y: number; } | null; readonly triggerId: string | null;} | { readonly _tag: "FocusedTrigger"; readonly closeDelay: number; readonly delay: number; readonly triggerId: string | null; readonly visible: boolean;} | { readonly _tag: "ChangedViewport"; readonly change: { readonly _tag: "Started"; readonly direction: string | null; } | { readonly _tag: "Advanced"; } | { readonly _tag: "Ended"; };} | { readonly _tag: "MovedToLine"; readonly at: { readonly x: number; readonly y: number; }; readonly line: number; readonly triggerId: string | null;} | { readonly _tag: "FocusedPopup";} | { readonly _tag: "BlurredPopup";} | { readonly _tag: "RemovedTrigger";}, QueryEntries<{}, {}>>PreviewCardTrigger
unstableA link that opens a preview card drawn somewhere else, such as in another
component. Call it with the caller's h, whether the card is open for this
link and the card's id as popupId, then chain the href and the label.
Details
Its hovers and focus reach the card with that id by themselves, so the card
emits RequestedOpen and RequestedClose for them as for its own trigger
part, with this link's id. Give the link an id and pass the one
RequestedOpen reports as the card's triggerId, so the popup is placed
against it. Nothing happens while no card with that id is drawn.
The card isn't next to this link in the page, so Tab from the link skips the card's content and the card closes. Anything interactive in the card must also be reachable from the link's own page.
It sets data-popup-open and the attribute the card finds its triggers by,
in forward, so they win over the caller's. It takes the element's mount,
so the caller can't set another one on it.
Signature
declare function PreviewCardTrigger<Msg, R = never>(h: { readonly a: PreviewCardTriggerElement<Msg>;}, options: { readonly element?: PreviewCardTriggerElement<Msg, R>; readonly props: PreviewCardTriggerProps;}): PreviewCardTriggerElement<Msg, R>Models
How a preview card lines up with its trigger along the side it shows on.
Signature
type PreviewCardAlign = typeof PreviewCardAlign.TypeWhy a preview card asks to close.
Signature
type PreviewCardDismissReason = typeof PreviewCardDismissReason.TypeWhat a PreviewCard emits.
Signature
type PreviewCardEvents = typeof PreviewCardEvents.TypeWhy a change of a preview card skips its transition, as data-instant
reports it: focus for an open by keyboard focus, dismiss for a close
by Escape.
Signature
type PreviewCardInstant = "focus" | "dismiss"Why a preview card asks to open.
Signature
type PreviewCardOpenReason = typeof PreviewCardOpenReason.TypeThe props of a PreviewCard.
Signature
type PreviewCardProps = typeof PreviewCardProps.TypeThe side of the trigger a preview card shows on.
Signature
type PreviewCardSide = typeof PreviewCardSide.TypeWhere a preview card is in its transitions: "starting" for the first
frame after it opens, "ending" while it closes, null otherwise.
Signature
type PreviewCardTransitionStatus = TransitionStatusAn <a> builder: what a preview card's trigger accepts as element, from
the caller's h.a, and what it returns.
Signature
type PreviewCardTriggerElement<Msg, R = never> = Html.Element<Msg, R, Html.EventsOf<"a">, HTMLAnchorElement, "a">The options of a PreviewCardTrigger.
Details
open: whether the card is open for this trigger.popupId: the card'sid.delay: milliseconds the mouse rests on the link, or keyboard focus stays on it, before the card opens, 600 by default.closeDelay: milliseconds after the pointer leaves the link and the card before the card closes, 300 by default.
Signature
interface PreviewCardTriggerProps { readonly closeDelay?: number; readonly delay?: number; readonly open: boolean; readonly popupId: string;}Schemas
PreviewCardAlign
unstableHow a preview card lines up with its trigger along the side it shows on.
Signature
declare const PreviewCardAlign: Literals<readonly ["start", "center", "end"]>PreviewCardDismissReason
unstableWhy a preview card asks to close: the pointer left, focus left, a press outside, Escape, or the trigger it is open for left the page.
Signature
declare const PreviewCardDismissReason: Literals<readonly ["trigger-hover", "trigger-focus", "outside-press", "escape-key", "trigger-removed"]>PreviewCardEvents
unstableRequestedOpen asks for the card to open, with the id of the trigger and
the reason. It is also emitted while the card is open, when another
trigger asks for it. RequestedClose asks for it to close, with the reason.
The consumer decides whether to change open. Settled reports that the
transitions and animations of an open or a close finished.
Signature
declare const PreviewCardEvents: TaggedEnum<{ RequestedClose: TaggedStruct<"RequestedClose", { readonly reason: Literals<readonly ["trigger-hover", "trigger-focus", "outside-press", "escape-key", "trigger-removed"]>; }>; RequestedOpen: TaggedStruct<"RequestedOpen", { readonly reason: Literals<readonly ["trigger-hover", "trigger-focus"]>; readonly triggerId: NullOr<String>; }>; Settled: TaggedStruct<"Settled", { readonly open: Boolean; }>;}>PreviewCardOpenReason
unstableWhy a preview card asks to open: the mouse resting on a trigger, or the keyboard focusing one.
Signature
declare const PreviewCardOpenReason: Literals<readonly ["trigger-hover", "trigger-focus"]>PreviewCardProps
unstableControls whether the preview card is open, which trigger it is open for, and where the popup appears.
Details
open: whether the card shows. Required: the consumer owns it.triggerId: theidof the trigger the card is open for, fromRequestedOpen. The popup is placed against it, and only it getsdata-popup-open. With none, the card remembers the trigger that last asked to open it.keepMounted: the portal stays in the page while the card is closed, hidden.holdExit: a closed card stays shown, in its ending state, while this istrue, for an exit animation that the consumer runs itself.id: the portal'sid, generated when left out. Set it to pair the card with aPreviewCardTriggerdrawn somewhere else, as itspopupId.side: the side of the trigger the popup shows on,"bottom"by default."inline-start"and"inline-end"follow the writing direction.align: how the popup lines up with the trigger along that side,"center"by default.sideOffset: the gap between the trigger and the popup, in pixels.alignOffset: pixels the popup moves along the side.collisionAvoidance,collisionBoundary,collisionPadding,sticky,arrowPadding,disableAnchorTracking: asPositioningdescribes them.positionMethod:"absolute", the default, which scrolls with the page, or"fixed", which is placed again on every scroll and doesn't make the page taller when the popup reaches past its end.anchor: the id of an element, or aRectin the viewport, to place the popup against instead of the trigger.topLayer:falsedraws the portal in place, out of the top layer, as the module describes.truewhen left out.
Signature
declare const PreviewCardProps: Struct<{ readonly align: optional<Literals<readonly ["start", "center", "end"]>>; readonly alignOffset: optional<Number>; readonly anchor: optional<Union<readonly [String, Struct<{ readonly height: Number; readonly width: Number; readonly x: Number; readonly y: Number; }>]>>; readonly arrowPadding: optional<Number>; readonly collisionAvoidance: optional<Struct<{ readonly align: optional<Literals<readonly ["flip", "shift", "none"]>>; readonly fallbackAxisSide: optional<Literals<readonly ["start", "end", "none"]>>; readonly side: optional<Literals<readonly ["flip", "shift", "none"]>>; }>>; readonly collisionBoundary: optional<Union<readonly [Literal<"clipping-ancestors">, Struct<{ readonly id: String; }>, Struct<{ readonly height: Number; readonly width: Number; readonly x: Number; readonly y: Number; }>]>>; readonly collisionPadding: optional<Union<readonly [Number, Struct<{ readonly bottom: optional<Number>; readonly left: optional<Number>; readonly right: optional<Number>; readonly top: optional<Number>; }>]>>; readonly disableAnchorTracking: optional<Boolean>; readonly holdExit: optional<Boolean>; readonly id: optional<String>; readonly keepMounted: optional<Boolean>; readonly open: Boolean; readonly positionMethod: optional<Literals<readonly ["absolute", "fixed"]>>; readonly side: optional<Literals<readonly ["top", "bottom", "left", "right", "inline-start", "inline-end"]>>; readonly sideOffset: optional<Number>; readonly sticky: optional<Boolean>; readonly topLayer: optional<Boolean>; readonly triggerId: optional<NullOr<String>>;}>PreviewCardSide
unstableThe side of the trigger a preview card shows on.
Signature
declare const PreviewCardSide: Literals<readonly ["top", "bottom", "left", "right", "inline-start", "inline-end"]>