@skolo-saka/school-search 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +133 -2
- package/THIRD_PARTY_NOTICES.md +25 -0
- package/dist/SchoolSearch.d.ts +45 -0
- package/dist/icons.d.ts +4 -0
- package/dist/index.cjs +291 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +262 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +60 -0
- package/dist/types.d.ts +51 -0
- package/dist/useSchoolSearch.d.ts +24 -0
- package/package.json +75 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Skolo Saka
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# school-search
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/skolo-saka/school-search/actions/workflows/ci.yml)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
`@skolo-saka/school-search` is an accessible React and TypeScript combobox for finding a school as you type and picking it. It was extracted from the "Which school will you help?" search on [Skolo Saka](https://skolosaka.co.za)'s homepage, and works in any React 18 or 19 app.
|
|
7
|
+
|
|
8
|
+
**[Try the demo](https://skolo-saka.github.io/school-search/)** (fictional schools only). Type **sunbird**: two schools share that name, and the one you pick is the one handed back.
|
|
9
|
+
|
|
10
|
+
You supply two functions:
|
|
11
|
+
|
|
12
|
+
- **`searchSchools(query, { signal })`** returns the schools that match the typed text, from any source: an API, a database or an array.
|
|
13
|
+
- **`onSelectSchool(school)`** receives the exact school the person picked.
|
|
14
|
+
|
|
15
|
+
The component handles the rest: a 250 ms search delay and two-letter minimum, names with a verified badge and a place line ("Primary school, Riverbend, Limpopo"), arrow keys, Enter and Escape, loading, no-results and error messages with "Try again", a clear button, screen-reader announcements and a mobile layout. A slow answer to an older query never replaces newer results.
|
|
16
|
+
|
|
17
|
+
## Status
|
|
18
|
+
|
|
19
|
+
**Version 0.1.0 is available as a [GitHub release](https://github.com/skolo-saka/school-search/releases/tag/v0.1.0).** It is not on npm yet. Publishing to npm and switching the Skolo Saka website over to this package are the next phase.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
From the GitHub release tarball:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install https://github.com/skolo-saka/school-search/releases/download/v0.1.0/skolo-saka-school-search-0.1.0.tgz
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
React and React DOM 18 or later are peer dependencies. The package ships ES module and CommonJS builds, TypeScript declarations and one stylesheet.
|
|
30
|
+
|
|
31
|
+
## Use it
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useState } from 'react';
|
|
35
|
+
import { SchoolSearch, schoolPlace, type School } from '@skolo-saka/school-search';
|
|
36
|
+
import '@skolo-saka/school-search/styles.css';
|
|
37
|
+
|
|
38
|
+
// Fictional schools. In a real app, searchSchools would call your API instead.
|
|
39
|
+
const SCHOOLS: School[] = [
|
|
40
|
+
{ id: 'sunbird-riverbend', name: 'Sunbird Primary School', level: 'primary', town: 'Riverbend', province: 'Limpopo', verified: true },
|
|
41
|
+
{ id: 'sunbird-kopjeskloof', name: 'Sunbird Primary School', level: 'primary', town: 'Kopjeskloof', province: 'Free State' },
|
|
42
|
+
{ id: 'baobab-ridge', name: 'Baobab Ridge High School', level: 'high', town: 'Riverbend', province: 'Limpopo', verified: true },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
async function searchSchools(query: string, { signal }: { signal: AbortSignal }): Promise<School[]> {
|
|
46
|
+
// With an API: const res = await fetch(`/api/schools?q=${encodeURIComponent(query)}`, { signal });
|
|
47
|
+
// if (!res.ok) throw new Error('Search failed'); // shows the error message and "Try again"
|
|
48
|
+
// return res.json();
|
|
49
|
+
signal.throwIfAborted();
|
|
50
|
+
const q = query.toLowerCase();
|
|
51
|
+
return SCHOOLS.filter(s => `${s.name} ${s.town} ${s.province}`.toLowerCase().includes(q));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default function App() {
|
|
55
|
+
const [school, setSchool] = useState<School | null>(null);
|
|
56
|
+
return <main>
|
|
57
|
+
<SchoolSearch searchSchools={searchSchools} onSelectSchool={setSchool} />
|
|
58
|
+
{school && <p>You picked {school.name} ({schoolPlace(school)}), id {school.id}.</p>}
|
|
59
|
+
</main>;
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`signal` is aborted when newer text replaces a search; pass it to `fetch` so stale requests are cancelled. Throw (or reject) to show the error message.
|
|
64
|
+
|
|
65
|
+
### Props
|
|
66
|
+
|
|
67
|
+
| Prop | Default | |
|
|
68
|
+
| --- | --- | --- |
|
|
69
|
+
| `searchSchools(query, { signal })` | required | Returns a promise of matching schools. Throw to show the error message. |
|
|
70
|
+
| `onSelectSchool(school)` | required | The exact school object picked, including any extra fields you returned. |
|
|
71
|
+
| `onSearchAll(query)` | none | "Find school" or Enter with several matches, or while loading or after a failure, e.g. to open a full results page. Without it, the button opens the list (or retries after a failure). One match is always picked directly. |
|
|
72
|
+
| `links.error`, `links.empty(query)` | none | `{ href, label }` links added to the error and no-results messages. |
|
|
73
|
+
| `renderLink` | plain `<a>` | Render those links with your router, e.g. Next.js `<Link>`. |
|
|
74
|
+
| `messages` | English | Replace any text, e.g. for translations. See `SchoolSearchMessages`. |
|
|
75
|
+
| `formatPlace(school)` | `schoolPlace` | The line under each name. |
|
|
76
|
+
| `limit` / `debounceMs` / `minChars` | 6 / 250 / 2 | Results shown, search delay, shortest search. |
|
|
77
|
+
| `focusHash` | `'#find-school'` | URL hash that focuses the box; `null` turns it off. |
|
|
78
|
+
| `className` | | Added to the outer `<form>`. |
|
|
79
|
+
|
|
80
|
+
A school is `{ id, name, level?, town?, municipality?, province?, verified? }`. A `level` of `primary`, `high` or `combined` shows as "Primary school", "High school" or "Combined school". For a custom layout, the `useSchoolSearch` hook gives you the search state without the markup.
|
|
81
|
+
|
|
82
|
+
### Styles
|
|
83
|
+
|
|
84
|
+
Import `@skolo-saka/school-search/styles.css` once. Classes are prefixed `skolo-search`. Colours follow these CSS custom properties when the page defines them: `--brand`, `--brand-deep`, `--sun`, `--sun-deep`, `--ink`, `--muted`, `--line`, `--glass-tint`, `--radius-control`, `--shadow-3` and `--z-raised`; otherwise a default green and yellow apply. Any `--skolo-search-*` property can also be set on `.skolo-search`.
|
|
85
|
+
|
|
86
|
+
### Next.js
|
|
87
|
+
|
|
88
|
+
The built files start with `'use client'`, so the App Router treats the component as a client component. Pass `renderLink={({ href, children }) => <Link href={href}>{children}</Link>}` to route the message links with `next/link`, and navigate in `onSelectSchool` with `useRouter()`.
|
|
89
|
+
|
|
90
|
+
## Develop
|
|
91
|
+
|
|
92
|
+
You need [Node.js 24](https://nodejs.org/) (24.11 or later; `.nvmrc` pins the major version) and npm.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
git clone https://github.com/skolo-saka/school-search.git
|
|
96
|
+
cd school-search
|
|
97
|
+
npm ci
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Run the demo at http://localhost:5173:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm run demo
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The demo imports `@skolo-saka/school-search` exactly as an app would (Vite points that name at `src/`) and uses fictional schools only. Try **sunbird** (same name, two towns), **zzz** (no results), "Every search fails" then **Try again**, and "Slow short queries" while typing **sun** then **bird** (the late answer for "sun" is logged but not shown).
|
|
107
|
+
|
|
108
|
+
Run the checks that CI runs:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
npm run typecheck
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
npm test
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
npm run build
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
npm run demo:build
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`npm run build` writes the package to `dist/`; `npm run demo:build` writes the static demo to `demo/dist/`. `npm pack` builds and writes `skolo-saka-school-search-<version>.tgz`, which another app can install with `npm install ./skolo-saka-school-search-0.1.0.tgz`.
|
|
127
|
+
|
|
128
|
+
## Contributing
|
|
129
|
+
|
|
130
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for the fork, branch, test and pull-request workflow, and the [good first issues](https://github.com/skolo-saka/school-search/labels/good%20first%20issue). Everyone taking part follows the [code of conduct](CODE_OF_CONDUCT.md). Report security problems privately as described in [SECURITY.md](SECURITY.md).
|
|
131
|
+
|
|
132
|
+
## Licence
|
|
133
|
+
|
|
134
|
+
[MIT](LICENSE) © 2026 Skolo Saka. The inlined Lucide icons are ISC licensed; see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
`@skolo-saka/school-search` is MIT licensed (see [LICENSE](LICENSE)). It includes the following third-party material.
|
|
4
|
+
|
|
5
|
+
## Lucide icons
|
|
6
|
+
|
|
7
|
+
`src/icons.tsx` inlines the SVG paths of the Search, X and BadgeCheck icons from [Lucide](https://lucide.dev), so the built package (`dist/`) contains them too.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
ISC License
|
|
11
|
+
|
|
12
|
+
Copyright (c) for portions of Lucide are held by Cole Bemis 2013-2022 as part of Feather (MIT). All other copyright (c) for Lucide are held by Lucide Contributors 2022.
|
|
13
|
+
|
|
14
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
15
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
16
|
+
copyright notice and this permission notice appear in all copies.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
19
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
20
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
21
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
22
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
23
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
24
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
25
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { FallbackLink, School, SchoolSearchMessages, SearchSchools } from './types';
|
|
3
|
+
export declare const defaultMessages: SchoolSearchMessages;
|
|
4
|
+
export interface SchoolSearchProps<S extends School = School> {
|
|
5
|
+
/** Supplies the schools matching the typed text. */
|
|
6
|
+
searchSchools: SearchSchools<S>;
|
|
7
|
+
/** Receives the school the person picked (click, Enter on a highlighted school, or "Find school" with one match). */
|
|
8
|
+
onSelectSchool: (school: S) => void;
|
|
9
|
+
/**
|
|
10
|
+
* "Find school" (or Enter with nothing highlighted) when there are several matches, or while a search
|
|
11
|
+
* is loading or has failed. On the website this opens the full directory search. Without it, the
|
|
12
|
+
* button opens the results list instead.
|
|
13
|
+
*/
|
|
14
|
+
onSearchAll?: (query: string) => void;
|
|
15
|
+
/** Links shown inside the messages. Leave out to show the messages without links. */
|
|
16
|
+
links?: {
|
|
17
|
+
/** Shown after a failed search, e.g. donate without choosing a school. */
|
|
18
|
+
error?: FallbackLink | null;
|
|
19
|
+
/** Shown when nothing matches, e.g. "tell us about the school". Gets the trimmed search text. */
|
|
20
|
+
empty?: ((query: string) => FallbackLink | null) | null;
|
|
21
|
+
};
|
|
22
|
+
/** Renders a link, e.g. with Next.js `<Link>`. Default: a plain `<a>`. */
|
|
23
|
+
renderLink?: (link: {
|
|
24
|
+
href: string;
|
|
25
|
+
children: ReactNode;
|
|
26
|
+
}) => ReactNode;
|
|
27
|
+
/** Replace any of the text, e.g. for translations. */
|
|
28
|
+
messages?: Partial<SchoolSearchMessages>;
|
|
29
|
+
/** The line under a school's name. Default: "High school, Town, Province". */
|
|
30
|
+
formatPlace?: (school: S) => string;
|
|
31
|
+
/** Most results to show. Default 6. */
|
|
32
|
+
limit?: number;
|
|
33
|
+
/** Search delay after the last keystroke, in ms. Default 250. */
|
|
34
|
+
debounceMs?: number;
|
|
35
|
+
/** Shortest text that searches. Default 2. */
|
|
36
|
+
minChars?: number;
|
|
37
|
+
/** When the page URL has this hash (default '#find-school'), the box takes focus. null turns it off. */
|
|
38
|
+
focusHash?: string | null;
|
|
39
|
+
className?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* "Which school will you help?" A combobox: type a school name or town, then pick a school with the
|
|
43
|
+
* mouse, or the arrow keys and Enter. The app decides where a picked school goes.
|
|
44
|
+
*/
|
|
45
|
+
export declare function SchoolSearch<S extends School = School>({ searchSchools, onSelectSchool, onSearchAll, links, renderLink, messages, formatPlace, limit, debounceMs, minChars, focusHash, className, }: SchoolSearchProps<S>): import("react").JSX.Element;
|
package/dist/icons.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SVGProps } from 'react';
|
|
2
|
+
export declare const SearchIcon: (props: SVGProps<SVGSVGElement>) => import("react").JSX.Element;
|
|
3
|
+
export declare const XIcon: (props: SVGProps<SVGSVGElement>) => import("react").JSX.Element;
|
|
4
|
+
export declare const BadgeCheckIcon: (props: SVGProps<SVGSVGElement>) => import("react").JSX.Element;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
SchoolSearch: () => SchoolSearch,
|
|
25
|
+
defaultMessages: () => defaultMessages,
|
|
26
|
+
schoolPlace: () => schoolPlace,
|
|
27
|
+
useSchoolSearch: () => useSchoolSearch
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/SchoolSearch.tsx
|
|
32
|
+
var import_react2 = require("react");
|
|
33
|
+
|
|
34
|
+
// src/useSchoolSearch.ts
|
|
35
|
+
var import_react = require("react");
|
|
36
|
+
var LEVEL = { primary: "Primary school", high: "High school", combined: "Combined school" };
|
|
37
|
+
var schoolPlace = (s) => [s.level ? LEVEL[s.level] ?? s.level : null, [s.town || s.municipality, s.province].filter(Boolean).join(", ")].filter(Boolean).join(", ");
|
|
38
|
+
function useSchoolSearch({ searchSchools, limit = 6, debounceMs = 250, minChars = 2 }) {
|
|
39
|
+
const [query, setQuery] = (0, import_react.useState)("");
|
|
40
|
+
const [state, setState] = (0, import_react.useState)("idle");
|
|
41
|
+
const [results, setResults] = (0, import_react.useState)([]);
|
|
42
|
+
const [attempt, setAttempt] = (0, import_react.useState)(0);
|
|
43
|
+
const request = (0, import_react.useRef)(0);
|
|
44
|
+
const search = (0, import_react.useRef)(searchSchools);
|
|
45
|
+
search.current = searchSchools;
|
|
46
|
+
(0, import_react.useEffect)(() => {
|
|
47
|
+
const term = query.trim();
|
|
48
|
+
if (term.length < minChars) {
|
|
49
|
+
request.current++;
|
|
50
|
+
setState("idle");
|
|
51
|
+
setResults([]);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const seq = ++request.current;
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
setState("loading");
|
|
57
|
+
const timer = setTimeout(async () => {
|
|
58
|
+
try {
|
|
59
|
+
const found = await search.current(term, { signal: controller.signal });
|
|
60
|
+
if (seq !== request.current) return;
|
|
61
|
+
const items = (found || []).slice(0, limit);
|
|
62
|
+
setResults(items);
|
|
63
|
+
setState(items.length ? "results" : "empty");
|
|
64
|
+
} catch {
|
|
65
|
+
if (seq === request.current) {
|
|
66
|
+
setResults([]);
|
|
67
|
+
setState("error");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}, debounceMs);
|
|
71
|
+
return () => {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
controller.abort();
|
|
74
|
+
};
|
|
75
|
+
}, [query, attempt, limit, debounceMs, minChars]);
|
|
76
|
+
return { query, setQuery, state, results, retry: () => setAttempt((n) => n + 1) };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/icons.tsx
|
|
80
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
81
|
+
var base = { xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round" };
|
|
82
|
+
var SearchIcon = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("svg", { ...base, ...props, children: [
|
|
83
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "m21 21-4.34-4.34" }),
|
|
84
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "11", cy: "11", r: "8" })
|
|
85
|
+
] });
|
|
86
|
+
var XIcon = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("svg", { ...base, ...props, children: [
|
|
87
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M18 6 6 18" }),
|
|
88
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "m6 6 12 12" })
|
|
89
|
+
] });
|
|
90
|
+
var BadgeCheckIcon = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("svg", { ...base, ...props, children: [
|
|
91
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z" }),
|
|
92
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "m9 12 2 2 4-4" })
|
|
93
|
+
] });
|
|
94
|
+
|
|
95
|
+
// src/SchoolSearch.tsx
|
|
96
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
97
|
+
var defaultMessages = {
|
|
98
|
+
label: "Search for a school",
|
|
99
|
+
placeholder: "School name or town",
|
|
100
|
+
submit: "Find school",
|
|
101
|
+
clear: "Clear search",
|
|
102
|
+
listLabel: "Schools",
|
|
103
|
+
verified: "Verified",
|
|
104
|
+
loading: "Searching schools\u2026",
|
|
105
|
+
retry: "Try again",
|
|
106
|
+
error: ({ retry, link }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
107
|
+
"We couldn\u2019t load schools. ",
|
|
108
|
+
retry,
|
|
109
|
+
link ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
110
|
+
", or ",
|
|
111
|
+
link
|
|
112
|
+
] }) : null,
|
|
113
|
+
"."
|
|
114
|
+
] }),
|
|
115
|
+
empty: ({ query, link }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
116
|
+
"No school matches \u201C",
|
|
117
|
+
query,
|
|
118
|
+
"\u201D. Try fewer words or the town name",
|
|
119
|
+
link ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
120
|
+
", or ",
|
|
121
|
+
link
|
|
122
|
+
] }) : null,
|
|
123
|
+
"."
|
|
124
|
+
] }),
|
|
125
|
+
announceResults: (count) => `${count} ${count === 1 ? "school" : "schools"} found. Choose one to see its page.`,
|
|
126
|
+
announceEmpty: "No schools found",
|
|
127
|
+
announceError: "We couldn\u2019t load schools"
|
|
128
|
+
};
|
|
129
|
+
var plainLink = ({ href, children }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("a", { href, children });
|
|
130
|
+
function SchoolSearch({
|
|
131
|
+
searchSchools,
|
|
132
|
+
onSelectSchool,
|
|
133
|
+
onSearchAll,
|
|
134
|
+
links,
|
|
135
|
+
renderLink = plainLink,
|
|
136
|
+
messages,
|
|
137
|
+
formatPlace = schoolPlace,
|
|
138
|
+
limit,
|
|
139
|
+
debounceMs,
|
|
140
|
+
minChars = 2,
|
|
141
|
+
focusHash = "#find-school",
|
|
142
|
+
className
|
|
143
|
+
}) {
|
|
144
|
+
const t = { ...defaultMessages, ...messages };
|
|
145
|
+
const id = (0, import_react2.useId)();
|
|
146
|
+
const listId = `${id}-schools`;
|
|
147
|
+
const { query, setQuery, state, results, retry } = useSchoolSearch({ searchSchools, limit, debounceMs, minChars });
|
|
148
|
+
const [active, setActive] = (0, import_react2.useState)(-1);
|
|
149
|
+
(0, import_react2.useEffect)(() => setActive(results.length ? 0 : -1), [results]);
|
|
150
|
+
const [open, setOpen] = (0, import_react2.useState)(false);
|
|
151
|
+
const input = (0, import_react2.useRef)(null);
|
|
152
|
+
(0, import_react2.useEffect)(() => {
|
|
153
|
+
if (!focusHash) return;
|
|
154
|
+
const focusFromHash = () => {
|
|
155
|
+
if (window.location.hash === focusHash) input.current?.focus({ preventScroll: true });
|
|
156
|
+
};
|
|
157
|
+
focusFromHash();
|
|
158
|
+
window.addEventListener("hashchange", focusFromHash);
|
|
159
|
+
return () => window.removeEventListener("hashchange", focusFromHash);
|
|
160
|
+
}, [focusHash]);
|
|
161
|
+
function pick(school) {
|
|
162
|
+
setOpen(false);
|
|
163
|
+
onSelectSchool(school);
|
|
164
|
+
}
|
|
165
|
+
function clear() {
|
|
166
|
+
setQuery("");
|
|
167
|
+
requestAnimationFrame(() => input.current?.focus());
|
|
168
|
+
}
|
|
169
|
+
function onKey(e) {
|
|
170
|
+
if (e.key === "Escape") {
|
|
171
|
+
setOpen(false);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (e.key === "Enter" && open && state === "results" && results[active]) {
|
|
175
|
+
e.preventDefault();
|
|
176
|
+
pick(results[active]);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (state !== "results") return;
|
|
180
|
+
if (e.key === "ArrowDown") {
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
setOpen(true);
|
|
183
|
+
setActive((i) => Math.min(results.length - 1, i + 1));
|
|
184
|
+
}
|
|
185
|
+
if (e.key === "ArrowUp") {
|
|
186
|
+
e.preventDefault();
|
|
187
|
+
setActive((i) => Math.max(0, i - 1));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function find(e) {
|
|
191
|
+
e.preventDefault();
|
|
192
|
+
const term = query.trim();
|
|
193
|
+
if (term.length < minChars) {
|
|
194
|
+
input.current?.focus();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (state === "results" && results.length === 1) {
|
|
198
|
+
pick(results[0]);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (state === "empty" || state === "error" && !onSearchAll) {
|
|
202
|
+
retry();
|
|
203
|
+
setOpen(true);
|
|
204
|
+
input.current?.focus();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (onSearchAll) {
|
|
208
|
+
setOpen(false);
|
|
209
|
+
onSearchAll(term);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
setOpen(true);
|
|
213
|
+
input.current?.focus();
|
|
214
|
+
}
|
|
215
|
+
const typed = query.trim();
|
|
216
|
+
const showList = open && state !== "idle";
|
|
217
|
+
const errorLink = links?.error ? renderLink({ href: links.error.href, children: links.error.label }) : null;
|
|
218
|
+
const emptyFallback = state === "empty" && links?.empty ? links.empty(typed) : null;
|
|
219
|
+
const emptyLink = emptyFallback ? renderLink({ href: emptyFallback.href, children: emptyFallback.label }) : null;
|
|
220
|
+
const retryButton = /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", className: "skolo-search__retry", onClick: () => {
|
|
221
|
+
retry();
|
|
222
|
+
input.current?.focus();
|
|
223
|
+
}, children: t.retry });
|
|
224
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("form", { className: `skolo-search${className ? ` ${className}` : ""}`, role: "search", onSubmit: find, children: [
|
|
225
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("label", { htmlFor: `${id}-input`, className: "skolo-search__sr-only", children: t.label }),
|
|
226
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: `skolo-search__box${showList ? " skolo-search__box--open" : ""}`, children: [
|
|
227
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SearchIcon, { "aria-hidden": "true", className: "skolo-search__icon" }),
|
|
228
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
229
|
+
"input",
|
|
230
|
+
{
|
|
231
|
+
ref: input,
|
|
232
|
+
id: `${id}-input`,
|
|
233
|
+
role: "combobox",
|
|
234
|
+
"aria-expanded": showList,
|
|
235
|
+
"aria-controls": listId,
|
|
236
|
+
"aria-autocomplete": "list",
|
|
237
|
+
"aria-activedescendant": showList && state === "results" && active >= 0 ? `${listId}-${active}` : void 0,
|
|
238
|
+
value: query,
|
|
239
|
+
placeholder: t.placeholder,
|
|
240
|
+
autoComplete: "off",
|
|
241
|
+
spellCheck: false,
|
|
242
|
+
onChange: (e) => {
|
|
243
|
+
setQuery(e.target.value);
|
|
244
|
+
setOpen(true);
|
|
245
|
+
},
|
|
246
|
+
onFocus: () => setOpen(true),
|
|
247
|
+
onKeyDown: onKey,
|
|
248
|
+
onBlur: () => setTimeout(() => setOpen(false), 150)
|
|
249
|
+
}
|
|
250
|
+
),
|
|
251
|
+
query && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", className: "skolo-search__clear", "aria-label": t.clear, onClick: clear, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(XIcon, { "aria-hidden": "true" }) }),
|
|
252
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "submit", className: "skolo-search__go", children: t.submit })
|
|
253
|
+
] }),
|
|
254
|
+
showList && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "skolo-search__panel", onMouseDown: (e) => e.preventDefault(), children: [
|
|
255
|
+
state === "loading" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "skolo-search__status", children: t.loading }),
|
|
256
|
+
state === "error" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "skolo-search__status", children: t.error({ retry: retryButton, link: errorLink }) }),
|
|
257
|
+
state === "empty" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "skolo-search__status", children: t.empty({ query: typed, link: emptyLink }) }),
|
|
258
|
+
state === "results" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("ul", { id: listId, role: "listbox", "aria-label": t.listLabel, children: results.map((school, i) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
259
|
+
"li",
|
|
260
|
+
{
|
|
261
|
+
id: `${listId}-${i}`,
|
|
262
|
+
role: "option",
|
|
263
|
+
"aria-selected": i === active,
|
|
264
|
+
className: i === active ? "skolo-search__option--active" : void 0,
|
|
265
|
+
onMouseEnter: () => setActive(i),
|
|
266
|
+
onMouseDown: (e) => {
|
|
267
|
+
e.preventDefault();
|
|
268
|
+
pick(school);
|
|
269
|
+
},
|
|
270
|
+
children: [
|
|
271
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("strong", { children: [
|
|
272
|
+
school.name,
|
|
273
|
+
school.verified && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(BadgeCheckIcon, { role: "img", "aria-label": t.verified })
|
|
274
|
+
] }),
|
|
275
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: formatPlace(school) })
|
|
276
|
+
]
|
|
277
|
+
},
|
|
278
|
+
school.id
|
|
279
|
+
)) })
|
|
280
|
+
] }),
|
|
281
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "skolo-search__sr-only", "aria-live": "polite", children: state === "results" ? t.announceResults(results.length) : state === "empty" ? t.announceEmpty : state === "error" ? t.announceError : "" })
|
|
282
|
+
] });
|
|
283
|
+
}
|
|
284
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
285
|
+
0 && (module.exports = {
|
|
286
|
+
SchoolSearch,
|
|
287
|
+
defaultMessages,
|
|
288
|
+
schoolPlace,
|
|
289
|
+
useSchoolSearch
|
|
290
|
+
});
|
|
291
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/SchoolSearch.tsx","../src/useSchoolSearch.ts","../src/icons.tsx"],"sourcesContent":["export { SchoolSearch, defaultMessages } from './SchoolSearch';\nexport type { SchoolSearchProps } from './SchoolSearch';\nexport { useSchoolSearch, schoolPlace } from './useSchoolSearch';\nexport type { UseSchoolSearchOptions } from './useSchoolSearch';\nexport type { School, SearchSchools, SearchState, FallbackLink, SchoolSearchMessages } from './types';\n","import { useEffect, useId, useRef, useState, type FormEvent, type KeyboardEvent, type ReactNode } from 'react';\nimport { schoolPlace, useSchoolSearch } from './useSchoolSearch';\nimport { BadgeCheckIcon, SearchIcon, XIcon } from './icons';\nimport type { FallbackLink, School, SchoolSearchMessages, SearchSchools } from './types';\n\nexport const defaultMessages: SchoolSearchMessages = {\n label: 'Search for a school',\n placeholder: 'School name or town',\n submit: 'Find school',\n clear: 'Clear search',\n listLabel: 'Schools',\n verified: 'Verified',\n loading: 'Searching schools…',\n retry: 'Try again',\n error: ({ retry, link }) => <>We couldn’t load schools. {retry}{link ? <>, or {link}</> : null}.</>,\n empty: ({ query, link }) => <>No school matches “{query}”. Try fewer words or the town name{link ? <>, or {link}</> : null}.</>,\n announceResults: count => `${count} ${count === 1 ? 'school' : 'schools'} found. Choose one to see its page.`,\n announceEmpty: 'No schools found',\n announceError: 'We couldn’t load schools',\n};\n\nexport interface SchoolSearchProps<S extends School = School> {\n /** Supplies the schools matching the typed text. */\n searchSchools: SearchSchools<S>;\n /** Receives the school the person picked (click, Enter on a highlighted school, or \"Find school\" with one match). */\n onSelectSchool: (school: S) => void;\n /**\n * \"Find school\" (or Enter with nothing highlighted) when there are several matches, or while a search\n * is loading or has failed. On the website this opens the full directory search. Without it, the\n * button opens the results list instead.\n */\n onSearchAll?: (query: string) => void;\n /** Links shown inside the messages. Leave out to show the messages without links. */\n links?: {\n /** Shown after a failed search, e.g. donate without choosing a school. */\n error?: FallbackLink | null;\n /** Shown when nothing matches, e.g. \"tell us about the school\". Gets the trimmed search text. */\n empty?: ((query: string) => FallbackLink | null) | null;\n };\n /** Renders a link, e.g. with Next.js `<Link>`. Default: a plain `<a>`. */\n renderLink?: (link: { href: string; children: ReactNode }) => ReactNode;\n /** Replace any of the text, e.g. for translations. */\n messages?: Partial<SchoolSearchMessages>;\n /** The line under a school's name. Default: \"High school, Town, Province\". */\n formatPlace?: (school: S) => string;\n /** Most results to show. Default 6. */\n limit?: number;\n /** Search delay after the last keystroke, in ms. Default 250. */\n debounceMs?: number;\n /** Shortest text that searches. Default 2. */\n minChars?: number;\n /** When the page URL has this hash (default '#find-school'), the box takes focus. null turns it off. */\n focusHash?: string | null;\n className?: string;\n}\n\nconst plainLink = ({ href, children }: { href: string; children: ReactNode }) => <a href={href}>{children}</a>;\n\n/**\n * \"Which school will you help?\" A combobox: type a school name or town, then pick a school with the\n * mouse, or the arrow keys and Enter. The app decides where a picked school goes.\n */\nexport function SchoolSearch<S extends School = School>({\n searchSchools, onSelectSchool, onSearchAll, links, renderLink = plainLink, messages, formatPlace = schoolPlace,\n limit, debounceMs, minChars = 2, focusHash = '#find-school', className,\n}: SchoolSearchProps<S>) {\n const t = { ...defaultMessages, ...messages };\n const id = useId();\n const listId = `${id}-schools`;\n const { query, setQuery, state, results, retry } = useSchoolSearch<S>({ searchSchools, limit, debounceMs, minChars });\n const [active, setActive] = useState(-1);\n useEffect(() => setActive(results.length ? 0 : -1), [results]);\n const [open, setOpen] = useState(false);\n const input = useRef<HTMLInputElement>(null);\n\n useEffect(() => {\n if (!focusHash) return;\n const focusFromHash = () => { if (window.location.hash === focusHash) input.current?.focus({ preventScroll: true }); };\n focusFromHash();\n window.addEventListener('hashchange', focusFromHash);\n return () => window.removeEventListener('hashchange', focusFromHash);\n }, [focusHash]);\n\n function pick(school: S) { setOpen(false); onSelectSchool(school); }\n function clear() { setQuery(''); requestAnimationFrame(() => input.current?.focus()); }\n\n function onKey(e: KeyboardEvent<HTMLInputElement>) {\n if (e.key === 'Escape') { setOpen(false); return; }\n // Enter with a school highlighted in the open list picks that school.\n if (e.key === 'Enter' && open && state === 'results' && results[active]) { e.preventDefault(); pick(results[active]); return; }\n if (state !== 'results') return;\n if (e.key === 'ArrowDown') { e.preventDefault(); setOpen(true); setActive(i => Math.min(results.length - 1, i + 1)); }\n if (e.key === 'ArrowUp') { e.preventDefault(); setActive(i => Math.max(0, i - 1)); }\n }\n\n // \"Find school\" (or Enter without a highlighted school): one match picks that school; several go to\n // onSearchAll. No matches searches again. While searching or after a failure, onSearchAll too.\n function find(e: FormEvent<HTMLFormElement>) {\n e.preventDefault();\n const term = query.trim();\n if (term.length < minChars) { input.current?.focus(); return; }\n if (state === 'results' && results.length === 1) { pick(results[0]); return; }\n if (state === 'empty' || (state === 'error' && !onSearchAll)) { retry(); setOpen(true); input.current?.focus(); return; }\n if (onSearchAll) { setOpen(false); onSearchAll(term); return; }\n setOpen(true); input.current?.focus();\n }\n\n const typed = query.trim();\n const showList = open && state !== 'idle';\n const errorLink = links?.error ? renderLink({ href: links.error.href, children: links.error.label }) : null;\n const emptyFallback = state === 'empty' && links?.empty ? links.empty(typed) : null;\n const emptyLink = emptyFallback ? renderLink({ href: emptyFallback.href, children: emptyFallback.label }) : null;\n const retryButton = <button type=\"button\" className=\"skolo-search__retry\" onClick={() => { retry(); input.current?.focus(); }}>{t.retry}</button>;\n\n return <form className={`skolo-search${className ? ` ${className}` : ''}`} role=\"search\" onSubmit={find}>\n <label htmlFor={`${id}-input`} className=\"skolo-search__sr-only\">{t.label}</label>\n <div className={`skolo-search__box${showList ? ' skolo-search__box--open' : ''}`}>\n <SearchIcon aria-hidden=\"true\" className=\"skolo-search__icon\" />\n <input ref={input} id={`${id}-input`} role=\"combobox\" aria-expanded={showList} aria-controls={listId} aria-autocomplete=\"list\"\n aria-activedescendant={showList && state === 'results' && active >= 0 ? `${listId}-${active}` : undefined}\n value={query} placeholder={t.placeholder} autoComplete=\"off\" spellCheck={false}\n onChange={e => { setQuery(e.target.value); setOpen(true); }} onFocus={() => setOpen(true)} onKeyDown={onKey}\n onBlur={() => setTimeout(() => setOpen(false), 150)} />\n {query && <button type=\"button\" className=\"skolo-search__clear\" aria-label={t.clear} onClick={clear}><XIcon aria-hidden=\"true\" /></button>}\n <button type=\"submit\" className=\"skolo-search__go\">{t.submit}</button>\n </div>\n {/* Pressing inside the panel keeps focus in the box, so the panel stays open for its links and buttons. */}\n {showList && <div className=\"skolo-search__panel\" onMouseDown={e => e.preventDefault()}>\n {state === 'loading' && <p className=\"skolo-search__status\">{t.loading}</p>}\n {state === 'error' && <p className=\"skolo-search__status\">{t.error({ retry: retryButton, link: errorLink })}</p>}\n {state === 'empty' && <p className=\"skolo-search__status\">{t.empty({ query: typed, link: emptyLink })}</p>}\n {state === 'results' && <ul id={listId} role=\"listbox\" aria-label={t.listLabel}>\n {results.map((school, i) => <li key={school.id} id={`${listId}-${i}`} role=\"option\" aria-selected={i === active}\n className={i === active ? 'skolo-search__option--active' : undefined}\n onMouseEnter={() => setActive(i)} onMouseDown={e => { e.preventDefault(); pick(school); }}>\n <strong>{school.name}{school.verified && <BadgeCheckIcon role=\"img\" aria-label={t.verified} />}</strong>\n <span>{formatPlace(school)}</span>\n </li>)}\n </ul>}\n </div>}\n <p className=\"skolo-search__sr-only\" aria-live=\"polite\">{state === 'results' ? t.announceResults(results.length) : state === 'empty' ? t.announceEmpty : state === 'error' ? t.announceError : ''}</p>\n </form>;\n}\n","import { useEffect, useRef, useState } from 'react';\nimport type { School, SearchSchools, SearchState } from './types';\n\nexport interface UseSchoolSearchOptions<S extends School> {\n searchSchools: SearchSchools<S>;\n /** Most results to show. Default 6. */\n limit?: number;\n /** Wait after the last keystroke before searching, in ms. Default 250 (the homepage's delay). */\n debounceMs?: number;\n /** Shortest text that searches. Default 2. */\n minChars?: number;\n}\n\nconst LEVEL: Record<string, string> = { primary: 'Primary school', high: 'High school', combined: 'Combined school' };\n\n/** \"High school, Polokwane, Limpopo\": the line under a school's name in the results. */\nexport const schoolPlace = (s: Pick<School, 'level' | 'town' | 'municipality' | 'province'>) =>\n [s.level ? LEVEL[s.level] ?? s.level : null, [s.town || s.municipality, s.province].filter(Boolean).join(', ')]\n .filter(Boolean)\n .join(', ');\n\n/**\n * Searches as the person types, keeping only the answer to the latest query: every new query (or\n * retry) bumps a sequence number and aborts the previous request, and a response is applied only if\n * its sequence number is still the current one. retry() runs the same search again.\n */\nexport function useSchoolSearch<S extends School>({ searchSchools, limit = 6, debounceMs = 250, minChars = 2 }: UseSchoolSearchOptions<S>) {\n const [query, setQuery] = useState('');\n const [state, setState] = useState<SearchState>('idle');\n const [results, setResults] = useState<S[]>([]);\n const [attempt, setAttempt] = useState(0);\n const request = useRef(0);\n // The latest search function, so an app passing a new function each render doesn't restart searches.\n const search = useRef(searchSchools);\n search.current = searchSchools;\n\n useEffect(() => {\n const term = query.trim();\n if (term.length < minChars) { request.current++; setState('idle'); setResults([]); return; }\n const seq = ++request.current;\n const controller = new AbortController();\n setState('loading');\n const timer = setTimeout(async () => {\n try {\n const found = await search.current(term, { signal: controller.signal });\n if (seq !== request.current) return;\n const items = (found || []).slice(0, limit);\n setResults(items);\n setState(items.length ? 'results' : 'empty');\n } catch {\n if (seq === request.current) { setResults([]); setState('error'); }\n }\n }, debounceMs);\n return () => { clearTimeout(timer); controller.abort(); };\n }, [query, attempt, limit, debounceMs, minChars]);\n\n return { query, setQuery, state, results, retry: () => setAttempt(n => n + 1) };\n}\n","import type { SVGProps } from 'react';\n\n// Search, X and BadgeCheck from Lucide (https://lucide.dev, ISC licence), inlined so the package has\n// no icon dependency. They match the lucide-react icons the website used. Licence: THIRD_PARTY_NOTICES.md.\nconst base = { xmlns: 'http://www.w3.org/2000/svg', width: 24, height: 24, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round' } as const;\n\nexport const SearchIcon = (props: SVGProps<SVGSVGElement>) =>\n <svg {...base} {...props}><path d=\"m21 21-4.34-4.34\" /><circle cx=\"11\" cy=\"11\" r=\"8\" /></svg>;\n\nexport const XIcon = (props: SVGProps<SVGSVGElement>) =>\n <svg {...base} {...props}><path d=\"M18 6 6 18\" /><path d=\"m6 6 12 12\" /></svg>;\n\nexport const BadgeCheckIcon = (props: SVGProps<SVGSVGElement>) =>\n <svg {...base} {...props}><path d=\"M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z\" /><path d=\"m9 12 2 2 4-4\" /></svg>;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAAuG;;;ACAvG,m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P;AAAA,OACF;AAAA,IACA,6CAAC,OAAE,WAAU,yBAAwB,aAAU,UAAU,oBAAU,YAAY,EAAE,gBAAgB,QAAQ,MAAM,IAAI,UAAU,UAAU,EAAE,gBAAgB,UAAU,UAAU,EAAE,gBAAgB,IAAG;AAAA,KACpM;AACF;","names":["import_react","import_jsx_runtime"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SchoolSearch, defaultMessages } from './SchoolSearch';
|
|
2
|
+
export type { SchoolSearchProps } from './SchoolSearch';
|
|
3
|
+
export { useSchoolSearch, schoolPlace } from './useSchoolSearch';
|
|
4
|
+
export type { UseSchoolSearchOptions } from './useSchoolSearch';
|
|
5
|
+
export type { School, SearchSchools, SearchState, FallbackLink, SchoolSearchMessages } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// src/SchoolSearch.tsx
|
|
4
|
+
import { useEffect as useEffect2, useId, useRef as useRef2, useState as useState2 } from "react";
|
|
5
|
+
|
|
6
|
+
// src/useSchoolSearch.ts
|
|
7
|
+
import { useEffect, useRef, useState } from "react";
|
|
8
|
+
var LEVEL = { primary: "Primary school", high: "High school", combined: "Combined school" };
|
|
9
|
+
var schoolPlace = (s) => [s.level ? LEVEL[s.level] ?? s.level : null, [s.town || s.municipality, s.province].filter(Boolean).join(", ")].filter(Boolean).join(", ");
|
|
10
|
+
function useSchoolSearch({ searchSchools, limit = 6, debounceMs = 250, minChars = 2 }) {
|
|
11
|
+
const [query, setQuery] = useState("");
|
|
12
|
+
const [state, setState] = useState("idle");
|
|
13
|
+
const [results, setResults] = useState([]);
|
|
14
|
+
const [attempt, setAttempt] = useState(0);
|
|
15
|
+
const request = useRef(0);
|
|
16
|
+
const search = useRef(searchSchools);
|
|
17
|
+
search.current = searchSchools;
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const term = query.trim();
|
|
20
|
+
if (term.length < minChars) {
|
|
21
|
+
request.current++;
|
|
22
|
+
setState("idle");
|
|
23
|
+
setResults([]);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const seq = ++request.current;
|
|
27
|
+
const controller = new AbortController();
|
|
28
|
+
setState("loading");
|
|
29
|
+
const timer = setTimeout(async () => {
|
|
30
|
+
try {
|
|
31
|
+
const found = await search.current(term, { signal: controller.signal });
|
|
32
|
+
if (seq !== request.current) return;
|
|
33
|
+
const items = (found || []).slice(0, limit);
|
|
34
|
+
setResults(items);
|
|
35
|
+
setState(items.length ? "results" : "empty");
|
|
36
|
+
} catch {
|
|
37
|
+
if (seq === request.current) {
|
|
38
|
+
setResults([]);
|
|
39
|
+
setState("error");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}, debounceMs);
|
|
43
|
+
return () => {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
controller.abort();
|
|
46
|
+
};
|
|
47
|
+
}, [query, attempt, limit, debounceMs, minChars]);
|
|
48
|
+
return { query, setQuery, state, results, retry: () => setAttempt((n) => n + 1) };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/icons.tsx
|
|
52
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
53
|
+
var base = { xmlns: "http://www.w3.org/2000/svg", width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round" };
|
|
54
|
+
var SearchIcon = (props) => /* @__PURE__ */ jsxs("svg", { ...base, ...props, children: [
|
|
55
|
+
/* @__PURE__ */ jsx("path", { d: "m21 21-4.34-4.34" }),
|
|
56
|
+
/* @__PURE__ */ jsx("circle", { cx: "11", cy: "11", r: "8" })
|
|
57
|
+
] });
|
|
58
|
+
var XIcon = (props) => /* @__PURE__ */ jsxs("svg", { ...base, ...props, children: [
|
|
59
|
+
/* @__PURE__ */ jsx("path", { d: "M18 6 6 18" }),
|
|
60
|
+
/* @__PURE__ */ jsx("path", { d: "m6 6 12 12" })
|
|
61
|
+
] });
|
|
62
|
+
var BadgeCheckIcon = (props) => /* @__PURE__ */ jsxs("svg", { ...base, ...props, children: [
|
|
63
|
+
/* @__PURE__ */ jsx("path", { d: "M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z" }),
|
|
64
|
+
/* @__PURE__ */ jsx("path", { d: "m9 12 2 2 4-4" })
|
|
65
|
+
] });
|
|
66
|
+
|
|
67
|
+
// src/SchoolSearch.tsx
|
|
68
|
+
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
69
|
+
var defaultMessages = {
|
|
70
|
+
label: "Search for a school",
|
|
71
|
+
placeholder: "School name or town",
|
|
72
|
+
submit: "Find school",
|
|
73
|
+
clear: "Clear search",
|
|
74
|
+
listLabel: "Schools",
|
|
75
|
+
verified: "Verified",
|
|
76
|
+
loading: "Searching schools\u2026",
|
|
77
|
+
retry: "Try again",
|
|
78
|
+
error: ({ retry, link }) => /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
79
|
+
"We couldn\u2019t load schools. ",
|
|
80
|
+
retry,
|
|
81
|
+
link ? /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
82
|
+
", or ",
|
|
83
|
+
link
|
|
84
|
+
] }) : null,
|
|
85
|
+
"."
|
|
86
|
+
] }),
|
|
87
|
+
empty: ({ query, link }) => /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
88
|
+
"No school matches \u201C",
|
|
89
|
+
query,
|
|
90
|
+
"\u201D. Try fewer words or the town name",
|
|
91
|
+
link ? /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
92
|
+
", or ",
|
|
93
|
+
link
|
|
94
|
+
] }) : null,
|
|
95
|
+
"."
|
|
96
|
+
] }),
|
|
97
|
+
announceResults: (count) => `${count} ${count === 1 ? "school" : "schools"} found. Choose one to see its page.`,
|
|
98
|
+
announceEmpty: "No schools found",
|
|
99
|
+
announceError: "We couldn\u2019t load schools"
|
|
100
|
+
};
|
|
101
|
+
var plainLink = ({ href, children }) => /* @__PURE__ */ jsx2("a", { href, children });
|
|
102
|
+
function SchoolSearch({
|
|
103
|
+
searchSchools,
|
|
104
|
+
onSelectSchool,
|
|
105
|
+
onSearchAll,
|
|
106
|
+
links,
|
|
107
|
+
renderLink = plainLink,
|
|
108
|
+
messages,
|
|
109
|
+
formatPlace = schoolPlace,
|
|
110
|
+
limit,
|
|
111
|
+
debounceMs,
|
|
112
|
+
minChars = 2,
|
|
113
|
+
focusHash = "#find-school",
|
|
114
|
+
className
|
|
115
|
+
}) {
|
|
116
|
+
const t = { ...defaultMessages, ...messages };
|
|
117
|
+
const id = useId();
|
|
118
|
+
const listId = `${id}-schools`;
|
|
119
|
+
const { query, setQuery, state, results, retry } = useSchoolSearch({ searchSchools, limit, debounceMs, minChars });
|
|
120
|
+
const [active, setActive] = useState2(-1);
|
|
121
|
+
useEffect2(() => setActive(results.length ? 0 : -1), [results]);
|
|
122
|
+
const [open, setOpen] = useState2(false);
|
|
123
|
+
const input = useRef2(null);
|
|
124
|
+
useEffect2(() => {
|
|
125
|
+
if (!focusHash) return;
|
|
126
|
+
const focusFromHash = () => {
|
|
127
|
+
if (window.location.hash === focusHash) input.current?.focus({ preventScroll: true });
|
|
128
|
+
};
|
|
129
|
+
focusFromHash();
|
|
130
|
+
window.addEventListener("hashchange", focusFromHash);
|
|
131
|
+
return () => window.removeEventListener("hashchange", focusFromHash);
|
|
132
|
+
}, [focusHash]);
|
|
133
|
+
function pick(school) {
|
|
134
|
+
setOpen(false);
|
|
135
|
+
onSelectSchool(school);
|
|
136
|
+
}
|
|
137
|
+
function clear() {
|
|
138
|
+
setQuery("");
|
|
139
|
+
requestAnimationFrame(() => input.current?.focus());
|
|
140
|
+
}
|
|
141
|
+
function onKey(e) {
|
|
142
|
+
if (e.key === "Escape") {
|
|
143
|
+
setOpen(false);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (e.key === "Enter" && open && state === "results" && results[active]) {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
pick(results[active]);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (state !== "results") return;
|
|
152
|
+
if (e.key === "ArrowDown") {
|
|
153
|
+
e.preventDefault();
|
|
154
|
+
setOpen(true);
|
|
155
|
+
setActive((i) => Math.min(results.length - 1, i + 1));
|
|
156
|
+
}
|
|
157
|
+
if (e.key === "ArrowUp") {
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
setActive((i) => Math.max(0, i - 1));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function find(e) {
|
|
163
|
+
e.preventDefault();
|
|
164
|
+
const term = query.trim();
|
|
165
|
+
if (term.length < minChars) {
|
|
166
|
+
input.current?.focus();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (state === "results" && results.length === 1) {
|
|
170
|
+
pick(results[0]);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (state === "empty" || state === "error" && !onSearchAll) {
|
|
174
|
+
retry();
|
|
175
|
+
setOpen(true);
|
|
176
|
+
input.current?.focus();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (onSearchAll) {
|
|
180
|
+
setOpen(false);
|
|
181
|
+
onSearchAll(term);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
setOpen(true);
|
|
185
|
+
input.current?.focus();
|
|
186
|
+
}
|
|
187
|
+
const typed = query.trim();
|
|
188
|
+
const showList = open && state !== "idle";
|
|
189
|
+
const errorLink = links?.error ? renderLink({ href: links.error.href, children: links.error.label }) : null;
|
|
190
|
+
const emptyFallback = state === "empty" && links?.empty ? links.empty(typed) : null;
|
|
191
|
+
const emptyLink = emptyFallback ? renderLink({ href: emptyFallback.href, children: emptyFallback.label }) : null;
|
|
192
|
+
const retryButton = /* @__PURE__ */ jsx2("button", { type: "button", className: "skolo-search__retry", onClick: () => {
|
|
193
|
+
retry();
|
|
194
|
+
input.current?.focus();
|
|
195
|
+
}, children: t.retry });
|
|
196
|
+
return /* @__PURE__ */ jsxs2("form", { className: `skolo-search${className ? ` ${className}` : ""}`, role: "search", onSubmit: find, children: [
|
|
197
|
+
/* @__PURE__ */ jsx2("label", { htmlFor: `${id}-input`, className: "skolo-search__sr-only", children: t.label }),
|
|
198
|
+
/* @__PURE__ */ jsxs2("div", { className: `skolo-search__box${showList ? " skolo-search__box--open" : ""}`, children: [
|
|
199
|
+
/* @__PURE__ */ jsx2(SearchIcon, { "aria-hidden": "true", className: "skolo-search__icon" }),
|
|
200
|
+
/* @__PURE__ */ jsx2(
|
|
201
|
+
"input",
|
|
202
|
+
{
|
|
203
|
+
ref: input,
|
|
204
|
+
id: `${id}-input`,
|
|
205
|
+
role: "combobox",
|
|
206
|
+
"aria-expanded": showList,
|
|
207
|
+
"aria-controls": listId,
|
|
208
|
+
"aria-autocomplete": "list",
|
|
209
|
+
"aria-activedescendant": showList && state === "results" && active >= 0 ? `${listId}-${active}` : void 0,
|
|
210
|
+
value: query,
|
|
211
|
+
placeholder: t.placeholder,
|
|
212
|
+
autoComplete: "off",
|
|
213
|
+
spellCheck: false,
|
|
214
|
+
onChange: (e) => {
|
|
215
|
+
setQuery(e.target.value);
|
|
216
|
+
setOpen(true);
|
|
217
|
+
},
|
|
218
|
+
onFocus: () => setOpen(true),
|
|
219
|
+
onKeyDown: onKey,
|
|
220
|
+
onBlur: () => setTimeout(() => setOpen(false), 150)
|
|
221
|
+
}
|
|
222
|
+
),
|
|
223
|
+
query && /* @__PURE__ */ jsx2("button", { type: "button", className: "skolo-search__clear", "aria-label": t.clear, onClick: clear, children: /* @__PURE__ */ jsx2(XIcon, { "aria-hidden": "true" }) }),
|
|
224
|
+
/* @__PURE__ */ jsx2("button", { type: "submit", className: "skolo-search__go", children: t.submit })
|
|
225
|
+
] }),
|
|
226
|
+
showList && /* @__PURE__ */ jsxs2("div", { className: "skolo-search__panel", onMouseDown: (e) => e.preventDefault(), children: [
|
|
227
|
+
state === "loading" && /* @__PURE__ */ jsx2("p", { className: "skolo-search__status", children: t.loading }),
|
|
228
|
+
state === "error" && /* @__PURE__ */ jsx2("p", { className: "skolo-search__status", children: t.error({ retry: retryButton, link: errorLink }) }),
|
|
229
|
+
state === "empty" && /* @__PURE__ */ jsx2("p", { className: "skolo-search__status", children: t.empty({ query: typed, link: emptyLink }) }),
|
|
230
|
+
state === "results" && /* @__PURE__ */ jsx2("ul", { id: listId, role: "listbox", "aria-label": t.listLabel, children: results.map((school, i) => /* @__PURE__ */ jsxs2(
|
|
231
|
+
"li",
|
|
232
|
+
{
|
|
233
|
+
id: `${listId}-${i}`,
|
|
234
|
+
role: "option",
|
|
235
|
+
"aria-selected": i === active,
|
|
236
|
+
className: i === active ? "skolo-search__option--active" : void 0,
|
|
237
|
+
onMouseEnter: () => setActive(i),
|
|
238
|
+
onMouseDown: (e) => {
|
|
239
|
+
e.preventDefault();
|
|
240
|
+
pick(school);
|
|
241
|
+
},
|
|
242
|
+
children: [
|
|
243
|
+
/* @__PURE__ */ jsxs2("strong", { children: [
|
|
244
|
+
school.name,
|
|
245
|
+
school.verified && /* @__PURE__ */ jsx2(BadgeCheckIcon, { role: "img", "aria-label": t.verified })
|
|
246
|
+
] }),
|
|
247
|
+
/* @__PURE__ */ jsx2("span", { children: formatPlace(school) })
|
|
248
|
+
]
|
|
249
|
+
},
|
|
250
|
+
school.id
|
|
251
|
+
)) })
|
|
252
|
+
] }),
|
|
253
|
+
/* @__PURE__ */ jsx2("p", { className: "skolo-search__sr-only", "aria-live": "polite", children: state === "results" ? t.announceResults(results.length) : state === "empty" ? t.announceEmpty : state === "error" ? t.announceError : "" })
|
|
254
|
+
] });
|
|
255
|
+
}
|
|
256
|
+
export {
|
|
257
|
+
SchoolSearch,
|
|
258
|
+
defaultMessages,
|
|
259
|
+
schoolPlace,
|
|
260
|
+
useSchoolSearch
|
|
261
|
+
};
|
|
262
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/SchoolSearch.tsx","../src/useSchoolSearch.ts","../src/icons.tsx"],"sourcesContent":["import { useEffect, useId, useRef, useState, type FormEvent, type KeyboardEvent, type ReactNode } from 'react';\nimport { schoolPlace, useSchoolSearch } from './useSchoolSearch';\nimport { BadgeCheckIcon, SearchIcon, XIcon } from './icons';\nimport type { FallbackLink, School, SchoolSearchMessages, SearchSchools } from './types';\n\nexport const defaultMessages: SchoolSearchMessages = {\n label: 'Search for a school',\n placeholder: 'School name or town',\n submit: 'Find school',\n clear: 'Clear search',\n listLabel: 'Schools',\n verified: 'Verified',\n loading: 'Searching schools…',\n retry: 'Try again',\n error: ({ retry, link }) => <>We couldn’t load schools. {retry}{link ? <>, or {link}</> : null}.</>,\n empty: ({ query, link }) => <>No school matches “{query}”. Try fewer words or the town name{link ? <>, or {link}</> : null}.</>,\n announceResults: count => `${count} ${count === 1 ? 'school' : 'schools'} found. Choose one to see its page.`,\n announceEmpty: 'No schools found',\n announceError: 'We couldn’t load schools',\n};\n\nexport interface SchoolSearchProps<S extends School = School> {\n /** Supplies the schools matching the typed text. */\n searchSchools: SearchSchools<S>;\n /** Receives the school the person picked (click, Enter on a highlighted school, or \"Find school\" with one match). */\n onSelectSchool: (school: S) => void;\n /**\n * \"Find school\" (or Enter with nothing highlighted) when there are several matches, or while a search\n * is loading or has failed. On the website this opens the full directory search. Without it, the\n * button opens the results list instead.\n */\n onSearchAll?: (query: string) => void;\n /** Links shown inside the messages. Leave out to show the messages without links. */\n links?: {\n /** Shown after a failed search, e.g. donate without choosing a school. */\n error?: FallbackLink | null;\n /** Shown when nothing matches, e.g. \"tell us about the school\". Gets the trimmed search text. */\n empty?: ((query: string) => FallbackLink | null) | null;\n };\n /** Renders a link, e.g. with Next.js `<Link>`. Default: a plain `<a>`. */\n renderLink?: (link: { href: string; children: ReactNode }) => ReactNode;\n /** Replace any of the text, e.g. for translations. */\n messages?: Partial<SchoolSearchMessages>;\n /** The line under a school's name. Default: \"High school, Town, Province\". */\n formatPlace?: (school: S) => string;\n /** Most results to show. Default 6. */\n limit?: number;\n /** Search delay after the last keystroke, in ms. Default 250. */\n debounceMs?: number;\n /** Shortest text that searches. Default 2. */\n minChars?: number;\n /** When the page URL has this hash (default '#find-school'), the box takes focus. null turns it off. */\n focusHash?: string | null;\n className?: string;\n}\n\nconst plainLink = ({ href, children }: { href: string; children: ReactNode }) => <a href={href}>{children}</a>;\n\n/**\n * \"Which school will you help?\" A combobox: type a school name or town, then pick a school with the\n * mouse, or the arrow keys and Enter. The app decides where a picked school goes.\n */\nexport function SchoolSearch<S extends School = School>({\n searchSchools, onSelectSchool, onSearchAll, links, renderLink = plainLink, messages, formatPlace = schoolPlace,\n limit, debounceMs, minChars = 2, focusHash = '#find-school', className,\n}: SchoolSearchProps<S>) {\n const t = { ...defaultMessages, ...messages };\n const id = useId();\n const listId = `${id}-schools`;\n const { query, setQuery, state, results, retry } = useSchoolSearch<S>({ searchSchools, limit, debounceMs, minChars });\n const [active, setActive] = useState(-1);\n useEffect(() => setActive(results.length ? 0 : -1), [results]);\n const [open, setOpen] = useState(false);\n const input = useRef<HTMLInputElement>(null);\n\n useEffect(() => {\n if (!focusHash) return;\n const focusFromHash = () => { if (window.location.hash === focusHash) input.current?.focus({ preventScroll: true }); };\n focusFromHash();\n window.addEventListener('hashchange', focusFromHash);\n return () => window.removeEventListener('hashchange', focusFromHash);\n }, [focusHash]);\n\n function pick(school: S) { setOpen(false); onSelectSchool(school); }\n function clear() { setQuery(''); requestAnimationFrame(() => input.current?.focus()); }\n\n function onKey(e: KeyboardEvent<HTMLInputElement>) {\n if (e.key === 'Escape') { setOpen(false); return; }\n // Enter with a school highlighted in the open list picks that school.\n if (e.key === 'Enter' && open && state === 'results' && results[active]) { e.preventDefault(); pick(results[active]); return; }\n if (state !== 'results') return;\n if (e.key === 'ArrowDown') { e.preventDefault(); setOpen(true); setActive(i => Math.min(results.length - 1, i + 1)); }\n if (e.key === 'ArrowUp') { e.preventDefault(); setActive(i => Math.max(0, i - 1)); }\n }\n\n // \"Find school\" (or Enter without a highlighted school): one match picks that school; several go to\n // onSearchAll. No matches searches again. While searching or after a failure, onSearchAll too.\n function find(e: FormEvent<HTMLFormElement>) {\n e.preventDefault();\n const term = query.trim();\n if (term.length < minChars) { input.current?.focus(); return; }\n if (state === 'results' && results.length === 1) { pick(results[0]); return; }\n if (state === 'empty' || (state === 'error' && !onSearchAll)) { retry(); setOpen(true); input.current?.focus(); return; }\n if (onSearchAll) { setOpen(false); onSearchAll(term); return; }\n setOpen(true); input.current?.focus();\n }\n\n const typed = query.trim();\n const showList = open && state !== 'idle';\n const errorLink = links?.error ? renderLink({ href: links.error.href, children: links.error.label }) : null;\n const emptyFallback = state === 'empty' && links?.empty ? links.empty(typed) : null;\n const emptyLink = emptyFallback ? renderLink({ href: emptyFallback.href, children: emptyFallback.label }) : null;\n const retryButton = <button type=\"button\" className=\"skolo-search__retry\" onClick={() => { retry(); input.current?.focus(); }}>{t.retry}</button>;\n\n return <form className={`skolo-search${className ? ` ${className}` : ''}`} role=\"search\" onSubmit={find}>\n <label htmlFor={`${id}-input`} className=\"skolo-search__sr-only\">{t.label}</label>\n <div className={`skolo-search__box${showList ? ' skolo-search__box--open' : ''}`}>\n <SearchIcon aria-hidden=\"true\" className=\"skolo-search__icon\" />\n <input ref={input} id={`${id}-input`} role=\"combobox\" aria-expanded={showList} aria-controls={listId} aria-autocomplete=\"list\"\n aria-activedescendant={showList && state === 'results' && active >= 0 ? `${listId}-${active}` : undefined}\n value={query} placeholder={t.placeholder} autoComplete=\"off\" spellCheck={false}\n onChange={e => { setQuery(e.target.value); setOpen(true); }} onFocus={() => setOpen(true)} onKeyDown={onKey}\n onBlur={() => setTimeout(() => setOpen(false), 150)} />\n {query && <button type=\"button\" className=\"skolo-search__clear\" aria-label={t.clear} onClick={clear}><XIcon aria-hidden=\"true\" /></button>}\n <button type=\"submit\" className=\"skolo-search__go\">{t.submit}</button>\n </div>\n {/* Pressing inside the panel keeps focus in the box, so the panel stays open for its links and buttons. */}\n {showList && <div className=\"skolo-search__panel\" onMouseDown={e => e.preventDefault()}>\n {state === 'loading' && <p className=\"skolo-search__status\">{t.loading}</p>}\n {state === 'error' && <p className=\"skolo-search__status\">{t.error({ retry: retryButton, link: errorLink })}</p>}\n {state === 'empty' && <p className=\"skolo-search__status\">{t.empty({ query: typed, link: emptyLink })}</p>}\n {state === 'results' && <ul id={listId} role=\"listbox\" aria-label={t.listLabel}>\n {results.map((school, i) => <li key={school.id} id={`${listId}-${i}`} role=\"option\" aria-selected={i === active}\n className={i === active ? 'skolo-search__option--active' : undefined}\n onMouseEnter={() => setActive(i)} onMouseDown={e => { e.preventDefault(); pick(school); }}>\n <strong>{school.name}{school.verified && <BadgeCheckIcon role=\"img\" aria-label={t.verified} />}</strong>\n <span>{formatPlace(school)}</span>\n </li>)}\n </ul>}\n </div>}\n <p className=\"skolo-search__sr-only\" aria-live=\"polite\">{state === 'results' ? t.announceResults(results.length) : state === 'empty' ? t.announceEmpty : state === 'error' ? t.announceError : ''}</p>\n </form>;\n}\n","import { useEffect, useRef, useState } from 'react';\nimport type { School, SearchSchools, SearchState } from './types';\n\nexport interface UseSchoolSearchOptions<S extends School> {\n searchSchools: SearchSchools<S>;\n /** Most results to show. Default 6. */\n limit?: number;\n /** Wait after the last keystroke before searching, in ms. Default 250 (the homepage's delay). */\n debounceMs?: number;\n /** Shortest text that searches. Default 2. */\n minChars?: number;\n}\n\nconst LEVEL: Record<string, string> = { primary: 'Primary school', high: 'High school', combined: 'Combined school' };\n\n/** \"High school, Polokwane, Limpopo\": the line under a school's name in the results. */\nexport const schoolPlace = (s: Pick<School, 'level' | 'town' | 'municipality' | 'province'>) =>\n [s.level ? LEVEL[s.level] ?? s.level : null, [s.town || s.municipality, s.province].filter(Boolean).join(', ')]\n .filter(Boolean)\n .join(', ');\n\n/**\n * Searches as the person types, keeping only the answer to the latest query: every new query (or\n * retry) bumps a sequence number and aborts the previous request, and a response is applied only if\n * its sequence number is still the current one. retry() runs the same search again.\n */\nexport function useSchoolSearch<S extends School>({ searchSchools, limit = 6, debounceMs = 250, minChars = 2 }: UseSchoolSearchOptions<S>) {\n const [query, setQuery] = useState('');\n const [state, setState] = useState<SearchState>('idle');\n const [results, setResults] = useState<S[]>([]);\n const [attempt, setAttempt] = useState(0);\n const request = useRef(0);\n // The latest search function, so an app passing a new function each render doesn't restart searches.\n const search = useRef(searchSchools);\n search.current = searchSchools;\n\n useEffect(() => {\n const term = query.trim();\n if (term.length < minChars) { request.current++; setState('idle'); setResults([]); return; }\n const seq = ++request.current;\n const controller = new AbortController();\n setState('loading');\n const timer = setTimeout(async () => {\n try {\n const found = await search.current(term, { signal: controller.signal });\n if (seq !== request.current) return;\n const items = (found || []).slice(0, limit);\n setResults(items);\n setState(items.length ? 'results' : 'empty');\n } catch {\n if (seq === request.current) { setResults([]); setState('error'); }\n }\n }, debounceMs);\n return () => { clearTimeout(timer); controller.abort(); };\n }, [query, attempt, limit, debounceMs, minChars]);\n\n return { query, setQuery, state, results, retry: () => setAttempt(n => n + 1) };\n}\n","import type { SVGProps } from 'react';\n\n// Search, X and BadgeCheck from Lucide (https://lucide.dev, ISC licence), inlined so the package has\n// no icon dependency. They match the lucide-react icons the website used. Licence: THIRD_PARTY_NOTICES.md.\nconst base = { xmlns: 'http://www.w3.org/2000/svg', width: 24, height: 24, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round' } as const;\n\nexport const SearchIcon = (props: SVGProps<SVGSVGElement>) =>\n <svg {...base} {...props}><path d=\"m21 21-4.34-4.34\" /><circle cx=\"11\" cy=\"11\" r=\"8\" /></svg>;\n\nexport const XIcon = (props: SVGProps<SVGSVGElement>) =>\n <svg {...base} {...props}><path d=\"M18 6 6 18\" /><path d=\"m6 6 12 12\" /></svg>;\n\nexport const BadgeCheckIcon = (props: SVGProps<SVGSVGElement>) =>\n <svg {...base} {...props}><path d=\"M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z\" /><path d=\"m9 12 2 2 4-4\" /></svg>;\n"],"mappings":";;;AAAA,SAAS,aAAAA,YAAW,OAAO,UAAAC,SAAQ,YAAAC,iBAAoE;;;ACAvG,SAAS,WAAW,QAAQ,gBAAgB;AAa5C,IAAM,QAAgC,EAAE,SAAS,kBAAkB,MAAM,eAAe,UAAU,kBAAkB;AAG7G,IAAM,cAAc,CAAC,MAC1B,CAAC,EAAE,QAAQ,MAAM,EAAE,KAAK,KAAK,EAAE,QAAQ,MAAM,CAAC,EAAE,QAAQ,EAAE,cAAc,EAAE,QAAQ,EAAE,OAAO,OAAO,EAAE,KAAK,IAAI,CAAC,EAC3G,OAAO,OAAO,EACd,KAAK,IAAI;AAOP,SAAS,gBAAkC,EAAE,eAAe,QAAQ,GAAG,aAAa,KAAK,WAAW,EAAE,GAA8B;AACzI,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,EAAE;AACrC,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAsB,MAAM;AACtD,QAAM,CAAC,SAAS,UAAU,IAAI,SAAc,CAAC,CAAC;AAC9C,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,CAAC;AACxC,QAAM,UAAU,OAAO,CAAC;AAExB,QAAM,SAAS,OAAO,aAAa;AACnC,SAAO,UAAU;AAEjB,YAAU,MAAM;AACd,UAAM,OAAO,MAAM,KAAK;AACxB,QAAI,KAAK,SAAS,UAAU;AAAE,cAAQ;AAAW,eAAS,MAAM;AAAG,iBAAW,CAAC,CAAC;AA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EAAE,OAAO,OAAO,MAAM,UAAU,CAAC,GAAE;AAAA,MACrG,UAAU,aAAa,gBAAAA,KAAC,QAAG,IAAI,QAAQ,MAAK,WAAU,cAAY,EAAE,WAClE,kBAAQ,IAAI,CAAC,QAAQ,MAAM,gBAAAC;AAAA,QAAC;AAAA;AAAA,UAAmB,IAAI,GAAG,MAAM,IAAI,CAAC;AAAA,UAAI,MAAK;AAAA,UAAS,iBAAe,MAAM;AAAA,UACvG,WAAW,MAAM,SAAS,iCAAiC;AAAA,UAC3D,cAAc,MAAM,UAAU,CAAC;AAAA,UAAG,aAAa,OAAK;AAAE,cAAE,eAAe;AAAG,iBAAK,MAAM;AAAA,UAAG;AAAA,UACxF;AAAA,4BAAAA,MAAC,YAAQ;AAAA,qBAAO;AAAA,cAAM,OAAO,YAAY,gBAAAD,KAAC,kBAAe,MAAK,OAAM,cAAY,EAAE,UAAU;AAAA,eAAG;AAAA,YAC/F,gBAAAA,KAAC,UAAM,sBAAY,MAAM,GAAE;AAAA;AAAA;AAAA,QAJQ,OAAO;AAAA,MAK5C,CAAK,GACP;AAAA,OACF;AAAA,IACA,gBAAAA,KAAC,OAAE,WAAU,yBAAwB,aAAU,UAAU,oBAAU,YAAY,EAAE,gBAAgB,QAAQ,MAAM,IAAI,UAAU,UAAU,EAAE,gBAAgB,UAAU,UAAU,EAAE,gBAAgB,IAAG;AAAA,KACpM;AACF;","names":["useEffect","useRef","useState","jsx","jsxs","useState","useEffect","useRef"]}
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/* Skolo Saka school search. Moved from the homepage hero (app/(site)/site.module.css on staging).
|
|
2
|
+
A solid white pill (not glass) so it reads as the page's main, active control over a dark hero.
|
|
3
|
+
|
|
4
|
+
Colours come from the website's tokens when the page defines them (--brand, --brand-deep, --sun,
|
|
5
|
+
--sun-deep, --ink, --muted, --line, --glass-tint, --radius-control, --shadow-3, --z-raised), so the
|
|
6
|
+
site's colour themes carry through. Elsewhere the default Skolo Saka values below apply. Any of the
|
|
7
|
+
--skolo-search-* properties can also be set directly on .skolo-search. Default values are plain
|
|
8
|
+
colours (no color-mix()) so older phones show them too. */
|
|
9
|
+
.skolo-search{
|
|
10
|
+
--skolo-search-brand:var(--brand,#0f5132);
|
|
11
|
+
--skolo-search-brand-deep:var(--brand-deep,#073824);
|
|
12
|
+
--skolo-search-sun:var(--sun,#f6d23f);
|
|
13
|
+
--skolo-search-sun-deep:var(--sun-deep,#e9c21f);
|
|
14
|
+
--skolo-search-ink:var(--ink,#0e261c);
|
|
15
|
+
--skolo-search-muted:var(--muted,#56655f);
|
|
16
|
+
--skolo-search-line:var(--line,#dae3df);
|
|
17
|
+
--skolo-search-tint:var(--glass-tint,rgba(232,243,238,.6));
|
|
18
|
+
--skolo-search-radius:var(--radius-control,6px);
|
|
19
|
+
--skolo-search-shadow:var(--shadow-3,0 18px 40px rgba(7,56,36,.35));
|
|
20
|
+
--skolo-search-focus-ring:rgba(246,210,63,.55);
|
|
21
|
+
--skolo-search-z:var(--z-raised,1);
|
|
22
|
+
position:relative;max-width:560px;margin-top:6px;box-sizing:border-box;text-align:left;
|
|
23
|
+
}
|
|
24
|
+
.skolo-search *,.skolo-search *::before,.skolo-search *::after{box-sizing:border-box}
|
|
25
|
+
.skolo-search__sr-only{position:absolute!important;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap;margin:0;padding:0}
|
|
26
|
+
.skolo-search__box{display:flex;align-items:center;gap:12px;background:#fff;border-radius:var(--skolo-search-radius);padding:0 8px 0 22px;height:64px;box-shadow:var(--skolo-search-shadow);transition:border-radius .15s}
|
|
27
|
+
.skolo-search__box>svg{flex:none;width:22px;height:22px;color:var(--skolo-search-brand)}
|
|
28
|
+
.skolo-search__box input{flex:1;min-width:0;border:0;outline:0;background:transparent;font:inherit;font-size:18px;color:var(--skolo-search-ink);padding:0}
|
|
29
|
+
.skolo-search__box input::placeholder{color:var(--skolo-search-muted);opacity:1}
|
|
30
|
+
.skolo-search__box:focus-within{box-shadow:0 0 0 4px var(--skolo-search-focus-ring),var(--skolo-search-shadow)}
|
|
31
|
+
.skolo-search__box--open{border-radius:var(--skolo-search-radius) var(--skolo-search-radius) 0 0}
|
|
32
|
+
.skolo-search__clear{flex:none;display:grid;place-items:center;width:40px;height:40px;padding:0;border:0;border-radius:50%;background:var(--skolo-search-tint);color:var(--skolo-search-brand-deep);cursor:pointer}
|
|
33
|
+
.skolo-search__clear svg{width:18px;height:18px}
|
|
34
|
+
.skolo-search__go{flex:none;height:48px;padding:0 20px;border:0;border-radius:4px;background:var(--skolo-search-sun);color:var(--skolo-search-brand-deep);font:inherit;font-size:15px;font-weight:800;cursor:pointer}
|
|
35
|
+
@media (hover:hover){.skolo-search__go:hover{background:var(--skolo-search-sun-deep)}}
|
|
36
|
+
.skolo-search__go:focus-visible{outline:3px solid var(--skolo-search-brand-deep);outline-offset:2px}
|
|
37
|
+
.skolo-search__clear:focus-visible,.skolo-search__retry:focus-visible,.skolo-search__status a:focus-visible{outline:3px solid var(--skolo-search-brand);outline-offset:3px;border-radius:4px}
|
|
38
|
+
.skolo-search__retry{border:0;background:none;padding:0;font:inherit;font-weight:700;color:var(--skolo-search-brand);text-decoration:underline;cursor:pointer}
|
|
39
|
+
.skolo-search__panel{position:relative;z-index:var(--skolo-search-z);background:#fff;border-top:1px solid var(--skolo-search-line);border-radius:0 0 var(--skolo-search-radius) var(--skolo-search-radius);box-shadow:var(--skolo-search-shadow);overflow:hidden}
|
|
40
|
+
.skolo-search__panel ul{list-style:none;margin:0;padding:6px}
|
|
41
|
+
.skolo-search__panel li{display:grid;gap:2px;padding:12px 16px;border-radius:4px;cursor:pointer}
|
|
42
|
+
.skolo-search__panel li strong{display:flex;align-items:center;gap:6px;color:var(--skolo-search-brand-deep);font-size:16px}
|
|
43
|
+
.skolo-search__panel li strong svg{flex:none;width:17px;height:17px;color:var(--skolo-search-brand)}
|
|
44
|
+
.skolo-search__panel li span{font-size:13.5px;color:var(--skolo-search-muted)}
|
|
45
|
+
.skolo-search__option--active{background:var(--skolo-search-tint)}
|
|
46
|
+
.skolo-search .skolo-search__status{margin:0;padding:16px 20px;font-size:14.5px;line-height:1.65;color:var(--skolo-search-muted);max-width:none}
|
|
47
|
+
.skolo-search__status a{color:var(--skolo-search-brand);font-weight:700}
|
|
48
|
+
|
|
49
|
+
@media (max-width:560px){
|
|
50
|
+
.skolo-search__box{height:56px;padding-left:18px;gap:8px}
|
|
51
|
+
.skolo-search__go{height:42px;padding:0 14px;font-size:14px}
|
|
52
|
+
.skolo-search__box input{font-size:16px}
|
|
53
|
+
}
|
|
54
|
+
/* Touch screens: 44px targets. */
|
|
55
|
+
@media (pointer:coarse){
|
|
56
|
+
.skolo-search__box input{align-self:stretch}
|
|
57
|
+
.skolo-search__go{height:44px}
|
|
58
|
+
}
|
|
59
|
+
/* Narrow phones: the box drops its icon so "School name or town" shows in full beside the button. */
|
|
60
|
+
@media (max-width:400px){.skolo-search__box{padding-left:16px}.skolo-search__box>svg{display:none}}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
/** A school as the search shows it. Apps may pass extra fields; they are handed back on selection. */
|
|
3
|
+
export interface School {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
/** 'primary' | 'high' | 'combined', or any other label the app uses. */
|
|
7
|
+
level?: string | null;
|
|
8
|
+
town?: string | null;
|
|
9
|
+
municipality?: string | null;
|
|
10
|
+
province?: string | null;
|
|
11
|
+
verified?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export type SearchState = 'idle' | 'loading' | 'results' | 'empty' | 'error';
|
|
14
|
+
/**
|
|
15
|
+
* Supplies the schools that match the typed text. Called after the search delay with the trimmed
|
|
16
|
+
* text (at least `minChars` long). `signal` is aborted when a newer search replaces this one.
|
|
17
|
+
* Throw (or reject) to show the error message and its "Try again" button.
|
|
18
|
+
*/
|
|
19
|
+
export type SearchSchools<S extends School = School> = (query: string, options: {
|
|
20
|
+
signal: AbortSignal;
|
|
21
|
+
}) => Promise<S[]>;
|
|
22
|
+
/** A link shown inside a message, e.g. "donate without choosing a school". */
|
|
23
|
+
export interface FallbackLink {
|
|
24
|
+
href: string;
|
|
25
|
+
label: ReactNode;
|
|
26
|
+
}
|
|
27
|
+
/** Every piece of text the search shows, so the app can translate it. */
|
|
28
|
+
export interface SchoolSearchMessages {
|
|
29
|
+
label: string;
|
|
30
|
+
placeholder: string;
|
|
31
|
+
submit: string;
|
|
32
|
+
clear: string;
|
|
33
|
+
listLabel: string;
|
|
34
|
+
verified: string;
|
|
35
|
+
loading: ReactNode;
|
|
36
|
+
retry: string;
|
|
37
|
+
/** `retry` is the "Try again" button; `link` is the app's error fallback link, or null. */
|
|
38
|
+
error: (parts: {
|
|
39
|
+
retry: ReactNode;
|
|
40
|
+
link: ReactNode | null;
|
|
41
|
+
}) => ReactNode;
|
|
42
|
+
/** `query` is the trimmed search text; `link` is the app's no-results link, or null. */
|
|
43
|
+
empty: (parts: {
|
|
44
|
+
query: string;
|
|
45
|
+
link: ReactNode | null;
|
|
46
|
+
}) => ReactNode;
|
|
47
|
+
/** Screen-reader announcements. */
|
|
48
|
+
announceResults: (count: number) => string;
|
|
49
|
+
announceEmpty: string;
|
|
50
|
+
announceError: string;
|
|
51
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { School, SearchSchools, SearchState } from './types';
|
|
2
|
+
export interface UseSchoolSearchOptions<S extends School> {
|
|
3
|
+
searchSchools: SearchSchools<S>;
|
|
4
|
+
/** Most results to show. Default 6. */
|
|
5
|
+
limit?: number;
|
|
6
|
+
/** Wait after the last keystroke before searching, in ms. Default 250 (the homepage's delay). */
|
|
7
|
+
debounceMs?: number;
|
|
8
|
+
/** Shortest text that searches. Default 2. */
|
|
9
|
+
minChars?: number;
|
|
10
|
+
}
|
|
11
|
+
/** "High school, Polokwane, Limpopo": the line under a school's name in the results. */
|
|
12
|
+
export declare const schoolPlace: (s: Pick<School, 'level' | 'town' | 'municipality' | 'province'>) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Searches as the person types, keeping only the answer to the latest query: every new query (or
|
|
15
|
+
* retry) bumps a sequence number and aborts the previous request, and a response is applied only if
|
|
16
|
+
* its sequence number is still the current one. retry() runs the same search again.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useSchoolSearch<S extends School>({ searchSchools, limit, debounceMs, minChars }: UseSchoolSearchOptions<S>): {
|
|
19
|
+
query: string;
|
|
20
|
+
setQuery: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
21
|
+
state: SearchState;
|
|
22
|
+
results: S[];
|
|
23
|
+
retry: () => void;
|
|
24
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skolo-saka/school-search",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Skolo Saka's homepage school search: a reusable React combobox that finds schools as people type and reports the school they pick.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/skolo-saka/school-search#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/skolo-saka/school-search/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/skolo-saka/school-search.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react",
|
|
16
|
+
"combobox",
|
|
17
|
+
"autocomplete",
|
|
18
|
+
"search",
|
|
19
|
+
"school",
|
|
20
|
+
"typescript",
|
|
21
|
+
"accessibility"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"./styles.css": "./dist/styles.css"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"THIRD_PARTY_NOTICES.md"
|
|
43
|
+
],
|
|
44
|
+
"sideEffects": [
|
|
45
|
+
"*.css"
|
|
46
|
+
],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "vite --config demo/vite.config.ts",
|
|
49
|
+
"demo": "vite --config demo/vite.config.ts",
|
|
50
|
+
"demo:build": "vite build --config demo/vite.config.ts",
|
|
51
|
+
"build": "tsup && tsc -p tsconfig.build.json && node scripts/copy-css.mjs",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"test:watch": "vitest",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"prepack": "npm run build"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"react": ">=18",
|
|
59
|
+
"react-dom": ">=18"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@testing-library/jest-dom": "^7.0.1",
|
|
63
|
+
"@testing-library/react": "^16.3.3",
|
|
64
|
+
"@testing-library/user-event": "^14.6.12",
|
|
65
|
+
"@types/node": "^26.6.6",
|
|
66
|
+
"@types/react": "^19.3.0",
|
|
67
|
+
"@types/react-dom": "^19.3.0",
|
|
68
|
+
"@vitejs/plugin-react": "^6.3.0",
|
|
69
|
+
"jsdom": "^29.1.1",
|
|
70
|
+
"react": "^19.3.1",
|
|
71
|
+
"react-dom": "^19.3.1",
|
|
72
|
+
"tsup": "^9.0.0",
|
|
73
|
+
"typescript": "^7.0.2",
|
|
74
|
+
"vite": "^8.3.8",
|
|
75
|
+
"vitest": "^5.1.0"
|
|
76
|
+
}
|
|
77
|
+
}
|