• Will show more text when the user clicks the toggle.
  • Will show less text when the user clicks the toggle again.
<script type="module">
    import {
        generateMarkup
    } from '../raw/c-show-more/c-show-more.js';
    generateMarkup(document.querySelectorAll(
        'p.js-show-more--one-line:not(.js-show-more--link)'
    ), {
        startIndex: 0
    });
    generateMarkup(document.querySelectorAll(
        'p.js-show-more--one-line.js-show-more--link'
    ), {
        shouldToggleResembleLink: true,
        startIndex: 10
    });
    generateMarkup(document.querySelectorAll(
        'p.js-show-more--many-lines:not(.js-show-more--link)'
    ), {
        lines: 3,
        startIndex: 20
    });
    generateMarkup(document.querySelectorAll(
        'p.js-show-more--many-lines.js-show-more--link'
    ), {
        lines: 3,
        shouldToggleResembleLink: true,
        startIndex: 30
    });
</script>

<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>
                <dl>
                    <dt>Input:</dt>
                    <dd>
                        <pre><code>&lt;p class="js-show-more--one-line"&gt;
  This is a JavaScript-enhanced single line example that will be truncated with an ellipsis. The markup is simpler, and the JavaScript will convert it to the full component structure. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
&lt;/p&gt;</code></pre>
                    </dd>
                    <dt>Output:</dt>
                    <dd>
                        <p class="js-show-more--one-line">
                            This is a JavaScript-enhanced single line example that will be truncated with an ellipsis. The markup is simpler, and the JavaScript will convert it to the full component structure. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                        </p>
                    </dd>
                </dl>
            </dd>
            <dt>With Link-Style Toggle:</dt>
            <dd>
                <dl>
                    <dt>Input:</dt>
                    <dd>
                        <pre><code>&lt;p class="js-show-more--one-line js-show-more--link"&gt;
  This is a JavaScript-enhanced single line example that will be truncated with an ellipsis. The markup is simpler, and the JavaScript will convert it to the full component structure. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
&lt;/p&gt;</code></pre>
                    </dd>
                    <dt>Output:</dt>
                    <dd>
                        <p class="js-show-more--one-line js-show-more--link">
                            This is a JavaScript-enhanced single line example that will be truncated with an ellipsis. The markup is simpler, and the JavaScript will convert it to the full component structure. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                        </p>
                    </dd>
                </dl>
            </dd>
        </dl>
    </dd>

    <dt>Many Lines Example</dt>
    <dd>
        <dl>
            <dt>Default:</dt>
            <dd>
                <dl>
                    <dt>Input:</dt>
                    <dd>
                        <pre><code>&lt;p class="js-show-more--many-lines"&gt;
  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.
&lt;/p&gt;</code></pre>
                    </dd>
                    <dt>Output:</dt>
                    <dd>
                        <p class="js-show-more--many-lines">
                            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.
                        </p>
                    </dd>
                </dl>
            </dd>
            <dt>With Link-Style Toggle:</dt>
            <dd>
                <dl>
                    <dt>Input:</dt>
                    <dd>
                        <pre><code>&lt;p class="js-show-more--many-lines js-show-more--link"&gt;
  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.
&lt;/p&gt;</code></pre>
                    </dd>
                    <dt>Output:</dt>
                    <dd>
                        <p class="js-show-more--many-lines js-show-more--link">
                            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.
                        </p>
                    </dd>
                </dl>
            </dd>
        </dl>
    </dd>
</dl>
  • Content:
    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);
      });
    }
    
  • URL: /components/raw/c-show-more/c-show-more.js
  • Filesystem Path: src/lib/_imports/components/c-show-more/c-show-more.js
  • Size: 2.3 KB
{
  "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"
}