@gravity-ui/uikit
Version:
Gravity UI base styling and components
47 lines (46 loc) • 1.88 kB
JavaScript
'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { block } from "../utils/cn.js";
import { filterDOMProps } from "../utils/filterDOMProps.js";
import { defsByPlatform } from "./definitions.js";
import { parseKeyGroups } from "./parse.js";
import { isMac } from "./utils.js";
import "./Hotkey.css";
const b = block('hotkey');
const Spaces = {
BetweenGroups: String.fromCharCode(160), //
BetweenKeys: String.fromCharCode(8239), // Narrow No-Break Space
};
export const Hotkey = React.forwardRef(function Hotkey(props, ref) {
const { value, platform, view = 'light', qa, style, className, ...restProps } = props;
const groups = parseHotkeys(value, { platform });
const content = [];
let hasGroups = false;
groups.forEach((keys, groupIdx) => {
if (keys.length === 0)
return;
if (hasGroups) {
content.push(Spaces.BetweenGroups);
}
else {
hasGroups = true;
}
keys.forEach((key, keyIdx) => {
const isFirstKey = keyIdx === 0;
if (!isFirstKey) {
content.push(Spaces.BetweenKeys, _jsx("span", { className: b('plus'), children: "+" }, `${key}_${groupIdx}_${keyIdx}_plus`), Spaces.BetweenKeys);
}
content.push(_jsx("kbd", { children: key }, `${key}_${groupIdx}_${keyIdx}`));
});
});
if (content.length === 0)
return null;
return (_jsx("kbd", { ...filterDOMProps(restProps, { labelable: true }), ref: ref, style: style, "data-qa": qa, className: b({ view }, className), children: content }));
});
export function parseHotkeys(value, opts) {
const platform = opts.platform ?? (isMac() ? 'mac' : 'pc');
const defs = defsByPlatform[platform];
return parseKeyGroups(defs, value);
}
//# sourceMappingURL=Hotkey.js.map