Skip to content

HTML Web Component

A framework-agnostic HTML Web Component (custom element) for Philippine barangay autocomplete search, powered by the GIS.PH API.

You can use it in plain HTML, React, Vue, Svelte, Angular, or any client-side environment that supports custom elements.

GitHub Repository: YahaayLabs/barangay-search

Install via your preferred package manager:

Terminal window
# Using npm
npm install barangay-search
# Using bun
bun add barangay-search
# Using pnpm
pnpm add barangay-search

Import the component as an ES module and add the <barangay-search> tag:

<script type="module" src="./node_modules/barangay-search/dist/barangay-search.js"></script>
<barangay-search api-key="YOUR_GIS_PH_API_KEY" placeholder="e.g. Poblacion Batangas"></barangay-search>
<script type="module">
document.querySelector('barangay-search')
.addEventListener('select', (e) => {
console.log('Selected Barangay:', e.detail);
});
</script>

You can configure the component using either HTML attributes or JavaScript properties:

PropertyAttributeTypeDefaultDescription
apiKeyapi-keystringYour GIS.PH API Key (gis_sk_...). Sent in Authorization header.
accessTokenaccess-tokenstringAlternative Bearer token.
placeholderplaceholderstringSearch barangay...Input field placeholder text.
disableddisabledbooleanfalseDisable the input control.
clearableclearablebooleantrueShow a clear button (×) when a barangay is selected.
debounceMsdebounce-msnumber300Search input debounce time in milliseconds.
minQueryLengthmin-query-lengthnumber2Minimum character length before triggering search query.
valueobject | nullnullProperty only. The currently selected barangay object.

Custom events dispatched by the component bubble up and compose out of the Shadow DOM:

Eventdetail PayloadDescription
selectBarangay objectDispatched when the user selects a barangay.
clearnullDispatched when the selection is cleared.
error{ message: string }Dispatched when an API or network error occurs.

You can call the following methods directly on the element instance:

  • clear() — Clears the selected barangay and search input query.
  • focus() — Focuses the inner input element.
  • blur() — Blurs the inner input element.

The component uses Shadow DOM for style encapsulation, but can be themed using CSS Custom Properties (Variables):

barangay-search {
--barangay-search-border: #e2e8f0;
--barangay-search-radius: 8px;
--barangay-search-focus: #3b82f6;
--barangay-search-selected-bg: #f0fdf4;
}

You can style specific parts of the component using the ::part() pseudo-element:

  • ::part(input) — The text search input.
  • ::part(list) — The floating results dropdown list.
  • ::part(option) — The list option items.
  • ::part(empty) — The “No results found” container.
  • ::part(error) — The API error message container.
  • ::part(clear) — The clear selection button.