Form
A structured form container for Monster controls, validation flows and consistent submission handling.
import { Form } from "@schukai/monster/source/components/form/form.mjs";Introduction
The Monster Form component is the outer structure for grouped controls, validation flow and submission behavior. Use it when multiple Monster inputs should behave like one coherent form instead of as isolated controls.
When to use Form
- Use it for structured data entry: Registration, checkout, settings and admin forms are typical cases.
- Use it when validation and submission belong together: The component helps keep one clear workflow boundary.
- Do not use it as a generic wrapper: If there is no submission or state change, a plain layout container may be enough.
Typical mistakes
Keep submission intent explicit. Users should understand what the form changes, when it validates and what happens after success or failure.
Form Simple
This example wraps two native inputs in monster-form and mirrors the current values into a small JSON preview.
Waiting for input…
Waiting for input…
Javascript
import "@schukai/monster/source/components/form/form.mjs";
import "@schukai/monster/source/components/form/field-set.mjs";
const form = document.getElementById("form-demo");
const output = document.getElementById("form-demo-output");
const render = () => {
const data = Object.fromEntries(new FormData(form).entries());
output.textContent = JSON.stringify(data, null, 2);
};
form.addEventListener("input", render);
render();<script type="module">import "@schukai/monster/source/components/form/form.mjs";
import "@schukai/monster/source/components/form/field-set.mjs";
const form = document.getElementById("form-demo-run");
const output = document.getElementById("form-demo-run-output");
const render = () => {
const data = Object.fromEntries(new FormData(form).entries());
output.textContent = JSON.stringify(data, null, 2);
};
form.addEventListener("input", render);
render();</script>HTML
<div style="display:grid;gap:1rem;">
<monster-form id="form-demo">
<monster-field-set data-monster-option-labels-title="Profile">
<label for="form-demo-name">Name</label>
<input id="form-demo-name" type="text" name="name" value="Jane Doe" />
<label for="form-demo-email">Email</label>
<input
id="form-demo-email"
type="email"
name="email"
value="jane@example.com"
/>
</monster-field-set>
</monster-form>
<pre
id="form-demo-output"
style="margin:0;padding:0.75rem;border:1px solid var(--monster-color-primary-2);border-radius:var(--monster-border-radius);background:var(--monster-bg-color-primary-1);color:var(--monster-color-primary-1);"
>Waiting for input…</pre>
</div>Stylesheet
/** no additional stylesheet is defined **/Component Design
The monster-form component is a container for grouping form elements. Built using the Shadow DOM, it encapsulates the form's structure and styling, ensuring isolation and consistency. Developers can use this component to wrap form controls while maintaining control over styling and layout.
Shadow DOM and Accessibility
The use of the Shadow DOM ensures that the internal form structure is isolated from the global styles of the application. This encapsulation prevents unintended interference from external CSS or scripts while providing a predictable layout for the form.
The component follows accessibility standards by allowing developers to use native form elements such as input, select, and textarea inside the form. It also supports reportValidity for validating child form elements.
Customizing Through Exported Parts
The monster-form component exposes internal parts for styling using the part attribute. This allows targeted CSS customization of the form container and its related elements.
Available Part Attributes
control: Represents the main control wrapper around the form.form: Targets the internal form element, allowing you to apply custom styles to the form layout.
Below is an example of how to customize the form using CSS:
monster-form::part(form) {
border: 1px solid #ccc;
padding: 20px;
border-radius: 6px;
background-color: #fafafa;
}
monster-form::part(control) {
margin: 10px 0;
display: flex;
flex-direction: column;
}
Explanation of the Example
monster-form::part(form): Styles the internal form element with padding, a border, and a light background.monster-form::part(control): Adds spacing and organizes the main form container into a column layout.
Accessibility
The monster-form component is designed to ensure accessibility for all users:
- Native form elements retain their default behaviors and semantics for screen readers.
- Keyboard navigation works seamlessly within the form container.
- The
reportValiditymethod allows developers to validate child elements programmatically, ensuring proper error feedback.
By providing a well-structured form container and supporting accessible form controls, the monster-form component ensures an inclusive user experience.
HTML Structure
<monster-form></monster-form>JavaScript Initialization
const element = document.createElement('monster-form');
document.body.appendChild(element);Exported
FormDerived from
DataSetOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the DataSet.
- since
- deprecated
Properties and Attributes
The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the DataSet.
data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.data-monster-option-[name]: Sets the value of the configuration option[name]for the collapse component when used as an HTML attribute.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the DataSet.
Behavioral methods
refresh()3.70.0- {Promise}
Static methods
getCSSStyleSheet()- {CSSStyleSheet[]}
getTag()- {string}
Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()Other methods
reportValidity()2.10.0- {boolean}
Events
The component emits the following events:
monster-options-setmonster-selectedmonster-changemonster-changed
For more information on how to handle events, see the mdn documentation.