Skip to content

Commit

Permalink
Skip initialisation when GOV.UK Frontend is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Jun 12, 2023
1 parent 73c692f commit 69e704a
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 13 deletions.
4 changes: 1 addition & 3 deletions docs/contributing/coding-standards/js.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export class Example {
* @param {Element} $module - HTML element to use for component
*/
constructor ($module) {
$module = undefined

if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
5 changes: 5 additions & 0 deletions packages/govuk-frontend/src/govuk/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import { Tabs } from './components/tabs/tabs.mjs'
function initAll (config) {
config = typeof config !== 'undefined' ? config : {}

// Skip initialisation when GOV.UK Frontend is not supported
if (!document.body.classList.contains('govuk-frontend-supported')) {
return
}

// Allow the user to initialise GOV.UK Frontend in only certain sections of the page
// Defaults to the entire document if nothing is set.
const $scope = config.scope instanceof HTMLElement ? config.scope : document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Accordion {
* @param {AccordionConfig} [config] - Accordion config
*/
constructor ($module, config) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Button {
* @param {ButtonConfig} [config] - Button config
*/
constructor ($module, config) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class CharacterCount {
* @param {CharacterCountConfig} [config] - Character count config
*/
constructor ($module, config) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Details {
* @param {Element} $module - HTML element to use for details
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ErrorSummary {
// To avoid breaking further JavaScript initialisation
// we need to safeguard against this so things keep
// working the same now we read the elements data attributes
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Header {
* @param {Element} $module - HTML element to use for header
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class NotificationBanner {
* @param {NotificationBannerConfig} [config] - Notification banner config
*/
constructor ($module, config) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Radios {
* @param {Element} $module - HTML element to use for radios
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class SkipLink {
* @param {Element} $module - HTML element to use for skip link
*/
constructor ($module) {
if (!($module instanceof HTMLAnchorElement)) {
if (!($module instanceof HTMLAnchorElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down
2 changes: 1 addition & 1 deletion packages/govuk-frontend/src/govuk/components/tabs/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Tabs {
* @param {Element} $module - HTML element to use for tabs
*/
constructor ($module) {
if (!($module instanceof HTMLElement)) {
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) {
return this
}

Expand Down

0 comments on commit 69e704a

Please sign in to comment.