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

fix(site-header): fixing language switcher in rtl - FRONT-4569 #3561

Merged
merged 8 commits into from
Aug 14, 2024
4 changes: 2 additions & 2 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
},
{
"path": "dist/packages/ec/scripts/ecl-ec.js",
"limit": "230 KB",
"limit": "235 KB",
"webpack": false,
"gzip": false,
"brotli": false
},
{
"path": "dist/packages/eu/scripts/ecl-eu.js",
"limit": "230 KB",
"limit": "235 KB",
"webpack": false,
"gzip": false,
"brotli": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ $language-list: null !default;
right: var(--ecl-language-arrow-position);
}

.ecl-site-header__language-container--push-left::before {
right: auto;
left: var(--ecl-language-arrow-position);
}

.ecl-site-header__language-header {
align-items: center;
display: flex;
Expand All @@ -83,6 +88,11 @@ $language-list: null !default;
position: absolute;
right: map.get($language-switcher, 'close-right');
top: map.get($language-switcher, 'close-top');

.ecl-site-header--rtl & {
left: map.get($language-switcher, 'close-right');
right: auto;
}
}

.ecl-site-header__language-content {
Expand All @@ -109,6 +119,10 @@ $language-list: null !default;
padding: 0;
}

.ecl-site-header--rtl .ecl-site-header__language-list {
margin: map.get($language-list, 'list-margin-rtl');
}

.ecl-site-header__language-link {
align-items: baseline;
display: flex;
Expand Down Expand Up @@ -183,6 +197,12 @@ $language-list: null !default;
transform: none;
}

.ecl-site-header__language-container--push-left {
left: 0;
right: auto;
transform: none;
}

// Horizontal sections
.ecl-site-header__language-content:not(
.ecl-site-header__language-content--stack
Expand All @@ -206,6 +226,13 @@ $language-list: null !default;
}
}

.ecl-site-header--rtl
.ecl-site-header__language-content:not(
.ecl-site-header__language-content--stack
) {
overflow-x: hidden;
}

// Columns display
.ecl-site-header__language-list {
column-gap: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ $language-list: null !default;
background-size: auto 100%;
}

&.ecl-site-header--rtl .ecl-site-header__background {
background-position: left bottom;
}

