Skip to content

withPopup(form, options?)

The withPopup() function provides floating, accessible error popups for a form. Popups update dynamically on scroll, resize, and during form and field validation events.

📥 Import

js
import { withPopup } from 'suriform/tools'

▶️ Usage

js
const form = document.querySelector('#signup')
const popup = withPopup(form, {
  displaySingle: false
})

popup.addError(form.username, 'Username is required!')
popup.removeError(form.username)

popup.addErrors([
  { field: form.email, message: 'Invalid email address' },
  { field: form.password, message: 'Password is too short' }
])

⚙️ Options

OptionTypeDefaultDescription
displaySinglebooleantrueIf true, only the first invalid field displays a popup. Otherwise, all invalid fields show popups.

🧾 API

withPopup(form, options?)

ParameterTypeRequiredDescription
formHTMLFormElementYesThe target form element.
optionsobjectNoPopup configuration options.

⚠️ Throws an Error if the argument is not a valid <form> element.

↩️ Returns

An object exposing popup control methods:

MethodDescription
addError(field, message)Show a popup message for a specific field.
removeError(field)Hide the popup for a specific field.
addErrors(errorsArray)Show multiple popups from an array of { field, message }.
removeErrors([exceptField])Hide all popups, optionally leaving one visible.

Released under the MIT License.