@keybindy/react 1.1.7 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { jsx } from 'react/jsx-runtime';
1
+ import { jsx, jsxs } from 'react/jsx-runtime';
2
2
  import React from 'react';
3
3
 
4
4
  /**
@@ -15,6 +15,10 @@ import React from 'react';
15
15
  * <ShortcutLabel keys={['ctrl', 's']} />
16
16
  *
17
17
  * @example
18
+ * // With multiple bindings
19
+ * <ShortcutLabel keys={[['ctrl', 's'], ['meta', 's']]} />
20
+ *
21
+ * @example
18
22
  * // With custom renderKey
19
23
  * <ShortcutLabel
20
24
  * keys={['ctrl', 'alt', 'delete']}
@@ -27,7 +31,7 @@ import React from 'react';
27
31
  * />
28
32
  *
29
33
  * @param {ShortcutLabelProps} props - Props for the ShortcutLabel component
30
- * @param {string[]} props.keys - The list of keys to display.
34
+ * @param {string[] | string[][]} props.keys - The list of keys to display.
31
35
  * @param {Function} [props.renderKey] - Optional custom render function for full control over how each key appears.
32
36
  *
33
37
  * @returns {JSX.Element} Rendered shortcut label
@@ -50,11 +54,13 @@ const ShortcutLabel = ({ keys, renderKey, style, ...props }) => {
50
54
  return key.toUpperCase();
51
55
  }
52
56
  };
53
- const rendered = renderKey
54
- ? keys.map((key, i) => (jsx(React.Fragment, { children: renderKey(key, i, keys) }, i)))
55
- : defaultRenderKey
56
- ? [keys.map(key => defaultRenderKey(key)).join(' + ')]
57
- : [];
57
+ const renderSingleBinding = (binding) => {
58
+ if (renderKey) {
59
+ return binding.map((key, i) => (jsx(React.Fragment, { children: renderKey(key, i, binding) }, i)));
60
+ }
61
+ return binding.map(key => defaultRenderKey(key)).join(' + ');
62
+ };
63
+ const isNestedArray = Array.isArray(keys[0]);
58
64
  return (jsx("kbd", { style: {
59
65
  fontFamily: 'monospace',
60
66
  padding: '2.5px 5px',
@@ -63,7 +69,9 @@ const ShortcutLabel = ({ keys, renderKey, style, ...props }) => {
63
69
  borderRadius: '4px',
64
70
  userSelect: 'none',
65
71
  ...style,
66
- }, ...props, children: rendered }));
72
+ }, ...props, children: isNestedArray
73
+ ? keys.map((binding, index) => (jsxs(React.Fragment, { children: [renderSingleBinding(binding), index < keys.length - 1 && ' / '] }, index)))
74
+ : renderSingleBinding(keys) }));
67
75
  };
68
76
 
69
77
  export { ShortcutLabel };
package/dist/index.d.ts CHANGED
@@ -56,11 +56,12 @@ type KeybindyProps = {
56
56
  };
57
57
  declare const Keybindy: React.NamedExoticComponent<KeybindyProps>;
58
58
 
59
+ type AllowedKeys = Omit<Keys, 'Ctrl (Left)' | 'Ctrl (Right)' | 'Shift (Left)' | 'Shift (Right)' | 'Alt (Left)' | 'Alt (Right)' | 'Meta (Left)' | 'Meta (Right)'>;
59
60
  interface ShortcutLabelProps extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> {
60
61
  /**
61
- * Array of keys to display
62
+ * Array of keys to display. Can be a single binding `['Ctrl', 'S']` or multiple bindings `[['Ctrl', 'S'], ['Cmd', 'S']]`.
62
63
  */
63
- keys: Omit<Keys, 'Ctrl (Left)' | 'Ctrl (Right)' | 'Shift (Left)' | 'Shift (Right)' | 'Alt (Left)' | 'Alt (Right)' | 'Meta (Left)' | 'Meta (Right)'>[];
64
+ keys: AllowedKeys[] | AllowedKeys[][];
64
65
  /**
65
66
  * Custom render function for each key
66
67
  */
@@ -74,7 +75,7 @@ interface ShortcutLabelProps extends React.DetailedHTMLProps<React.HTMLAttribute
74
75
  */
75
76
  index: number,
76
77
  /**
77
- * All keys in the array
78
+ * All keys in the current binding
78
79
  */
79
80
  allKeys: string[]) => React.ReactNode;
80
81
  }
@@ -92,6 +93,10 @@ interface ShortcutLabelProps extends React.DetailedHTMLProps<React.HTMLAttribute
92
93
  * <ShortcutLabel keys={['ctrl', 's']} />
93
94
  *
94
95
  * @example
96
+ * // With multiple bindings
97
+ * <ShortcutLabel keys={[['ctrl', 's'], ['meta', 's']]} />
98
+ *
99
+ * @example
95
100
  * // With custom renderKey
96
101
  * <ShortcutLabel
97
102
  * keys={['ctrl', 'alt', 'delete']}
@@ -104,7 +109,7 @@ interface ShortcutLabelProps extends React.DetailedHTMLProps<React.HTMLAttribute
104
109
  * />
105
110
  *
106
111
  * @param {ShortcutLabelProps} props - Props for the ShortcutLabel component
107
- * @param {string[]} props.keys - The list of keys to display.
112
+ * @param {string[] | string[][]} props.keys - The list of keys to display.
108
113
  * @param {Function} [props.renderKey] - Optional custom render function for full control over how each key appears.
109
114
  *
110
115
  * @returns {JSX.Element} Rendered shortcut label
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keybindy/react",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Keybindy for React: Simple, scoped keyboard shortcuts that require little setup. designed to smoothly blend in with your React applications, allowing for robust keybinding functionality without the overhead.",
5
5
  "author": {
6
6
  "name": "PRASSamin",