Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions packages/components/src/web-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,55 @@ class WebPreview extends Component {
}

/**
* Add or remove applicable body classes
* Add or remove applicable body classes and keyboard event listeners
*/
componentDidUpdate() {
componentDidUpdate( prevProps, prevState ) {
if ( this.state.isPreviewVisible === true ) {
document.body.classList.add( 'newspack-web-preview--open' );
if ( prevState.isPreviewVisible === false ) {
// Add event listener when modal opens
document.addEventListener( 'keydown', this.handleKeyDown );
}
} else {
document.body.classList.remove( 'newspack-web-preview--open' );
if ( prevState.isPreviewVisible === true ) {
// Remove event listener when modal closes
document.removeEventListener( 'keydown', this.handleKeyDown );
}
}
}

/**
* Clean up event listeners on unmount
*/
componentWillUnmount() {
document.removeEventListener( 'keydown', this.handleKeyDown );
document.body.classList.remove( 'newspack-web-preview--open' );
}

/**
* Handle keyboard events
*/
handleKeyDown = event => {
if ( event.key === 'Escape' && this.state.isPreviewVisible ) {
this.closePreview();
}
};

/**
* Close the preview modal
*/
closePreview = () => {
const { onClose = () => {} } = this.props;
onClose();
this.setState( { isPreviewVisible: false, loaded: false } );
};

/**
* Create JSX for the modal
*/
getWebPreviewModal = () => {
const { beforeLoad = () => {}, onClose = () => {}, url, title } = this.props;
const { beforeLoad = () => {}, url, title } = this.props;
const { device, loaded, isPreviewVisible } = this.state;

if ( ! this.modalDOMElement || ! isPreviewVisible ) {
Expand All @@ -83,7 +117,7 @@ class WebPreview extends Component {
<div className="newspack-web-preview__toolbar">
{ title && (
<div className="newspack-web-preview__toolbar-left">
<h2>{ title }</h2>
<h3>{ title }</h3>
</div>
) }
<div className="newspack-web-preview__toolbar-right">
Expand Down Expand Up @@ -126,14 +160,7 @@ class WebPreview extends Component {
</>
) }
</DropdownMenu>
<Button
onClick={ () => {
onClose();
this.setState( { isPreviewVisible: false, loaded: false } );
} }
icon={ close }
label={ __( 'Close Preview', 'newspack-plugin' ) }
/>
<Button onClick={ this.closePreview } icon={ close } label={ __( 'Close Preview', 'newspack-plugin' ) } />
</div>
</div>
<div className="newspack-web-preview__content">
Expand Down
9 changes: 7 additions & 2 deletions packages/components/src/web-preview/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@
align-items: center;
border-bottom: 1px solid wp-colors.$gray-300;
display: flex;
gap: 16px;
justify-content: space-between;
height: 71px;
padding: 0 24px;
min-height: 71px;
padding: 12px 24px;

&-left {
display: flex;

h3 {
margin: 0;
}
}

&-right {
Expand Down
14 changes: 10 additions & 4 deletions src/wizards/audience/components/segment-group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* WordPress dependencies.
*/
import { __ } from '@wordpress/i18n';
import { sprintf, __ } from '@wordpress/i18n';
import { useState, Fragment } from '@wordpress/element';
import { blockTable, header, layout, postList } from '@wordpress/icons';

Expand Down Expand Up @@ -58,13 +58,15 @@ const SegmentGroup = props => {
{ id ? (
<Button
href={ `#/segments/${ id }` }
label={ __( 'Edit Segment ', 'newspack-plugin' ) }
label={ __( 'Edit Segment', 'newspack-plugin' ) }
isLink
showTooltip
tooltipPosition="bottom center"
>
{ __( 'Segment: ', 'newspack-plugin' ) }
{ label }
{
/* translators: %s: segment label */
sprintf( __( 'Segment: %s', 'newspack-plugin' ), label )
}
</Button>
) : (
label
Expand All @@ -84,6 +86,10 @@ const SegmentGroup = props => {
{ __( 'Preview Segment', 'newspack-plugin' ) }
</Button>
) }
title={
/* translators: %s: segment label */
sprintf( __( 'Segment: %s', 'newspack-plugin' ), label )
}
/>
{ 'unassigned' !== campaignId && (
<Fragment>
Expand Down
10 changes: 7 additions & 3 deletions src/wizards/audience/views/campaigns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import '../../../../shared/js/public-path';
* WordPress dependencies.
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';

/**
Expand Down Expand Up @@ -56,6 +56,7 @@ class AudienceCampaigns extends Component {
segments: [],
settings: [],
previewUrl: null,
previewTitle: null,
duplicated: null,
inFlight: false,
};
Expand Down Expand Up @@ -212,10 +213,12 @@ class AudienceCampaigns extends Component {

render() {
const { pluginRequirements, setError, isLoading, wizardApiFetch, startLoading, doneLoading } = this.props;
const { campaigns, inFlight, prompts, segments, settings, previewUrl, duplicated } = this.state;
const { campaigns, inFlight, prompts, segments, settings, previewUrl, previewTitle, duplicated } = this.state;
return (
<WebPreview
url={ previewUrl }
title={ previewTitle ? /* translators: %s: prompt title */ sprintf( __( 'Prompt: %s', 'newspack-plugin' ), previewTitle ) : null }
onClose={ () => this.setState( { previewUrl: null, previewTitle: null } ) }
renderButton={ ( { showPreview } ) => {
const sharedProps = {
headerText,
Expand All @@ -238,7 +241,8 @@ class AudienceCampaigns extends Component {
deletePopup: this.deletePopup,
restorePopup: this.restorePopup,
duplicatePopup: this.duplicatePopup,
previewPopup: popup => this.setState( { previewUrl: this.previewUrlForPopup( popup ) }, () => showPreview() ),
previewPopup: popup =>
this.setState( { previewUrl: this.previewUrlForPopup( popup ), previewTitle: popup.title }, () => showPreview() ),
publishPopup: this.publishPopup,
resetDuplicated: () => this.setState( { duplicated: null } ),
unpublishPopup: this.unpublishPopup,
Expand Down