'use client' import { SizableText } from '@hanzo/gui' import { createContext, createElement, useContext, type MouseEvent, type ReactNode } from 'react' import { paint } from '../material/paint' import { reach } from '../material/reach' /** * A link out of one screen and into another, or out of this interface * altogether. * * The anchor keeps its `href`, so a middle click, a cmd-click and "open in new * tab" all still do what the reader meant. What is left is the part every * screen was writing for itself: a height a finger can hit. * * It is not painted, and it is not sized. A link gets no colour and no rung of * its own: a trail's first name reads at the same secondary as the name beside * it, a footer's legal links at the same faint as the sentence they sit in. So * the colour comes from where the link stands, and hovering it brightens to the * primary tone. The rung comes from the same place and for the same reason — a * link set inside a sentence at a rung of its own is two sizes in one sentence. * * Standing on its own that is the whole of it: a link in a nav row, a trail or * a footer is announced by where it is, and an underline on top of that is * decoration. Inside a sentence nothing announces it, and a phrase that differs * from the words around it in no way at all is not a link anybody can find — * hover is not available to a finger and colour alone is not available to * everyone. So the prose arm carries the underline and the standing arm does * not, which is the same rule stated once: the sentence gets what its position * cannot give it. * * Which of the two it is comes from the address rather than from a prop. A path * is a screen here and an absolute address is somebody else's site, and that is * a fact about the string; asked as a prop it would be a fact a call site could * get wrong, and the wrong answer either hands a foreign host to the router or * takes a reader out of the app to reach the next screen. * * It is a text primitive rendered as an anchor rather than a box around one, so * the tier named here is the height the anchor itself measures — a link whose * box is its type is about eighteen pixels, six short of the smallest square * WCAG 2.2 asks anything clickable to be. */ const { plain } = paint /** * Whether an address leaves this interface. * * A scheme in front of it is one answer: a path is a screen here and anything * with `https:` or `mailto:` ahead of it is somebody else's site. Two leading * slashes are the other, and it is the one worth stating out loud — `//host` * and `/\host` read as paths and resolve as absolute addresses on somebody * else's origin, so an address of that shape kept in the app is a foreign host * fetched with our referrer attached and no new tab. Where a screen draws a * link out of a proposal, a token record or anything else it did not write, * that shape is what arrives. * * It sits beside the rule about which clicks are ours because it is the same * question asked one step earlier — a link handed to the router that turns out * to be a foreign host is a host fed to the router, and a link kept from it * that was ours is a document fetched again. */ export const away = (href: string) => /^([a-z][a-z0-9+.-]*:|[/\\]{2})/i.test(href) /** * How an app moves between its own screens without fetching the document again. * * The router is the app's, and it is the only part of following a link that * this package cannot know. A build that states nothing gets ordinary anchors, * which always work: the page is fetched again, which is slower and correct. */ export type Go = (to: string) => void const Router = createContext(null) /** * The router a tree walks with. One at an app's root. * * A provider rather than a prop on every link: how this app moves between * screens is one fact stated once, and repeating it down fifteen screens is * fifteen chances for one of them to disagree about what a cmd-click means. */ export const Walk = ({ value, children }: { value: Go; children: ReactNode }) => createElement(Router.Provider, { value }, children) /** * The click handler for an address inside this app, or nothing where no router * was stated. * * The rule about which clicks are ours lives here rather than in each part * because a row in a table leaves for another screen the same way a nav link * does. Two copies of it are two answers waiting to disagree. * * `done` is for a link that closes what it sits in — a menu, a sheet — which * has to happen after the navigation rather than instead of it. */ export function useWalk(done?: () => void) { const go = useContext(Router) return (to: string) => go ? (e: MouseEvent) => { if (e.defaultPrevented || e.button !== 0) return if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return e.preventDefault() go(to) done?.() } : undefined } /** * Roughly what a line of running type measures, and half of what a target has to * gain over it to reach the inline rung. * * Roughly, because a link takes the size of the sentence it stands in and the * line box moves with it — five either side of a body line measures thirty at * the site's own ramp, and less where the sentence is smaller. The pad is a * fixed pair rather than a computed one for that reason: what it guarantees is * the floor, not a rung. */ const TYPE = 18 const PAD = (reach.dense - TYPE) / 2 export function Link({ href, label, prose, children, }: { href: string /** * What it is read out as, where the words alone will not do. A table whose * every row links "Open" gives a screen reader the same link a dozen times * with nothing to tell them apart. */ label?: string /** * Set inside a sentence rather than standing on its own. * * A link offered as a control is one atomic thing; a linked phrase is not, * and five words that cannot break widen the document rather than wrapping * inside it — on a phone at a large type scale that is the whole page * scrolling sideways. So the phrase keeps the sentence's own line breaking. * * The height is kept as well, as padding rather than as a box: the anchor * measures the same tier a finger is asked for everywhere else, the * difference sits either side of the type where it costs nothing, and the * sentence goes on breaking where it would have broken. */ prose?: boolean /** * The words. Optional because a link set inside a translated sentence is * handed in as an element with nothing in it — the catalogue holds the * phrase, and the runtime puts it between these tags when it draws the * sentence. */ children?: ReactNode }) { const walk = useWalk() const out = away(href) return ( ) : ( ) } // Inline-flex and started rather than stretched: a link is as wide as its // words, and one that fills the column is a target a page wide. In a // sentence it is plain inline instead, so the words break where the // sentence breaks. display={prose ? 'inline' : 'inline-flex'} alignSelf={prose ? undefined : 'flex-start'} alignItems={prose ? undefined : 'center'} // The height is padding either way, and it costs no layout either way. // // A link is as tall as its type, which is short of the smallest square // WCAG 2.2 asks of anything a finger is offered. A minimum height would // buy the target and spend the layout: the words sit at their own size // inside a taller box, and a column of links stands as much taller as // there are links in it. Padding is part of the box, so the five either // side are five pixels of target; a negative margin of the same size // gives the layout back and leaves the padding where a thumb lands. // // On an inline box the vertical pair costs nothing to begin with, so only // the horizontal one is taken back there — a phrase widened by ten inside // a sentence pushes the words around it, which is what a one-letter link // needs most: an `X` is fifteen pixels of glyph and was fifteen pixels of // target. paddingVertical={PAD} marginVertical={prose ? undefined : -PAD} paddingHorizontal={prose ? PAD : undefined} marginHorizontal={prose ? -PAD : undefined} // `unset` on an inherited property computes to the parent's value, which // is the whole ask: a link takes the size, the leading and the tone of the // sentence it stands in. fontSize="unset" lineHeight="unset" color="inherit" textDecorationLine={prose ? 'underline' : 'none'} hoverStyle={{ color: plain }} > {children} ) }