-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathKbd.tsx
139 lines (126 loc) · 3.1 KB
/
Kbd.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { filterDOMProps, isMac as getIsMac } from '@react-aria/utils';
import {
ForwardedRef,
forwardRef,
ForwardRefExoticComponent,
HTMLAttributes,
ReactNode,
Ref,
useMemo,
useSyncExternalStore,
} from 'react';
import { DOMProps } from '@react-types/shared';
import { useSlotProps } from '@keystar/ui/slots';
import { BaseStyleProps, css } from '@keystar/ui/style';
import { TextProps } from '@keystar/ui/types';
import { useTextStyles } from './text';
function noopSubscribe() {
return () => {};
}
function useIsMac() {
return useSyncExternalStore(noopSubscribe, getIsMac, () => false);
}
export type KbdProps = {
/** Keyboard shortcut text. */
children: ReactNode;
/**
* Prefix with OS-specific "alt" key.
* @MacOS `⎇`
* @Windows `Alt`
*/
alt?: boolean;
/**
* Prefix with OS-specific "meta" key.
* @MacOS `⌘`
* @Windows `Ctrl`
*/
meta?: boolean;
/**
* Prefix with OS-specific "shift" key.
* @MacOS `⇧`
* @Windows `Shift`
*/
shift?: boolean;
/**
* A slot to place the shortcut text.
* @default 'kbd'
*/
slot?: string;
} & Pick<TextProps, 'trim'> &
DOMProps &
BaseStyleProps;
/** Represents text that specifies a keyboard command. */
export const Kbd: ForwardRefExoticComponent<
KbdProps & { ref?: Ref<HTMLElement> }
> = forwardRef(function Kbd(
props: KbdProps,
forwardedRef: ForwardedRef<HTMLElement>
) {
props = useSlotProps(props, 'kbd');
let { alt, meta, shift, children, ...otherProps } = props;
const styleProps = useTextStyles({
casing: 'full-width',
color: 'neutral',
size: 'regular',
weight: 'regular',
...otherProps,
});
const isMac = useIsMac();
const modifiers = useMemo(() => {
const SYSTEM_KEYS = isMac
? {
alt: '⌥',
meta: '⌘',
shift: '⇧',
}
: {
alt: 'Alt',
meta: 'Ctrl',
shift: 'Shift',
// shift: '⇧', // maybe?
};
let keys = [
alt && SYSTEM_KEYS.alt,
shift && SYSTEM_KEYS.shift,
meta && SYSTEM_KEYS.meta,
].filter(Boolean) as string[];
return joinModifierKeys(keys, isMac);
}, [alt, meta, shift, isMac]);
return (
<kbd
{...filterDOMProps(otherProps)}
{...styleProps}
dir="ltr"
ref={forwardedRef}
>
{modifiers}
<Char>{children}</Char>
</kbd>
);
});
/**
* NOTE: The 'full-width' text-transform has limited support.
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform#browser_compatibility
* This hack ensures that single character shortcuts are visually aligned when
* stacked, like when end-aligned in menu items, without needing to use a
* monospace font.
*/
function Char(props: HTMLAttributes<HTMLSpanElement>) {
return (
<span
className={css({
display: 'inline-block',
minWidth: '1em',
textAlign: 'center',
})}
{...props}
/>
);
}
function joinModifierKeys(modifiers: string[], isMac: boolean) {
if (modifiers.length === 0) {
return '';
}
let delimiter = isMac ? '' : '+';
return modifiers.join(delimiter) + delimiter;
}