-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2782 from Automattic/master
Alpha release Nov 30
- Loading branch information
Showing
28 changed files
with
695 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './gate.scss'; | ||
|
||
/** | ||
* Specify a function to execute when the DOM is fully loaded. | ||
* | ||
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/dom-ready/ | ||
* | ||
* @param {Function} callback A function to execute after the DOM is ready. | ||
* @return {void} | ||
*/ | ||
function domReady( callback ) { | ||
if ( typeof document === 'undefined' ) { | ||
return; | ||
} | ||
if ( | ||
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly. | ||
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly. | ||
) { | ||
return void callback(); | ||
} | ||
// DOMContentLoaded has not fired yet, delay callback until then. | ||
document.addEventListener( 'DOMContentLoaded', callback ); | ||
} | ||
|
||
/** | ||
* Adds 'memberships_content_gate' hidden input to every form inside the gate. | ||
* | ||
* @param {HTMLElement} gate The gate element. | ||
*/ | ||
function addFormInputs( gate ) { | ||
const forms = gate.querySelectorAll( 'form' ); | ||
forms.forEach( form => { | ||
const input = document.createElement( 'input' ); | ||
input.type = 'hidden'; | ||
input.name = 'memberships_content_gate'; | ||
input.value = '1'; | ||
form.appendChild( input ); | ||
} ); | ||
} | ||
|
||
/** | ||
* Push gate 'seen' event to Google Analytics. | ||
*/ | ||
function pushSeenEvent() { | ||
const eventName = 'np_gate_interaction'; | ||
const payload = { | ||
action: 'seen', | ||
}; | ||
if ( 'function' === typeof window.gtag && payload ) { | ||
window.gtag( 'event', eventName, payload ); | ||
} | ||
} | ||
|
||
/** | ||
* Initializes the overlay gate. | ||
* | ||
* @param {HTMLElement} gate The gate element. | ||
*/ | ||
function initOverlay( gate ) { | ||
let entry = document.querySelector( '.entry-content' ); | ||
if ( ! entry ) { | ||
entry = document.querySelector( '#content' ); | ||
} | ||
gate.style.removeProperty( 'display' ); | ||
let seen = false; | ||
const handleScroll = () => { | ||
const delta = ( entry?.getBoundingClientRect().top || 0 ) - window.innerHeight / 2; | ||
let visible = false; | ||
if ( delta < 0 ) { | ||
visible = true; | ||
if ( ! seen ) { | ||
pushSeenEvent(); | ||
} | ||
seen = true; | ||
} | ||
gate.setAttribute( 'data-visible', visible ); | ||
}; | ||
document.addEventListener( 'scroll', handleScroll ); | ||
handleScroll(); | ||
} | ||
|
||
domReady( function () { | ||
const gate = document.querySelector( '.newspack-memberships__gate' ); | ||
if ( ! gate ) { | ||
return; | ||
} | ||
addFormInputs( gate ); | ||
|
||
if ( gate.classList.contains( 'newspack-memberships__overlay-gate' ) ) { | ||
initOverlay( gate ); | ||
} else { | ||
// Seen event for inline gate. | ||
const detectSeen = () => { | ||
const delta = ( gate?.getBoundingClientRect().top || 0 ) - window.innerHeight / 2; | ||
if ( delta < 0 ) { | ||
pushSeenEvent(); | ||
document.removeEventListener( 'scroll', detectSeen ); | ||
} | ||
}; | ||
document.addEventListener( 'scroll', detectSeen ); | ||
detectSeen(); | ||
} | ||
} ); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.