Skip to content

Commit 0f965ed

Browse files
authored
feat: Add headers to key in resting / non hover state (#157)
Defines short names for behaviors for display on key headers
1 parent 4b75e17 commit 0f965ed

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/keyboard/Key.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PropsWithChildren } from "react";
2+
import BehaviorShortNames from "./behavior-short-names.json";
23

34
interface 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+
1237
export 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
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

0 commit comments

Comments
 (0)