Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit feb19b9

Browse files
committed
Use kbd
1 parent ba3289b commit feb19b9

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

res/css/views/rooms/wysiwyg_composer/components/_FormattingButtons.pcss

+9
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ limitations under the License.
6363
color: $tertiary-content;
6464
}
6565
}
66+
67+
.mx_FormattingButtons_Tooltip_KeyboardShortcut {
68+
kbd {
69+
text-align: center;
70+
display: inline-block;
71+
text-transform: capitalize;
72+
font-family: Inter, sans-serif;
73+
}
74+
}

src/components/views/elements/AccessibleButton.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ type Props<T extends keyof JSX.IntrinsicElements> = DynamicHtmlElementProps<T> &
9292
/**
9393
* The tooltip to show on hover or focus.
9494
*/
95-
title?: string;
95+
title?: TooltipProps["label"];
9696
/**
9797
* The caption is a secondary text displayed under the `title` of the tooltip.
9898
* Only valid when used in conjunction with `title`.
9999
*/
100-
caption?: string;
100+
caption?: TooltipProps["caption"];
101101
/**
102102
* The placement of the tooltip.
103103
*/

src/components/views/rooms/wysiwyg_composer/components/FormattingButtons.tsx

+13-17
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ import { _t } from "../../../../../languageHandler";
3434
import AccessibleButton, { ButtonEvent } from "../../../elements/AccessibleButton";
3535
import { openLinkModal } from "./LinkModal";
3636
import { useComposerContext } from "../ComposerContext";
37-
import { IS_MAC, Key } from "../../../../../Keyboard";
38-
import { ALTERNATE_KEY_NAME } from "../../../../../accessibility/KeyboardShortcuts";
37+
import { KeyboardShortcut } from "../../../settings/KeyboardShortcut";
38+
import { KeyCombo } from "../../../../../KeyBindingsManager";
3939

4040
interface ButtonProps {
4141
icon: ReactNode;
4242
actionState: ActionState;
4343
onClick: MouseEventHandler<HTMLButtonElement>;
4444
label: string;
45-
shortcut?: string;
45+
keyCombo?: KeyCombo;
4646
}
4747

48-
function Button({ label, shortcut, onClick, actionState, icon }: ButtonProps): JSX.Element {
48+
function Button({ label, keyCombo, onClick, actionState, icon }: ButtonProps): JSX.Element {
4949
return (
5050
<AccessibleButton
5151
element="button"
@@ -57,22 +57,18 @@ function Button({ label, shortcut, onClick, actionState, icon }: ButtonProps): J
5757
mx_FormattingButtons_disabled: actionState === "disabled",
5858
})}
5959
title={actionState === "disabled" ? undefined : label}
60-
caption={shortcut}
60+
caption={
61+
keyCombo && (
62+
<KeyboardShortcut value={keyCombo} className="mx_FormattingButtons_Tooltip_KeyboardShortcut" />
63+
)
64+
}
6165
placement="top"
6266
>
6367
{icon}
6468
</AccessibleButton>
6569
);
6670
}
6771

68-
/**
69-
* Get the shortcut string for a given key.
70-
* @param key
71-
*/
72-
function getShortcutFromKey(key: string): string {
73-
return (IS_MAC ? "⌘" : _t(ALTERNATE_KEY_NAME[Key.CONTROL])) + "+" + key;
74-
}
75-
7672
interface FormattingButtonsProps {
7773
composer: FormattingFunctions;
7874
actionStates: AllActionStates;
@@ -86,21 +82,21 @@ export function FormattingButtons({ composer, actionStates }: FormattingButtonsP
8682
<Button
8783
actionState={actionStates.bold}
8884
label={_t("composer|format_bold")}
89-
shortcut={getShortcutFromKey("B")}
85+
keyCombo={{ ctrlOrCmdKey: true, key: "b" }}
9086
onClick={() => composer.bold()}
9187
icon={<BoldIcon className="mx_FormattingButtons_Icon" />}
9288
/>
9389
<Button
9490
actionState={actionStates.italic}
9591
label={_t("composer|format_italic")}
96-
shortcut={getShortcutFromKey("I")}
92+
keyCombo={{ ctrlOrCmdKey: true, key: "i" }}
9793
onClick={() => composer.italic()}
9894
icon={<ItalicIcon className="mx_FormattingButtons_Icon" />}
9995
/>
10096
<Button
10197
actionState={actionStates.underline}
10298
label={_t("composer|format_underline")}
103-
shortcut={getShortcutFromKey("U")}
99+
keyCombo={{ ctrlOrCmdKey: true, key: "u" }}
104100
onClick={() => composer.underline()}
105101
icon={<UnderlineIcon className="mx_FormattingButtons_Icon" />}
106102
/>
@@ -147,7 +143,7 @@ export function FormattingButtons({ composer, actionStates }: FormattingButtonsP
147143
<Button
148144
actionState={actionStates.inlineCode}
149145
label={_t("composer|format_inline_code")}
150-
shortcut={getShortcutFromKey("E")}
146+
keyCombo={{ ctrlOrCmdKey: true, key: "e" }}
151147
onClick={() => composer.inlineCode()}
152148
icon={<InlineCodeIcon className="mx_FormattingButtons_Icon" />}
153149
/>

src/components/views/settings/KeyboardShortcut.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ interface IKeyboardShortcutProps {
4343
className?: string;
4444
}
4545

46-
export const KeyboardShortcut: React.FC<IKeyboardShortcutProps> = ({ value, className = "mx_KeyboardShortcut" }) => {
46+
export const KeyboardShortcut: React.FC<IKeyboardShortcutProps> = ({
47+
value,
48+
className = "mx_KeyboardShortcut",
49+
// We are using this component as the caption in compound tooltip and we need to forward the props
50+
...props
51+
}) => {
4752
if (!value) return null;
4853

4954
const modifiersElement: JSX.Element[] = [];
@@ -62,7 +67,7 @@ export const KeyboardShortcut: React.FC<IKeyboardShortcutProps> = ({ value, clas
6267
}
6368

6469
return (
65-
<div className={className}>
70+
<div className={className} {...props}>
6671
{modifiersElement}
6772
<KeyboardKey name={value.key} last />
6873
</div>

0 commit comments

Comments
 (0)