File size: 7,346 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | /* eslint-disable wpcalypso/jsx-classname-namespace */
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { filter, isEqual } from 'lodash';
import PropTypes from 'prop-types';
import { Component, createRef } from 'react';
import { findDOMNode } from 'react-dom';
import ResizableIframe from 'calypso/components/resizable-iframe';
import { hasTouch } from 'calypso/lib/touch-detect';
import ButtonsPreviewButton from './preview-button';
import previewWidget from './preview-widget';
class SharingButtonsPreviewButtons extends Component {
static displayName = 'SharingButtonsPreviewButtons';
static propTypes = {
buttons: PropTypes.array,
visibility: PropTypes.oneOf( [ 'hidden', 'visible' ] ),
style: PropTypes.oneOf( [ 'icon', 'icon-text', 'text', 'official' ] ),
onButtonClick: PropTypes.func,
showMore: PropTypes.bool,
};
static defaultProps = {
buttons: Object.freeze( [] ),
style: 'icon',
onButtonClick: function () {},
showMore: false,
};
previewIframeRef = createRef();
moreButtonRef = createRef();
state = {
morePreviewOffset: null,
morePreviewVisible: false,
};
componentDidMount() {
this.maybeListenForWidgetMorePreview();
this.updateMorePreviewVisibility();
document.addEventListener( 'click', this.hideMorePreview );
}
componentDidUpdate( prevProps ) {
this.maybeListenForWidgetMorePreview();
if (
prevProps.showMore !== this.props.showMore ||
! isEqual( prevProps.buttons, this.props.buttons )
) {
// We trigger an update to the preview visibility if buttons have
// changed to account for a change in visibility from hidden to
// visible, or vice-versa
this.updateMorePreviewVisibility();
}
}
componentWillUnmount() {
window.removeEventListener( 'message', this.detectWidgetPreviewChanges );
document.removeEventListener( 'click', this.hideMorePreview );
}
maybeListenForWidgetMorePreview = () => {
if ( 'official' === this.props.style && this.props.showMore ) {
window.removeEventListener( 'message', this.detectWidgetPreviewChanges );
window.addEventListener( 'message', this.detectWidgetPreviewChanges );
}
};
detectWidgetPreviewChanges = ( event ) => {
let offset;
// Ensure this only triggers in the context of an official preview
if ( ! this.previewIframeRef.current ) {
return;
}
const preview = this.previewIframeRef.current;
// Parse the JSON message data
let data;
try {
data = JSON.parse( event.data );
} catch ( error ) {}
if ( data && event.source === preview.contentWindow ) {
if ( 'more-show' === data.action ) {
offset = { top: preview.offsetTop, left: preview.offsetLeft };
offset.top += data.rect.top + data.height;
offset.left += data.rect.left;
this.setState( {
morePreviewOffset: offset,
morePreviewVisible: true,
} );
} else if ( 'more-hide' === data.action ) {
this.hideMorePreview();
} else if ( 'more-toggle' === data.action ) {
this.toggleMorePreview();
} else if ( 'resize' === data.action ) {
// If the frame size changes, we want to be sure to update the
// more preview position if it's currently visible
this.updateMorePreviewVisibility();
}
}
};
updateMorePreviewVisibility = () => {
if ( ! this.props.showMore ) {
this.hideMorePreview();
} else {
this.showMorePreview();
}
};
showMorePreview = ( event ) => {
let moreButton;
let offset;
if ( event && ( event.currentTarget.contains( event.relatedTarget ) || hasTouch() ) ) {
// Only allow the preview to be shown if cursor has moved from outside
// the element to inside. This restriction should only apply to non-
// touch devices
return;
}
if ( 'official' === this.props.style ) {
// To show the more preview when rendering official style buttons,
// we request that the frame emit a show message with the offset
this.previewIframeRef.current.iframeRef.current.contentWindow.postMessage( 'more-show', '*' );
} else {
// For custom styles, we can calculate the offset using the
// position of the rendered button
moreButton = findDOMNode( this.moreButtonRef.current );
offset = {
top: moreButton.offsetTop + moreButton.clientHeight,
left: moreButton.offsetLeft,
};
this.setState( {
morePreviewOffset: offset,
morePreviewVisible: true,
} );
}
};
toggleMorePreview = ( event ) => {
if ( event ) {
// Prevent document click handler from doubling or counteracting this
// toggle action
event.nativeEvent.stopImmediatePropagation();
}
if ( this.state.morePreviewVisible ) {
this.hideMorePreview();
} else {
this.showMorePreview();
}
};
hideMorePreview = () => {
if ( ! this.props.showMore && this.state.morePreviewVisible ) {
this.setState( { morePreviewVisible: false } );
}
};
getOfficialPreviewElement = () => {
// We filter by visibility for official buttons since we'll never need
// to include the non-enabled icons in a preview. Non-enabled icons are
// only needed in the button selection tray, where official buttons are
// rendered in the text-only style.
const buttons = filter( this.props.buttons, { visibility: this.props.visibility } );
const previewUrl = previewWidget.generatePreviewUrlFromButtons( buttons, this.props.showMore );
return (
<ResizableIframe
ref={ this.previewIframeRef }
src={ previewUrl }
width="100%"
frameBorder="0"
className="official-preview"
role="presentation"
/>
);
};
getCustomPreviewElement = () => {
const buttons = this.props.buttons.map( function ( button ) {
return (
<ButtonsPreviewButton
key={ button.ID }
button={ button }
enabled={ button.visibility === this.props.visibility }
style={ this.props.style }
onClick={ this.props.onButtonClick.bind( null, button ) }
/>
);
}, this );
if ( this.props.showMore ) {
buttons.push(
<ButtonsPreviewButton
ref={ this.moreButtonRef }
key="more"
button={ {
ID: 'more',
name: this.props.translate( 'More' ),
genericon: '\\f415',
} }
style={ 'icon' === this.props.style ? 'icon-text' : this.props.style }
onMouseOver={ this.showMorePreview }
onClick={ this.toggleMorePreview }
/>
);
}
return buttons;
};
getMorePreviewElement = () => {
if ( ! this.props.showMore ) {
return;
}
const classes = clsx( 'sharing-buttons-preview-buttons__more', {
'is-visible': this.state.morePreviewVisible,
} );
// The more preview is only ever used to show hidden buttons, so we
// filter on the current set of buttons
const hiddenButtons = filter( this.props.buttons, { visibility: 'hidden' } );
return (
<div className={ classes } style={ this.state.morePreviewOffset }>
<div className="sharing-buttons-preview-buttons__more-inner">
<SharingButtonsPreviewButtons
buttons={ hiddenButtons }
visibility="hidden"
style={ this.props.style }
showMore={ false }
/>
</div>
</div>
);
};
render() {
return (
<div className="sharing-buttons-preview-buttons">
{ 'official' === this.props.style
? this.getOfficialPreviewElement()
: this.getCustomPreviewElement() }
{ this.getMorePreviewElement() }
</div>
);
}
}
export default localize( SharingButtonsPreviewButtons );
|