Skip to content

Commit c284491

Browse files
committed
add search from kit
1 parent 7b8df19 commit c284491

19 files changed

Lines changed: 3409 additions & 3 deletions

‎apps/codingcatdev/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"eslint-config-prettier": "^8.6.0",
3535
"eslint-plugin-svelte3": "^4.0.0",
3636
"firebase-admin": "^11.5.0",
37+
"flexsearch": "^0.7.31",
3738
"marked": "^4.2.12",
3839
"postcss": "^8.4.21",
3940
"postcss-load-config": "^4.0.1",
@@ -54,6 +55,7 @@
5455
"@cloudinary/url-gen": "^1.9.1",
5556
"@codingcatdev/videojs-codingcatdev-youtube": "^0.0.2",
5657
"@floating-ui/dom": "^1.2.6",
58+
"esm-env": "^1.0.0",
5759
"firebase": "^9.19.1",
5860
"mdsvex": "^0.10.6",
5961
"prism-svelte": "^0.5.0",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export function focusable_children(node: HTMLElement) {
2+
const nodes = Array.from(
3+
node.querySelectorAll(
4+
'a[href], button, input, textarea, select, details, [tabindex]:not([tabindex="-1"])'
5+
)
6+
);
7+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
8+
// @ts-expect-error
9+
const index = nodes.indexOf(document.activeElement);
10+
11+
const update = (d: number) => {
12+
let i = index + d;
13+
i += nodes.length;
14+
i %= nodes.length;
15+
16+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
17+
// @ts-expect-error
18+
nodes[i].focus();
19+
};
20+
21+
return {
22+
next: (selector: string) => {
23+
const reordered: any[] = [...nodes.slice(index + 1), ...nodes.slice(0, index + 1)];
24+
25+
for (let i = 0; i < reordered.length; i += 1) {
26+
if (!selector || reordered[i].matches(selector)) {
27+
reordered[i].focus();
28+
return;
29+
}
30+
}
31+
},
32+
prev: (selector: string) => {
33+
const reordered: any[] = [...nodes.slice(index + 1), ...nodes.slice(0, index + 1)];
34+
35+
for (let i = reordered.length - 2; i >= 0; i -= 1) {
36+
if (!selector || reordered[i].matches(selector)) {
37+
reordered[i].focus();
38+
return;
39+
}
40+
}
41+
},
42+
update
43+
};
44+
}
45+
46+
export function trap(node: HTMLDivElement) {
47+
const handle_keydown = (e: { key: string; preventDefault: () => void; shiftKey: any; }) => {
48+
if (e.key === 'Tab') {
49+
e.preventDefault();
50+
51+
const group = focusable_children(node);
52+
// if (e.shiftKey) {
53+
// group.prev();
54+
// } else {
55+
// group.next();
56+
// }
57+
}
58+
};
59+
60+
node.addEventListener('keydown', handle_keydown);
61+
62+
return {
63+
destroy: () => {
64+
node.removeEventListener('keydown', handle_keydown);
65+
}
66+
};
67+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { focusable_children, trap } from './focus';
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<!-- @component
2+
Renders a search widget which when clicked (or the corresponding keyboard shortcut is pressed) opens a search overlay.
3+
-->
4+
<script>
5+
import { BROWSER } from 'esm-env';
6+
import { query, searching } from './stores.js';
7+
8+
export let q = '';
9+
</script>
10+
11+
<form class="search-container" action="/search">
12+
<input
13+
value={q}
14+
on:input={(e) => {
15+
$searching = true;
16+
$query = e.currentTarget.value;
17+
e.currentTarget.value = '';
18+
}}
19+
on:mousedown|preventDefault={() => ($searching = true)}
20+
on:touchend|preventDefault={() => ($searching = true)}
21+
type="search"
22+
name="q"
23+
placeholder="Search"
24+
aria-label="Search"
25+
spellcheck="false"
26+
/>
27+
28+
{#if BROWSER}
29+
<div class="shortcut">
30+
<kbd>{navigator.platform === 'MacIntel' ? '⌘' : 'Ctrl'}</kbd> <kbd>K</kbd>
31+
</div>
32+
{/if}
33+
</form>
34+
35+
<style>
36+
@keyframes fade-in {
37+
from {
38+
opacity: 0;
39+
}
40+
to {
41+
opacity: 1;
42+
}
43+
}
44+
45+
.search-container {
46+
position: relative;
47+
display: flex;
48+
align-items: center;
49+
width: 100%;
50+
height: 100%;
51+
}
52+
53+
input {
54+
padding: 0.5em 0.5em 0.4em 2em;
55+
border: 1px solid var(--sk-back-translucent);
56+
font-family: inherit;
57+
font-size: 1.4rem;
58+
/* text-align: center; */
59+
appearance: none;
60+
-webkit-appearance: none;
61+
width: 100%;
62+
height: 3.2rem;
63+
border-radius: var(--sk-border-radius);
64+
background: no-repeat 1rem 50% / 1em 1em url(../icons/search.svg);
65+
color: var(--sk-text-3);
66+
}
67+
68+
input:focus + .shortcut {
69+
display: none;
70+
}
71+
72+
input::placeholder {
73+
font-size: 1.2rem;
74+
text-transform: uppercase;
75+
}
76+
77+
.shortcut {
78+
color: var(--sk-text-3);
79+
position: absolute;
80+
top: calc(50% - 0.9rem);
81+
right: 0;
82+
width: 100%;
83+
text-align: right;
84+
pointer-events: none;
85+
font-size: 1.2rem;
86+
text-transform: uppercase;
87+
animation: fade-in 0.2s;
88+
}
89+
90+
kbd {
91+
display: none;
92+
background: var(--sk-back-2);
93+
border: 1px solid var(--sk-back-translucent);
94+
padding: 0.2rem 0.2rem 0rem 0.2rem;
95+
color: var(--sk-text-3);
96+
font-size: inherit;
97+
font-family: inherit;
98+
border-radius: 2px;
99+
}
100+
101+
@media (min-width: 800px) {
102+
.search-container {
103+
width: 11rem;
104+
}
105+
106+
.shortcut {
107+
padding: 0 1.6rem 0 0;
108+
}
109+
110+
input {
111+
border-radius: 1.6rem;
112+
}
113+
114+
input::placeholder {
115+
opacity: 0;
116+
}
117+
118+
/* we're using media query as an imperfect proxy for mobile/desktop */
119+
kbd {
120+
display: inline;
121+
}
122+
}
123+
124+
@media (min-width: 960px) {
125+
.search-container {
126+
width: 19rem;
127+
}
128+
129+
input::placeholder {
130+
opacity: 1;
131+
}
132+
}
133+
</style>

0 commit comments

Comments
 (0)