1.1.0 • Published 6 days ago

@pagefind/modular-ui v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 days ago

Pagefind Modular UI

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 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:

<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:

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:

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

Instance

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.

OptionDescription
bundlePathSee UI > Bundle path
mergeIndexSee Searching additional sites from Pagefind UI
MethodDescription
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

instance.add(new Input({
    containerElement: "#searchbox"
}));
// or
instance.add(new Input({
    inputElement: "#existinginput"
}));
OptionDescription
containerElementA selector to an element that a new search input should be placed within
inputElementA selector to an existing <input /> element that should be used as the search input. (NB: No Pagefind styling will be applied)
debounceTimeoutMsNumber of ms (default: 300) to wait before performing a search while a user is typing

ResultList

instance.add(new ResultList({
    containerElement: "#results"
}));
OptionDescription
containerElementA selector to an element that the results should be placed within
placeholderTemplateA function that returns the template for a result that has not yet loaded
resultTemplateA function that returns the template for a search result
// 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

instance.add(new Summary({
    containerElement: "#summary"
}));
OptionDescription
containerElementA selector to an element that the search summary text should be placed within
defaultMessageThe text to show when there is no summary. Defaults to nothing

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:

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:

const instance = new Instance({
    bundlePath: "/_pagefind/"
});
instance.on("results", (results) => {
    // Search results are available
});
1.1.1-alpha.1

6 days ago

1.1.0

1 month ago

1.1.0-rc1

2 months ago

1.0.5-rc2

3 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

1.0.4

6 months ago

1.0.0-alpha.9

10 months ago

1.0.3

8 months ago

1.0.0-alpha.8

10 months ago

1.0.0-alpha.7

10 months ago

1.0.0-alpha.6

10 months ago

1.0.0-alpha.10

9 months ago

1.0.5-rc1

6 months ago

1.0.5-rc0

6 months ago

1.0.4-rc0

7 months ago

1.0.4-rc2

6 months ago

1.0.4-rc1

6 months ago

1.0.4-beta.0

7 months ago

1.0.0-beta.2

8 months ago

1.0.0-beta.3

8 months ago

1.0.3-rc1

8 months ago

1.0.0-beta.4

8 months ago

1.0.3-rc0

8 months ago

1.0.0-beta.5

8 months ago

1.0.0-beta.0

8 months ago

1.0.0-beta.1

8 months ago

1.0.1-rc1

8 months ago

1.0.0-beta.6

8 months ago

1.0.1-rc0

8 months ago

1.0.0-alpha.5

12 months ago

0.12.0-beta.3

1 year ago

0.12.0-beta.2

1 year ago

1.0.0-alpha.4

1 year ago

1.0.0-alpha.3

1 year ago

1.0.0-alpha.2

1 year ago

1.0.0-alpha.1

1 year ago

1.0.0-alpha.0

1 year ago

0.12.0-beta.1

1 year ago

0.12.0

1 year ago

0.12.0-beta.0

1 year ago

0.13.0-alpha.0

1 year ago

0.12.1-beta.0

1 year ago

0.11.1-beta.3

1 year ago

0.11.1-rc5

1 year ago

0.11.1-rc4

1 year ago

0.11.1-rc3

1 year ago

0.11.1-rc2

1 year ago

0.11.1-rc1

1 year ago

0.11.1-rc0

1 year ago

0.11.0

1 year ago

0.11.0-rc4

1 year ago