diff --git a/core/core.libraries.yml b/core/core.libraries.yml index 5da51e8ae5..e86e9177ad 100644 --- a/core/core.libraries.yml +++ b/core/core.libraries.yml @@ -215,13 +215,25 @@ drupal.form: version: VERSION js: misc/form.js: {} + misc/offset/html5forms.js: {} dependencies: - core/jquery - core/drupal - core/drupal.debounce + - core/drupal.displace + - core/drupal.fragment - core/jquery.cookie - core/jquery.once +drupal.fragment: + version: VERSION + js: + misc/offset/fragment.js: {} + dependencies: + - core/jquery + - core/drupal + - core/drupal.displace + drupal.machine-name: version: VERSION js: diff --git a/core/misc/offset/fragment.es6.js b/core/misc/offset/fragment.es6.js new file mode 100644 index 0000000000..00ff6923ab --- /dev/null +++ b/core/misc/offset/fragment.es6.js @@ -0,0 +1,26 @@ +/** + * @file + * Offsets the window to allow fragments navigation with fixed positioning elements. + */ +(function ($, Drupal, displace) { + + "use strict"; + + function adjustOffset () { + const hash = window.location.hash; + if (hash) { + const element = document.getElementById(hash.replace('#', '')); + if (element) { + // Display the element in the viewport. + setTimeout(function () { + window.scrollTo(0, $(element).offset().top - displace.offsets.top); + }, 5); + } + } + } + + // Checks for a fragment on page load, outside Drupal behaviors. + Drupal.behaviors.adjustOffset = { attach: adjustOffset }; + $(window).on('hashchange', adjustOffset); + +})(jQuery, Drupal, Drupal.displace); diff --git a/core/misc/offset/fragment.js b/core/misc/offset/fragment.js new file mode 100644 index 0000000000..53e6984732 --- /dev/null +++ b/core/misc/offset/fragment.js @@ -0,0 +1,26 @@ +/** +* DO NOT EDIT THIS FILE. +* See the following change record for more information, +* https://www.drupal.org/node/2815083 +* @preserve +**/ + +(function ($, Drupal, displace) { + + "use strict"; + + function adjustOffset() { + var hash = window.location.hash; + if (hash) { + var element = document.getElementById(hash.replace('#', '')); + if (element) { + setTimeout(function () { + window.scrollTo(0, $(element).offset().top - displace.offsets.top); + }, 5); + } + } + } + + Drupal.behaviors.adjustOffset = { attach: adjustOffset }; + $(window).on('hashchange', adjustOffset); +})(jQuery, Drupal, Drupal.displace); \ No newline at end of file diff --git a/core/misc/offset/html5forms.es6.js b/core/misc/offset/html5forms.es6.js new file mode 100644 index 0000000000..32e0d33154 --- /dev/null +++ b/core/misc/offset/html5forms.es6.js @@ -0,0 +1,13 @@ +/** + * @file + * Focus the HTML5 validation error in view. + */ +(function (displace) { + + "use strict"; + + document.addEventListener('invalid', function (event) { + setTimeout(function () { window.scrollBy(0, -displace.offsets.top); }, 5); + }, true); + +})(Drupal.displace); diff --git a/core/misc/offset/html5forms.js b/core/misc/offset/html5forms.js new file mode 100644 index 0000000000..2e985e18eb --- /dev/null +++ b/core/misc/offset/html5forms.js @@ -0,0 +1,17 @@ +/** +* DO NOT EDIT THIS FILE. +* See the following change record for more information, +* https://www.drupal.org/node/2815083 +* @preserve +**/ + +(function (displace) { + + "use strict"; + + document.addEventListener('invalid', function (event) { + setTimeout(function () { + window.scrollBy(0, -displace.offsets.top); + }, 5); + }, true); +})(Drupal.displace); \ No newline at end of file diff --git a/core/modules/toolbar/toolbar.libraries.yml b/core/modules/toolbar/toolbar.libraries.yml index dcef0841bb..cd8b630add 100644 --- a/core/modules/toolbar/toolbar.libraries.yml +++ b/core/modules/toolbar/toolbar.libraries.yml @@ -29,6 +29,7 @@ toolbar: - core/matchmedia.addListener - core/jquery.once - core/drupal.displace + - core/drupal.fragment - toolbar/toolbar.menu toolbar.menu: