Skip to content

Commit

Permalink
feat: add helper function to generate custom event.
Browse files Browse the repository at this point in the history
  • Loading branch information
behoney committed Feb 21, 2023
1 parent acccb9a commit 38c90ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/Keyboard/Key/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ export default (props: KeyProps) => {
</group>
)
}

export const genCustomKeyEventFromCharacter = (
char: string,
): CustomEvent<ThreeEventType> => {
if (char.length !== 1) {
throw new Error(
`Invalid input for custom key event. Input should be single charater. Input is ${char}, though.`,
)
}

return new CustomEvent<ThreeEventType>('threekeyboardevent', {
detail: { keyId: char.toLowerCase() },
})
}
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
import App from './App'
import { genCustomKeyEventFromCharacter } from './components/Keyboard/Key'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(<App />)

document.addEventListener('keydown', (evt) => {
const event = genCustomKeyEventFromCharacter(evt.key)
document.dispatchEvent(event)
})

0 comments on commit 38c90ac

Please # to comment.