> For the complete documentation index, see [llms.txt](https://atomico.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://atomico.gitbook.io/doc/packages/deprecated/atomico-magic-form.md).

# @atomico/magic-form

Improves the form development experience thanks to the use of webcomponents to centralize submission

{% embed url="<https://github.com/atomicojs/magic-form>" %}

A powerful form submission manager created with Atomicojs webcomponents, with MagicForm you can:

1. Simplify the sending of forms by centralizing the sending actions through a provider.
2. Know at all times the forms that are being provided and show their status.
3. Isolate a group of actions according to provider, this means that if a provider does not register the action it will bubble to the parent provider.

### Installation and import

{% tabs %}
{% tab title="Instalation" %}

```
npm install @atomico/magic-form
```

{% endtab %}

{% tab title="Module webcomponents" %}

```javascript
import { 
    MagicForm,
    MagicFormProvider 
} from "@atomico/magic-form";
```

{% endtab %}

{% tab title="Module hooks" %}

```javascript
import { 
    useMagicForm, 
    useMagicFormProvider 
} from "@atomico/magic-form/hooks";
```

{% endtab %}
{% endtabs %}

### Usage

{% tabs %}
{% tab title="HTML" %}

```html
<magic-form-provider>
  <magic-form>
    <form action="user">
      <input type="text" name="name" />
      <input type="text" name="email" />
      <button>Create user</button>
    </form>
  </magic-form>
</magic-form-provider>
<script>
  document.querySelector("magic-form-provider").actions = {
    async add(form) {
      return fetch("./my-api", {
        method: "post",
        body: new FormData(form),
      }).then((res) => res.json());
    },
  };
</script>
```

{% endtab %}

{% tab title="JSX Atomico" %}

```jsx
import { MagicForm, MagicFormProvider } from "@atomico/magic-form";

function component() {
  return (
    <host>
      <MagicFormProvider 
        actions={{
          async add(form) {
            return fetch("./my-api", {
              method: "post",
              body: new FormData(form),
            }).then((res) => res.json());
          },
        }}
      >
        <MagicForm>
          <form action="user">
            <input type="text" name="name" />
            <input type="text" name="email" />
            <button>Create user</button>
          </form>
        </MagicForm>
      </MagicFormProvider>
    </host>
  );
}
```

{% endtab %}
{% endtabs %}

`magic-form-provider` captures all the forms nested in `magic-form` when executing the submit event by the form and distributes them according to the definition of the action attribute to each method of the actions object

### API

{% content-ref url="/pages/KYZbYvKI4lfGbTGWEuuL" %}
[MagicFormProvider | \<magic-form-provider>](/doc/packages/deprecated/atomico-magic-form/magicformprovider-or-less-than-magic-form-provider-greater-than.md)
{% endcontent-ref %}

{% content-ref url="/pages/u9vM1hAIcBUtsCXWg8VW" %}
[MagicForm | \<magic-form>](/doc/packages/deprecated/atomico-magic-form/magicform-or-less-than-magic-form-greater-than.md)
{% endcontent-ref %}

{% content-ref url="/pages/atlZQ3k0bngJoP66TE6x" %}
[MagicForm Hooks](/doc/packages/deprecated/atomico-magic-form/magicform-hooks.md)
{% endcontent-ref %}

{% content-ref url="/pages/sMmkCDKXTFDi5bxqrRXm" %}
[MagicForm in React and Preact](/doc/packages/deprecated/atomico-magic-form/magicform-in-react-and-preact.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://atomico.gitbook.io/doc/packages/deprecated/atomico-magic-form.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
