Skip to content

Commit

Permalink
#461424 #461425 - Improve boilerplate and add component nesting featu…
Browse files Browse the repository at this point in the history
…re. Add global linked images and CTA definitions.
  • Loading branch information
infloent committed Apr 17, 2024
1 parent 62102ab commit 4b8c4c2
Show file tree
Hide file tree
Showing 25 changed files with 933 additions and 248 deletions.
4 changes: 2 additions & 2 deletions blocks/accordion/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Accordion extends ComponentBase {
}
return child;
});
// console.log(children)

this.setupControls(children.filter((_, ind) => ind % 2 === 0));
this.setupContent(children.filter((_, ind) => ind % 2 === 1));
}
Expand All @@ -25,7 +25,7 @@ export default class Accordion extends ComponentBase {
const icon = document.createElement('raqn-icon');
icon.setAttribute('icon', 'chevron-right');
const children = Array.from(control.children);
if (children.length === 0) {
if (!children.length) {
const child = document.createElement('span');
child.textContent = control.textContent;
control.innerHTML = '';
Expand Down
37 changes: 29 additions & 8 deletions blocks/breadcrumbs/breadcrumbs.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import ComponentBase from '../../scripts/component-base.js';
import { getBaseUrl } from '../../scripts/libs.js';

export default class BreadCrumbs extends ComponentBase {
capitalize(string) {
return string
.split('-')
.map((str) => str.charAt(0).toUpperCase() + str.slice(1))
.join(' ');
export default class Breadcrumbs extends ComponentBase {
static loaderConfig = {
...ComponentBase.loaderConfig,
targetsSelectors: 'main > div',
targetsSelectorsLimit: 1,
targetsAsContainers: true,
};

extendConfig() {
return [
...super.extendConfig(),
{
contentFromTargets: false,
addToTargetMethod: 'replaceWith',
targetsAsContainers: {
addToTargetMethod: 'prepend',
},
},
];
}

ready() {
connected() {
this.classList.add('full-width');
this.classList.add('breadcrumbs');
this.path = window.location.pathname.split('/');
this.path = window.location.href.split(getBaseUrl()).join('/').split('/');
this.innerHTML = `
<ul>
${this.path
Expand All @@ -28,4 +42,11 @@ export default class BreadCrumbs extends ComponentBase {
.join('<li class="separator">›</li>')}
</ul>`;
}

capitalize(string) {
return string
.split('-')
.map((str) => str.charAt(0).toUpperCase() + str.slice(1))
.join(' ');
}
}
1 change: 0 additions & 1 deletion blocks/button/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ raqn-button {
width: 100%;
display: grid;
align-content: center;
justify-content: center;
align-items: center;
justify-items: var(--scope-justify, start);
}
Expand Down
18 changes: 15 additions & 3 deletions blocks/button/button.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import ComponentBase from '../../scripts/component-base.js';

export default class Button extends ComponentBase {
ready() {
this.setAttribute('role', 'button');
this.setAttribute('tabindex', '0');
static loaderConfig = {
...ComponentBase.loaderConfig,
targetsSelectors: ':is(p,div):has(> a:only-child)',
selectorTest: (el) => el.childNodes.length === 1,
};

extendConfig() {
return [
...super.extendConfig(),
{
targetsAsContainers: {
addToTargetMethod: 'append',
},
},
];
}
}
60 changes: 29 additions & 31 deletions blocks/card/card.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@ raqn-card {
padding: var(--scope-padding, 20px 0);
}

raqn-card a {
font-size: var(--raqn-font-size-3, 1.2em);
font-weight: bold;
}

raqn-card p a {
margin-block: 0;
}

raqn-card > picture {
grid-column: span var(--card-columns, 1fr);
}

raqn-card > div {
display: flex;
gap: var(--scope-gap, 20px);
position: relative;
background: var(--scope-inner-background, transparent);
padding: var(--scope-inner-padding, 20px);
Expand All @@ -31,33 +20,42 @@ raqn-card > div {
border-inline-end: var(--scope-border-inline-end, none);
}

raqn-card > div div:last-child > a {
raqn-card :where(a, button) {
position: relative;
z-index: 2;
}

/* Make entire item clickable */
raqn-card div > div:first-child > p > em:only-child > a:only-child {
position: absolute;
inset-inline-start: 0;
inset-block-start: 0;
inset-block-end: 0;
inset-inline-end: 0;
width: 100%;
height: 100%;
cursor: pointer;
text-indent: -10000px;
margin: 0;
padding: 0;
color: transparent;
user-select: none;
z-index: 1;
}

raqn-card > div:has(raqn-icon) {
padding-block-end: 0;
padding-block-end: var(--scope-icon-size, 20px);
raqn-card div > div:first-child > p:has(> em:only-child > a:only-child) {
margin: 0;
}

raqn-card p:has(raqn-icon) {
display: inline-grid;
}

raqn-card raqn-icon {
width: 100%;
raqn-card div > div {
display: flex;
position: absolute;
flex-direction: column;
height: 100%;
inset-block-end: 0;
inset-inline-end: 0;
box-sizing: border-box;
padding: var(--scope-inner-padding, 20px);
}

raqn-card div > div p:last-child:has(> raqn-button, raqn-icon) {
flex-grow: 1;
display: flex;
align-items: flex-end;
}

raqn-card div > div p:last-child:has(> raqn-icon) {
justify-content: flex-end;
}
3 changes: 0 additions & 3 deletions blocks/card/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ export default class Card extends ComponentBase {
static observedAttributes = ['columns', 'ratio', 'eager', 'background', 'button'];

ready() {
if (this.getAttribute('button') === 'true') {
Array.from(this.querySelectorAll('a')).forEach((a) => this.convertLink(a));
}
this.eager = parseInt(this.getAttribute('eager') || 0, 10);
this.classList.add('inner');
if (this.eager) {
Expand Down
5 changes: 5 additions & 0 deletions blocks/column/column.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
raqn-column {
margin: var(--scope-margin, 0);
width: 100%;
display: grid;
}
71 changes: 71 additions & 0 deletions blocks/column/column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import ComponentBase from '../../scripts/component-base.js';

// ! Solution 1 to remove mixins
export default class ColumnN extends ComponentBase {
static observedAttributes = ['position', 'size', 'justify'];

connected() {
this.position = parseInt(this.getAttribute('position'), 10);
this.size = this.getAttribute('size');
this.justify = this.getAttribute('justify') || 'stretch';
this.calculateGridTemplateColumns();
}

calculateGridTemplateColumns() {
this.style.setProperty('justify-content', this.justify);
if (this.position) {
const parent = this.parentElement;
const children = Array.from(parent.children);
parent.classList.add('raqn-grid');
let parentGridTemplateColumns = parent.style.getPropertyValue(
'--grid-template-columns',
);
if (!parentGridTemplateColumns) {
// we have no grid template columns yet
parentGridTemplateColumns = children
.map((child, index) => {
if (this.position === index + 1) {
return this.size || 'auto';
}
return 'auto';
})
.join(' ');
// set the new grid template columns
parent.style.setProperty(
'--grid-template-columns',
parentGridTemplateColumns,
);
} else {
const { position } = this;
const prio = children.indexOf(this) + 1;
parentGridTemplateColumns = parentGridTemplateColumns
.split(' ')
.map((size, i) => {
// we have a non standard value for this position
const hasValue = size !== 'auto';
// we are at the position
const isPosition = i + 1 === position;
// we are at a position before the prio
const isBeforePrio = i + 1 <= prio;
// we have a non standard value for this position and we are at the position
if (!hasValue && isPosition) {
return this.size || 'auto';
}
// we have a non standard value for this position and we are at a position before the prio
if (hasValue && isPosition && isBeforePrio) {
return this.size || size;
}
return size;
})
.join(' ');
// set the new grid template columns
parent.style.setProperty(
'--grid-template-columns',
parentGridTemplateColumns,
);
}
this.style.gridColumn = this.position;
this.style.gridRow = 1;
}
}
}
3 changes: 3 additions & 0 deletions blocks/columns/columns.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
raqn-column {
margin: var(--scope-margin, 0);
}
94 changes: 94 additions & 0 deletions blocks/columns/columns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { collectAttributes } from '../../scripts/libs.js';

// ! Solution 2 to remove mixins
export default class Columns {
static observedAttributes = ['position', 'size', 'justify'];

constructor(data) {
this.element = data.target;

const { currentAttributes } = collectAttributes(
data.componentName,
data.rawClasses,
[],
Columns.observedAttributes,
this.element,
);

Object.keys(currentAttributes).forEach((key) => {
this.element.setAttribute(key, currentAttributes[key]);
});

this.position = parseInt(this.getAttribute('position'), 10);
this.size = this.getAttribute('size');
this.justify = this.getAttribute('justify') || 'stretch';
this.calculateGridTemplateColumns();
}

calculateGridTemplateColumns() {
if (this.justify) {
this.element.style.justifyContent = this.justify;
}
if (this.position) {
const parent = this.element.parentElement;
const children = Array.from(parent.children);
parent.classList.add('raqn-grid');
let parentGridTemplateColumns = parent.style.getPropertyValue(
'--grid-template-columns',
);
if (!parentGridTemplateColumns) {
// we have no grid template columns yet
parentGridTemplateColumns = children
.map((child, index) => {
if (this.position === index + 1) {
return this.size || 'auto';
}
return 'auto';
})
.join(' ');
// set the new grid template columns
parent.style.setProperty(
'--grid-template-columns',
parentGridTemplateColumns,
);
} else {
const { position } = this;
const prio = children.indexOf(this.element) + 1;
parentGridTemplateColumns = parentGridTemplateColumns
.split(' ')
.map((size, i) => {
// we have a non standard value for this position
const hasValue = size !== 'auto';
// we are at the position
const isPosition = i + 1 === position;
// we are at a position before the prio
const isBeforePrio = i + 1 <= prio;
// we have a non standard value for this position and we are at the position
if (!hasValue && isPosition) {
return this.size || 'auto';
}
// we have a non standard value for this position and we are at a position before the prio
if (hasValue && isPosition && isBeforePrio) {
return this.size || size;
}
return size;
})
.join(' ');
// set the new grid template columns
parent.style.setProperty(
'--grid-template-columns',
parentGridTemplateColumns,
);
}
this.element.style.gridColumn = this.position;
this.element.style.gridRow = 1;
}
}

getAttribute(name) {
return (
this.element.getAttribute(name) ||
this.element.getAttribute(`data-${name}`)
);
}
}
19 changes: 17 additions & 2 deletions blocks/footer/footer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import ComponentBase from '../../scripts/component-base.js';
import { getMeta } from '../../scripts/libs.js';

const metaFooter = getMeta('footer');
const metaFragment = !!metaFooter && `${metaFooter}.plain.html`;
export default class Footer extends ComponentBase {
// keep as it is
fragment = 'footer.plain.html';
fragment = metaFragment || 'footer.plain.html';

extendConfig() {
return [
...super.extendConfig(),
{
addToTargetMethod: 'append',
},
];
}

static earlyStopRender() {
return metaFragment === false;
}

ready() {
const child = this.children[0];
Expand Down
Loading

0 comments on commit 4b8c4c2

Please sign in to comment.