-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
111 lines (88 loc) · 3.11 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Keyboard-Control</title>
<script type="module" src="./keyboard-control.js"></script>
<style>
form {
display: grid;
gap: 1ch;
grid-template-columns: 1fr auto;
align-items: center;
}
input {
padding: 8px 10px;
border: 1px solid #ddd;
border-radius: 3px;
}
button {
padding: 8px 10px;
}
output {
margin: 0;
padding: 0;
display: grid;
gap: 0.5ch;
list-style-type: none;
}
keyboard-control {
grid-column: span 2;
&:not([hidden]) {
display: block;
}
a {
padding: 5px;
border: 1px solid color-mix(in lch, var(--bg, #ddd) 60%, black);
border-radius: 3px;
background: var(--bg, #ddd);
transition: background ease 0.2s;
&[active] {
color: white;
--bg: color-mix(in lch, cyan 80%, black);
}
&:hover {
--bg: color-mix(in lch, cyan 85%, black);
}
}
}
</style>
</head>
<body>
<form action="#search">
<input type="search" name="search" value="a search term" autofocus>
<button type="submit">Search</button>
<keyboard-control items="a" loop hidden>
<output>
<a href="#1">a search result</a>
<a href="#2">another matched term</a>
<a href="#3">searches are great</a>
<a href="#4">search terminalogy</a>
</output>
</keyboard-control>
</form>
<script>
document.querySelector("form").addEventListener("submit", (event) => {
event.preventDefault();
let control = event.target.querySelector('keyboard-control')
control.removeAttribute('hidden');
control.focus();
});
document.querySelectorAll("a").forEach((result) => {
result.addEventListener("select", (event) => {
event.target.focus()
});
result.addEventListener("click", (event) => {
event.preventDefault();
alert(event.target.title || event.target.textContent);
});
result.addEventListener("focus", (event) => {
event.target.closest("keyboard-control").select(event.target);
});
});
document
.querySelector("keyboard-control")
.addEventListener("cancel", (event) => console.log(event));
</script>
</body>
</html>