Hooks and Utils
Following utils can be imported from @findkit/ui
for slot component usage.
Functions starting with the use
keyword are
Preact hooks.
useParams()
Slot override hook version of updateParams
.
Example
import { FindkitUI, html, useInput } from "@findkit/ui";
const ui = new FindkitUI({
publicToken: "<TOKEN>",
slots: {
Header(props) {
const [params, setParams] = useParams();
const checked = params.filter.tags === "crawler";
return html`
${props.children}
<label>
<input
type="checkbox"
checked=${checked}
onChange=${() => {
setParams((params) => {
if (checked) {
params.filter.tags = "crawler";
} else {
delete params.filter.tags;
}
});
}}
/>
Limit results with tag "crawler"
</label>
`;
},
},
});
useGroups()
Slot override Hook version of updateGroups
.
Example
const [groups, setGroups] = useGroups();
const onClick = () => {
setGroups((group1, groups2 /* ...groups */) => {
group1.title = "new title";
group2.params.filter.tags = "crawler";
});
};
useTerms()
Return the search terms used for the current search results.
api docsuseInput()
Hook for binding custom inputs as the search terms inputs. Usually used in the Header slot to override the default search input with a fully custom one
import { FindkitUI, html, useInput } from "@findkit/ui";
const ui = new FindkitUI({
publicToken: "<TOKEN>",
slots: {
Header(props) {
const ref = useInput();
return html`<input type="text" ref=${ref} />`;
},
},
});
useTotalHitCount()
Return total hit count. Includes count from all groups if multiple groups are used.
api docsuseLoading()
Delayed loading event as a hook. Return true when a network requet is taking longer than usual. Use to to implement custom loading indicators in a slot overrides.
api docsuseLang()
Return the current UI language. Can be used to implement custom ui string
translations on slot overrides. See setLang
.
useCustomRouterData()
New in v0.16.0
Preact Hook version of the
setCustomRouterData
method. Works like
useState
but the value must be a flat object of string values. Eg. {[key:
string]: string}
import { FindkitUI, html, useCustomRouterData, useParams } from "@findkit/ui";
const ui = new FindkitUI({
publicToken: "<TOKEN>",
slots: {
Header(props) {
const [data, setData] = useCustomRouterData({ value: "yes" });
const [params, setParams] = useParams();
const onClick = () => {
setData((data) => {
if (data.value === "yes") {
data.value = "no";
} else {
data.value = "yes";
}
});
// Update search params
updateParams(/* ... */);
};
return html`
<button type="button" onClick=${onClick}>${data.value}</button>
`;
},
},
});
preact
Object of commonly used Preact Hooks. See the api docs for details.
api docshtml
Prebound HTM (Hyperscript Tagged Markup) tagged template. For more information see: https://github.com/developit/htm
api docsh
The Preact JSX pragma
api docs