import React, { useMemo } from 'react' import withDefaults from '../utils/with-defaults' import useTheme from '../use-theme' import { HuiThemes } from '../themes' interface Props { command?: boolean shift?: boolean option?: boolean ctrl?: boolean small?: boolean className?: string } const defaultProps = { command: false, shift: false, option: false, ctrl: false, small: false, className: '' } type NativeAttrs = Omit, keyof Props> export type KeyboardProps = Props & typeof defaultProps & NativeAttrs type CustomLayout = { padding: number | string fontSize: string minWidth: string } const getLayout = (small: boolean, theme: HuiThemes): CustomLayout => { if (small) return { padding: 0, fontSize: '.75rem', minWidth: theme.layout.gap } return { padding: theme.layout.gapQuarter, fontSize: '0.875rem', minWidth: `calc(1.5 * ${theme.layout.gap})` } } const Keyboard: React.FC> = ({ command, shift, option, ctrl, small, children, className, ...props }) => { const theme = useTheme() const { padding, fontSize, minWidth } = useMemo( () => getLayout(small, theme), [small, theme] ) return ( {command && } {shift && } {option && } {ctrl && } {children && {children}} ) } const MemoKeyboard = React.memo(Keyboard) export default withDefaults(MemoKeyboard, defaultProps)