<script>
  let { sendMessage = async (message) => {} } = $props();

  let value = $state('');
  let disabled = $state(false);

  async function onsubmit(event) {
    event.preventDefault();
    disabled = true;
    try {
      await sendMessage(value);
      event.target.reset();
    } finally {
      disabled = false;
    }
  }

  function onkeydown(event) {
    let modifier = event.shiftKey || event.altKey || event.ctrlKey || event.metaKey;
    if (!modifier && event.key === 'Enter') {
      event.preventDefault();
      event.target.form.requestSubmit();
    }
  }
</script>

<form {onsubmit}>
  <textarea {onkeydown} bind:value {disabled} placeholder="Say something..."></textarea>
  <button type="submit">&raquo;</button>
</form>
