Skip to content

Commit

Permalink
Quick Modular UI README
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Feb 16, 2023
1 parent 5ece87c commit 060c4c2
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
* **Default UI**: Added a `processTerm` hook that can normalize the search query. See [Pagefind UI > Process term](https://pagefind.app/docs/ui/#process-result)
* **Default UI**: Added a `Clear` button to the search input
* **Default UI**: Added functionality to clear the search input when `Esc` is pressed while the input is focused
* **Default UI**: Published UI to npm under `@pagefind/default-ui`, as an alternative to using the files output by the Pagefind CLI
* **Default UI**: Published UI to npm under [@pagefind/default-ui](https://www.npmjs.com/package/@pagefind/default-ui), as an alternative to using the files output by the Pagefind CLI

### Fixes & Tweaks
* **Default UI**: Fixed a syntax error in the CSS

### Prelease: Modular UI
* Work is underway on a new "Modular UI" that will live alongside the current "Default UI". Full support and documentation will be provided in a future release — the prerelease version can be found on npm under [@pagefind/modular-ui](https://www.npmjs.com/package/@pagefind/modular-ui)
* As this package is still under development, some of the configuration may change in a future release. Make sure to pin your Pagefind versions for any production site relying on the Modular UI.

## v0.10.7 (January 19, 2023)

* Avoid using bsdtar in the release flow, as that will sometimes create sparse tar files that some packages cannot decompress. (Fixes lumeland/lume#362)
Expand Down
187 changes: 186 additions & 1 deletion pagefind_ui/modular/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,194 @@
# Pagefind Modular UI

> The Pagefind Modular UI is under development — check back soon!
> The Pagefind Modular UI is under development, and as it currently stands should be treated as a prerelease version. If you rely on this on a production website, make sure to pin your Pagefind versions.
Pagefind is a fully static search library that aims to perform well on large sites, while using as little of your users' bandwidth as possible.

Pagefind runs after any static site generator and automatically indexes the built static files. Pagefind then outputs a static search bundle to your website, and exposes a JavaScript search API that can be used anywhere on your site.

See the [Pagefind Documentation](https://pagefind.app/) for full usage.

The Pagefind Modular UI allows you to build a search UI out of Modules, all connected to one instance of Pagefind. With this, rich search experiences can be quickly created, and the look+feel of your website can more easily be matched.

## Quick usage

> These code snippets assume you have already indexed your website with the Pagefind CLI.
### Quick usage via output files
The Pagefind CLI outputs assets for the Modular UI that can be loaded directly:

```html
<link href="/_pagefind/pagefind-modular-ui.css" rel="stylesheet">
<script src="/_pagefind/pagefind-modular-ui.js" type="text/javascript"></script>

<script>
window.addEventListener('DOMContentLoaded', (event) => {
const instance = new PagefindModularUI.Instance();
instance.add(new PagefindModularUI.Input({
containerElement: "#search"
}));
instance.add(new PagefindModularUI.ResultList({
containerElement: "#results"
}));
});
</script>
```

If using the output files, all code snippets below will require the `PagefindModularUI` prefix to access modules.

### Quick usage via npm
The Modular UI is also distributed as an NPM package:

```js
import { Instance, Input, ResultList } from "@pagefind/modular-ui";

const instance = new Instance({
bundlePath: "/_pagefind/"
});
instance.add(new Input({
containerElement: "#searchbox"
}));
instance.add(new ResultList({
containerElement: "#searchresults"
}));
```

With a bundler configuration that supports CSS:

```js
import styles from "@pagefind/modular-ui/css/ui.css";
```

## Instance

```js
const instance = new Instance({
bundlePath: "/_pagefind/"
});
```

An `Instance` serves as the central hub that all modules are connected to, and facilitates communication between each module and the Pagefind JS API.

| Option | Description |
|-|-|
| `bundlePath` | See [UI > Bundle path](https://pagefind.app/docs/ui/#bundle-path) |
| `mergeIndex` | See [Searching additional sites from Pagefind UI](https://pagefind.app/docs/multisite/#searching-additional-sites-from-pagefind-ui) |

| Method | Description |
|-|-|
| `add(module)` | Connects a module to this `Instance` |

## Modules

The Modular UI currently ships with a small handful of prebuilt modules, and more will be added in future releases.

### Input

```js
instance.add(new Input({
containerElement: "#searchbox"
}));
// or
instance.add(new Input({
inputElement: "#existinginput"
}));
```

| Option | Description |
|-|-|
| `containerElement` | A selector to an element that a new search input should be placed within |
| `inputElement` | A selector to an existing `<input />` element that should be used as the search input. _(NB: No Pagefind styling will be applied)_ |
| `debounceTimeoutMs` | Number of ms (default: `300`) to wait before performing a search while a user is typing |

### ResultList

```js
instance.add(new ResultList({
containerElement: "#results"
}));
```

| Option | Description |
|-|-|
| `containerElement` | A selector to an element that the results should be placed within |
| `placeholderTemplate` | A function that returns the template for a result that has not yet loaded |
| `resultTemplate` | A function that returns the template for a search result |

```js
// Larger example:
instance.add(new ResultList({
containerElement: "#results",
placeholderTemplate: () => {
return "<p>Loading...</p>";
},
resultTemplate: (result) => {
const el = document.createElement("p");
el.classList.add("my-result-class");
el.innerText = result.meta.title;
return el;
}
}));
```

The template functions can return either a string, a DOM node, or an array of DOM nodes.

### Summary

```js
instance.add(new Summary({
containerElement: "#summary"
}));
```

| Option | Description |
|-|-|
| `containerElement` | A selector to an element that the search summary text should be placed within |

### Custom Modules

Full documentation to come for custom modules, as this syntax may change. For the adventurous, here is a template for a UI module using the event system:

```js
export class MyCustomComponent {
constructor(opts = {}) {
// Handle adding MyCustomComponent to the page
}

// This function is called by the containing Instance when this component is added
register(instance) {
this.instance = instance; // Store the instance so we can trigger events

instance.on("search", (term, filters) => {
// A new search has been started
});

instance.on("loading", () => {
// A search is running and results are being loaded
});

instance.on("results", (results) => {
// Search results are available
});

instance.on("filters", (filters) => {
// The set of available filters has been updated
});
}

// Assuming this function is triggered by some user action
myFunction(searchTerm) {
this.instance.triggerSearch(searchTerm);
}
}
```

Alternatively, you can react to events from the instance directly:

```js
const instance = new Instance({
bundlePath: "/_pagefind/"
});
instance.on("results", (results) => {
// Search results are available
});
```

0 comments on commit 060c4c2

Please sign in to comment.