|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + ComponentsOverrides, |
| 4 | + styled, |
| 5 | + SxProps, |
| 6 | + Typography, |
| 7 | +} from '@mui/material'; |
| 8 | +import clsx from 'clsx'; |
| 9 | + |
| 10 | +export const KeyboardShortcut = ({ |
| 11 | + className, |
| 12 | + keyboardShortcut, |
| 13 | + ...rest |
| 14 | +}: KeyboardShortcutProps) => { |
| 15 | + if (!keyboardShortcut) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + return ( |
| 20 | + <Root |
| 21 | + className={clsx(KeyboardShortcutClasses.root, className)} |
| 22 | + {...rest} |
| 23 | + > |
| 24 | + {keyboardShortcut |
| 25 | + .split('>') |
| 26 | + .map((sequence, sequenceIndex, sequences) => ( |
| 27 | + <React.Fragment key={sequence}> |
| 28 | + {sequence.split('+').map((key, keyIndex, keys) => ( |
| 29 | + <React.Fragment key={key}> |
| 30 | + <Typography |
| 31 | + component="kbd" |
| 32 | + className={KeyboardShortcutClasses.kbd} |
| 33 | + key={key} |
| 34 | + > |
| 35 | + {KeyMap[key] |
| 36 | + ? KeyMap[key] |
| 37 | + : key.toUpperCase()} |
| 38 | + </Typography> |
| 39 | + {keyIndex < keys.length - 1 ? ( |
| 40 | + <> </> |
| 41 | + ) : null} |
| 42 | + </React.Fragment> |
| 43 | + ))} |
| 44 | + {sequenceIndex < sequences.length - 1 ? ( |
| 45 | + <> </> |
| 46 | + ) : null} |
| 47 | + </React.Fragment> |
| 48 | + ))} |
| 49 | + </Root> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +const KeyMap = { |
| 54 | + meta: '⌘', |
| 55 | + ctrl: '⌃', |
| 56 | + shift: '⇧', |
| 57 | + alt: '⌥', |
| 58 | + enter: '⏎', |
| 59 | + escape: '⎋', |
| 60 | + backspace: '⌫', |
| 61 | + delete: '⌦', |
| 62 | + tab: '⇥', |
| 63 | + space: '␣', |
| 64 | + up: '↑', |
| 65 | + down: '↓', |
| 66 | + left: '←', |
| 67 | + right: '→', |
| 68 | + home: '↖', |
| 69 | + end: '↘', |
| 70 | + pageup: '⇞', |
| 71 | + pagedown: '⇟', |
| 72 | +}; |
| 73 | + |
| 74 | +export interface KeyboardShortcutProps |
| 75 | + extends React.DetailedHTMLProps< |
| 76 | + React.HTMLAttributes<HTMLDivElement>, |
| 77 | + HTMLDivElement |
| 78 | + > { |
| 79 | + keyboardShortcut?: string; |
| 80 | + sx?: SxProps; |
| 81 | +} |
| 82 | + |
| 83 | +const PREFIX = 'RaKeyboardShortcut'; |
| 84 | +const KeyboardShortcutClasses = { |
| 85 | + root: `${PREFIX}-root`, |
| 86 | + kbd: `${PREFIX}-kbd`, |
| 87 | +}; |
| 88 | + |
| 89 | +const Root = styled('div')(({ theme }) => ({ |
| 90 | + [`& .${KeyboardShortcutClasses.kbd}`]: { |
| 91 | + padding: '4px 5px', |
| 92 | + display: 'inline-block', |
| 93 | + whiteSpace: 'nowrap', |
| 94 | + margin: '0 1px', |
| 95 | + fontSize: '11px', |
| 96 | + lineHeight: '10px', |
| 97 | + color: theme.palette.text.primary, |
| 98 | + verticalAlign: 'middle', |
| 99 | + border: `1px solid ${theme.palette.divider}`, |
| 100 | + borderRadius: 6, |
| 101 | + boxShadow: `inset 0 -1px 0 ${theme.palette.divider}`, |
| 102 | + }, |
| 103 | +})); |
| 104 | + |
| 105 | +declare module '@mui/material/styles' { |
| 106 | + interface ComponentNameToClassKey { |
| 107 | + [PREFIX]: 'root' | 'kbd'; |
| 108 | + } |
| 109 | + |
| 110 | + interface ComponentsPropsList { |
| 111 | + [PREFIX]: Partial<KeyboardShortcutProps>; |
| 112 | + } |
| 113 | + |
| 114 | + interface Components { |
| 115 | + [PREFIX]?: { |
| 116 | + defaultProps?: ComponentsPropsList[typeof PREFIX]; |
| 117 | + styleOverrides?: ComponentsOverrides< |
| 118 | + Omit<Theme, 'components'> |
| 119 | + >[typeof PREFIX]; |
| 120 | + }; |
| 121 | + } |
| 122 | +} |
0 commit comments