<h3>Using Generated Markup</h3>
<p>JavaScript will convert static markup into a full component structure.</p>
<dl>
<dt>One Line Example</dt>
<dd>
<dl>
<dt>Default:</dt>
<dd>
<p class="c-show-more c-show-more--one-line">
<input type="checkbox" class="c-show-more__state" id="show-more-1">
<span class="c-show-more__target">
This is a single line of text that will be truncated with an ellipsis when it gets too long for its container width. The full text will be revealed when you click "Show More".
</span>
<label class="c-show-more__toggle" for="show-more-1">
<span class="c-show-more__on-text">Show More</span>
<span class="c-show-more__off-text">Show Less</span>
</label>
</p>
</dd>
<dt>With Link-Style Toggle:</dt>
<dd>
<p class="c-show-more c-show-more--one-line">
<input type="checkbox" class="c-show-more__state" id="show-more-2">
<span class="c-show-more__target">
This is a single line of text that will be truncated with an ellipsis when it gets too long for its container width. The full text will be revealed when you click "Show More".
</span>
<label class="c-show-more__toggle" for="show-more-2">
<span class="x-link c-show-more__on-text">Show More</span>
<span class="x-link c-show-more__off-text">Show Less</span>
</label>
</p>
</dd>
</dl>
</dd>
<dt>Many Lines Example</dt>
<dd>
<dl>
<dt>Default:</dt>
<dd>
<p class="c-show-more c-show-more--many-lines">
<input type="checkbox" class="c-show-more__state" id="show-more-3">
<span class="c-show-more__target">
This is a JavaScript-enhanced multi-line example. The markup starts simple, then JavaScript builds the full component structure. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</span>
<label class="c-show-more__toggle" for="show-more-3">
<span class="c-show-more__on-text">Show More</span>
<span class="c-show-more__off-text">Show Less</span>
</label>
</p>
</dd>
<dt>With Link-Style Toggle:</dt>
<dd>
<p class="c-show-more c-show-more--many-lines">
<input type="checkbox" class="c-show-more__state" id="show-more-4">
<span class="c-show-more__target">
This is a JavaScript-enhanced multi-line example. The markup starts simple, then JavaScript builds the full component structure. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</span>
<label class="c-show-more__toggle" for="show-more-4">
<span class="x-link c-show-more__on-text">Show More</span>
<span class="x-link c-show-more__off-text">Show Less</span>
</label>
</p>
</dd>
</dl>
</dd>
</dl>
export const defaults = {
elements: document.querySelectorAll(`
[class*="js-show-more"]
`),
options: {
lines: 0,
targetTag: 'span',
shouldToggleResembleLink: false,
startIndex: 0,
},
};
/**
* Convert elements with js-show-more--* classes into show-more components
*/
export function generateMarkup(
elements = defaults.elements,
options = defaults.options
) {
options = { ...defaults.options, ...options };
[...elements].forEach((element, index) => {
if (element.querySelector('p, div, section, article, aside, nav, header, hgroup, footer, main')) {
console.warn(
'A "c-show-more" component expects only text and inline elements. Block elements may not truncate correctly. Consider restructuring content to avoid block elements.',
element
);
}
const wrapper = document.createElement(element.tagName);
wrapper.className = element.className;
wrapper.classList.add('c-show-more');
wrapper.classList.add(`c-show-more--${options.lines <= 1 ? 'one-line' : 'many-lines'}`);
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'c-show-more__state';
checkbox.id = `show-more-${options.startIndex + index}`;
const target = document.createElement(options.targetTag);
target.className = 'c-show-more__target';
target.innerHTML = element.innerHTML;
if (options.lines) {
target.style.setProperty('--lines', options.lines);
}
const toggle = document.createElement('label');
toggle.className = 'c-show-more__toggle';
toggle.htmlFor = checkbox.id;
const onText = document.createElement('span');
onText.className =
`${options.shouldToggleResembleLink ? 'x-link ' : ''}`
+ 'c-show-more__on-text';
onText.textContent = 'Show More';
const offText = document.createElement('span');
offText.className =
`${options.shouldToggleResembleLink ? 'x-link ' : ''}`
+ 'c-show-more__off-text';
offText.textContent = 'Show Less';
toggle.appendChild(onText);
toggle.appendChild(offText);
wrapper.appendChild(checkbox);
wrapper.appendChild(target);
wrapper.appendChild(toggle);
element.replaceWith(wrapper);
});
}
{
"shouldSkipPattern": false,
"globalStyles": [
{
"isInternal": true,
"layer": "base",
"path": "/assets/core-styles.demo.css"
},
{
"isInternal": true,
"layer": "base",
"path": "/assets/core-styles.base.css"
}
],
"cmsStyles": [
{
"isInternal": true,
"layer": "base",
"path": "/assets/core-styles.cms.css"
}
],
"docsStyles": [
{
"isInternal": true,
"layer": "base",
"path": "/assets/core-styles.docs.css"
}
],
"portalStyles": [
{
"isInternal": true,
"layer": "base",
"path": "/assets/core-styles.portal.css"
}
],
"subdir": "components",
"📝 shouldSkipPattern": "because core-styles.….css does not import this"
}