Primitives / DOM

Mounts for layout

KeepInView({ selector }) keeps what is marked in view: whenever an element in its subtree newly matches selector (the row just selected, the node just inserted), it is scrolled into view the least amount that shows it. It sends no Message and redraws nothing, so a focused row stays focused. Where there is no layout (jsdom), it scrolls nothing.

Measure({ targets: { selected: '[aria-selected="true"]' } }) measures, for each named target, the first element in its subtree that matches, relative to its scroll box, and writes --fk-selected-x, -y, -w, -h (pixels) and --fk-selected-display (block, or none while nothing matches) on the element, and --fk-selected-moving: 1 for the measure that finds the mark on another element, 0 while it stays on one, so an overlay that scales its transition by it glides to a new mark but keeps to its element as the layout settles; measured('selected') names them. A child placed absolutely from them, such as an editor's selection outline, sits over the target and scrolls with it. Where an element is, is presentation: it sends no Message and keeps nothing in the Model. It measures again when the subtree changes, as the records arrive, and when it or a target changes size, as the size is observed, so the box moves in the frame the change is drawn in; and, at most once a frame, when the element scrolls, when the window resizes, when an image or a font inside loads, and on each frame of a transition or animation inside. Anything else that moves a target without resizing it (a stylesheet added) is seen at the next of these.

TrackBox({ name: 'mark', radius: true }) follows another element's box, for an overlay fixed over something outside it: a tour's cutout, a coach mark's ring. The element followed is the one whose id is the mounted element's data-target (TRACK_TARGET_ATTRIBUTE), and its box in viewport pixels is written on the mounted element as --fk-mark-x, -y, -w and -h; with radius, its top left corner as --fk-mark-radius, in pixels (cornerRadius is the arithmetic: a percentage of the width, at most half the shorter side). trackedBox('mark') names them. While nothing is followed they are removed, so a stylist's fallbacks apply.

