2021-06-23 05:37:08 -04:00
|
|
|
import { useEffect, useRef } from 'react'
|
2021-05-13 06:24:12 -04:00
|
|
|
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
|
|
export default function SymbolPaletteItem({
|
|
|
|
focused,
|
|
|
|
handleSelect,
|
|
|
|
handleKeyDown,
|
|
|
|
symbol,
|
|
|
|
}) {
|
|
|
|
const buttonRef = useRef(null)
|
|
|
|
|
|
|
|
// call focus() on this item when appropriate
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
focused &&
|
|
|
|
buttonRef.current &&
|
|
|
|
document.activeElement?.closest('.symbol-palette-items')
|
|
|
|
) {
|
|
|
|
buttonRef.current.focus()
|
|
|
|
}
|
|
|
|
}, [focused])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<OverlayTrigger
|
|
|
|
placement="top"
|
|
|
|
trigger={['hover', 'focus']}
|
|
|
|
overlay={
|
|
|
|
<Tooltip id={`tooltip-symbol-${symbol.codepoint}`}>
|
|
|
|
<div className="symbol-palette-item-description">
|
|
|
|
{symbol.description}
|
|
|
|
</div>
|
2021-05-25 05:55:10 -04:00
|
|
|
<div className="symbol-palette-item-command">{symbol.command}</div>
|
2021-05-13 06:24:12 -04:00
|
|
|
{symbol.notes && (
|
|
|
|
<div className="symbol-palette-item-notes">{symbol.notes}</div>
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
key={symbol.codepoint}
|
|
|
|
className="symbol-palette-item"
|
|
|
|
onClick={() => handleSelect(symbol)}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
tabIndex={focused ? 0 : -1}
|
|
|
|
ref={buttonRef}
|
|
|
|
role="option"
|
2021-05-25 05:55:10 -04:00
|
|
|
aria-label={symbol.description}
|
|
|
|
aria-selected={focused ? 'true' : 'false'}
|
2021-05-13 06:24:12 -04:00
|
|
|
>
|
2021-05-25 05:55:10 -04:00
|
|
|
{symbol.character}
|
2021-05-13 06:24:12 -04:00
|
|
|
</button>
|
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
SymbolPaletteItem.propTypes = {
|
|
|
|
symbol: PropTypes.shape({
|
|
|
|
codepoint: PropTypes.string.isRequired,
|
|
|
|
description: PropTypes.string.isRequired,
|
|
|
|
command: PropTypes.string.isRequired,
|
|
|
|
character: PropTypes.string.isRequired,
|
|
|
|
notes: PropTypes.string,
|
|
|
|
}),
|
|
|
|
handleKeyDown: PropTypes.func.isRequired,
|
|
|
|
handleSelect: PropTypes.func.isRequired,
|
|
|
|
focused: PropTypes.bool,
|
|
|
|
}
|