---
const { isOpen } = Astro.props;
---

<div id="mobile-nav" class="off-canvas">
  <div class="off-canvas-content" transition:persist>
    <button class="off-canvas-toggle" data-target="#mobile-nav">
      <svg
        width="40px"
        height="40px"
        viewBox="0 0 1024 1024"
        xmlns="http://www.w3.org/2000/svg"
        fill="#ffffff"
        ><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g
          id="SVGRepo_tracerCarrier"
          stroke-linecap="round"
          stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"
          ><path
            fill="#cad3f5"
            d="M195.2 195.2a64 64 0 0 1 90.496 0L512 421.504 738.304 195.2a64 64 0 0 1 90.496 90.496L602.496 512 828.8 738.304a64 64 0 0 1-90.496 90.496L512 602.496 285.696 828.8a64 64 0 0 1-90.496-90.496L421.504 512 195.2 285.696a64 64 0 0 1 0-90.496z"
          ></path></g
        ></svg
      >
    </button>

    <div class="content">
      <slot />
    </div>
  </div>

  <div class="off-canvas-backdrop off-canvas-toggle" data-target="#mobile-nav">
  </div>
</div>

<style lang="scss">
  .off-canvas {
    .off-canvas-content {
      overflow: hidden;
      position: fixed;
      height: 100vh;
      z-index: 5;

      background-color: var(--prj-bg);

      top: 0;
      right: 0;
      left: 100%;

      padding: var(--prj-spacing-3);

      transition: left 0.4s ease-in-out;
    }

    &.active .off-canvas-content {
      left: 50%;
    }

    .off-canvas-backdrop {
      position: fixed;
      height: 100vh;
      z-index: 4;

      background-color: rgba(0, 0, 0);
      opacity: 0;

      top: 0;
      right: 0;
      left: 100%;

      padding: var(--prj-spacing-3);

      // Delay the left transition on remove so it's desn't appear to be sliding or to be not working
      transition: opacity 0.8s ease, left 0s linear 1s;
    }

    &.active .off-canvas-backdrop {
      left: 0%;
      opacity: 40%;
      transition: opacity 0.8s ease, left 0s linear;
    }
  }

  button.off-canvas-toggle {
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    background: none;
    cursor: pointer;
  }
</style>

<script>
  document.addEventListener('astro:page-load', () => {
    document
      .querySelectorAll<HTMLElement>('.off-canvas-toggle')
      .forEach((btn) =>
        btn.addEventListener('click', () => {
          const { target } = btn.dataset;

          if (!target) return;

          document.querySelector(target)?.classList.toggle('active');
        }),
      );
  });
</script>
