debian-mirror-gitlab/app/assets/javascripts/read_more.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
/**
* ReadMore
*
* Adds "read more" functionality to elements.
*
* Specifically, it looks for a trigger, by default ".js-read-more-trigger", and adds the class
* "is-expanded" to the previous element in order to provide a click to expand functionality.
*
* This is useful for long text elements that you would like to truncate, especially for mobile.
*
* Example Markup
* <div class="read-more-container">
* <p>Some text that should be long enough to have to truncate within a specified container.</p>
* <p>This text will not appear in the container, as only the first line can be truncated.</p>
* <p>This should also not appear, if everything is working correctly!</p>
* </div>
* <button class="js-read-more-trigger">Read more</button>
*
*/
export default function initReadMore(triggerSelector = '.js-read-more-trigger') {
const triggerEls = document.querySelectorAll(triggerSelector);
if (!triggerEls) return;
2021-03-08 18:12:59 +05:30
triggerEls.forEach((triggerEl) => {
2018-11-20 20:47:30 +05:30
const targetEl = triggerEl.previousElementSibling;
if (!targetEl) {
return;
}
triggerEl.addEventListener(
'click',
2023-03-17 16:20:25 +05:30
() => {
2018-11-20 20:47:30 +05:30
targetEl.classList.add('is-expanded');
2023-03-17 16:20:25 +05:30
triggerEl.remove();
2018-11-20 20:47:30 +05:30
},
{ once: true },
);
});
}