Making drag-selection opt-in
OpenTUI's text renderables are selectable by default, so a click-to-navigate UI starts a drag-selection instead. Flipping the default without disabling selection.
- #opentui
- #patches
- #input
In a terminal UI where most labels are also click targets, pressing the mouse down on a label and moving a cell or two highlights text rather than navigating. Release, and the click has become a selection.
That is OpenTUI behaving as designed. The renderer turns a left mouse-down over
a selectable renderable into a drag-selection, and every text-bearing
renderable ships selectable: true, including <text>, <code>,
<markdown>, <input>, <textarea> and <ascii-font>. The base
Renderable is false, and they override it.
Some places still want selection. A log view or a console pane has to be
copyable. So the fix flips the default while keeping the prop meaningful. A
renderable given no selectable prop comes up non-selectable, and one given
the prop gets exactly what the caller asked for.
Where the default lives
selectable is resolved inside each renderable's own constructor:
_defaultOptions is a class field, so each instance gets its own copy during
construction. Nothing on the prototype can change it, and nothing outside the
constructor can get in front of that line. Subclassing is the only seam.
0.6.0 moved selectable itself onto Renderable.prototype, as an accessor
backed by the renderable's native scene hooks rather than a plain instance
field. That does not open a new seam. The line above still runs in the
constructor and still writes through that accessor, so redefining the
descriptor would only intercept a write whose value has already been decided.
Correcting the flag after super() remains the way in.
The dead end
The obvious one-liner disables the renderer's selection entirely:
Selection then stops everywhere, including on the <text selectable> that
wants it, and the prop silently means nothing. Anyone reading the component
later sees a selectable that the type system accepts and the runtime ignores,
which is worse than the original problem.
Subclassing the catalogue
@opentui/react keeps a catalogue mapping an element name to the renderable
class the reconciler constructs. getComponentCatalogue() reads it, extend()
writes it, and the reconciler builds every entry the same way:
So a uniform (ctx, options) subclass is safe for all of them. The patch wraps
each entry in a subclass that lets the real constructor run and then corrects
the flag:
Both halves of that condition matter. options?.selectable === undefined is
what makes this a default rather than a ban, so selectable={false} and
selectable both pass straight through. this.selectable === true leaves
alone everything that never opted in: <box>, span nodes, and anything whose
_defaultOptions already said false.
The text fields keep their own selection
The patch leaves <input> and <textarea> as upstream built them. The field
itself is a box, and the selection inside it belongs to its text buffer rather
than to the renderer's drag-selection, so flipping the flag there breaks
selecting inside a field without fixing anything. They are the default of
keep, and the option exists because the right set depends on which
renderables an app treats as text entry.
The module
Skipping a kept name rather than wrapping it keeps the entry the catalogue already holds, which matters when another patch has wrapped it first.
Installing it
The reconciler reads the catalogue as it constructs elements, so the call has to land before the first render:
After that, a component asks for selection where it wants it:
When it can go
Upstream would need a way to set the default from outside the constructor,
either a renderer option or a selectable that reads a context value instead
of a class field. Either one removes the need for the subclass wrapper.
Still required as of @opentui/core 0.6.6, and the module runs unchanged
there. Constructing each catalogue class directly on 0.6.6 and reading the flag
back gives true for text, code, input, textarea and ascii-font, and
false for box and scrollbox, exactly as on 0.5.x. Mounting the patched
catalogue leaves <text> at false, <text selectable> at true and
<text selectable={false}> at false.
Related entries
- Terminal-relative dimensions. Uses the same catalogue seam, for a prop upstream rejects outright.
- Clipping box borders. A prototype patch rather than a catalogue one, and the reason why. Resolved upstream in 0.6.0.
Files in this entry
Saved beside the document, and served as-is.
Related entries
Clipping box borders
OpenTUI's native box draw ignored the scissor stack, so a bordered box painted its border over everything around its scroll container. Fixed upstream in 0.6.0; kept for the detour it describes.
Terminal-relative dimensions
Giving a negative width or height the meaning "terminal size minus that many cells", re-resolved on every resize, since OpenTUI percentages resolve against the parent.
Reading the host terminal's colours
Subscribing to the terminal's own palette from React, including the DEC mode OpenTUI never enables and the cache the poll fallback has to clear.