@@ -2,15 +2,15 @@ import React, { useEffect, useMemo, useRef, useState } from 'react';
22import type { ComponentProps , PropsWithChildren } from 'react' ;
33import { render , Box , Text , useStdout } from 'ink' ;
44import type { DOMElement } from 'ink' ;
5+ import { useMap } from '@react-hookz/web' ;
56
67import {
78 MouseProvider ,
8- useElementPosition ,
99 useMousePosition ,
1010 useMouseAction ,
1111 useOnMouseHover ,
1212 useOnMouseClick ,
13- } from '../..' ;
13+ } from '../../src/ink-mouse.ts ' ;
1414
1515function App ( ) {
1616 return (
@@ -48,36 +48,14 @@ function useTerminalSize() {
4848}
4949
5050function View ( ) {
51- const [ count , setCount ] = useState ( 0 ) ;
51+ const map = useMap < 'button1' | 'button2' | 'button3' | 'listitem1' | 'listitem2' , number > ( )
5252 const size = useTerminalSize ( ) ;
5353 const mouse = useMousePosition ( ) ;
5454 const action = useMouseAction ( ) ;
5555
56- const ref = useRef < DOMElement > ( null ) ;
57- const position = useElementPosition ( ref ) ;
58- const label = useMemo ( ( ) => {
59- // is the mouse getting close to the button?
60- if ( ! position || ! mouse ) {
61- return ' 🤔 ' ;
62- }
63-
64- const h = Math . pow ( position . left - mouse . x , 2 ) ;
65- const v = Math . pow ( position . top - mouse . y , 2 ) ;
66-
67- const distance = Math . sqrt ( h + v ) ;
68-
69- if ( distance < 10 ) {
70- return ' 💀 ' ;
71- }
72-
73- if ( distance < 20 ) {
74- return ' 🤯 ' ;
75- }
76-
77- return ' 🤭 ' ;
78- } , [ count , position , mouse ] ) ;
7956
8057 return (
58+ < >
8159 < Box
8260 flexDirection = "column"
8361 paddingY = { 1 }
@@ -90,34 +68,95 @@ function View() {
9068 alignItems = "flex-start"
9169 justifyContent = "center"
9270 >
93- < Box ref = { ref } >
71+ < Box gap = { 1 } flexDirection = 'column' width = "20" >
72+ < Box gap = { 1 } >
9473 < Button
95- label = { label }
74+ label = 'button 1'
9675 onClick = { ( ) => {
97- setCount ( ( prev ) => prev + 1 ) ;
76+ map . set ( 'button1' , ( map . get ( 'button1' ) || 0 ) + 1 )
9877 } }
9978 />
79+ < Button
80+ label = 'button 2'
81+ onClick = { ( ) => {
82+ map . set ( 'button2' , ( map . get ( 'button2' ) || 0 ) + 1 )
83+ } }
84+ />
85+ </ Box >
86+ < Button onClick = { ( ) => {
87+ map . set ( 'listitem1' , ( map . get ( 'listitem1' ) || 0 ) + 1 )
88+
89+ } } >
90+ < Box flexDirection = 'row' flexGrow = { 1 } >
91+ < Box flexDirection = 'column' >
92+ < Text color = "yellowBright" bold > listitem 1</ Text >
93+ < Box flexDirection = 'row' gap = { 1 } >
94+ < Text color = "gray" > subtitle</ Text >
95+ < Text color = "gray" > things: 1</ Text >
96+ </ Box >
97+ </ Box >
98+ < Box justifyContent = 'flex-end' flexGrow = { 1 } >
99+ < Text color = "brown" > clicks: { map . get ( 'listitem1' ) || 0 } </ Text >
100+ </ Box >
101+ </ Box >
102+ </ Button >
103+ < Button onClick = { ( ) => {
104+ map . set ( 'listitem2' , ( map . get ( 'listitem2' ) || 0 ) + 1 )
105+ } } >
106+ < Box flexDirection = 'row' flexGrow = { 1 } >
107+ < Box flexDirection = 'column' >
108+ < Text color = "yellowBright" bold > listitem 2</ Text >
109+ < Box flexDirection = 'row' gap = { 1 } >
110+ < Text color = "gray" > subtitle</ Text >
111+ < Text color = "gray" > things: 1</ Text >
112+ </ Box >
113+ </ Box >
114+ < Box justifyContent = 'flex-end' flexGrow = { 1 } >
115+ < Text color = "brown" > clicks: { map . get ( 'listitem2' ) || 0 } </ Text >
116+ </ Box >
117+ </ Box >
118+ </ Button >
119+
100120 </ Box >
101121
102- < Box >
122+ < Box marginTop = { 3 } >
103123 < Text > Log:</ Text >
104124 < Box flexDirection = "column" >
105- < Text > Count: { count } </ Text >
125+ < Text > Button 1 Count: { map . get ( 'button1' ) } </ Text >
126+ < Text > Button 2 Count: { map . get ( 'button2' ) } </ Text >
127+ < Text > Button 3 Count: { map . get ( 'button3' ) } </ Text >
106128 < Text >
107129 Mouse: { mouse . x } ,{ mouse . y }
108130 </ Text >
109131 < Text > Action: { action } </ Text >
110132 </ Box >
111133 </ Box >
134+
135+
136+
112137 </ Box >
113- ) ;
138+ < Button
139+ position = "absolute"
140+ marginTop = { 1 }
141+ marginLeft = { 43 }
142+ onClick = { ( ) => {
143+ map . set ( 'button3' , ( map . get ( 'button3' ) || 0 ) + 1 )
144+ } }
145+ >
146+ < Text > Button 3</ Text >
147+ </ Button >
148+
149+ </ >
150+
151+ ) ;
114152}
115153
116154function Button ( {
117155 label,
118156 children,
119157 onClick,
120- } : PropsWithChildren < {
158+ ...props
159+ } : PropsWithChildren < ComponentProps < typeof Box > & {
121160 label ?: string ;
122161 onClick ?: ( ) => void ;
123162} > ) {
@@ -147,15 +186,16 @@ function Button({
147186 } , [ clicking , hovering ] ) ;
148187
149188 return (
150- < Box
151- gap = { 1 }
152- paddingX = { 1 }
153- ref = { ref }
154- borderStyle = { border }
155- >
156- { ! ! label && < Text > { label } </ Text > }
157- { children }
158- </ Box >
189+ < Box
190+ { ...props }
191+ paddingX = { 1 }
192+ gap = { 1 }
193+ ref = { ref }
194+ borderStyle = { border }
195+ >
196+ { ! ! label && < Text > { label } </ Text > }
197+ { children }
198+ </ Box >
159199 ) ;
160200}
161201
0 commit comments