1

I have buttons that use the react library react-ink with onClick canvas animation. That animation only works when button is clicked by mouse(cursor). In my view I use buttons that have a hotkey assigned to them and I need to trigger "click" animation even when I click hotkey on my keyboard.

I tried to use dispatchEvent to simulate click on proper height and width but its doesn't work

my example:

const el = wrapperRef.current.querySelectorAll(`[data-target="${value.id}"]`)[0]
const elPosition = el.getBoundingClientRect();
const evt = new MouseEvent("mousedown", {
              'view': window,
              'bubbles': true,
              'cancelable': true,
              'screenX': elPosition.left,
              'screenY': elPosition.top
            });
 el.dispatchEvent(evt);

// button
  <div
     data-target={props.values.id}
     role="button">
     <Ink hasTouch={false} />
  </div>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
peon123
  • 266
  • 1
  • 8

1 Answers1

1

did you tried useRef() on ink button component, if it had click prop, so you can fire it like below: const btn =useref();

<Ink ref={btn} ...

in keybord event listener exec this

btn.current.click()