ts
import type { Html, HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
import * as Mount from 'foldkit/mount'
import { Schema } from 'effect'
import { TRACK_TARGET_ATTRIBUTE, TrackBox } from 'foldkit-primitives/dom'

const Message = defineMessageUnion({
  /** The element marked is `found` on the page, or not. */
  Marked: { found: Schema.Boolean },
})

// A ring 4px outside the element whose id is `target`, wherever it scrolls.
const ring = (target: string, h: HtmlBuilder<typeof Message.Type>): Html =>
  h.div(
    [
      h.Attribute(TRACK_TARGET_ATTRIBUTE, target),
      h.OnMount(
        Mount.mapMessage(TrackBox({ name: 'mark', radius: true }), ({ found }) =>
          Message.Marked({ found }),
        ),
      ),
    ],
    [
      h.div(
        [
          h.Style({
            position: 'fixed',
            left: 'calc(var(--fk-mark-x) - 4px)',
            top: 'calc(var(--fk-mark-y) - 4px)',
            width: 'calc(var(--fk-mark-w) + 8px)',
            height: 'calc(var(--fk-mark-h) + 8px)',
            borderRadius: 'calc(var(--fk-mark-radius) + 4px)',
            outline: '2px solid',
            pointerEvents: 'none',
          }),
        ],
        [],
      ),
    ],
  )

It reads the box again on a scroll anywhere (in any scroller, heard on the way down), a window resize, the target's own resize, and any change to the document's elements, which is how a target drawn anew under the same id, or drawn late, is found. data-target is read again whenever it changes, so one element glides from target to target, which args, read once, could not do; anything else that moves the target (a stylesheet added) is seen at the next of these. Whether the target is on the page is the parent's to act on, so each change of target, or of whether it is found, sends TrackedTarget { target, found }, the first at insert; an empty data-target follows nothing and sends nothing. Since it sends a Message, a component may draw it by default, and a Scene test resolves it with what the parent maps it to. Unlike Measure, which places an overlay inside its own scroll box over an element in it, the target here can be anywhere on the page, and the box is the viewport's. foldkit-mixins-ui's Tour draws its cutout with it.

FluidHover({ name: 'hover', item: '[data-item]', axis: 'y', gapClick: true }) moves one highlight across a container's items: the item nearest the pointer is the one highlighted, so in a gap between two items the pointer still points at one, and the highlight moves from item to item rather than blinking off between them. axis says how nearness is read: y for a list (the row level with the pointer wins), x for a strip, xy for a grid. A disabled item (aria-disabled="true", data-disabled, :disabled) is never highlighted, an item reached by the keyboard (:focus-visible) is, and a touch highlights nothing. It writes --fk-hover-x, -y, -w, -h (pixels, relative to the scroll box like Measure's), --fk-hover-shown (1 or 0) and --fk-hover-moving (0 for the frame the highlight appears in, so a movement's duration scaled by it puts the highlight on its item rather than sliding it in); fluidHovered('hover') names them. With gapClick, a click in a gap presses the highlighted item: its descendant marked data-fluid-press (FLUID_PRESS_ATTRIBUTE), or the item; gapReach: 16 limits that to a click within 16px of it, as a grid of cards with wide gutters wants. Like Measure, it sends no Message.

follows names the item the highlight sits on while the pointer is not moving it: a menu marks the row its keys reach with data-active, so follows: '[data-active]' lights that row whenever the mark moves, and the highlight fades once no item is marked and neither the pointer nor focus is inside. from names the item a fresh highlight grows out of and returns to as it fades: from: '[aria-selected="true"]' makes a tab's hover leave the chosen tab and rejoin it. A marker on the chosen item itself is Measure's job, on the same element. While it runs, the container carries data-fluid-hover (FLUID_HOVER_ATTRIBUTE) set to the name, so a stylist draws the highlight, and drops each item's own hover, only where it really follows the pointer: before a page hydrates, the items keep theirs. Where there is no layout (jsdom), it starts and highlights nothing; an item with no layout box yet (a popup still opening) keeps the highlight's last place, and is measured again for three frames, rather than drawing it at zero.

The item the highlight is shown on carries data-fluid-lit (FLUID_LIT_ATTRIBUTE), so a style changes what sits beside it: a table clears the lines around its lit row.

Give only what a click reaches a highlight: lighting an item that does nothing promises a press with nowhere to land, so a card group joins only its pressable cards. Fluid Functionalism's list of when to leave it off holds here too: where a wrong click would hurt, where only some items are clickable, where items stand in a lot of empty space, and where rows move as the page scrolls.

ScrollBars({}) draws an overlay scrollbar, after Fluid Functionalism's: a thin thumb over the scroller's edge that shows while it scrolls or the pointer moves over it, and fades once it has been idle. It runs on the scroller and drives the bars drawn beside it, its parent's children marked data-scroll-bar="block" or "inline" (SCROLL_BAR_ATTRIBUTE), which a style places over the scroller's edge:

ts
import type { Html, HtmlBuilder } from 'foldkit/html'
import { SCROLL_BAR_ATTRIBUTE, ScrollBars } from 'foldkit-primitives/dom'

// The thumb is the bar's child; a style places the bar and the thumb.
const scrolling = <Message>(content: Html, h: HtmlBuilder<Message>): Html =>
  h.div(
    [h.Style({ position: 'relative' })],
    [
      h.div([h.OnMount(ScrollBars({})), h.Style({ overflowY: 'auto', maxHeight: '16rem' })], [content]),
      h.div([h.Attribute(SCROLL_BAR_ATTRIBUTE, 'block')], [h.div([], [])]),
    ],
  )

Each bar gets data-scrollable while its axis has more to scroll, data-shown while the scroller scrolls or the pointer moves over the scroller and its bars (until idle milliseconds pass with neither, and for as long as the pointer rests on a bar), and data-dragging while its thumb is dragged; and --fk-scroll-thumb-offset and --fk-scroll-thumb-size (scrollThumb), in pixels along the bar, for the thumb inside it to read. Pressing a bar off its thumb moves the thumb there and drags it. It sends no Message: where a box is scrolled is the browser's. When the scroller is another component's and holds a Mount of its own (an element holds one), put ScrollBars on an element around it and name the scroller: ScrollBars({ scroller: 'ul' }), looked for once, when it mounts; the bars are still the scroller's siblings.

Where the primary pointer is coarse or cannot hover ((hover: hover) and (pointer: fine) fails when it mounts) it does nothing, so touch keeps the browser's bar and its physics. Otherwise the scroller carries data-scroll-overlaid (SCROLL_OVERLAID_ATTRIBUTE) while it runs, so a style hides the native bar only then: before a page hydrates, the browser's bar stays. How the thumb rests, widens and fades is the style's; foldkit-mixins-ui's ScrollArea recipe draws Fluid's (4px, 6px on hover).

InputMask({ pattern }) masks a field against # (digit), A (letter), and * (either) placeholders with literal separators, rewrites the field, and emits Input { value, raw } with the masked and unmasked text. The caret stays after the same typed character, counted in the unmasked text, since the browser leaves it at an index of the text before the mask added its separators: typing straight through, in the middle, or pasting a number each leave it where a person expects. A Backspace (or Delete) that would remove only a separator takes the typed character on that side of the caret instead, and a composition is left alone until the input method commits it. The parent owns the value, like any controlled input. applyMask(value, pattern) is the pure rule.

KeyboardInset writes --fk-keyboard-inset on its element: how much of the layout viewport's bottom the on-screen keyboard covers, from the visual viewport's height and scroll, rounded down so a fraction of a pixel is no keyboard. A style pads or shrinks by var(--fk-keyboard-inset, 0px). While the page is pinch-zoomed in (scale above 1) the visual viewport is short because it shows less of the page, not because something covers it, so it writes 0px; with no visual viewport it writes nothing, and it removes the property when the element goes. It sends no Message, so a Scene test of a view that draws it by default cannot resolve it: attach it as an option.