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

govuk-frontend-html

v1.1.1

Published

govuk-frontend as plain JS functions that return HTML strings

Downloads

31

Readme

govuk-frontend-html

Coverage Status

govuk-frontend-html is a port of govuk-frontend from nunjucks to plain JS functions, with the intention of providing higher performance and more portable implementation which can be used anywhere within node.js and in any templating language that can execute a native JS function.

All of the GOV.UK Design system components have been implemented with a govuk-frontend compatible API, where each function takes the same object parameters as their nunjucks counterpart.

[!IMPORTANT] govuk-frontend-html does not include client side JS or styles, so you will also need to install govuk-frontend.

Installation

npm install govuk-frontend-html

Usage

All GOV.UK Design System components are importable and can be called with an options object to receive an HTML string:

import { govukButton } from 'govuk-frontend-html'

const buttonHtml = govukButton({
  text: 'Start now',
  isStartButton: true
})

console.log(buttonHtml)
// <button type="submit" class="govuk-button govuk-button--start" data-module="govuk-button">
//   Start now
//   <svg class="govuk-button__start-icon" xmlns="http://www.w3.org/2000/svg" width="17.5" height="19" viewBox="0 0 33 40" aria-hidden="true" focusable="false">
//     <path fill="currentColor" d="M0 0h13l20 20-20 20H0l20-20z"/>
//   </svg>
// </button>

Example of a simple fastify server using fastify-html:

import { join } from 'node:path'
import fastifyStatic from '@fastify/static'
import fastify from 'fastify'
import fastifyHtml from 'fastify-html'
import { govukButton, govukTemplate } from 'govuk-frontend-html'

const app = fastify({ logger: true })

app.register(fastifyStatic, {
  root: join(import.meta.dirname, './node_modules/govuk-frontend/dist/govuk/')
})

await app.register(fastifyHtml)

app.addLayout((inner, reply) =>
  govukTemplate({
    assetPath: '/assets',
    headHtml: '<link href="/govuk-frontend.min.css" rel="stylesheet">',
    contentHtml: inner,
    bodyEndHtml: `<script type="module" src="/govuk-frontend.min.js"></script>
  <script type="module">
  import { initAll } from '/govuk-frontend.min.js'
  initAll()
  </script>`
  })
)

app.get('/', async (req, reply) => {
  const startButton = govukButton({
    classes: 'govuk-!-margin-top-2 govuk-!-margin-bottom-8',
    text: 'Start now',
    isStartButton: true
  })

  return reply.html`
    <div class="govuk-grid-row">
      <div class="govuk-grid-column-two-thirds">
        <h1 class="govuk-heading-xl">Apply online for a UK passport</h1>
        <p class="govuk-body">You can apply for, renew, replace or update your passport and pay for it online.</p>
        !${startButton}
      </div>
    </div>
  `
})

await app.listen({ port: 3_000 })

Components

Performance

The performance of each component is significantly faster than the govuk-frontend nunjucks alternatives, for example govukButton():

cpu: Apple M1 Pro
runtime: node v22.3.0 (arm64-darwin)

benchmark            time (avg)             (min … max)       p75       p99      p999
------------------------------------------------------- -----------------------------
• govuk-frontend-html
------------------------------------------------------- -----------------------------
start now link      120 ns/iter       (106 ns … 471 ns)    128 ns    159 ns    249 ns
input             93.48 ns/iter     (81.46 ns … 219 ns)    102 ns    126 ns    161 ns
button              161 ns/iter       (142 ns … 348 ns)    170 ns    210 ns    341 ns
with attributes     249 ns/iter       (223 ns … 352 ns)    255 ns    292 ns    310 ns

• nunjucks
------------------------------------------------------- -----------------------------
start now link    5'505 ns/iter   (4'666 ns … 3'417 µs)  5'083 ns 10'042 ns    118 µs
input             5'051 ns/iter   (4'865 ns … 5'398 ns)  5'122 ns  5'327 ns  5'398 ns
button            5'323 ns/iter   (5'111 ns … 5'593 ns)  5'400 ns  5'574 ns  5'593 ns
with attributes   8'346 ns/iter   (8'059 ns … 8'826 ns)  8'460 ns  8'756 ns  8'826 ns