@etoile-dev/react 0.2.0 → 0.2.2
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/README.md +2 -2
- package/dist/components/SearchInput.d.ts +4 -2
- package/dist/components/SearchInput.js +36 -12
- package/dist/components/SearchResult.d.ts +3 -2
- package/dist/components/SearchResult.js +3 -2
- package/dist/components/SearchResultThumbnail.d.ts +4 -2
- package/dist/components/SearchResultThumbnail.js +3 -2
- package/dist/components/SearchResults.d.ts +3 -2
- package/dist/components/SearchResults.js +5 -4
- package/dist/components/SearchRoot.js +51 -3
- package/dist/context/SearchContext.d.ts +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -254,7 +254,7 @@ Controlled input with ARIA combobox role.
|
|
|
254
254
|
**Keyboard shortcuts:**
|
|
255
255
|
- `ArrowUp` / `ArrowDown` — Navigate results
|
|
256
256
|
- `Enter` — Select active result
|
|
257
|
-
- `Escape` —
|
|
257
|
+
- `Escape` — Close results (press again to clear)
|
|
258
258
|
|
|
259
259
|
---
|
|
260
260
|
|
|
@@ -369,7 +369,7 @@ type SearchResultData = {
|
|
|
369
369
|
## Why @etoile-dev/react?
|
|
370
370
|
|
|
371
371
|
- **Radix / shadcn-style primitives** — Composable and unstyled
|
|
372
|
-
- **Accessibility built-in** — ARIA
|
|
372
|
+
- **Accessibility built-in** — ARIA combobox, keyboard navigation, focus management, click-outside dismiss
|
|
373
373
|
- **Behavior, not appearance** — You own the design
|
|
374
374
|
- **TypeScript-first** — Full type safety
|
|
375
375
|
- **Zero dependencies** — Only React and @etoile-dev/client
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
import * as React from "react";
|
|
1
2
|
export type SearchInputProps = {
|
|
2
3
|
/** Placeholder text for the input field */
|
|
3
4
|
placeholder?: string;
|
|
4
5
|
/** CSS class name for styling the input */
|
|
5
6
|
className?: string;
|
|
6
|
-
}
|
|
7
|
+
} & React.InputHTMLAttributes<HTMLInputElement>;
|
|
7
8
|
/**
|
|
8
9
|
* Search input component with built-in keyboard navigation and accessibility.
|
|
9
10
|
*
|
|
10
11
|
* Integrates with SearchRoot context to provide debouncing and keyboard controls
|
|
11
12
|
* (ArrowUp, ArrowDown, Enter, Escape). Implements ARIA combobox pattern.
|
|
13
|
+
* Accepts standard input props like aria-label and autoComplete.
|
|
12
14
|
*
|
|
13
15
|
* @param props - Component props
|
|
14
16
|
*
|
|
@@ -25,4 +27,4 @@ export type SearchInputProps = {
|
|
|
25
27
|
* />
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
export declare const SearchInput: ({ placeholder, className }: SearchInputProps) => import("react/jsx-runtime").JSX.Element;
|
|
30
|
+
export declare const SearchInput: ({ placeholder, className, ...props }: SearchInputProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useSearchContext } from "../context/SearchContext.js";
|
|
3
3
|
/**
|
|
4
4
|
* Search input component with built-in keyboard navigation and accessibility.
|
|
5
5
|
*
|
|
6
6
|
* Integrates with SearchRoot context to provide debouncing and keyboard controls
|
|
7
7
|
* (ArrowUp, ArrowDown, Enter, Escape). Implements ARIA combobox pattern.
|
|
8
|
+
* Accepts standard input props like aria-label and autoComplete.
|
|
8
9
|
*
|
|
9
10
|
* @param props - Component props
|
|
10
11
|
*
|
|
@@ -21,15 +22,38 @@ import { useSearchContext } from "../context/SearchContext.js";
|
|
|
21
22
|
* />
|
|
22
23
|
* ```
|
|
23
24
|
*/
|
|
24
|
-
export const SearchInput = ({ placeholder, className }) => {
|
|
25
|
-
const { query, setQuery, results, selectedIndex, setSelectedIndex, listboxId, getResultId, handleKeyDown, autoFocus, } = useSearchContext();
|
|
26
|
-
const
|
|
27
|
-
const activeId = selectedIndex >= 0 &&
|
|
28
|
-
return (_jsx("input", { type: "text", placeholder: placeholder, className: className, value: query, autoFocus: autoFocus, role: "combobox", "aria-expanded":
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
export const SearchInput = ({ placeholder, className, ...props }) => {
|
|
26
|
+
const { query, setQuery, results, isOpen, setOpen, selectedIndex, setSelectedIndex, listboxId, getResultId, handleKeyDown, autoFocus, } = useSearchContext();
|
|
27
|
+
const showResults = isOpen && results.length > 0;
|
|
28
|
+
const activeId = selectedIndex >= 0 && showResults ? getResultId(selectedIndex) : undefined;
|
|
29
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ...props, type: "text", placeholder: placeholder, className: className, value: query, autoFocus: autoFocus, role: "combobox", "aria-expanded": showResults, "aria-controls": listboxId, "aria-activedescendant": activeId, "aria-autocomplete": "list", onChange: (event) => {
|
|
30
|
+
props.onChange?.(event);
|
|
31
|
+
const nextValue = event.target.value;
|
|
32
|
+
setQuery(nextValue);
|
|
33
|
+
if (nextValue.trim() !== "") {
|
|
34
|
+
setSelectedIndex(0);
|
|
35
|
+
}
|
|
36
|
+
}, onFocus: (event) => {
|
|
37
|
+
props.onFocus?.(event);
|
|
38
|
+
if (!event.defaultPrevented && query.trim() !== "" && results.length > 0) {
|
|
39
|
+
setOpen(true);
|
|
40
|
+
}
|
|
41
|
+
}, onKeyDown: (event) => {
|
|
42
|
+
props.onKeyDown?.(event);
|
|
43
|
+
if (!event.defaultPrevented) {
|
|
44
|
+
handleKeyDown(event);
|
|
45
|
+
}
|
|
46
|
+
} }), _jsx("div", { role: "status", "aria-live": "polite", "aria-atomic": "true", style: {
|
|
47
|
+
position: "absolute",
|
|
48
|
+
width: 1,
|
|
49
|
+
height: 1,
|
|
50
|
+
padding: 0,
|
|
51
|
+
margin: -1,
|
|
52
|
+
overflow: "hidden",
|
|
53
|
+
clip: "rect(0, 0, 0, 0)",
|
|
54
|
+
whiteSpace: "nowrap",
|
|
55
|
+
border: 0,
|
|
56
|
+
}, children: showResults
|
|
57
|
+
? `${results.length} result${results.length === 1 ? "" : "s"} available`
|
|
58
|
+
: "" })] }));
|
|
35
59
|
};
|
|
@@ -4,12 +4,13 @@ export type SearchResultProps = {
|
|
|
4
4
|
className?: string;
|
|
5
5
|
/** Content to render inside the result */
|
|
6
6
|
children: React.ReactNode;
|
|
7
|
-
}
|
|
7
|
+
} & React.HTMLAttributes<HTMLDivElement>;
|
|
8
8
|
/**
|
|
9
9
|
* Individual search result item with selection state and keyboard navigation.
|
|
10
10
|
*
|
|
11
11
|
* Manages selection state and accessibility attributes. Provides `data-selected`
|
|
12
12
|
* attribute for styling the active result. Must be used inside SearchResults.
|
|
13
|
+
* Accepts standard div props like onClick for custom behavior.
|
|
13
14
|
*
|
|
14
15
|
* @param props - Component props
|
|
15
16
|
*
|
|
@@ -27,4 +28,4 @@ export type SearchResultProps = {
|
|
|
27
28
|
* // CSS: .result-item[data-selected="true"] { background: #f0f9ff; }
|
|
28
29
|
* ```
|
|
29
30
|
*/
|
|
30
|
-
export declare const SearchResult: ({ className, children }: SearchResultProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
31
|
+
export declare const SearchResult: ({ className, children, ...props }: SearchResultProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -7,6 +7,7 @@ import { SearchResultIndexContext } from "./SearchResults.js";
|
|
|
7
7
|
*
|
|
8
8
|
* Manages selection state and accessibility attributes. Provides `data-selected`
|
|
9
9
|
* attribute for styling the active result. Must be used inside SearchResults.
|
|
10
|
+
* Accepts standard div props like onClick for custom behavior.
|
|
10
11
|
*
|
|
11
12
|
* @param props - Component props
|
|
12
13
|
*
|
|
@@ -24,7 +25,7 @@ import { SearchResultIndexContext } from "./SearchResults.js";
|
|
|
24
25
|
* // CSS: .result-item[data-selected="true"] { background: #f0f9ff; }
|
|
25
26
|
* ```
|
|
26
27
|
*/
|
|
27
|
-
export const SearchResult = ({ className, children }) => {
|
|
28
|
+
export const SearchResult = ({ className, children, ...props }) => {
|
|
28
29
|
const { selectedIndex, registerResult, getResultId } = useSearchContext();
|
|
29
30
|
const index = React.useContext(SearchResultIndexContext);
|
|
30
31
|
if (index === null) {
|
|
@@ -35,5 +36,5 @@ export const SearchResult = ({ className, children }) => {
|
|
|
35
36
|
const setRef = React.useCallback((node) => {
|
|
36
37
|
registerResult(index, node);
|
|
37
38
|
}, [index, registerResult]);
|
|
38
|
-
return (_jsx("div", { ref: setRef, id: id, role: "option", "aria-selected": isSelected, "data-selected": isSelected ? "true" : "false", "data-index": index, tabIndex: isSelected ? 0 : -1, className: className, children: children }));
|
|
39
|
+
return (_jsx("div", { ...props, ref: setRef, id: id, role: "option", "aria-selected": isSelected, "data-selected": isSelected ? "true" : "false", "data-index": index, tabIndex: isSelected ? 0 : -1, className: className, children: children }));
|
|
39
40
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as React from "react";
|
|
1
2
|
export type SearchResultThumbnailProps = {
|
|
2
3
|
/** Image source URL (defaults to result.metadata.thumbnailUrl) */
|
|
3
4
|
src?: string;
|
|
@@ -7,12 +8,13 @@ export type SearchResultThumbnailProps = {
|
|
|
7
8
|
size?: number;
|
|
8
9
|
/** CSS class name for styling */
|
|
9
10
|
className?: string;
|
|
10
|
-
}
|
|
11
|
+
} & React.ImgHTMLAttributes<HTMLImageElement>;
|
|
11
12
|
/**
|
|
12
13
|
* Thumbnail image for search results with automatic source detection.
|
|
13
14
|
*
|
|
14
15
|
* Automatically uses `metadata.thumbnailUrl` if available. Returns null
|
|
15
16
|
* if no image source is found. Must be used inside SearchResults.
|
|
17
|
+
* Accepts standard img props like loading and decoding.
|
|
16
18
|
*
|
|
17
19
|
* @param props - Component props
|
|
18
20
|
*
|
|
@@ -33,4 +35,4 @@ export type SearchResultThumbnailProps = {
|
|
|
33
35
|
* <SearchResultThumbnail size={48} className="rounded-full" />
|
|
34
36
|
* ```
|
|
35
37
|
*/
|
|
36
|
-
export declare const SearchResultThumbnail: ({ src, alt, size, className, }: SearchResultThumbnailProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
38
|
+
export declare const SearchResultThumbnail: ({ src, alt, size, className, ...props }: SearchResultThumbnailProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -6,6 +6,7 @@ import { SearchResultDataContext } from "./SearchResults.js";
|
|
|
6
6
|
*
|
|
7
7
|
* Automatically uses `metadata.thumbnailUrl` if available. Returns null
|
|
8
8
|
* if no image source is found. Must be used inside SearchResults.
|
|
9
|
+
* Accepts standard img props like loading and decoding.
|
|
9
10
|
*
|
|
10
11
|
* @param props - Component props
|
|
11
12
|
*
|
|
@@ -26,12 +27,12 @@ import { SearchResultDataContext } from "./SearchResults.js";
|
|
|
26
27
|
* <SearchResultThumbnail size={48} className="rounded-full" />
|
|
27
28
|
* ```
|
|
28
29
|
*/
|
|
29
|
-
export const SearchResultThumbnail = ({ src, alt, size = 40, className, }) => {
|
|
30
|
+
export const SearchResultThumbnail = ({ src, alt, size = 40, className, ...props }) => {
|
|
30
31
|
const result = React.useContext(SearchResultDataContext);
|
|
31
32
|
const imageSrc = src ?? result?.metadata?.thumbnailUrl;
|
|
32
33
|
const imageAlt = alt ?? result?.title ?? "";
|
|
33
34
|
if (!imageSrc) {
|
|
34
35
|
return null;
|
|
35
36
|
}
|
|
36
|
-
return (_jsx("img", { src: imageSrc, alt: imageAlt, width: size, height: size, className: className, draggable: false }));
|
|
37
|
+
return (_jsx("img", { ...props, src: imageSrc, alt: imageAlt, width: size, height: size, className: className, draggable: false }));
|
|
37
38
|
};
|
|
@@ -5,7 +5,7 @@ export type SearchResultsProps = {
|
|
|
5
5
|
className?: string;
|
|
6
6
|
/** Render function that receives each search result */
|
|
7
7
|
children: (result: SearchResultData) => React.ReactNode;
|
|
8
|
-
}
|
|
8
|
+
} & Omit<React.HTMLAttributes<HTMLDivElement>, "children">;
|
|
9
9
|
export declare const SearchResultIndexContext: React.Context<number | null>;
|
|
10
10
|
export declare const SearchResultDataContext: React.Context<SearchResultData | null>;
|
|
11
11
|
/**
|
|
@@ -13,6 +13,7 @@ export declare const SearchResultDataContext: React.Context<SearchResultData | n
|
|
|
13
13
|
*
|
|
14
14
|
* Accepts a render function that receives each result. Automatically hides
|
|
15
15
|
* when query is empty or no results found. Includes ARIA listbox role.
|
|
16
|
+
* Accepts standard div props like onScroll and style.
|
|
16
17
|
*
|
|
17
18
|
* @param props - Component props
|
|
18
19
|
*
|
|
@@ -35,4 +36,4 @@ export declare const SearchResultDataContext: React.Context<SearchResultData | n
|
|
|
35
36
|
* </SearchResults>
|
|
36
37
|
* ```
|
|
37
38
|
*/
|
|
38
|
-
export declare const SearchResults: ({ className, children }: SearchResultsProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
39
|
+
export declare const SearchResults: ({ className, children, ...props }: SearchResultsProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -8,6 +8,7 @@ export const SearchResultDataContext = React.createContext(null);
|
|
|
8
8
|
*
|
|
9
9
|
* Accepts a render function that receives each result. Automatically hides
|
|
10
10
|
* when query is empty or no results found. Includes ARIA listbox role.
|
|
11
|
+
* Accepts standard div props like onScroll and style.
|
|
11
12
|
*
|
|
12
13
|
* @param props - Component props
|
|
13
14
|
*
|
|
@@ -30,8 +31,8 @@ export const SearchResultDataContext = React.createContext(null);
|
|
|
30
31
|
* </SearchResults>
|
|
31
32
|
* ```
|
|
32
33
|
*/
|
|
33
|
-
export const SearchResults = ({ className, children }) => {
|
|
34
|
-
const { query, results, selectedIndex, listboxId, getResultNode } = useSearchContext();
|
|
34
|
+
export const SearchResults = ({ className, children, ...props }) => {
|
|
35
|
+
const { query, results, isOpen, selectedIndex, listboxId, getResultNode } = useSearchContext();
|
|
35
36
|
const listboxRef = React.useRef(null);
|
|
36
37
|
React.useEffect(() => {
|
|
37
38
|
if (selectedIndex < 0) {
|
|
@@ -45,8 +46,8 @@ export const SearchResults = ({ className, children }) => {
|
|
|
45
46
|
activeNode.focus();
|
|
46
47
|
}
|
|
47
48
|
}, [getResultNode, selectedIndex]);
|
|
48
|
-
if (query.trim() === "" || results.length === 0) {
|
|
49
|
+
if (!isOpen || query.trim() === "" || results.length === 0) {
|
|
49
50
|
return null;
|
|
50
51
|
}
|
|
51
|
-
return (_jsx("div", { role: "listbox", id: listboxId, className: className, ref: listboxRef, children: results.map((result, index) => (_jsx(SearchResultIndexContext.Provider, { value: index, children: _jsx(SearchResultDataContext.Provider, { value: result, children: children(result) }) }, result.external_id))) }));
|
|
52
|
+
return (_jsx("div", { ...props, role: "listbox", id: listboxId, className: className, ref: listboxRef, children: results.map((result, index) => (_jsx(SearchResultIndexContext.Provider, { value: index, children: _jsx(SearchResultDataContext.Provider, { value: result, children: children(result) }) }, result.external_id))) }));
|
|
52
53
|
};
|
|
@@ -31,6 +31,44 @@ export const SearchRoot = ({ apiKey, collections, limit, debounceMs, autoFocus =
|
|
|
31
31
|
const search = useSearch({ apiKey, collections, limit, debounceMs, baseUrl });
|
|
32
32
|
const listboxId = React.useId();
|
|
33
33
|
const resultRefs = React.useRef(new Map());
|
|
34
|
+
const rootRef = React.useRef(null);
|
|
35
|
+
const [isOpen, setOpen] = React.useState(false);
|
|
36
|
+
// Open the results list whenever results arrive and query is non-empty
|
|
37
|
+
React.useEffect(() => {
|
|
38
|
+
if (search.results.length > 0 && search.query.trim() !== "") {
|
|
39
|
+
setOpen(true);
|
|
40
|
+
}
|
|
41
|
+
}, [search.results, search.query]);
|
|
42
|
+
// Close results when query is cleared
|
|
43
|
+
React.useEffect(() => {
|
|
44
|
+
if (search.query.trim() === "") {
|
|
45
|
+
setOpen(false);
|
|
46
|
+
}
|
|
47
|
+
}, [search.query]);
|
|
48
|
+
// Click-outside: close results when clicking outside the root element
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
const handlePointerDown = (event) => {
|
|
51
|
+
if (rootRef.current &&
|
|
52
|
+
event.target instanceof Node &&
|
|
53
|
+
!rootRef.current.contains(event.target)) {
|
|
54
|
+
setOpen(false);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
58
|
+
return () => document.removeEventListener("pointerdown", handlePointerDown);
|
|
59
|
+
}, []);
|
|
60
|
+
// Focus-out: close results when focus leaves the component entirely
|
|
61
|
+
const handleFocusOut = (event) => {
|
|
62
|
+
if (rootRef.current &&
|
|
63
|
+
event.relatedTarget instanceof Node &&
|
|
64
|
+
!rootRef.current.contains(event.relatedTarget)) {
|
|
65
|
+
setOpen(false);
|
|
66
|
+
}
|
|
67
|
+
// relatedTarget is null when focus moves outside the document (e.g. address bar)
|
|
68
|
+
if (!event.relatedTarget) {
|
|
69
|
+
setOpen(false);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
34
72
|
const registerResult = (index, node) => {
|
|
35
73
|
resultRefs.current.set(index, node);
|
|
36
74
|
};
|
|
@@ -50,11 +88,13 @@ export const SearchRoot = ({ apiKey, collections, limit, debounceMs, autoFocus =
|
|
|
50
88
|
const handleKeyDown = (event) => {
|
|
51
89
|
if (event.key === "ArrowDown") {
|
|
52
90
|
event.preventDefault();
|
|
91
|
+
setOpen(true);
|
|
53
92
|
search.setSelectedIndex(search.selectedIndex + 1);
|
|
54
93
|
return;
|
|
55
94
|
}
|
|
56
95
|
if (event.key === "ArrowUp") {
|
|
57
96
|
event.preventDefault();
|
|
97
|
+
setOpen(true);
|
|
58
98
|
search.setSelectedIndex(search.selectedIndex - 1);
|
|
59
99
|
return;
|
|
60
100
|
}
|
|
@@ -67,11 +107,19 @@ export const SearchRoot = ({ apiKey, collections, limit, debounceMs, autoFocus =
|
|
|
67
107
|
}
|
|
68
108
|
if (event.key === "Escape") {
|
|
69
109
|
event.preventDefault();
|
|
70
|
-
|
|
110
|
+
// First Escape closes results, second clears the query
|
|
111
|
+
if (isOpen) {
|
|
112
|
+
setOpen(false);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
search.clear();
|
|
116
|
+
}
|
|
71
117
|
}
|
|
72
118
|
};
|
|
73
119
|
const value = React.useMemo(() => ({
|
|
74
120
|
...search,
|
|
121
|
+
isOpen,
|
|
122
|
+
setOpen,
|
|
75
123
|
listboxId,
|
|
76
124
|
getResultId,
|
|
77
125
|
registerResult,
|
|
@@ -79,6 +127,6 @@ export const SearchRoot = ({ apiKey, collections, limit, debounceMs, autoFocus =
|
|
|
79
127
|
selectActiveResult,
|
|
80
128
|
handleKeyDown,
|
|
81
129
|
autoFocus,
|
|
82
|
-
}), [search, listboxId, autoFocus]);
|
|
83
|
-
return (_jsx(SearchProvider, { value: value, children: _jsx("div", { className: className ? `etoile-search ${className}` : "etoile-search", children: children }) }));
|
|
130
|
+
}), [search, isOpen, listboxId, autoFocus]);
|
|
131
|
+
return (_jsx(SearchProvider, { value: value, children: _jsx("div", { ref: rootRef, className: className ? `etoile-search ${className}` : "etoile-search", onBlur: handleFocusOut, children: children }) }));
|
|
84
132
|
};
|
|
@@ -12,6 +12,10 @@ type SearchContextValue = {
|
|
|
12
12
|
selectedIndex: number;
|
|
13
13
|
setSelectedIndex: (i: number) => void;
|
|
14
14
|
clear: () => void;
|
|
15
|
+
/** Whether the results list is currently open/visible */
|
|
16
|
+
isOpen: boolean;
|
|
17
|
+
/** Open or close the results list */
|
|
18
|
+
setOpen: (open: boolean) => void;
|
|
15
19
|
listboxId: string;
|
|
16
20
|
getResultId: (index: number) => string;
|
|
17
21
|
registerResult: (index: number, node: HTMLElement | null) => void;
|