Documentation · reference

Framework integration

React, Next.js, Vue, Angular and state-sync patterns.

Framework integration

The library owns the visible chrome but never the form state: native inputs remain the source of truth. In component frameworks that means:

  1. initialise after the DOM exists (mounted / hydrated),
  2. push external state down with setValue() (and setIndeterminate()),
  3. read user changes from onChange / zidan:change,
  4. destroy() before the nodes disappear.

Do not let the framework re-render the same input element with different data-zidan* attributes; recreate the field instead.

React 18

import { useEffect, useRef, useState } from 'react';
import { ZidanForm, ZidanSelect, initZidanForms, initZidanSelects } from 'zidan-select';
import 'zidan-select/css/core';
import 'zidan-select/css/theme';

export default function ProfileForm() {
  const host = useRef(null);
  const [plan, setPlan] = useState('pro');

  useEffect(() => {
    initZidanSelects(host.current);
    initZidanForms(host.current, {
      onChange: ({ instance, value }) => {
        if (instance.kind === 'radio-group') setPlan(value);
      }
    });

    return () => {
      ZidanForm.fields
        .filter(f => host.current?.contains(f.el))
        .forEach(f => f.destroy());
      ZidanSelect.instances
        .filter(s => host.current?.contains(s.el))
        .forEach(s => s.destroy());
    };
  }, []);

  useEffect(() => {
    const radios = ZidanForm.fields.find(f => f.kind === 'radio-group');
    if (radios && radios.getValue() !== plan) radios.setValue(plan);
  }, [plan]);

  return (
    <form ref={host}>
      <input name="email" data-zidan="input" data-zidan-label="Email" required />
      <fieldset data-zidan="radio-group" data-zidan-variant="inline">
        <legend>Plan</legend>
        <label><input type="radio" name="plan" value="pro" defaultChecked /> Pro</label>
        <label><input type="radio" name="plan" value="basic" /> Basic</label>
      </fieldset>
    </form>
  );
}

React re-renders can be blocked by key changes: when the markup changes structurally, destroy in the effect cleanup and recreate in the next effect (the cleanup above runs before the DOM is removed).

Next.js (App Router)

// app/layout.js
import 'zidan-select/css/core';
import 'zidan-select/css/theme';
'use client';
import { useEffect, useRef } from 'react';
import { ZidanForm, ZidanSelect, initZidanForms, initZidanSelects } from 'zidan-select';

export default function CheckoutForm() {
  const ref = useRef(null);

  useEffect(() => {
    initZidanSelects(ref.current);
    initZidanForms(ref.current);
    return () => {
      ZidanForm.fields.filter(f => ref.current?.contains(f.el)).forEach(f => f.destroy());
      ZidanSelect.instances.filter(s => ref.current?.contains(s.el)).forEach(s => s.destroy());
    };
  }, []);

  return <form ref={ref}>{/* data-zidan markup */}</form>;
}

If you need the library only in the browser:

useEffect(() => {
  let api;
  import('zidan-select').then(mod => {
    api = mod;
    mod.initZidanForms(ref.current);
  });
  return () => api?.ZidanForm.destroyAll();
}, []);

Pages Router works the same; keep the form in a client component and avoid importing CSS inside a server-only module.

Vue 3

<script setup>
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { ZidanForm, ZidanSelect, initZidanForms, initZidanSelects } from 'zidan-select';
import 'zidan-select/css/core';
import 'zidan-select/css/theme';

const host = ref(null);
const model = ref('France');

onMounted(() => {
  initZidanSelects(host.value);
  initZidanForms(host.value);
  const country = host.value.querySelector('select[name="country"]');
  country.addEventListener('zidan:change', event => {
    model.value = event.detail.value;
  });
});

watch(model, value => {
  const country = host.value?.querySelector('select[name="country"]');
  const field = ZidanSelect.instances.find(instance => instance.el === country);
  field?.setValue(value);
});

onBeforeUnmount(() => {
  ZidanForm.fields
    .filter(field => host.value?.contains(field.el))
    .forEach(field => field.destroy());
  ZidanSelect.instances
    .filter(instance => host.value?.contains(instance.el))
    .forEach(instance => instance.destroy());
});
</script>

<template>
  <form ref="host">
    <input name="email" data-zidan="input" data-zidan-label="Email" />
    <select name="country" data-zidan-select="combobox" data-zidan-label="Country">
      <option>France</option>
    </select>
  </form>
</template>

The model ref above demonstrates two-way binding: the native zidan:change event updates Vue state, and watch() pushes external changes back through the select instance.

Angular

import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
import { ZidanForm, ZidanSelect, initZidanForms, initZidanSelects } from 'zidan-select';

@Component({ selector: 'app-profile', templateUrl: './profile.component.html' })
export class ProfileComponent implements AfterViewInit, OnDestroy {
  @ViewChild('host', { static: true }) host!: ElementRef<HTMLFormElement>;

  ngAfterViewInit() {
    initZidanSelects(this.host.nativeElement);
    initZidanForms(this.host.nativeElement);
  }

  ngOnDestroy() {
    ZidanForm.fields
      .filter(field => this.host.nativeElement.contains(field.el))
      .forEach(field => field.destroy());
    ZidanSelect.instances
      .filter(instance => this.host.nativeElement.contains(instance.el))
      .forEach(instance => instance.destroy());
  }
}

Angular strips unknown attributes from templates only when using property binding; plain data-zidan attributes render normally. With reactive forms, the native control still carries the value, so FormControl continues to receive input/change events. For a custom ControlValueAccessor, call field.setValue(value) in writeValue() and onChange from the field's onChange callback.

CSS frameworks

Set the adapter once — on the form, a wrapper or <html>:

<form data-zs-framework="bootstrap" data-zs-theme="default">
AdapterWhat changes
none / vanillaPlugin's own tokens.
bootstrapMaps --bs-* colours/radius, .form-control/.form-select sizing, input-group corners, is-valid/is-invalid.
tailwindSlate/indigo palette, 0.5rem radius, tighter shadows.
bulma--bulma-* palette, 4px radius, Bulma shadow.
foundationSquare corners, Foundation blue, #cacaca borders.

Bootstrap 5 example:

<div class="input-group">
  <span class="input-group-text">@</span>
  <select class="form-select" data-zidan-select="combobox" data-zs-framework="bootstrap">…</select>
</div>

Tailwind users can still pass utility strings through the select's customClasses:

zidanSelect('#tags', {
  customClasses: { control: 'shadow-sm ring-1 ring-slate-200', root: 'max-w-md' }
});

State-sync rules of thumb

NeedAPI
External value → controlsetValue() (select/field/radio/range), stepUp() / stepDown() (stepper), setPhoneCountry() / setPhoneFormat(), setIndeterminate(), clear() (file)
Control → stateonChange callback or zidan:change event
File upload progresszidan:upload-* events or getUploadState()
Re-render with same instancesupdate() / ZidanForm.refresh(scope)
New nodes from a list re-renderZidanForm.observe(scope) — additions/removals inside the scope; call destroy() first when removing the scope itself
Unmountdestroy(), or ZidanForm.destroyAll() for a page
Programmatic validation`setValidation('valid' \'invalid' \null)`

Markdown source: docs/frameworks.md. Generated by scripts/docs.mjs — edit the Markdown, not this page.