Skip to content

Commit

Permalink
Consolidate GOV.UK Frontend into application scripts
Browse files Browse the repository at this point in the history
We need to ensure that only `initAll()` on example pages suppresses auto focus
  • Loading branch information
colinrotherham committed Jul 20, 2023
1 parent 4f2b0b0 commit a17c27f
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 30 deletions.
9 changes: 2 additions & 7 deletions lib/metalsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,9 @@ module.exports = metalsmith
done()
})

// build the entrypoint for application specific JavaScript
// build the entrypoints for application specific JavaScript
.use(rollup('javascripts/application.mjs'))

// build GOV.UK Frontend JavaScript
.use(rollup('javascripts/govuk-frontend.mjs'))

// build the entrypoint for example specific JavaScript
.use(rollup('javascripts/example.mjs'))
.use(rollup('javascripts/application-example.mjs'))

// add hash to files in production
.use((files, metalsmith, done) => {
Expand Down
13 changes: 13 additions & 0 deletions src/javascripts/application-example.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { initAll } from 'govuk-frontend'

import ExamplePage from './components/example-page.mjs'

// Initialise GOV.UK Frontend
initAll({
// Auto focusing the error summary and notification banner is not useful
// when used in examples, and causes the viewport to scroll
errorSummary: { disableAutoFocus: true },
notificationBanner: { disableAutoFocus: true }
})

new ExamplePage(document).init()
5 changes: 5 additions & 0 deletions src/javascripts/application.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { initAll } from 'govuk-frontend'

import Analytics from './components/analytics.mjs'
import BackToTop from './components/back-to-top.mjs'
import CookieBanner from './components/cookie-banner.mjs'
Expand All @@ -11,6 +13,9 @@ import OptionsTable from './components/options-table.mjs'
import Search from './components/search.mjs'
import AppTabs from './components/tabs.mjs'

// Initialise GOV.UK Frontend
initAll()

// Initialise cookie banner
var $cookieBanner = document.querySelector('[data-module="govuk-cookie-banner"]')
new CookieBanner($cookieBanner).init()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function ExamplePage ($module) {
this.$module = $module
}

ExamplePage.prototype.init = function () {
var $module = this.$module
if (!$module) {
Expand All @@ -9,6 +10,7 @@ ExamplePage.prototype.init = function () {
var $form = $module.querySelector('form[action="/form-handler"]')
this.preventFormSubmission($form)
}

ExamplePage.prototype.preventFormSubmission = function ($form) {
// we should only have one form per example
if (!$form) {
Expand All @@ -19,4 +21,4 @@ ExamplePage.prototype.preventFormSubmission = function ($form) {
})
}

new ExamplePage(document).init()
export default ExamplePage
12 changes: 0 additions & 12 deletions src/javascripts/govuk-frontend.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion src/styles/page-template/block-areas/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ stylesheets:
<span class="app-annotate-block__label">block: bodyEnd</span>
{{ super() }}
{# Since we’re not extending the Design System layout we need to add this manually #}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
</div>
{%- endblock %}
2 changes: 1 addition & 1 deletion src/styles/page-template/custom/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ ignoreInSitemap: true
{% endblock %}

{% block bodyEnd %}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
2 changes: 1 addition & 1 deletion src/styles/page-template/default/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ layout: false
{% endblock %}

{% block bodyEnd %}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
1 change: 0 additions & 1 deletion views/layouts/_generic.njk
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@
{% block footer %}{% endblock %}

{% block bodyEnd %}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application.js') }}"></script>
{% endblock %}
3 changes: 1 addition & 2 deletions views/layouts/layout-example-full-page-govuk.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
{{ contents | safe }}
{% endblock %}
{% block bodyEnd %}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
3 changes: 1 addition & 2 deletions views/layouts/layout-example-full-page.njk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
{{ contents | safe }}
{% endblock %}
{% block bodyEnd %}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}
3 changes: 1 addition & 2 deletions views/layouts/layout-example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
{% endblock %}

{% block bodyEnd %}
<script type="module" src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/application-example.js') }}"></script>
<script type="module" src="{{ getFingerprint('javascripts/vendor/iframeResizer.contentWindow.min.js') }}"></script>
{% endblock %}

Expand Down

0 comments on commit a17c27f

Please sign in to comment.