Hello, GOV
I really really love the GOV.UK design system, and the Frontend framework that enables it GOV.UK Frontend. It’s an absolute tour de force of a design system tested on 68.35 million people1. It’s battle hardened, robust and accessible. Essentially it’s the gold standard for common components and form design. It follows Government Design Principles and GOV.UK Service Manual. As such it’s often a no brainer for me including it on WordPress builds. The simplest approach would be to grab the compiled CSS and JS from their latest release (or via npm), pop it into our wrapper and off we go. The bundles are an impressive 118KB for the CSS and 42.7KB for the JS (minified). But we can also tailor it for our specific needs as well. In this post I’ll be covering how we can set up GOV.UK Frontend with a smaller footprint and integrate it into Gravity Forms one of the leading Forms plugins for WordPress. The post is based on version 5.8.0; releases are pretty regular from the GOV.UK team. Init After adding the core JS, the only extra thing that’s required for any JS enhanced components is we need to conditionally add a class is JS is enabled by the user. This allows the system to progressively enhance components with JavaScript. and in our footer.php we need to initialise govuk-frontend. Sass So far so easy. But I don’t always want the full stack for every project. I do tend to include the minified JS as at 42KB as I feel this is an acceptable payload. But we definitely don’t always need every component, as such I usually set up my Sass files so I only include what I want (after installing govuk-frontend via npm). Below is my default. Note we need to redeclare a few variables to override the GOV.UK defaults. For the most part I am only including form stylings, we can add additional components as a when we need them. //// /// GOV.UK Frontend - https://frontend.design-system.service.gov.uk/ //// //Variables - need to be declared here to take effect $govuk-include-default-font-face: false; $govuk-focus-colour: #FFDD00; $govuk-button-background-colour: #0c6f3e; $govuk-button-text-colour: #FFFFFF; $govuk-font-family: system-ui, sans-serif; // Custom build // 'Base' includes everything from settings, tools and helpers. Nothing // in the base outputs any CSS. @import "../../../node_modules/govuk-frontend/dist/govuk/base"; // Basic content styles for typography, links etc. Approximately 10% of // the CSS output if you include everything. @import "../../../node_modules/govuk-frontend/dist/govuk/core/index"; // Objects include things like the page template, grid and form groups. // Approximately 5% of the CSS output if you include everything. @import "../../../node_modules/govuk-frontend/dist/govuk/objects/index"; // The components themselves - try to only include the components you // are using in your project. Approximately 70% of the CSS output if // you include everything. @import "../../../node_modules/govuk-frontend/dist/govuk/components/accordion/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/breadcrumbs/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/button/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/checkboxes/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/details/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/error-message/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/error-summary/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/fieldset/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/input/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/label/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/notification-banner/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/pagination/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/radios/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/select/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/skip-link/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/textarea/index"; @import "../../../node_modules/govuk-frontend/dist/govuk/components/warning-text/index"; // Utilities, for example govuk-clearfix or govuk-visually-hidden. // Approximately 1% of the CSS output if you include everything. @import "../../../node_modules/govuk-frontend/dist/govuk/utilities/index"; // Overrides, used to adjust things like the amount of spacing on an // element. Override classes always include -!- in the class name. // Approximately 15% of the CSS output if you include everything. @import "../../../node_modules/govuk-frontend/dist/govuk/overrides/index"; Gravity forms This gives us a great foundation to work from. Now, given we are mostly using forms, we need to make sure this works with our chosen Form builder. My go to is Gravity Forms. However the markup for a Gravity Form and GOV.UK Form component is different. Here is GOV.UK’s for a text input:
What is the name of the event?
There is a problem
'; $message .= '- '; foreach ( $form['fields'] as $field ) { if ( $field->failed_validation ) { $message .= sprintf( '
- %s ', $field->formId, $field->id, $field->validation_message ); } } $message .= '
Discussion in the ATmosphere