nationalarchives / tna-components

This repo will host all components and it's assets, built with JS & React
https://nationalarchives.gov.uk
MIT License
2 stars 1 forks source link

The National Archives React

The National Archives React components

Jump to:

Automatic deployment to CDN

These components are available on our CDN, for example, at:

Note these important things:

Appendices

Background

In September 2018, front end developers at The National Archives met to identify a mechanism to address two significant factors impacting developer morale:

The agreed approach was to create a 'single source of truth' for UI components which could then be imported as isolated units of functionality into target services as required. This repository is the implementation of that approach.

For more information about this see the:

This is progressive enhancement

Since these are JavaScript components it is vitally important developers recognise this builds upon but does not replace our progressive enhancement approach. The basic functionality required to achieve user goals must always be provided by the HTML of target applications.

An example

We'll use The National Archives' 'global search' as an example to demonstrate this because it is present across many digital services. Since it's original implementation (in around 2014) it has been further developed and we have found that, since each implementation resides within the target application, some of the work has not found its way to all those places where it is implemented. Here is how we'd address this using a shared component.

The basic HTML

Each target environment has HTML introduced that will allow the user to achieve their goal. In this example, that is two links: one to the Discovery homepage and one to our website search homepage.

<div id="global-search">
    <nav>
        <ul>
            <li><a href="http://www.nationalarchives.gov.uk/search/">Search our website</a></li>
            <li><a href="http://discovery.nationalarchives.gov.uk">Search Discovery, our catalogue</a></li>
        </ul>
    </nav>
</div>

Since the destinations for these provide the user with the full range of search functionality we can improve the user experience through progressive enhancement.

Component that can be used across environments

We then develop a JavaScript component that enables the search to be submitted from the source page. This is added to the tna-components library.

A runner for each environment

So as to ensure target applications include only those components they need, we configure an entry point for each target environment in Webpack. These entry points:

In this way we are able to develop and maintain components centrally while allowing each target environments to have control the deployment of scripts to staging environments.

Development machine configuration

Download the repository.

Get the latest files from the repository

  # Download the project on your development machine on your preferred location
  git clone https://github.com/nationalarchives/tna-components.git

Start developing.

Navigate into the project’s directory and start it up.

  cd tna-components
  npm install
  npm start

Optional: Change the development mode to development inside webpack.config.js file and revert back to production when ready to deploy

Open the source code and start editing!

Your site is now running at http://localhost:3000

Testing

Jest - Unit|Itegration|Snapshot(s) testing

Cypress E2E testing

Steps to create a new component

Here's an example commit introducing the code that's needed to create a new component. At a high-level, the steps are:

  1. Preparing the local development server to serve the HTML into which the component will be mounted.
  2. Creating the component
  3. Bumping the relevant version number(s) in webpack.config.js (using Semantic Versioning)
  4. Preparing a runner that will mount the component when specific conditions are met.

Here are the individual steps:

Core technologies

This repository uses a number of technologies, including: