npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nationalarchives/frontend-cookie-banner

v0.1.59

Published

The National Archives frontend cookie banner

Downloads

314

Readme

TNA Frontend Cookie Banner

Latest release NPM version Licence

Use the cookie banner from TNA Frontend in your service.

HTML

<section class="tna-cookie-banner" data-module="tna-cookie-banner" data-policies="" data-preferenceskey="" data-policieskey="" data-domain="myservice.nationalarchives.gov.uk" data-path="/" data-insecure="false" aria-label="Cookies on my service" hidden>
  <div class="tna-container">
    <div class="tna-column tna-column--full tna-cookie-banner__message tna-cookie-banner__message--prompt">
      <h2 class="tna-heading-m">This website uses cookies</h2>
      <p>We use some essential cookies to make this service work.</p>
      <p>We'd also like to use analytics cookies so we can understand how you use the service and make improvements.</p>
      <div class="tna-button-group">
        <button class="tna-button" type="button" value="accept">
          Accept cookies
        </button>
        <button class="tna-button" type="button" value="reject">
          Reject cookies
        </button>
        <a href="/cookies" class="tna-button tna-button--plain">
          Set cookie preferences
        </a>
      </div>
    </div>
    <div class="tna-column tna-column--full tna-cookie-banner__message tna-cookie-banner__message--accepted" tabindex="0" hidden>
      <p>
        You have accepted optional cookies. You can change your cookie settings on the <a href="/cookies">Cookies page</a>.
      </p>
      <div class="tna-button-group">
        <button class="tna-button" type="button" value="close">
          Close this message
        </button>
      </div>
    </div>
    <div class="tna-column tna-column--full tna-cookie-banner__message tna-cookie-banner__message--rejected" tabindex="0" hidden>
      <p>
        You have rejected optional cookies. You can change your cookie settings on the <a href="/cookies">Cookies page</a>.
      </p>
      <div class="tna-button-group">
        <button class="tna-button" type="button" value="close">
          Close this message
        </button>
      </div>
    </div>
  </div>
</section>

Attributes

| Attribute | Purpose | Default | | --------------------- | ------------------------------------------------------------------------- | ------------------------ | | data-policies | Extra cookie policies in addition to "essential", "settings" and "usage" | [none] | | data-preferenceskey | The cookie name to state that the user preferences have already been set | cookie_preferences_set | | data-policieskey | The cookie name for the policy preferences | cookies_policy | | data-domain | The domain to save cookies for | [The current domain] | | data-path | The path that cookies are available within | / | | data-insecure | Allow cookies to be sent in HTTP environments (designed for testing only) | false |

JavaScript

Initialise the component

Include the JavaScript in your service. This should add a TNAFrontend object to your window. You can then initialise the component:

const $cookieBanner = document.querySelector(
  '[data-module="tna-cookie-banner"]',
);

if ($cookieBanner) {
  new TNAFrontend.CookieBanner($cookieBanner);
}

Work with cookies using the Cookies class

import Cookies from "@nationalarchives/frontend/nationalarchives/lib/cookies.mjs";

const cookies = new Cookies();

if (cookies.isPolicyAccepted("usage")) {
  // Add tracking code
}

Use cookie events

import Cookies from "@nationalarchives/frontend/nationalarchives/lib/cookies.mjs";

const cookies = new Cookies();

cookies.on("acceptPolicy", function(policy) {
  if (policy === "usage") {
    console.log("Usage cookies are permitted");
    // Add tracking code
  }
});

cookies.on("rejectPolicy", function(policy) {
  if (policy === "usage") {
    console.log("Usage cookies have been rejected");
    // Remove tracking code
  }
});

| Event | Trigger | | ------------------- | ------------------------------------------------------------------------------------------- | | setCookie | Any time a cookie is changed in the browser using the Cookie class | | deleteCookie | When any cookie is deleted using delete() | | deleteAllCookies | When all cookies are deleted using deleteAll() | | acceptPolicy | When any policy is accepted using acceptPolicy() | | acceptAllPolicies | When all policies are accepted using acceptAllPolicies() | | rejectPolicy | When any policy is rejected using rejectPolicy() | | rejectAllPolicies | When all policies are rejected using rejectAllPolicies() | | changePolicy | When any policy is changed using either acceptPolicy(), rejectPolicy() or setPolicy() |