You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeyboardShortcutsDialog.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Theme } from '@mui/material';
  2. import React from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  6. import Dialog from '../../../base/ui/components/web/Dialog';
  7. /**
  8. * The type of the React {@code Component} props of
  9. * {@link KeyboardShortcutsDialog}.
  10. */
  11. interface IProps {
  12. /**
  13. * A Map with keyboard keys as keys and translation keys as values.
  14. */
  15. shortcutDescriptions: Map<string, string>;
  16. }
  17. /**
  18. * Creates the styles for the component.
  19. *
  20. * @param {Object} theme - The current UI theme.
  21. *
  22. * @returns {Object}
  23. */
  24. const useStyles = makeStyles()((theme: Theme) => {
  25. return {
  26. list: {
  27. listStyleType: 'none',
  28. padding: 0,
  29. '& .shortcuts-list__item': {
  30. display: 'flex',
  31. justifyContent: 'space-between',
  32. marginBottom: theme.spacing(2),
  33. ...withPixelLineHeight(theme.typography.labelRegular),
  34. color: theme.palette.text01
  35. },
  36. '& .item-action': {
  37. backgroundColor: theme.palette.ui04,
  38. fontWeight: 'bold',
  39. padding: '1px 4px',
  40. borderRadius: '4px'
  41. }
  42. }
  43. };
  44. });
  45. const KeyboardShortcutsDialog = ({ shortcutDescriptions }: IProps) => {
  46. const { classes, cx } = useStyles();
  47. const { t } = useTranslation();
  48. // eslint-disable-next-line react/no-multi-comp
  49. const _renderShortcutsListItem = (keyboardKey: string, translationKey: string) => {
  50. let modifierKey = 'Alt';
  51. if (window.navigator?.platform) {
  52. if (window.navigator.platform.indexOf('Mac') !== -1) {
  53. modifierKey = '⌥';
  54. }
  55. }
  56. return (
  57. <li
  58. className = 'shortcuts-list__item'
  59. key = { keyboardKey }>
  60. <span
  61. aria-label = { t(translationKey) }
  62. className = 'shortcuts-list__description'>
  63. {t(translationKey)}
  64. </span>
  65. <span className = 'item-action'>
  66. {keyboardKey.startsWith(':')
  67. ? `${modifierKey} + ${keyboardKey.slice(1)}`
  68. : keyboardKey}
  69. </span>
  70. </li>
  71. );
  72. };
  73. return (
  74. <Dialog
  75. cancel = {{ hidden: true }}
  76. ok = {{ hidden: true }}
  77. titleKey = 'keyboardShortcuts.keyboardShortcuts'>
  78. <div
  79. id = 'keyboard-shortcuts'>
  80. <ul
  81. className = { cx('shortcuts-list', classes.list) }
  82. id = 'keyboard-shortcuts-list'>
  83. {Array.from(shortcutDescriptions)
  84. .map(description => _renderShortcutsListItem(...description))}
  85. </ul>
  86. </div>
  87. </Dialog>
  88. );
  89. };
  90. export default KeyboardShortcutsDialog;