File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { PropsWithChildren } from "react" ;
2+ import BehaviorShortNames from "./behavior-short-names.json" ;
23
34interface KeyProps {
45 selected ?: boolean ;
@@ -9,6 +10,30 @@ interface KeyProps {
910 onClick ?: ( ) => void ;
1011}
1112
13+ interface BehaviorShortName {
14+ short ?: string ;
15+ }
16+
17+ const MAX_HEADER_LENGTH = 9 ;
18+ const shortNames : Record < string , BehaviorShortName > = BehaviorShortNames ;
19+
20+ const shortenHeader = ( header : string | undefined ) => {
21+ if ( typeof header === "undefined" ) {
22+ return "" ;
23+ }
24+ // Empty string is a valid header for behaviors where we don't want to see a header, which is falsy
25+ // So we use an undefined check here
26+ if ( typeof shortNames [ header ] ?. short !== "undefined" ) {
27+ return shortNames [ header ] . short ;
28+ } else if ( header . length > MAX_HEADER_LENGTH ) {
29+ const words = header . split ( / [ \s , - ] + / ) ;
30+ const lettersPerWord = Math . trunc ( MAX_HEADER_LENGTH / words . length ) ;
31+ return words . map ( ( word ) => ( word . substring ( 0 , lettersPerWord ) ) ) . join ( "" ) ;
32+ } else {
33+ return header ;
34+ }
35+ }
36+
1237export const Key = ( {
1338 selected = false ,
1439 width,
@@ -31,7 +56,7 @@ export const Key = ({
3156 } }
3257 onClick = { onClick }
3358 >
34- < div className = { `absolute text-xs ${ selected ? "text-primary-content" : "z1text-base-content" } opacity-0 group-hover:opacity- 80 top-1 text-nowrap left-1/2 font-light -translate-x-1/2 text-center transition-opacity ` } > { header } </ div >
59+ < div className = { `absolute text-xs ${ selected ? "text-primary-content" : "z1text-base-content" } opacity-80 top-1 text-nowrap left-1/2 font-light -translate-x-1/2 text-center` } > { shortenHeader ( header ) } </ div >
3560 { children }
3661 </ button >
3762 ) ;
Original file line number Diff line number Diff line change 1+ {
2+ "Key Press" : {"short" : " " },
3+ "Bootloader" : {"short" : " Bootloadr" },
4+ "External Power" : {"short" : " Ext Pwr" },
5+ "Grave/Escape" : {"short" : " Grv/Esc" },
6+ "Key Repeat" : {"short" : " Key Rept" },
7+ "Key Toggle" : {"short" : " Key Togg" },
8+ "Momentary Layer" : {"short" : " MLayer" },
9+ "Output Selection" : {"short" : " OutputSel" },
10+ "Sticky Key" : {"short" : " Stcky Key" },
11+ "Studio Unlock" : {"short" : " Unlock" },
12+ "Toggle Layer" : {"short" : " Togg Layr" },
13+ "Transparent" : {"short" : " Transprnt" }
14+ }
You can’t perform that action at this time.
0 commit comments