2019-08-15 14:55:57 +00:00
|
|
|
/**
|
2019-11-08 16:30:11 +00:00
|
|
|
* External dependencies
|
2019-08-15 14:55:57 +00:00
|
|
|
*/
|
2023-03-02 14:26:00 +00:00
|
|
|
import { createRef, Component } from '@wordpress/element';
|
2019-08-15 14:55:57 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-03-02 14:26:00 +00:00
|
|
|
import type { MouseEvent, RefObject, ReactNode } from 'react';
|
2019-08-15 14:55:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { clampLines } from './utils';
|
|
|
|
|
2021-12-06 17:00:33 +00:00
|
|
|
export interface ReadMoreProps {
|
|
|
|
/**
|
|
|
|
* The entire content to clamp
|
|
|
|
*/
|
2023-03-02 14:26:00 +00:00
|
|
|
children: ReactNode;
|
2021-12-06 17:00:33 +00:00
|
|
|
/**
|
|
|
|
* Class names for the wrapped component
|
|
|
|
*/
|
|
|
|
className: string;
|
|
|
|
/**
|
|
|
|
* What symbol to show after the allowed lines are reached
|
|
|
|
*
|
|
|
|
* @default '&hellip';
|
|
|
|
*/
|
|
|
|
ellipsis: string;
|
|
|
|
/**
|
|
|
|
* The string to show to collapse the entire text into its clamped form
|
|
|
|
*
|
|
|
|
* @default 'Read less'
|
|
|
|
*/
|
|
|
|
lessText: string;
|
|
|
|
/**
|
|
|
|
* How many lines to show before the text is clamped
|
|
|
|
*
|
|
|
|
* @default 3
|
|
|
|
*/
|
|
|
|
maxLines: number;
|
|
|
|
/**
|
|
|
|
* The string to show to expande the entire text
|
|
|
|
*
|
|
|
|
* @default 'Read more'
|
|
|
|
*/
|
|
|
|
moreText: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ReadMoreState {
|
|
|
|
/**
|
|
|
|
* This is true when read more has been pressed and the full review is shown.
|
|
|
|
*/
|
|
|
|
isExpanded: boolean;
|
|
|
|
/**
|
|
|
|
* True if we are clamping content. False if the review is short. Null during init.
|
|
|
|
*/
|
|
|
|
clampEnabled: boolean | null;
|
|
|
|
/**
|
|
|
|
* Content is passed in via children.
|
|
|
|
*/
|
2023-03-02 14:26:00 +00:00
|
|
|
content: ReactNode;
|
2021-12-06 17:00:33 +00:00
|
|
|
/**
|
|
|
|
* Summary content generated from content HTML.
|
|
|
|
*/
|
|
|
|
summary: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultProps = {
|
|
|
|
className: 'read-more-content',
|
|
|
|
ellipsis: '…',
|
|
|
|
lessText: __( 'Read less', 'woo-gutenberg-products-block' ),
|
|
|
|
maxLines: 3,
|
|
|
|
moreText: __( 'Read more', 'woo-gutenberg-products-block' ),
|
|
|
|
};
|
|
|
|
|
2019-11-08 16:30:11 +00:00
|
|
|
/**
|
|
|
|
* Show text based content, limited to a number of lines, with a read more link.
|
|
|
|
*
|
|
|
|
* Based on https://github.com/zoltantothcom/react-clamp-lines.
|
|
|
|
*/
|
2021-12-06 17:00:33 +00:00
|
|
|
class ReadMore extends Component< ReadMoreProps, ReadMoreState > {
|
|
|
|
static defaultProps = defaultProps;
|
|
|
|
|
2023-03-02 14:26:00 +00:00
|
|
|
private reviewSummary: RefObject< HTMLDivElement >;
|
|
|
|
private reviewContent: RefObject< HTMLDivElement >;
|
2021-12-06 17:00:33 +00:00
|
|
|
|
|
|
|
constructor( props: ReadMoreProps ) {
|
|
|
|
super( props );
|
2019-08-15 14:55:57 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
/**
|
|
|
|
* This is true when read more has been pressed and the full review is shown.
|
|
|
|
*/
|
|
|
|
isExpanded: false,
|
|
|
|
/**
|
|
|
|
* True if we are clamping content. False if the review is short. Null during init.
|
|
|
|
*/
|
|
|
|
clampEnabled: null,
|
|
|
|
/**
|
|
|
|
* Content is passed in via children.
|
|
|
|
*/
|
|
|
|
content: props.children,
|
|
|
|
/**
|
|
|
|
* Summary content generated from content HTML.
|
|
|
|
*/
|
|
|
|
summary: '.',
|
|
|
|
};
|
|
|
|
|
2021-12-06 17:00:33 +00:00
|
|
|
this.reviewContent = createRef< HTMLDivElement >();
|
|
|
|
this.reviewSummary = createRef< HTMLDivElement >();
|
2019-08-15 14:55:57 +00:00
|
|
|
this.getButton = this.getButton.bind( this );
|
|
|
|
this.onClick = this.onClick.bind( this );
|
|
|
|
}
|
|
|
|
|
2021-12-06 17:00:33 +00:00
|
|
|
componentDidMount(): void {
|
2022-01-31 12:56:19 +00:00
|
|
|
this.setSummary();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps: ReadMoreProps ): void {
|
|
|
|
if (
|
|
|
|
prevProps.maxLines !== this.props.maxLines ||
|
|
|
|
prevProps.children !== this.props.children
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
* if maxLines or content changed we need to reset the state to
|
|
|
|
* initial values so that summary can be calculated again
|
|
|
|
*/
|
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
clampEnabled: null,
|
|
|
|
summary: '.',
|
|
|
|
},
|
|
|
|
this.setSummary
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSummary(): void {
|
2019-08-15 14:55:57 +00:00
|
|
|
if ( this.props.children ) {
|
|
|
|
const { maxLines, ellipsis } = this.props;
|
|
|
|
|
2021-12-06 17:00:33 +00:00
|
|
|
if (
|
|
|
|
! this.reviewSummary.current ||
|
|
|
|
! this.reviewContent.current
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:55:57 +00:00
|
|
|
const lineHeight = this.reviewSummary.current.clientHeight + 1;
|
|
|
|
const reviewHeight = this.reviewContent.current.clientHeight + 1;
|
2019-09-05 15:09:31 +00:00
|
|
|
const maxHeight = lineHeight * maxLines + 1;
|
2019-08-15 14:55:57 +00:00
|
|
|
const clampEnabled = reviewHeight > maxHeight;
|
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
clampEnabled,
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( clampEnabled ) {
|
|
|
|
this.setState( {
|
2019-09-05 15:09:31 +00:00
|
|
|
summary: clampLines(
|
|
|
|
this.reviewContent.current.innerHTML,
|
|
|
|
this.reviewSummary.current,
|
|
|
|
maxHeight,
|
|
|
|
ellipsis
|
|
|
|
),
|
2019-08-15 14:55:57 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 17:00:33 +00:00
|
|
|
getButton(): JSX.Element | undefined {
|
2019-08-15 14:55:57 +00:00
|
|
|
const { isExpanded } = this.state;
|
|
|
|
const { className, lessText, moreText } = this.props;
|
|
|
|
|
|
|
|
const buttonText = isExpanded ? lessText : moreText;
|
|
|
|
|
|
|
|
if ( ! buttonText ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
href="#more"
|
|
|
|
className={ className + '__read_more' }
|
|
|
|
onClick={ this.onClick }
|
|
|
|
aria-expanded={ ! isExpanded }
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
{ buttonText }
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the click event for the read more/less button.
|
|
|
|
*/
|
2023-03-02 14:26:00 +00:00
|
|
|
onClick( e: MouseEvent< HTMLAnchorElement, MouseEvent > ): void {
|
2019-08-15 14:55:57 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const { isExpanded } = this.state;
|
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
isExpanded: ! isExpanded,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-12-06 17:00:33 +00:00
|
|
|
render(): JSX.Element | null {
|
2019-08-15 14:55:57 +00:00
|
|
|
const { className } = this.props;
|
|
|
|
const { content, summary, clampEnabled, isExpanded } = this.state;
|
|
|
|
|
|
|
|
if ( ! content ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-07 14:45:45 +00:00
|
|
|
if ( clampEnabled === false ) {
|
2019-08-15 14:55:57 +00:00
|
|
|
return (
|
|
|
|
<div className={ className }>
|
2019-09-05 15:09:31 +00:00
|
|
|
<div ref={ this.reviewContent }>{ content }</div>
|
2019-08-15 14:55:57 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ className }>
|
2019-11-07 14:45:45 +00:00
|
|
|
{ ( ! isExpanded || clampEnabled === null ) && (
|
2019-08-15 14:55:57 +00:00
|
|
|
<div
|
|
|
|
ref={ this.reviewSummary }
|
|
|
|
aria-hidden={ isExpanded }
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html: summary,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
2019-11-07 14:45:45 +00:00
|
|
|
{ ( isExpanded || clampEnabled === null ) && (
|
2019-09-09 10:52:48 +00:00
|
|
|
<div
|
|
|
|
ref={ this.reviewContent }
|
|
|
|
aria-hidden={ ! isExpanded }
|
|
|
|
>
|
2019-08-15 14:55:57 +00:00
|
|
|
{ content }
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
{ this.getButton() }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ReadMore;
|