@hmlr/govuk-react-components-library v1.0.1
GOVUK React Components Library
This project is a collection of React components built based on GDS Specifications. The components are designed to be reused by applications, with a focus on providing consistent and accessible user experiences. The components are based on the govuk-frontend library, ensuring compatibility and adherence to the UK Government's digital design standards.
Disclaimer
:warning: This project is not officially supported by the Government Digital Service (GDS). It is maintained on a best-efforts basis by the HM Land Registry (HMLR) only. While we strive to ensure the accuracy and reliability of the code, we cannot guarantee its full compliance with GDS standards or the availability of ongoing support.
Installation
Then install this component library:
npm install @hmlr/govuk-react-components-libraryComponents
Note that any component with a - *** below means that - This component may javascript to be enabled because of the transitions present in the components using createAll or initAll recommended by govuk-frontend documentation
The Components includes:
- Accordion
- *** - Boolean
- Breadcrumbs
- Button
- *** - CharacterCount
- CheckBoxes
- *** - CookieBanner
- DateInput
- Details
- ErrorMessage
- ErrorSummary
- *** - Fieldset
- FileUpload
- Footer
- Header
- *** - Hint
- Input
- InsetText
- Label
- NotificationBanner
- Pagination
- Panel
- PhaseBanner
- Radios
- *** - Select
- ServiceNavigation
- SkipLink
- *** - SummaryList
- Table
- Tabs
- *** - Tag
- TaskList
- Textarea
- WarningText
There are Also Several components that can be used for error processing and other functionality like dashboard display:
Note that some of the components below with - *** - Means like the above components, javascript needs to be enabled.
- CardColumn
- CardLayout
- DataNavigation
- DifferenceNavigation - Read Documentation to see usage
- ErrorBoundary
- Landing
- Loading
- Main
- NotFoundPage
- PDFViewer
- PDFViewerCanvas - Read Documentation to see usage
- ProblemWithService
- ***-createAll(ErrorSummary..)orConfigureOverallErrorSummary() - WarningInfo
There are also some useful components :
There are some convenience functions that can be used to configure components with - *** mentioned above.
- ExtractAccordionConfigFromAttributes - Extracts Accordion configurations from fixtures attributes e.g.
showAllSectionsTexttranslates toshowAllSectionsinAccordionConfig. - ConfigureOverallAccordion - Sets overall behavior and configurations for all accordions or in a scope (document or specified element) as per govuk-frontend accordion api reference.
- ConfigureOverallButton - Sets overall behavior and configurations for all button or in a scope (document or specified element) as per govuk-frontend button api reference.
- ConfigureOverallCheckboxes - Sets overall behavior and configurations for all checkboxes or in a scope.
- ConfigureOverallErrorSummary - Sets overall behavior and configurations for all error summary or in a scope (document or specified element) as per govuk-frontend error-summary api reference.
- ConfigureOverallHeader - Sets overall behavior and configurations for all Header or in a scope.
- ConfigureOverallRadios - Sets overall behavior and configurations for all Radios or in a scope.
- ConfigureOverallSkipLink - Sets overall behavior and configurations for all SkipLink or in a scope.
- ConfigureOverallTabs - Sets overall behavior and configurations for all Tabs or in a scope.
Here are some examples of the above convenient configurations functions:
// in scope accordion configuration
import React, { useEffect } from "react";
import {
ConfigureOverallAccordion,
ExtractAccordionConfigFromAttributes,
} from "@hmlr/govuk-react-components-library";
function App() {
useEffect(() => {
const attributes = {
hideAllSectionsText: "Collapse all sections",
showAllSectionsText: "Expand all sections",
hideSectionText: "Collapse",
hideSectionAriaLabelText: "Collapse this section",
showSectionText: "Expand",
showSectionAriaLabelText: "Expand this section",
};
const accordionConfig = ExtractAccordionConfigFromAttributes(attributes);
const $element = document.querySelector(".govuk-accordion");
ConfigureOverallAccordion($element, accordionConfig);
}, []);
return (
<Accordion
id="default-example"
items={[
{
content: {
children:
"We need to know your nationality so we can work out which elections you’re entitled to vote in. If you cannot provide your nationality, you’ll have to send copies of identity documents through the post.",
},
heading: {
children: "Section A",
},
},
{
content: {
children: [
<ul key="0" className="govuk-list govuk-list--bullet">
{""}
<li>Example item 2</li>
</ul>,
"\n",
],
},
heading: {
children: "Section B",
},
},
]}
/>
);
}or
// in scope accordion configuration
import React, { useEffect } from "react";
import {
Accordion,
ConfigureOverallAccordion,
} from "@hmlr/govuk-react-components-library";
function App() {
useEffect(() => {
const accordionConfig = {
i18n: {
hideAllSections: "Collapse all sections",
showAllSections: "Expand all sections",
hideSection: "Collapse",
hideSectionAriaLabel: "Collapse this section",
showSection: "Expand",
showSectionAriaLabel: "Expand this section",
},
};
const $element = document.querySelector(".govuk-accordion");
ConfigureOverallAccordion($element, accordionConfig);
}, []);
return (
<Accordion
id="default-example"
items={[
{
content: {
children:
"We need to know your nationality so we can work out which elections you’re entitled to vote in. If you cannot provide your nationality, you’ll have to send copies of identity documents through the post.",
},
heading: {
children: "Section A",
},
},
{
content: {
children: [
<ul key="0" className="govuk-list govuk-list--bullet">
{""}
<li>Example item 2</li>
</ul>,
"\n",
],
},
heading: {
children: "Section B",
},
},
]}
/>
);
}<!-- in scope accordion configuration -->
<script type="module" src="./govuk-frontend.min.js"></script>
<script type="module">
import { Accordion, createAll } from "./govuk-frontend.min.js";
const $accordion = document.querySelector(".govuk-accordion");
createAll(
Accordion,
{
i18n: {
hideAllSections: "Collapse all sections",
showAllSections: "Expand all sections",
hideSection: "Collapse",
hideSectionAriaLabel: "Collapse this section",
showSection: "Expand",
showSectionAriaLabel: "Expand this section",
},
},
$accordion,
);
</script>or
<!-- in scope accordion configuration -->
<script type="module" src="./govuk-frontend.min.js"></script>
<script type="module">
import { Accordion } from "./govuk-frontend.min.js";
const $accordions = document.querySelectorAll(
'[data-module="govuk-accordion"]',
);
$accordions.forEach(($accordion) => {
new Accordion($accordion, {
i18n: {
hideAllSections: "Collapse all sections",
showAllSections: "Expand all sections",
hideSection: "Collapse",
hideSectionAriaLabel: "Collapse this section",
showSection: "Expand",
showSectionAriaLabel: "Expand this section",
},
});
});
</script>// use default configuration
import React, { useEffect } from "react";
import {
Accordion,
ConfigureOverallAccordion,
} from "@hmlr/govuk-react-components-library";
function App() {
useEffect(() => {
ConfigureOverallAccordion();
}, []);
return (
<Accordion
id="default-example"
items={[
{
content: {
children:
"We need to know your nationality so we can work out which elections you’re entitled to vote in. If you cannot provide your nationality, you’ll have to send copies of identity documents through the post.",
},
heading: {
children: "Section A",
},
},
{
content: {
children: [
<ul key="0" className="govuk-list govuk-list--bullet">
{""}
<li>Example item 2</li>
</ul>,
"\n",
],
},
heading: {
children: "Section B",
},
},
]}
/>
);
}or in html
<!-- use default configuration -->
<script type="module" src="./govuk-frontend.min.js"></script>
<script type="module">
import { Accordion, createAll } from "./govuk-frontend.min.js";
createAll(Accordion);
</script>// use default configuration
import React, { useEffect } from "react";
import { initAll } from "govuk-frontend";
import { Accordion } from "@hmlr/govuk-react-components-library";
function App() {
useEffect(() => {
initAll();
}, []);
return (
<Accordion
id="default-example"
items={[
{
content: {
children:
"We need to know your nationality so we can work out which elections you’re entitled to vote in. If you cannot provide your nationality, you’ll have to send copies of identity documents through the post.",
},
heading: {
children: "Section A",
},
},
{
content: {
children: [
<ul key="0" className="govuk-list govuk-list--bullet">
{""}
<li>Example item 2</li>
</ul>,
"\n",
],
},
heading: {
children: "Section B",
},
},
]}
/>
);
}or in html
<!-- use default configuration -->
<script type="module" src="./govuk-frontend.min.js"></script>
<script type="module">
import { initAll } from "./govuk-frontend.min.js";
initAll();
</script>Usage
Use the above components like the Panel component:
import { Panel } from "@hmlr/govuk-react-component-library";
export default function SuccessPanel() {
return (
<Panel titleChildren="Application complete">
Your reference number: HDJ2123F
</Panel>
);
}Note: For the PDFViewer above you must provide a PDFJS Distribution version to be used along with the PDFViewer component. follow the steps to use PDFViewer component:
- Download any version of the PDFJS Distribution version suitable for your application
- Unzip the distribution and add it into you project's static directory i.e.
publicdirectory. Meaning in the root of your project you will have the distribution inpublic/pdfjs-4.4.168-dist - Use the viewer located in
public/pdfjs-4.4.168-dist/web/viewer.htmlasviewerLocationvalue in thePDFViewercomponent. Meaning sincepublicis the static directory the viewer is located in/pdfjs-4.4.168-dist/web/viewer.html.
An example of the PDFViewer component usage based on the 3 steps above will be:
import {
PDFViewer,
PDFViewerBackend,
} from "@hmlr/govuk-react-component-library";
export default function ViewDocument() {
return (
<PDFViewer
backend={PDFViewerBackend}
documentName="LCOP 2020-0035"
iframeId="document_iframe"
src="/document.pdf"
toolbar="fullHidePrint"
viewerLocation="/pdfjs-4.4.168-dist/web/viewer.html"
/>
);
}or base64 encoded pdf data
import {
PDFViewer,
PDFViewerBackend,
} from "@hmlr/govuk-react-component-library";
import { base64EncodedPDFDocument } from "./testutilities/SampleBase64";
export default function ViewDocument() {
return (
<PDFViewer
backend={PDFViewerBackend}
documentName="LCOP 2020-0035"
iframeId="document_iframe"
src={base64EncodedPDFDocument}
toolbar="fullHidePrint"
viewerLocation="/pdfjs-4.4.168-dist/web/viewer.html"
/>
);
}Also one can add custom javascript to viewer to hide or show certain parts of the PDF Viewer or any custom functionality. An example is in the Custom Toolbar in this project. Which then means that in the PDF Viewer. one will add
<script src="../../customToolbar.js"></script>to the head tag of the ./.storybook/public/pdfjs-4.4.168-dist/web/viewer.html file.
But for the example above we recommend to use the Custom Toolbar in this project. This is tested against /pdfjs-4.4.168-dist only. Copy Custom Toolbar into your project static folder where it is called public and the use it in the public/pdfjs-4.4.168-dist/web/viewer.html thus:
<head>
.....
<script src="../../customToolbar.js"></script>
</head>Using this library
In order to use this library, you will need to install the peer dependencies of this application as show in the package.json.peerDependencies. Which includes the following dependencies:
- govuk-frontend - any version >=5.6.0
- react - any version >=18.3.1
- react-dom - any version >=18.3.1
- react-router-dom - any version >=6.26.1
The source of truth can be found the package.json.peerDependencies.
Using the PDFViewerCanvas Component
The PDFViewerCanvas component is optional and requires the pdfjs-dist package. To use it:
- Install the optional dependency:
npm install pdfjs-dist- pdfjs-dist - any version >=4.6.82 and only if you are using
PDFViewerCanvascomponent. This is an optional dependency.
- Import the PDFViewerCanvas component from the separate entry point:
import { PDFViewerCanvas } from "@hmlr/govuk-react-components-library/pdfViewer";- Use the component in your React application:
<PDFViewerCanvas src="path/to/your/pdf" />Development
Prerequisites
- Node.js ( v20.19.0 or higher)
- npm ( v10.8.2 or higher)
- govuk-frontend (v5.8.0 or higher)
- react (v18.2.0 or higher)
- react-dom (v18.2.0 or higher)
- react-router-dom (v18.2.0 or higher)
Setup
- Clone the repository:
git clone https://github.com/LandRegistry/govuk-react-component-library.git
cd govuk-react-component-library- Install dependencies:
npm install
npm install pdfjs-dist@4.10.38 --save-dev # for PDFCanvas- Start the development server:
npm run dev- Run Storybook:
npm run storybookRunning Builds
To run the build in this project use the following command:
npm run buildRunning Linting and formatting
To run the linting and formating in this project use the following command:
npm run lintor
npm run lint-fixand for prettifying
npm run formatTesting
To run the test in this project use the following command:
npm run testContributing
Contributions are welcome! Please feel free to submit a Pull Request.
UML Diagram
The UML diagram for all components in this library can be found here:
Release New version of this package
Use the floowing command to release a new version of the application
npm run releaseor
npm run release -- --release-as minoror for specific versions
npm run release -- --release-as 1.9.1Then publish the version with the following command
git push --follow-tags origin mainand publish as an npm package run
npm publishLicense
This project is licensed under the MIT License - see the LICENSE file for details.
9 months ago