@@ -32,23 +32,13 @@ document.addEventListener('DOMContentLoaded', () => {
3232 abstracts . forEach ( abstract => {
3333 const content = abstract . querySelector ( '.abstract-content' ) ;
3434 const button = abstract . querySelector ( '.toggle-button' ) ;
35- const lineClamp = 8 ; // Number of lines set in line-clamp
3635
37- // Function to count the number of lines in the content
38- const countLines = ( element ) => {
39- const computedStyle = window . getComputedStyle ( element ) ;
40- const lineHeight = parseFloat ( computedStyle . lineHeight ) ;
41- const height = parseFloat ( computedStyle . height ) ;
42- return Math . round ( height / lineHeight ) ;
43- } ;
44-
45- // Determine if the content exceeds the line-clamp
36+ // Function to determine if the content is truncated
4637 const isTruncated = ( ) => {
47- const totalLines = countLines ( content ) ;
48- return totalLines > lineClamp ;
38+ return content . scrollHeight > content . clientHeight ;
4939 } ;
5040
51- // Show or hide the toggle button based on content length
41+ // Show or hide the toggle button based on truncation
5242 if ( isTruncated ( ) ) {
5343 button . classList . remove ( 'hidden' ) ;
5444 } else {
@@ -60,22 +50,41 @@ document.addEventListener('DOMContentLoaded', () => {
6050
6151 toggleButtons . forEach ( button => {
6252 button . addEventListener ( 'click' , ( ) => {
63- const wrapper = button . closest ( '.abstract-wrapper' ) ;
64- const content = wrapper . querySelector ( '.abstract-content' ) ;
65-
66- // Toggle expanded state
67- // wrapper.classList.toggle('max-h-60'); // Tailwind's max-height: 13rem (52 * 0.25rem)
68- // content.classList.toggle('max-h-48'); // Tailwind's max-height: 13rem (52 * 0.25rem)
69- content . classList . toggle ( 'line-clamp-[8]' ) ;
70- content . classList . toggle ( 'line-clamp-none' ) ;
53+ const abstract = button . closest ( '.abstract-wrapper' ) ;
54+ const content = abstract . querySelector ( '.abstract-content' ) ;
55+
56+ // Toggle the line-clamp class
57+ content . classList . toggle ( 'line-clamp-[10]' ) ;
7158
72- // Update button text
73- const isExpanded = ! content . classList . contains ( 'line-clamp-[8 ]' ) ;
74- const buttonText = button . querySelector ( '.button-text' )
59+ // Update button text based on the new state
60+ const isExpanded = ! content . classList . contains ( 'line-clamp-[10 ]' ) ;
61+ const buttonText = button . querySelector ( '.button-text' ) ;
7562 buttonText . textContent = isExpanded ? 'Show Less' : 'Show More' ;
76-
77- // Update aria-expanded attribute for accessibility
78- // button.setAttribute('aria-expanded', isExpanded);
63+ } ) ;
64+ } ) ;
65+
66+ // Re-evaluate button visibility on window resize for responsiveness
67+ window . addEventListener ( 'resize' , ( ) => {
68+ abstracts . forEach ( abstract => {
69+ const content = abstract . querySelector ( '.abstract-content' ) ;
70+ const button = abstract . querySelector ( '.toggle-button' ) ;
71+
72+ // Reset line-clamp to accurately measure
73+ content . classList . add ( 'line-clamp-[10]' ) ;
74+ // content.classList.remove('line-clamp-none'); // Ensure no conflicting classes
75+
76+ // Determine truncation
77+ if ( content . scrollHeight > content . clientHeight ) {
78+ button . classList . remove ( 'hidden' ) ;
79+ } else {
80+ button . classList . add ( 'hidden' ) ;
81+ }
82+
83+ // If content is not truncated, ensure it is not expanded
84+ if ( ! content . classList . contains ( 'line-clamp-[10]' ) && ! button . classList . contains ( 'hidden' ) ) {
85+ content . classList . add ( 'line-clamp-[10]' ) ;
86+ button . querySelector ( '.button-text' ) . textContent = 'Show More' ;
87+ }
7988 } ) ;
8089 } ) ;
8190} ) ;
0 commit comments