Repository navigation
Expand file tree
/
Copy pathbutton.tsx
More file actions
112 lines (96 loc) · 3.7 KB
/
Copy pathbutton.tsx
File metadata and controls
112 lines (96 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { cloneElement, isValidElement, memo, useRef } from 'react'
import { useRenderer } from '@opentui/react'
import type { ReactElement, ReactNode } from 'react'
import type { MousePointerStyle } from '@opentui/core'
interface ElementProps {
children?: ReactNode
[key: string]: unknown
}
/**
* Makes all text content within a React node tree non-selectable.
*
* This is important for interactive elements (buttons, clickable boxes) because
* text inside them should not be selectable when the user clicks - it creates
* a poor UX where text gets highlighted during interactions.
*
* Handles both `<text>` and `<span>` OpenTUI elements by adding `selectable={false}`.
*/
export function makeNodeTextNonSelectable(node: ReactNode): ReactNode {
if (node === null || node === undefined || typeof node === 'boolean') return node
if (typeof node === 'string' || typeof node === 'number') return node
if (Array.isArray(node)) {
return node.map((child, idx) => <React.Fragment key={idx}>{makeNodeTextNonSelectable(child)}</React.Fragment>)
}
if (!isValidElement(node)) return node
const el = node as ReactElement<ElementProps>
const type = el.type
// Ensure text and span nodes are not selectable
if (typeof type === 'string' && (type === 'text' || type === 'span')) {
const nextProps = { ...el.props, selectable: false }
const nextChildren = el.props.children ? makeNodeTextNonSelectable(el.props.children) : el.props.children
return cloneElement(el, nextProps, nextChildren)
}
// Recurse into other host elements and components' children
const nextChildren = el.props.children ? makeNodeTextNonSelectable(el.props.children) : el.props.children
return cloneElement(el, el.props, nextChildren)
}
interface ButtonProps {
onClick?: (e?: unknown) => void | Promise<unknown>
onMouseOver?: () => void
onMouseOut?: () => void
style?: Record<string, unknown>
children?: ReactNode
cursor?: MousePointerStyle
// pass-through for box host props
[key: string]: unknown
}
/**
* A button component with proper click detection and non-selectable text.
*
* Key behavior:
* - All nested `<text>`/`<span>` children are made `selectable={false}` via `makeNodeTextNonSelectable`
* - Uses mouseDown/mouseUp tracking so hover or stray mouse events don't trigger clicks
*
* When to use:
* - Use `Button` for standard button-like interactions (primary choice for clickable controls)
*/
export const Button = memo(function Button({ onClick, onMouseOver, onMouseOut, style, children, cursor, ...rest }: ButtonProps) {
const nonSelectableChildren = makeNodeTextNonSelectable(children)
const renderer = useRenderer()
const resolvedCursor = cursor ?? 'pointer'
// Track whether mouse down occurred on this element to implement proper click detection
// This prevents hover from triggering clicks in some terminals
const pressStartedRef = useRef(false)
const handlePressStart = () => {
pressStartedRef.current = true
}
const handlePressEnd = (e?: unknown) => {
// Only trigger click if mouse down happened on this element
if (pressStartedRef.current && onClick) {
onClick(e)
}
pressStartedRef.current = false
}
const handleMouseOverInternal = () => {
renderer.setMousePointer(resolvedCursor)
onMouseOver?.()
}
const handlePointerLeave = () => {
renderer.setMousePointer('default')
// Reset mouse down state when leaving the element
pressStartedRef.current = false
onMouseOut?.()
}
return (
<box
{...rest}
style={style}
onMouseDown={handlePressStart}
onMouseUp={handlePressEnd}
onMouseOver={handleMouseOverInternal}
onMouseOut={handlePointerLeave}
>
{nonSelectableChildren}
</box>
)
})