Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions blocks/overview/overview.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@
}

.overview .overview-title {
color: var(--text-color);
font-size: 21px;
font-style: normal;
font-weight: 600;
line-height: 30px;
letter-spacing: -0.6px;
margin-bottom: var(--spacing-xs);
}

.overview .overview-para,
.overview .overview-hiddenPara {
color: var(--text-color);
font-size: var(--body-font-size-s);
font-weight: 400;
line-height: 24px;
letter-spacing: -0.5px;
}

.overview .overview-hiddenPara {
margin-top: 1rem;
font-size: var(--body-font-size-m);
line-height: 1.4;
}

.overview .overview-readMore {
margin-top: 1rem;
font-size: var(--body-font-size-s);
font-weight: 600;
color: #54565b;
font-size: var(--body-font-size-m);
color: var(--text-red-color);
font-weight: 500;
cursor: pointer;
background-color: transparent !important;
padding: 0;
width: max-content;
}

/* Inline read more inside single truncated paragraph */
.overview .overview-para .overview-readMore {
display: inline;
margin-left: 0;
user-select: none;
}

.overview .overview-para--expanded .overview-readMore {
margin-left: 0.25em;
}
55 changes: 53 additions & 2 deletions blocks/overview/overview.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const WORD_LIMIT = 34;

function splitWords(text) {
return text.trim().split(/\s+/).filter(Boolean);
}

export default function decorate(block) {
block.id = 'overview';
const heading = block.querySelector('h2');
Expand All @@ -7,8 +13,53 @@ export default function decorate(block) {
if (heading) heading.classList.add('overview-title');
if (paragraphs[0]) paragraphs[0].classList.add('overview-para');

// If there's more than one paragraph, create hidden container and button
if (paragraphs.length > 1) {
// Single paragraph: truncate to first WORD_LIMIT words with inline Read more / Read less
if (paragraphs.length === 1 && paragraphs[0]) {
const p = paragraphs[0];
const words = splitWords(p.textContent);
if (words.length > WORD_LIMIT) {
const fullText = words.join(' ');
const truncatedText = words.slice(0, WORD_LIMIT).join(' ');
p.textContent = '';

const textSpan = document.createElement('span');
textSpan.className = 'overview-paraText';

const ellipsisSpan = document.createElement('span');
ellipsisSpan.className = 'overview-ellipsis';
ellipsisSpan.textContent = '... ';

const readMoreBtn = document.createElement('span');
readMoreBtn.className = 'overview-readMore';
readMoreBtn.setAttribute('role', 'button');
readMoreBtn.tabIndex = 0;
readMoreBtn.textContent = 'Read more';

let expanded = false;
const sync = () => {
textSpan.textContent = expanded ? fullText : truncatedText;
ellipsisSpan.hidden = expanded;
readMoreBtn.textContent = expanded ? 'Read less' : 'Read more';
p.classList.toggle('overview-para--expanded', expanded);
};

const toggle = () => {
expanded = !expanded;
sync();
};

readMoreBtn.addEventListener('click', toggle);
readMoreBtn.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggle();
}
});

p.append(textSpan, ellipsisSpan, readMoreBtn);
sync();
}
} else if (paragraphs.length > 1) {
// Create the hidden container
const hiddenWrapper = document.createElement('div');
hiddenWrapper.classList.add('overview-hiddenPara');
Expand Down
3 changes: 2 additions & 1 deletion templates/mayura/mayura.css
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ body {

.mayura main .cards-container h2,
.mayura main .cards-icon-container h2,
.mayura main .tabs-container h2 {
.mayura main .tabs-container h2,
.mayura main .overview-container h2 {
text-align: center;
font-style: normal;
font-weight: 700;
Expand Down
Loading