Skip to content

Add JS for responsive tables in long text fields #771

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

Open
wants to merge 12 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/install/localgov_base.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ localgov_base_header_behaviour: 'default'
localgov_base_localgov_guides_stacked_heading: FALSE
localgov_base_localgov_guides_vertical_navigation: TRUE
localgov_base_mobile_breakpoint_js: '768'
localgov_base_responsive_wysiwyg_tables: TRUE
5 changes: 5 additions & 0 deletions config/schema/localgov_base.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ localgov_base.settings:
label: 'Mobile breakpoint for JS'
description: 'The mobile breakpoint for JS. This is used to determine when to apply mobile styles.'
default_value: '768'
localgov_base_responsive_wysiwyg_tables:
type: boolean
label: 'Responsive WYSIWYG tables'
description: 'Choose whether to make WYSIWYG tables responsive or not.'
default_value: TRUE
3 changes: 3 additions & 0 deletions css/components/responsive-tables.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.lgd-responsive-table {
overflow-y: auto;
}
35 changes: 35 additions & 0 deletions js/responsive-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file JS file for the responsive tables component.
*/

(function responsiveTablesScript(Drupal, once) {
Drupal.behaviors.responsiveTables = {
attach(context) {
const tables = [];
const tablesInTextWithSummaryFields = once(
'allResponsiveTables',
'.field--type-text-with-summary table',
context,
);
const tablesInTextLongFields = once(
'responsiveTables',
'.field--type-text-long table',
context,
);

// Push all tables into the tables array.
tables.push(...tablesInTextWithSummaryFields);
tables.push(...tablesInTextLongFields);
tables.forEach((table) => {
// Clone the table to avoid modifying the original.
const clonedTable = table.cloneNode(true);
// Add a div around the cloned table.
const wrapper = document.createElement('div');
wrapper.classList.add('lgd-responsive-table');
wrapper.appendChild(clonedTable);
// Replace the original table with the wrapper.
table.parentNode.replaceChild(wrapper, table);
});
},
};
})(Drupal, once);
8 changes: 8 additions & 0 deletions localgov_base.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,17 @@ accordion:
theme:
css/components/accordion.css: {}

responsive-tables:
css:
theme:
css/components/responsive-tables.css: {}
js:
js/responsive-tables.js: {}

images:
js:
js/images.js: {}

dependencies:
- core/drupal
- core/once
14 changes: 14 additions & 0 deletions localgov_base.theme
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ function localgov_base_form_system_theme_settings_alter(&$form, FormStateInterfa
],
];

$form['localgov_base_responsive_wysiwyg_tables'] = [
'#type' => 'checkbox',
'#title' => t('Make WYSIWYG tables responsive.'),
'#description' => t('Places tables inserted via WYSIWYG into a responsive container, so they can scroll horizontally on small screens.'),
'#default_value' => theme_get_setting('localgov_base_responsive_wysiwyg_tables') ? theme_get_setting('localgov_base_responsive_wysiwyg_tables') : FALSE,
Copy link
Member

Choose a reason for hiding this comment

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

Duplicate #default_value with line 105 above

Copy link

@tonypaulbarker tonypaulbarker Jun 9, 2025

Choose a reason for hiding this comment

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

@markconroy @millnut line 107 has some additional logic compared to line 105. Presume we want line 107. "If we have this setting, use the setting, otherwise false" ?

];

$form['#validate'][] = 'localgov_base_theme_settings_validate';
}

Expand Down Expand Up @@ -147,6 +154,13 @@ function localgov_base_preprocess_html(&$variables): void {
}
}
}

// Add the 'localgov_base_responsive_wysiwyg_tables' value to drupalSettings.
if (!empty(theme_get_setting('localgov_base_responsive_wysiwyg_tables'))) {
if (theme_get_setting('localgov_base_responsive_wysiwyg_tables') === TRUE || 1) {
$variables['#attached']['library'][] = 'localgov_base/responsive-tables';
}
}
}

/**
Expand Down
Loading