Skip to content

Commit

Permalink
Merge pull request #641 from tblivet/feat/scroll_padding
Browse files Browse the repository at this point in the history
Feat: handle scroll offset on anchor links
  • Loading branch information
tblivet authored Jun 26, 2024
2 parents 1d6a9e8 + 6e9fbef commit 3491039
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/js/constants/selectors-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* file that was distributed with this source code.
*/

export const layout = {
stickyHeader: '.js-sticky-header',
};

export const facetedsearch = {
range: '.js-faceted-slider',
rangeContainer: '.js-faceted-slider-container',
Expand Down Expand Up @@ -145,6 +149,7 @@ export const passwordPolicy = {
};

const selectorsMap = {
layout,
qtyInput,
alert: {
selector: '#notifications .container',
Expand Down
22 changes: 22 additions & 0 deletions src/js/helpers/scrollPadding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import SelectorsMap from '@constants/selectors-map';

const setScrollPaddingTop = () => {
const header = document.querySelector(SelectorsMap.layout.stickyHeader) as HTMLElement;

if (header) {
const headerHeight = header.offsetHeight;
document.documentElement.style.setProperty('scroll-padding-top', `${headerHeight}px`);
}
};

const initScrollPaddingTop = () => {
window.addEventListener('load', setScrollPaddingTop);
window.addEventListener('resize', setScrollPaddingTop);
};

export default initScrollPaddingTop;
2 changes: 2 additions & 0 deletions src/js/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import './modules/facetedsearch';
import initDesktopMenu from './modules/ps_mainmenu';
import initFormValidation from './form-validation';
import initCategoryTree from './modules/ps_categorytree';
import initScrollPaddingTop from './helpers/scrollPadding';

initEmitter();

Expand All @@ -52,6 +53,7 @@ $(() => {
initErrorHandler();
usePasswordPolicy('.field-password-policy');
initCategoryTree();
initScrollPaddingTop();

prestashop.on(events.responsiveUpdate, () => {
initSearchbar();
Expand Down
2 changes: 1 addition & 1 deletion templates/layouts/layout-both-columns.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{include file='catalog/_partials/product-activation.tpl'}
{/block}

<header id="header" class="header">
<header id="header" class="header js-sticky-header">
{block name='header'}
{include file='_partials/header.tpl'}
{/block}
Expand Down

0 comments on commit 3491039

Please sign in to comment.