@ongov/ontario-search
v2.0.2
Published
React components containing search bar and autosuggest to be used for search on ontario government websites
Keywords
Readme
@ongov/ontario-search
This package provides a React-based search bar with autosuggest dropdown for Ontario.ca websites. It includes:
- Typeahead completion suggestions (from Elasticsearch)
- Page suggestions with popular page badges
- Cookie-based search history
- Bilingual support (EN/FR) with automatic language detection from the URL path
- Ontario.ca URL redirect detection
- Google Analytics (dataLayer) event tracking
Development
- Install dependencies:
npm installfrom this folder - Run
npm run startto build and serve with hot reload (Webpack dev server)
Build Modes
Default build (UMD — recommended)
npm run buildProduces a self-contained UMD bundle at dist/index.js with all dependencies
(including React) bundled in. Works with a plain <script> tag — no bundler
required. This is the build published to NPM and used by both the ontario.ca
search application and non-React consumers (e.g. Drupal).
No-React build (ES module)
npm run build:no-reactProduces an ES module build at dist/index.js with React, axios, i18next, and
other dependencies externalized. Use this if you already have React in your
project and want a smaller bundle. Requires a bundler (Vite, Webpack, etc.) to
resolve the peer dependencies.
Packaging & Deployment
NPM Package Deployment (Automated)
NPM publishing is fully automated via GitLab CI.
To publish a new version of the package:
- Ensure your changes are merged into the
mainbranch - Use Conventional Commits with the
(npm-components)scope, for example:feat(npm-components): add autocompletefix(npm-components): fix crash
What happens automatically
Once changes are merged into main, the CI pipeline will:
- Analyze commits since the last release
- Determine the version bump:
feat→ minorfix→ patchBREAKING CHANGE→ major
- Update the version in
package.json - Generate/update the
CHANGELOG.md - Create and push a git tag in the format:
npm-vX.X.X
- Trigger a new pipeline for packaging and publishing
Publishing to NPM
- The pipeline will prepare the package automatically
- If CI is set to manual deploy, you will need to click the publish job in GitLab
- Once completed, the package will be available on NPM
Important Notes
- Only commits with
(npm-components)scope trigger a release - No release will occur if there are no relevant commits
- Do not manually run
npm run npm-publish - Do not manually create
npm-vtags
Manual Override (Optional)
If needed, a release can still be triggered manually:
git tag npm-v1.2.3
git push origin npm-v1.2.3Package Use In Projects
1. Install the package
npm install @ongov/ontario-search2. Add runtime configuration
The package reads configuration from window.apiConfig and
window.endpointConfig. Add these before the package script loads:
<script type="module">
// Required: Elasticsearch index name used by autosuggest
window.apiConfig = {
elasticsearchUrl: 'elastic-url',
index: 'your-elasticsearch-index-name',
apiKey: 'public-api-key',
}
// Required: search results page endpoints for EN and FR
window.endpointConfig = {
en: '/search/',
fr: '/fr/recherche/',
}
</script>| Config | Required | Description |
| ----------------------------------- | -------- | ------------------------------------------------ |
| window.apiConfig.index | Yes | Elasticsearch index name for autosuggest queries |
| window.apiConfig.elasticsearchUrl | Yes | Ontario ES url (stage/prod) |
| window.apiConfig.apiKey | Yes | ES public api key (stage/prod) |
| window.endpointConfig.en | Yes | English search results page path |
| window.endpointConfig.fr | Yes | French search results page path |
3. Add the mount target element
Create an element with id="ontario-search-autosuggest":
<div id="ontario-search-autosuggest"></div>The package will automatically mount into this element once the DOM is ready. If the element is added dynamically (e.g. by a SPA), the package uses a MutationObserver to detect it.
4. Import the package
<script
type="module"
src="path/to/@ongov/ontario-search/dist/index.js"
></script>Or in a bundled project:
import '@ongov/ontario-search'Exported API
The package exports the following for programmatic use:
import {
mount,
AutosuggestBar,
getSuggestPages,
getSuggestSearches,
} from '@ongov/ontario-search'| Export | Description |
| ---------------------------------------- | ---------------------------------------------------------------------------------------------- |
| mount(elementOrId, props) | Mount the search bar on a specific element (by ID string or DOM node). Returns the React root. |
| AutosuggestBar | The React component (for use within your own React app). Accepts an index prop. |
| getSuggestPages(index, query, lang) | Fetch page suggestions from Elasticsearch. |
| getSuggestSearches(index, query, lang) | Fetch completion suggestions from Elasticsearch. |
| window.__ontarioSearchMount(language?) | Re-mount after DOM changes (e.g. language switch). Available globally after first load. |
Styles & Shadow DOM
The Ontario Design System header renders its content inside a shadow DOM.
Normal page-level <link> or <style> tags cannot penetrate the shadow
boundary, so the autosuggest styles must be injected directly into the shadow
root.
How it works
Before mounting, set window.__ontarioSearchStylesHref to a URL pointing to
the compiled autosuggest stylesheet. At mount time, the package injects a
<link rel="stylesheet"> with that URL into the header's shadow root.
<script type="module">
// Point to the stylesheet from the npm package (adjust path to your setup)
import ontarioSearchStylesHref from '@ongov/ontario-search/styles?url'
window.__ontarioSearchStylesHref = ontarioSearchStylesHref
</script>If you're not using a bundler that supports ?url imports, you can set it to
any publicly accessible URL where the compiled CSS is hosted:
<script>
window.__ontarioSearchStylesHref = '/path/to/ontario-search-styles.css'
</script>Package export
The package exposes the SCSS source via the ./styles export:
import '@ongov/ontario-search/styles' // → dist/styles/index.scssThis is useful if your build pipeline compiles SCSS and you want to include the styles in a global bundle (for pages that don't use shadow DOM).
Summary
| Scenario | What to do |
| ----------------------------- | -------------------------------------------------------------------------------------------------------------- |
| DS header with shadow DOM | Set window.__ontarioSearchStylesHref to a CSS/SCSS URL — it gets injected into the shadow root automatically |
| Standard page (no shadow DOM) | Import @ongov/ontario-search/styles in your SCSS/CSS bundle, or add a <link> to the compiled stylesheet |
Note: If
window.__ontarioSearchStylesHrefis not set, the component will still mount but will be unstyled inside the shadow root, and a warning will appear in the console.
Language Detection
The package automatically detects language from the URL path:
- URLs containing
/fr/→ French - All other URLs → English
You can override this by passing a language to
window.__ontarioSearchMount('fr').
Search History
The package stores recent searches in a cookie (search_history) with the
following behaviour:
- Maximum 7 entries, shown when the input is focused with no text
- Users can remove individual history items
- History is deduplicated by normalized text
- Entries are sanitized (HTML stripped) before storage
Tips
- Contact the Ontario.ca Search team for the correct Elasticsearch index name.
- The package must load after
window.apiConfigandwindow.endpointConfigare set. - It is recommended to nest a simple search bar inside the mount element as a
fallback in case this package does not load. The form should submit to
https://www.ontario.ca/search/?query=QUERY(EN) orhttps://www.ontario.ca/fr/recherche/?query=QUERY(FR). - Make sure mobile search open/close buttons work as expected, including moving focus to the input on button click.
