This library contains custom components based on the GovUK frontend.
We use Storybook.js to render the components in various states and test for interactions and accessibility.
The project is divided into different directories.
Read documentation on how to configure storybook.
.storybook/main.js
contains some custom config so that we can...
- use nunjucks for our templating
- additionally load the Govuk base sass file into all our sass modules
This contains helper classes for unit tests.
This contains three global files.
_overrides.scss
This overrides the font as we can't use the GovUK fontall.scss
This imports the govuk frontend sass file, the _overrides sass file and the all components sass file.govuk.scss
This imports only the govuk frontend sass, which is compiled for use within Storybookspreview-head.html
index.ts
This imports all the js classes defined in the components directories.
This contains one directory per component and an _all.scss
which imports the sass files from each component.
This has the following structure.
_index.scss
This contains all the custom sass needed for the component.
<component-name>.stories.ts
- Using the Component Story Format to define a different UI state perexport
. See Writing stories in Storybook documentation.
macro.njk
This generates the html for the examples app but can also be used by consumers of the library to generate html.template.njk
This is used by the example app to dynamically load based on the component name.story.njk
This is used by storybook. The only difference between this andmacro.njk
is that this file calls the macro immediately.
<component-name>.ts
This is the typescript for the component. It will be packaged and published to npm.
<component-name>.stories.ts
These are Storybook CSF files which render the UI in varying states. They can be used to test interactive parts of the component.<component-name>.unit.test.ts
These are unit tests for the<component-name>.ts
file.
<component-name>.yaml
(optional)
This example data can be consumed by stories to render different UI states.
export interface ComponentData {
name: string;
params: ComponentDataParams;
examples: ComponentDataExamples[];
}
interface ComponentDataExamples {
name: string;
description: string;
data: any;
}
The example data is any
because this data is specific to the component you are rendering.
You need to create a directory with a name in kebab case which describes the component. For example, card
or navigation-text
This contains the nunjucks template for the component.
Given a component directory called component-name
The template would look like:
{% macro tdrComponentName(params) %}
{%- include "./template.njk" -%}
{% endmacro %}
Given a component directory called component-name
The template would look like:
{% macro tdrComponentName(params) %}
{%- include "./template.njk" -%}
{% endmacro %}
tdrComponentName( params )
Create this file based on this description
This contains all the sass needed for the component. You can use existing govuk classes without redefining them here.
The typescript for the component
Unit tests for the typescript
You can build your components locally by running:
npm run start-storybook
npm run test
This will run unit tests using Jest.
npm run test-storybook
This runs accessibility and interaction tests on the cli. You need to have an instance of storybook running locally (npm run storybook
) to be able to use the storybook CLI test runner.
There is one webpack file for creating the files for the npm package.
webpack.config.js
is used during the npm run build
command which generates js
and d.ts
files from the typescript in the src/nationalarchives/**
directories.
There is also webpack config inside the .storybook/main.js
that loads the nunjucks, yaml and css/sass.