<div class="demo-grid">
  <ray-input label="Email" type="email" name="email" placeholder="you@example.com" description="We never share it." required></ray-input>
  <ray-input label="Search" type="search" placeholder="Find files"><svg slot="left" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg></ray-input>
  <ray-input label="Username" value="ray" error="This username is taken"></ray-input>
  <ray-input label="Disabled" value="read only" disabled></ray-input>
  <ray-input label="Filled variant" variant="filled" placeholder="Filled"></ray-input>
  <ray-input aria-label="No visible label" placeholder="aria-label only" size="lg" radius="full"></ray-input>
</div>

<div class="demo-caption">Highlighted text</div>
<div class="demo-grid">
  <ray-input id="demo-input-vars" label="Request URL" value="https://{{host}}/v1/{{resource}}?key={{api_key}}" description="highlight marks the {{variables}}: defined ones tinted, the undefined api_key in red. Hover one; type {{ for suggestions (complete)."></ray-input>
</div>
<style>
  #demo-input-vars::part(highlight-var) { background: color-mix(in srgb, var(--ray-teal-6) 20%, transparent); }
</style>
<script type="module">
  const defined = { host: "api.example.com", resource: "releases" };
  const vars = document.getElementById("demo-input-vars");
  // After an unclosed {{: the defined names that start with what is typed.
  vars.complete = (text, caret) => {
    const open = text.lastIndexOf("{{", caret);
    const partial = open < 0 ? null : text.slice(open + 2, caret);
    if (partial === null || !/^[\w.-]*$/.test(partial)) return null;
    const items = Object.entries(defined)
      .filter(([name]) => name.startsWith(partial))
      .map(([name, value]) => ({ value: `${name}}}`, label: name, detail: value }));
    return { from: open + 2, to: caret, items };
  };
  vars.highlight = (text) =>
    [...text.matchAll(/\{\{([\w.-]+)\}\}/g)].map((m) => ({
      start: m.index,
      end: m.index + m[0].length,
      kind: m[1] in defined ? "var" : "error",
      title: m[1] in defined ? `${m[1]} = ${defined[m[1]]}` : `${m[1]} is not defined`,
    }));
</script>