.ecl-menu,
.ecl-mega-menu {
bottom: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ $language-list: null !default;
right: 0;
width: calc(44px + var(--s-m) * 2);

&.ecl-mega-menu--rtl {
&.ecl-mega-menu--rtl,
&.ecl-menu--rtl {
left: 0;
right: auto;
}
Expand Down Expand Up @@ -169,8 +170,8 @@ $language-list: null !default;
align-items: center;
display: flex;

.ecl-button:not(.ecl-site-header__search-toggle) {
margin-inline-end: var(--s-2xs);
.ecl-button {
margin-inline-start: var(--s-2xs);
}

.ecl-site-header--has-menu &,
Expand Down
42 changes: 32 additions & 10 deletions src/implementations/vanilla/components/site-header/site-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createFocusTrap } from 'focus-trap';
* @param {String} options.searchFormSelector
* @param {String} options.loginToggleSelector
* @param {String} options.loginBoxSelector
* @param {integer} options.tabletBreakpoint
* @param {Boolean} options.attachClickListener Whether or not to bind click events
* @param {Boolean} options.attachKeyListener Whether or not to bind keyboard events
* @param {Boolean} options.attachResizeListener Whether or not to bind resize events
Expand Down Expand Up @@ -50,6 +51,7 @@ export class SiteHeader {
attachClickListener = true,
attachKeyListener = true,
attachResizeListener = true,
tabletBreakpoint = 768,
} = {},
) {
// Check element
Expand All @@ -76,6 +78,7 @@ export class SiteHeader {
this.attachClickListener = attachClickListener;
this.attachKeyListener = attachKeyListener;
this.attachResizeListener = attachResizeListener;
this.tabletBreakpoint = tabletBreakpoint;

// Private variables
this.languageMaxColumnItems = 8;
Expand All @@ -91,6 +94,7 @@ export class SiteHeader {
this.loginToggle = null;
this.loginBox = null;
this.resizeTimer = null;
this.direction = null;

// Bind `this` for use in callbacks
this.openOverlay = this.openOverlay.bind(this);
Expand Down Expand Up @@ -138,6 +142,12 @@ export class SiteHeader {
this.languageListContent = queryOne(this.languageListContentSelector);
this.close = queryOne(this.closeOverlaySelector);

// direction
this.direction = getComputedStyle(this.element).direction;
if (this.direction === 'rtl') {
this.element.classList.add('ecl-site-header--rtl');
}

// Create focus trap
this.focusTrap = createFocusTrap(this.languageListOverlay, {
onDeactivate: this.closeOverlay,
Expand Down Expand Up @@ -222,6 +232,7 @@ export class SiteHeader {

if (this.element) {
this.element.removeAttribute('data-ecl-auto-initialized');
this.element.classList.remove('ecl-site-header--rtl');
ECL.components.delete(this.element);
}
}
Expand Down Expand Up @@ -310,6 +321,7 @@ export class SiteHeader {
// Check available space
this.languageListOverlay.classList.remove(
'ecl-site-header__language-container--push-right',
'ecl-site-header__language-container--push-left',
);
this.languageListOverlay.classList.remove(
'ecl-site-header__language-container--full',
Expand All @@ -318,36 +330,47 @@ export class SiteHeader {
'--ecl-language-arrow-position',
);
this.languageListOverlay.style.removeProperty('right');
this.languageListOverlay.style.removeProperty('left');

popoverRect = this.languageListOverlay.getBoundingClientRect();
const screenWidth = window.innerWidth;

const linkRect = this.languageLink.getBoundingClientRect();
// Popover too large
if (popoverRect.right > screenWidth) {
const linkRect = this.languageLink.getBoundingClientRect();

if (this.direction === 'ltr' && popoverRect.right > screenWidth) {
// Push the popover to the right
this.languageListOverlay.classList.add(
'ecl-site-header__language-container--push-right',
);
this.languageListOverlay.style.setProperty(
'right',
`-${containerRect.right - linkRect.right}px`,
`calc(-${containerRect.right}px + ${linkRect.right}px)`,
);

// Adapt arrow position
const arrowPosition =
containerRect.right - linkRect.right + linkRect.width / 2;
this.languageListOverlay.style.setProperty(
'--ecl-language-arrow-position',
`calc(${arrowPosition}px - ${this.arrowSize})`,
);
} else if (this.direction === 'rtl' && popoverRect.left < 0) {
this.languageListOverlay.classList.add(
'ecl-site-header__language-container--push-left',
);
this.languageListOverlay.style.setProperty(
'left',
`calc(-${linkRect.left}px + ${containerRect.left}px)`,
);
// Adapt arrow position
const arrowPosition =
linkRect.right - containerRect.left - linkRect.width / 2;
this.languageListOverlay.style.setProperty(
'--ecl-language-arrow-position',
`${arrowPosition}px`,
);
}

// Mobile popover (full width)
if (popoverRect.left === 0) {
const linkRect = this.languageLink.getBoundingClientRect();

if (window.innerWidth < this.tabletBreakpoint) {
// Push the popover to the right
this.languageListOverlay.classList.add(
'ecl-site-header__language-container--full',
Expand All @@ -357,7 +380,6 @@ export class SiteHeader {
// Adapt arrow position
const arrowPosition =
popoverRect.right - linkRect.right + linkRect.width / 2;

this.languageListOverlay.style.setProperty(
'--ecl-language-arrow-position',
`calc(${arrowPosition}px - ${this.arrowSize})`,
Expand Down
1 change: 1 addition & 0 deletions src/themes/ec/variables/_language-list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $language-list: (
category-separator: 1px solid var(--c-n),
category-stack-margin: var(--s-s),
list-margin: 0 0 0 calc(-1 * var(--s-s)),
list-margin-rtl: calc(-1 * var(--s-s)) 0 0 0,
item-padding: var(--s-xs) var(--s-s),
code-color: var(--c-d),
code-font-weight: map.get($font-weight, 'regular'),
Expand Down
1 change: 1 addition & 0 deletions src/themes/eu/variables/_language-list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $language-list: (
category-separator: 1px solid var(--c-p-20),
category-stack-margin: var(--s-s),
list-margin: 0 0 0 calc(-1 * var(--s-s)),
list-margin-rtl: calc(-1 * var(--s-s)) 0 0 0,
item-padding: var(--s-s),
code-color: var(--c-d-140),
code-font-weight: map.get($font-weight, 'bold'),
Expand Down
Loading
Loading