Skip to content

JLink: never block the caller from adding an attribute #768

Description

@romain-pm

Part of #749. A developer must never be pushed off the component because it will not let them add an attribute.

Adopting the API on Jahia/jahia.com moved all 24 link sites, but only 5 could use <JLink>. The other 19 dropped to getLinkProps plus a hand-written <a {...anchor}> plus a state.navigable branch — three lines where the component would have been one. The site puts five analytics attributes on every link (data-element-url, -type, -text, -location, -name), and two of them are derived from the href and the label the component just computed and did not hand back.

  • attributes: an open map spread onto the <a>, taking either a record or a function of the resolved link (({ href, label, isCurrent }) => Record<string, string>). Static values are already expressible; the function form is the whole point. React's typings do not model data-* on a component's props, so this is the only route.
  • asChild, for a wrapper that is not a bare <a> — jahia.com's <CTA> renders a button-shaped thing. Next.js met the same need and called it passHref ("Forces Link to send the href property to its child"), still in the next/link 16.3.2 types.
  • Derive the omit from the component's own props (Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof JLinkProps>) rather than a hand-written list, so a prop added later cannot silently swallow an attribute. This is how Next composes both its components, and the image side got the same bug in a different place (JImage: never swallow an attribute the caller needs #762).

This is the single highest-value change on the link API: it is the difference between 5 and ~26 component call sites on one real site.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featA new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions