CRISTICODES / Javascript / Banner cookie-consent minimal

HTML:

<div id="miniCookie" role="dialog" aria-live="polite" aria-label="Cookie consent" data-delay="3000">
  <div class="casetaCookie">
    <div style="flex:1; min-width:0">
      <strong>🍪 Folosim cookie-uri</strong>
      <div style="color:#000; font-size:13px; margin-top:4px; white-space:normal; line-height: var(--text-size); opacity: 0.8;">
        Folosim cookie-uri pentru a îmbunătăți experiența pe site, pentru statistici și pentru notificări. Alege ce accepți sau citește 
        <a href="https://www.artamasonica.ro/utilizare-cookie-uri" style="color: #000; text-decoration: underline;">Politica de cookie</a>.
      </div>
    </div>
    <div class="actions" style="display:flex; gap:8px; margin-left:8px; flex-shrink:0">
      <button class="decline" id="miniDecline">Refuz</button>
      <button class="accept" id="miniAccept">Accept</button>
    </div>
  </div>
</div>

CSS:

<style>
#miniCookie {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 320px;
    background: #ffc73f;
    box-shadow: 0 6px 18px rgba(0,0,0,0.12);
    padding: 16px;
    display: flex; /* nu ascuns! */
    flex-direction: column;
    gap: 12px;
    font-family: Arial, sans-serif;
    font-size: 14px;
    color: #000;
    z-index: 99999;

    /* implicit: bannerul e vizibil DAR invizibil prin opacitate */
    opacity: 0;
    transform: translateY(10px);
    transition: opacity .3s ease, transform .3s ease;
}

#miniCookie.hidden {
    display: none !important; /* ascundem doar dupa accept/decline */
}

#miniCookie.show {
    opacity: 1;
    transform: translateY(0);
}

  #miniCookie:hover {
    box-shadow: 0 10px 22px rgba(0,0,0,0.18);
  }

  .casetaCookie {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  /* Butoane */
  #miniCookie button {
    cursor: pointer;
    padding: 8px 16px;
    border: 0;
    font-weight: 600;
    font-size: 14px;
    flex: 1;
    transition: background .2s ease;
  }

  /* Accept */
  #miniCookie .accept {
    background: #0c2e7e;
    color: #fff;
  }
  #miniCookie .accept:hover {
    background: #059287;
  }

  /* Decline */
  #miniCookie .decline {
    background: #f3f3f3;
    border: 1px solid #ddd;
    color: #333;
  }
  #miniCookie .decline:hover {
    background: #e6e6e6;
  }

  /* Butoane orizontale */
  #miniCookie .actions {
    display: flex;
    gap: 8px;
    margin-top: 12px;
  }

  @media (max-width: 520px) {
    #miniCookie {
      width: 90%;
      right: 5%;
      bottom: 15px;
    }
    #miniCookie .actions {
      flex-direction: column;
    }
  }
</style>

Javascript:

<script>
 (function(){
    const KEY = "mini_cookie_ok_v1";

    /* Detectare reală localStorage în Brave */
    function canUseLocalStorage() {
        try {
            const x = "__test_ls__";
            localStorage.setItem(x, "1");
            localStorage.removeItem(x);
            return true;
        } catch(e) {
            return false;
        }
    }

    const useLS = canUseLocalStorage();

    function getValue() {
        if (useLS) {
            return localStorage.getItem(KEY);
        } else {
            const m = document.cookie.match(new RegExp('(?:^| )' + KEY + '=([^;]+)'));
            return m ? m[1] : null;
        }
    }

    function setValue(v) {
        if (useLS) {
            localStorage.setItem(KEY, v);
        } else {
            document.cookie = KEY + "=" + v + ";path=/;max-age=31536000";
        }
    }

    function hideBanner() {
        const el = document.getElementById("miniCookie");
        if (el) el.classList.add("hidden");
    }

    function showBanner() {
        const el = document.getElementById("miniCookie");
        if (!el) return;

        el.classList.add("show");
    }

    function init() {
        const el = document.getElementById("miniCookie");
        const a = document.getElementById("miniAccept");
        const d = document.getElementById("miniDecline");

        if (!el) return;

        if (getValue() === "yes" || getValue() === "no") {
            hideBanner();
            return;
        }

        a.onclick = () => { setValue("yes"); hideBanner(); };
        d.onclick = () => { setValue("no"); hideBanner(); };

        const delay = parseInt(el.getAttribute("data-delay")) || 3000;
        setTimeout(showBanner, delay);
    }

    if (document.readyState === "loading")
        document.addEventListener("DOMContentLoaded", init);
    else
        init();
})();
</script>