UNPKG

3.36 kBTypeScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import classNames from "classnames";
18import * as React from "react";
19import { polyfill } from "react-lifecycles-compat";
20
21import { AbstractPureComponent2, Classes, DISPLAYNAME_PREFIX, Props } from "../../common";
22import { Icon, IconName } from "../icon/icon";
23import { normalizeKeyCombo } from "./hotkeyParser";
24
25const KeyIcons: { [key: string]: { icon: IconName; iconTitle: string } } = {
26 alt: { icon: "key-option", iconTitle: "Alt/Option key" },
27 cmd: { icon: "key-command", iconTitle: "Command key" },
28 ctrl: { icon: "key-control", iconTitle: "Control key" },
29 delete: { icon: "key-delete", iconTitle: "Delete key" },
30 down: { icon: "arrow-down", iconTitle: "Down key" },
31 enter: { icon: "key-enter", iconTitle: "Enter key" },
32 left: { icon: "arrow-left", iconTitle: "Left key" },
33 meta: { icon: "key-command", iconTitle: "Command key" },
34 right: { icon: "arrow-right", iconTitle: "Right key" },
35 shift: { icon: "key-shift", iconTitle: "Shift key" },
36 up: { icon: "arrow-up", iconTitle: "Up key" },
37};
38
39// eslint-disable-next-line deprecation/deprecation
40export type KeyComboTagProps = IKeyComboProps;
41/** @deprecated use KeyComboTagProps */
42export interface IKeyComboProps extends Props {
43 /** The key combo to display, such as `"cmd + s"`. */
44 combo: string;
45
46 /**
47 * Whether to render in a minimal style.
48 * If `false`, each key in the combo will be rendered inside a `<kbd>` tag.
49 * If `true`, only the icon or short name of a key will be rendered with no wrapper styles.
50 *
51 * @default false
52 */
53 minimal?: boolean;
54}
55
56@polyfill
57export class KeyCombo extends AbstractPureComponent2<KeyComboTagProps> {
58 public static displayName = `${DISPLAYNAME_PREFIX}.KeyCombo`;
59
60 public render() {
61 const { className, combo, minimal } = this.props;
62 const keys = normalizeKeyCombo(combo)
63 .map(key => (key.length === 1 ? key.toUpperCase() : key))
64 .map(minimal ? this.renderMinimalKey : this.renderKey);
65 return <span className={classNames(Classes.KEY_COMBO, className)}>{keys}</span>;
66 }
67
68 private renderKey = (key: string, index: number) => {
69 const icon = KeyIcons[key];
70 const reactKey = `key-${index}`;
71 return (
72 <kbd className={classNames(Classes.KEY, { [Classes.MODIFIER_KEY]: icon != null })} key={reactKey}>
73 {icon != null && <Icon icon={icon.icon} title={icon.iconTitle} />}
74 {key}
75 </kbd>
76 );
77 };
78
79 private renderMinimalKey = (key: string, index: number) => {
80 const icon = KeyIcons[key];
81 return icon == null ? key : <Icon icon={icon.icon} title={icon.iconTitle} key={`key-${index}`} />;
82 };
83}