Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createAll to initialise components in the Design System site #4053

Merged
merged 10 commits into from
Aug 20, 2024
3 changes: 3 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# This list builds on the GOV.UK service manual's browser testing recommendations
# https://www.gov.uk/service-manual/technology/designing-for-different-browsers-and-devices

[javascripts]
supports es6-module

[stylesheets]
> 0.1% in GB and not dead
last 6 Chrome versions
Expand Down
14 changes: 12 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
* @type {import('@babel/core').ConfigFunction}
*/
module.exports = function (api) {
const browserslistEnv = !api.env('test') ? 'production' : 'node'
const isBrowser = !api.env('test')

const browserslistEnv = isBrowser ? 'javascripts' : 'node'

const presets = [
[
'@babel/preset-env',
{
browserslistEnv
browserslistEnv,
// Apply bug fixes to avoid transforms
bugfixes: true,

// Apply smaller "loose" transforms for browsers
loose: isBrowser,

// Skip ES module transforms for browsers
modules: isBrowser ? false : 'auto'
}
]
]
Expand Down
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@metalsmith/permalinks": "^3.0.1",
"@metalsmith/postcss": "^5.4.1",
"@metalsmith/sass": "^1.10.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
Expand Down
9 changes: 8 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const babel = require('@rollup/plugin-babel')
const commonjs = require('@rollup/plugin-commonjs')
const resolve = require('@rollup/plugin-node-resolve')
const terser = require('@rollup/plugin-terser')
Expand Down Expand Up @@ -35,5 +36,11 @@ module.exports = defineConfig({
],

// Input plugins
plugins: [resolve(), commonjs()]
plugins: [
resolve(),
commonjs(),
babel({
babelHelpers: 'bundled'
})
]
})
5 changes: 4 additions & 1 deletion src/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ module.exports = {
extends: ['plugin:es-x/restrict-to-es2015', 'prettier'],
parserOptions: {
// Note: Allow ES2015 for import/export syntax
ecmaVersion: '2015'
ecmaVersion: 'latest'
},
plugins: ['es-x'],
rules: {
// Babel transpiles ES2020 class fields
'es-x/no-class-fields': 'off',

// JSDoc blocks are mandatory in source code
'jsdoc/require-jsdoc': [
'error',
Expand Down
52 changes: 11 additions & 41 deletions src/javascripts/application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './components/cookie-functions.mjs'
import CookiesPage from './components/cookies-page.mjs'
import Copy from './components/copy.mjs'
import Example from './components/example.mjs'
import ExampleFrame from './components/example-frame.mjs'
import Navigation from './components/navigation.mjs'
import OptionsTable from './components/options-table.mjs'
import Search from './components/search.mjs'
Expand All @@ -23,15 +23,11 @@ createAll(Button)
createAll(NotificationBanner)
createAll(SkipLink)

// Initialise cookie banner
const $cookieBanner = document.querySelector(
'[data-module="govuk-cookie-banner"]'
)
if ($cookieBanner) {
new CookieBanner($cookieBanner)
}
// Cookies and analytics
createAll(CookieBanner)
createAll(CookiesPage)

// Initialise analytics if consent is given
// Check for consent before initialising analytics
const userConsent = getConsentCookie()
if (userConsent && isValidConsentCookie(userConsent) && userConsent.analytics) {
loadAnalytics()
Expand All @@ -41,44 +37,18 @@ if (userConsent && isValidConsentCookie(userConsent) && userConsent.analytics) {
removeUACookies()
}

// Initialise example frames
const $examples = document.querySelectorAll('[data-module="app-example-frame"]')
$examples.forEach(($example) => {
new Example($example)
})

// Initialise tabs
const $tabs = document.querySelectorAll('[data-module="app-tabs"]')
$tabs.forEach(($tabs) => {
new AppTabs($tabs)
})

// Code examples
createAll(ExampleFrame)
createAll(AppTabs)
// Do this after initialising tabs
createAll(Copy)
new OptionsTable()

// Add copy to clipboard to code blocks inside tab containers
const $codeBlocks = document.querySelectorAll('[data-module="app-copy"] pre')
$codeBlocks.forEach(($codeBlock) => {
new Copy($codeBlock)
})

// Initialise mobile navigation
new Navigation(document)

// Initialise search
const $searchContainer = document.querySelector('[data-module="app-search"]')
if ($searchContainer) {
new Search($searchContainer)
}
createAll(Search)

// Initialise back to top
const $backToTop = document.querySelector('[data-module="app-back-to-top"]')
if ($backToTop) {
new BackToTop($backToTop)
}

// Initialise cookie page
const $cookiesPage = document.querySelector('[data-module="app-cookies-page"]')
if ($cookiesPage) {
new CookiesPage($cookiesPage)
}
createAll(BackToTop)
2 changes: 2 additions & 0 deletions src/javascripts/components/back-to-top.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Website back to top link
*/
class BackToTop {
static moduleName = 'app-back-to-top'

/**
* @param {Element} $module - HTML element
*/
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/components/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const cookieConfirmationRejectSelector = '.js-cookie-banner-confirmation-reject'
* Website cookie banner
*/
class CookieBanner {
static moduleName = 'govuk-cookie-banner'
/**
* @param {Element} $module - HTML element
*/
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/components/cookies-page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getConsentCookie, setConsentCookie } from './cookie-functions.mjs'
* Website cookies page
*/
class CookiesPage {
static moduleName = 'app-cookies-page'
/**
* @param {Element} $module - HTML element
*/
Expand Down
11 changes: 8 additions & 3 deletions src/javascripts/components/copy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ClipboardJS from 'clipboard'
* Copy button for code examples
*/
class Copy {
static moduleName = 'app-copy'

/**
* @param {Element} $module - HTML element
*/
Expand All @@ -18,6 +20,9 @@ class Copy {

this.$module = $module

this.$pre = this.$module.querySelector('pre')
// TODO: Throw once GOV.UK Frontend exports its errors

/** @type {number | null} */
this.resetTimeoutId = null

Expand All @@ -29,11 +34,11 @@ class Copy {
this.$status.className = 'govuk-visually-hidden'
this.$status.setAttribute('aria-live', 'assertive')

this.$module.insertAdjacentElement('beforebegin', this.$status)
this.$module.insertAdjacentElement('beforebegin', this.$button)
this.$module.prepend(this.$status)
this.$module.prepend(this.$button)
Comment on lines +37 to +38
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note $module is no longer the <pre> element containing the code, but the <div> wrapping it, which simplifies the insertion of the button and hidden status for assistive technology.


const $clipboard = new ClipboardJS(this.$button, {
target: (trigger) => trigger.nextElementSibling
target: () => this.$pre
})

$clipboard.on('success', (event) => this.successAction(event))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import iFrameResize from 'iframe-resizer/js/iframeResizer.js'
*
* @param {Element} $module - HTML element to use for example
*/
class Example {
class ExampleFrame {
static moduleName = 'app-example-frame'
/**
* @param {Element} $module - HTML element
*/
Expand Down Expand Up @@ -39,4 +40,4 @@ class Example {
}
}

export default Example
export default ExampleFrame
1 change: 1 addition & 0 deletions src/javascripts/components/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const DEBOUNCE_TIME_TO_WAIT = () => {
* Website search
*/
class Search {
static moduleName = 'app-search'
/**
* @param {Element} $module - HTML element
*/
Expand Down
2 changes: 2 additions & 0 deletions src/javascripts/components/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* - panels - the content that is shown/hidden/switched; same across all breakpoints
*/
class AppTabs {
static moduleName = 'app-tabs'

/**
* @param {Element} $module - HTML element
*/
Expand Down