forked from sunstar-engineering/sunstar-engineering
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added network-item block, modified horizontal tabs hero
- Loading branch information
Dragos Cristian Bute
committed
Sep 19, 2023
1 parent
049ce50
commit 5d2ca0f
Showing
9 changed files
with
423 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,121 @@ | ||
import { getNamedValueFromTable } from '../../scripts/scripts.js'; | ||
|
||
function normalizeURL(url) { | ||
if (url.endsWith('/')) { | ||
return url; | ||
export function createTabs($block) { | ||
const $ul = $block.querySelector('ul'); | ||
if (!$ul) { | ||
return null; | ||
} | ||
return url.concat('/'); | ||
} | ||
/** @type TabInfo[] */ | ||
const tabs = [...$ul.querySelectorAll('li')].map(($li) => { | ||
const title = $li.textContent; | ||
const name = title.toLowerCase().trim(); | ||
return { | ||
title, | ||
name, | ||
$tab: $li, | ||
}; | ||
}); | ||
const panel = document.createElement('div'); | ||
panel.classList.add('hero-horiz-tabs-panel'); | ||
panel.appendChild($ul); | ||
// move $ul below section div | ||
$block.replaceChildren(panel); | ||
|
||
// search referenced sections and move them inside the tab-container | ||
const $wrapper = $block.parentElement; | ||
const $container = $wrapper.parentElement; | ||
const $sections = document.querySelectorAll('[data-tab]'); | ||
|
||
// move the tab's sections before the tab riders. | ||
[...$sections].forEach(($tabContent) => { | ||
const name = $tabContent.dataset.tab.toLowerCase().trim(); | ||
/** @type TabInfo */ | ||
const tab = tabs.find((t) => t.name === name); | ||
if (tab) { | ||
const sectionWrapper = document.createElement('div'); | ||
|
||
function getImage(block) { | ||
const div = getNamedValueFromTable(block, 'Image'); | ||
div.classList.add('hero-horiz-tabs-img'); | ||
return div; | ||
} | ||
|
||
export function getTabs(block, curLocation) { | ||
const tabs = document.createElement('nav'); | ||
const div = getNamedValueFromTable(block, 'tabs'); | ||
[...$tabContent.classList].forEach((c) => { | ||
sectionWrapper.classList.add(c); | ||
}); | ||
|
||
div.querySelectorAll('ul > li > a').forEach((a) => { | ||
if (normalizeURL(a.href) === normalizeURL(curLocation.href)) { | ||
a.classList.add('current'); | ||
sectionWrapper.classList.add(); | ||
const $el = document.createElement('div'); | ||
$el.classList.add('tab-item'); | ||
$el.append(...$tabContent.children); | ||
$el.classList.add('hidden'); | ||
sectionWrapper.append($el); | ||
$container.insertBefore(sectionWrapper, $wrapper); | ||
$tabContent.remove(); | ||
tab.$content = $el; | ||
} | ||
tabs.appendChild(a); | ||
}); | ||
return tabs; | ||
} | ||
|
||
function getImage(block) { | ||
const div = getNamedValueFromTable(block, 'Image'); | ||
if (!div) return null; | ||
div.classList.add('hero-horiz-tabs-img'); | ||
return div; | ||
} | ||
|
||
function getText(block) { | ||
const div = getNamedValueFromTable(block, 'Contents'); | ||
if (!div) return null; | ||
div.classList.add('hero-horiz-tabs-text'); | ||
return div; | ||
} | ||
|
||
export default async function decorate(block, curLocation = window.location) { | ||
const text = getText(block); | ||
const image = getImage(block); | ||
const tabs = getTabs(block, curLocation); | ||
export default function decorate($block) { | ||
const image = getImage($block); | ||
const text = getText($block); | ||
const tabs = createTabs($block); | ||
|
||
// move the tab riders in front | ||
const $wrapper = $block.parentElement; | ||
const $container = $wrapper.parentElement; | ||
$container.insertBefore($wrapper, $container.firstElementChild); | ||
|
||
tabs.forEach((tab, index) => { | ||
const $button = document.createElement('button'); | ||
const { $tab, title, name } = tab; | ||
$button.textContent = title.split(','); | ||
$button.classList.add('tab'); | ||
|
||
$tab.replaceChildren($button); | ||
|
||
const leftDiv = document.createElement('div'); | ||
leftDiv.classList.add('hero-horiz-tabs-panel'); | ||
leftDiv.append(text); | ||
leftDiv.append(tabs); | ||
$tab.addEventListener('click', () => { | ||
const $activeButton = $block.querySelector('button.active'); | ||
|
||
if ($activeButton !== $tab) { | ||
$activeButton.classList.remove('active'); | ||
// remove active class from parent li | ||
$activeButton.parentElement.classList.remove('active'); | ||
|
||
$button.classList.add('active'); | ||
// add active class to parent li | ||
$tab.classList.add('active'); | ||
|
||
tabs.forEach((t) => { | ||
if (name === t.name) { | ||
t.$content.classList.remove('hidden'); | ||
} else { | ||
t.$content.classList.add('hidden'); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
if (index === 0) { | ||
$button.classList.add('active'); | ||
// add active class to parent li | ||
$tab.classList.add('active'); | ||
tab.$content.classList.remove('hidden'); | ||
tab.$content.classList.add('active'); | ||
} | ||
}); | ||
|
||
block.replaceChildren(leftDiv); | ||
block.append(image); | ||
if (text) $block.append(text); | ||
if (image) $block.append(image); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* Horizontal List */ | ||
.horizontal-list ul { | ||
list-style-type: none; | ||
margin: 0; | ||
padding: 0; | ||
overflow: hidden; | ||
} | ||
|
||
.horizontal-list.careers-tags ul { | ||
font-size: var(--body-font-size-xs); | ||
} | ||
|
||
.horizontal-list li { | ||
display: inline-block; | ||
margin-right: 1rem; | ||
text-align: center; | ||
} | ||
|
||
.horizontal-list li::before { | ||
display: inline-block; | ||
content: ''; | ||
background: inherit; | ||
background-color: var(--text-color); | ||
height: 12px; | ||
width: 12px; | ||
margin-right: 4px; | ||
border-radius: 50%; | ||
} | ||
|
||
.horizontal-list.careers-tags li.blue { | ||
color: #009CBD;/*oral care*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.yellow { | ||
color: #D9AB41; /*safety mobility*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.orange { | ||
color: #F87C56; /*health beauty*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.green { | ||
color: #00A499; /*living environment*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.green::before { | ||
background-color: #00A499; /*living environment*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.blue::before { | ||
background-color: #009CBD; /*oral care*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.yellow::before { | ||
background-color: #D9AB41; /*safety mobility*/ | ||
} | ||
|
||
.horizontal-list.careers-tags li.orange::before { | ||
background-color: #F87C56; /*health beauty*/ | ||
} | ||
|
||
.horizontal-list li:last-child { | ||
margin-right: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export default function decorate(block) { | ||
const list = block.children[0].children[0].children[0]; | ||
|
||
addCareerColors(block, list); | ||
|
||
block.replaceChildren(list); | ||
}; | ||
|
||
function addCareerColors(block, list) { | ||
if (block.classList.contains('careers-tags')) { | ||
const listItems = [...list.children]; | ||
listItems.forEach((x) => { | ||
if (x.innerText === 'Oral Care') { | ||
x.classList.add('blue'); | ||
} else if (x.innerText === 'Safety & Mobility') { | ||
x.classList.add('yellow'); | ||
} else if (x.innerText === 'Living Environment') { | ||
x.classList.add('green'); | ||
} else if (x.innerText === 'Health & Beauty') { | ||
x.classList.add('orange'); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.