<div class="demo-caption">In a form: required, one entry per value</div>
<form class="demo-auth" id="demo-cbg-form" novalidate>
  <ray-checkbox-group id="demo-cbg-channels" name="channels" label="Send me updates by" description="Pick at least one." required value="email">
    <ray-checkbox value="email" label="Email" description="A weekly summary"></ray-checkbox>
    <ray-checkbox value="push" label="Push" description="On this device"></ray-checkbox>
    <ray-checkbox value="sms" label="SMS" description="Only for security alerts"></ray-checkbox>
    <ray-checkbox value="slack" label="Slack" description="Connect a workspace first" disabled></ray-checkbox>
  </ray-checkbox-group>
  <div class="demo-actions"><ray-button type="submit">Save</ray-button><span class="demo-muted" id="demo-cbg-out"></span></div>
</form>

<div class="demo-caption">max: up to three</div>
<ray-checkbox-group label="Interests" orientation="horizontal" max="3" color="violet" value="components,tokens">
  <ray-checkbox value="components" label="Components"></ray-checkbox>
  <ray-checkbox value="tokens" label="Design tokens"></ray-checkbox>
  <ray-checkbox value="a11y" label="Accessibility"></ray-checkbox>
  <ray-checkbox value="charts" label="Charts"></ray-checkbox>
  <ray-checkbox value="mobile" label="Mobile"></ray-checkbox>
  <ray-checkbox value="raylang" label="raylang"></ray-checkbox>
</ray-checkbox-group>

<div class="demo-caption">Select all (indeterminate)</div>
<div class="demo-cbg-all">
  <ray-checkbox id="demo-cbg-all" label="All environments"></ray-checkbox>
  <ray-checkbox-group id="demo-cbg-envs" aria-label="Environments" value="staging" class="demo-cbg-nested">
    <ray-checkbox value="dev" label="Development"></ray-checkbox>
    <ray-checkbox value="staging" label="Staging"></ray-checkbox>
    <ray-checkbox value="prod" label="Production"></ray-checkbox>
  </ray-checkbox-group>
</div>

<div class="demo-caption">Sizes, colors and disabled</div>
<div class="demo-grid">
  <ray-checkbox-group label="Small, teal" size="xs" color="teal" value="a"><ray-checkbox value="a" label="Alpha"></ray-checkbox><ray-checkbox value="b" label="Beta"></ray-checkbox></ray-checkbox-group>
  <ray-checkbox-group label="Large, orange" size="md" color="orange" value="b"><ray-checkbox value="a" label="Alpha"></ray-checkbox><ray-checkbox value="b" label="Beta"></ray-checkbox></ray-checkbox-group>
  <ray-checkbox-group label="Disabled" disabled value="a"><ray-checkbox value="a" label="Alpha"></ray-checkbox><ray-checkbox value="b" label="Beta"></ray-checkbox></ray-checkbox-group>
  <ray-checkbox-group label="With an error" error="Choose a plan feature." description-position="below" description="Billed monthly."><ray-checkbox value="a" label="Backups"></ray-checkbox><ray-checkbox value="b" label="Analytics"></ray-checkbox></ray-checkbox-group>
</div>

<script type="module">
  // Required: an error on submit until one is checked; the form data has one entry per value.
  const form = document.getElementById("demo-cbg-form");
  const channels = document.getElementById("demo-cbg-channels");
  form.addEventListener("submit", (e) => {
    e.preventDefault();
    if (!channels.value.length) {
      channels.error = "Pick at least one channel.";
      document.getElementById("demo-cbg-out").textContent = "";
      return;
    }
    document.getElementById("demo-cbg-out").textContent = `channels = ${new FormData(form).getAll("channels").join(", ")}`;
  });
  channels.addEventListener("change", () => { channels.error = ""; });

  // Select all: checked, unchecked or indeterminate from the group.
  const all = document.getElementById("demo-cbg-all");
  const envs = document.getElementById("demo-cbg-envs");
  const values = ["dev", "staging", "prod"];
  const sync = () => {
    const n = envs.value.length;
    all.checked = n === values.length;
    all.indeterminate = n > 0 && n < values.length;
  };
  all.addEventListener("change", () => { envs.value = all.checked ? values.slice() : []; sync(); });
  envs.addEventListener("change", sync);
  customElements.whenDefined("ray-checkbox-group").then(() => queueMicrotask(sync));
</script>