-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Live Preview: Introduce the upgrade modal in Premium and WooCommerce …
…themes (#85013)
- Loading branch information
Showing
21 changed files
with
292 additions
and
70 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
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
121 changes: 121 additions & 0 deletions
121
apps/wpcom-block-editor/src/wpcom/features/live-preview/hooks/use-override-save-button.ts
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,121 @@ | ||
import { useSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useEffect } from 'react'; | ||
import { getUnlock } from '../utils'; | ||
|
||
const SAVE_HUB_SAVE_BUTTON_SELECTOR = '.edit-site-save-hub__button'; | ||
const HEADER_SAVE_BUTTON_SELECTOR = '.edit-site-save-button__button'; | ||
|
||
const unlock = getUnlock(); | ||
|
||
/** | ||
* This overrides the `SaveButton` behavior by adding a listener and changing the copy. | ||
* Our objective is to open a custom modal ('ThemeUpgradeModal') instead of proceeding with the default behavior. | ||
* For more context, see the discussion on adding an official customization method: https://github.com/WordPress/gutenberg/pull/56807. | ||
*/ | ||
export const useOverrideSaveButton = ( { | ||
setIsThemeUpgradeModalOpen, | ||
}: { | ||
setIsThemeUpgradeModalOpen: ( isThemeUpgradeModalOpen: boolean ) => void; | ||
} ) => { | ||
const canvasMode = useSelect( | ||
( select ) => | ||
unlock && select( 'core/edit-site' ) && unlock( select( 'core/edit-site' ) ).getCanvasMode(), | ||
[] | ||
); | ||
|
||
useEffect( () => { | ||
const saveButtonClickHandler: EventListener = ( e ) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setIsThemeUpgradeModalOpen( true ); | ||
}; | ||
const overrideSaveButtonClick = ( selector: string ) => { | ||
const button = document.querySelector( selector ); | ||
if ( button ) { | ||
button.textContent = __( 'Upgrade now', 'wpcom-live-preview' ); | ||
button.addEventListener( 'click', saveButtonClickHandler ); | ||
} | ||
}; | ||
|
||
/** | ||
* This overrides the tooltip text for the save button. | ||
* | ||
* The tooltip is shown after a delay. | ||
* So we observe the DOM changes to avoid being fragile to the delay. | ||
* The observer is activated only when the user hovers over the button to optimize performance. | ||
*/ | ||
const observer = new MutationObserver( ( mutations ) => { | ||
mutations.forEach( ( mutation ) => { | ||
mutation.addedNodes.forEach( ( node ) => { | ||
if ( node.nodeType === Node.ELEMENT_NODE ) { | ||
const tooltip = ( node as Element ).querySelector( '.components-tooltip' ); | ||
if ( tooltip ) { | ||
tooltip.textContent = __( 'Upgrade now', 'wpcom-live-preview' ); | ||
} | ||
} | ||
} ); | ||
} ); | ||
} ); | ||
const startObserver = () => { | ||
observer.observe( document.body, { childList: true } ); | ||
}; | ||
const stopObserver = () => { | ||
observer.disconnect(); | ||
}; | ||
const overrideSaveButtonHover = ( selector: string ) => { | ||
const button = document.querySelector( selector ); | ||
if ( button ) { | ||
button.addEventListener( 'mouseover', startObserver ); | ||
button.addEventListener( 'mouseout', stopObserver ); | ||
} | ||
}; | ||
|
||
if ( canvasMode === 'view' ) { | ||
overrideSaveButtonClick( SAVE_HUB_SAVE_BUTTON_SELECTOR ); | ||
overrideSaveButtonHover( SAVE_HUB_SAVE_BUTTON_SELECTOR ); | ||
return; | ||
} | ||
if ( canvasMode === 'edit' ) { | ||
overrideSaveButtonClick( HEADER_SAVE_BUTTON_SELECTOR ); | ||
overrideSaveButtonHover( HEADER_SAVE_BUTTON_SELECTOR ); | ||
return; | ||
} | ||
|
||
return () => { | ||
document | ||
.querySelector( SAVE_HUB_SAVE_BUTTON_SELECTOR ) | ||
?.removeEventListener( 'click', saveButtonClickHandler ); | ||
document | ||
.querySelector( HEADER_SAVE_BUTTON_SELECTOR ) | ||
?.removeEventListener( 'click', saveButtonClickHandler ); | ||
document | ||
.querySelector( SAVE_HUB_SAVE_BUTTON_SELECTOR ) | ||
?.removeEventListener( 'mouseover', startObserver ); | ||
document | ||
.querySelector( HEADER_SAVE_BUTTON_SELECTOR ) | ||
?.removeEventListener( 'mouseover', startObserver ); | ||
document | ||
.querySelector( SAVE_HUB_SAVE_BUTTON_SELECTOR ) | ||
?.removeEventListener( 'mouseout', stopObserver ); | ||
document | ||
.querySelector( HEADER_SAVE_BUTTON_SELECTOR ) | ||
?.removeEventListener( 'mouseout', stopObserver ); | ||
}; | ||
}, [ canvasMode, setIsThemeUpgradeModalOpen ] ); | ||
|
||
useEffect( () => { | ||
// This overrides the keyboard shortcut (⌘S) for saving. | ||
const overrideSaveButtonKeyboardShortcut = ( e: KeyboardEvent ) => { | ||
if ( e.key === 's' && ( e.metaKey || e.ctrlKey ) ) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setIsThemeUpgradeModalOpen( true ); | ||
} | ||
}; | ||
document.addEventListener( 'keydown', overrideSaveButtonKeyboardShortcut ); | ||
return () => { | ||
document.removeEventListener( 'keydown', overrideSaveButtonKeyboardShortcut ); | ||
}; | ||
}, [ setIsThemeUpgradeModalOpen ] ); | ||
}; |
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
2 changes: 2 additions & 0 deletions
2
apps/wpcom-block-editor/src/wpcom/features/live-preview/index.scss
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,2 @@ | ||
@import "./upgrade-modal"; | ||
@import "./upgrade-notice"; |
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
40 changes: 40 additions & 0 deletions
40
apps/wpcom-block-editor/src/wpcom/features/live-preview/upgrade-modal.scss
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,40 @@ | ||
@import "@automattic/typography/styles/variables"; | ||
// stylelint-disable-next-line scss/at-import-no-partial-leading-underscore | ||
@import "calypso/assets/stylesheets/shared/mixins/_breakpoints"; | ||
// stylelint-disable-next-line scss/at-import-no-partial-leading-underscore | ||
@import "calypso/assets/stylesheets/shared/_loading"; | ||
|
||
// Overriding `.wp-core-ui` styles back to Calypso styles. | ||
.wpcom-live-preview-upgrade-modal { | ||
&.card { | ||
border: none; | ||
} | ||
.theme-upgrade-modal p { | ||
font-size: $font-body; | ||
} | ||
.theme-upgrade-modal .theme-upgrade-modal__actions.bundle { | ||
.button { | ||
color: var(--color-neutral-70); | ||
border-color: var(--color-neutral-10); | ||
background-color: transparent; | ||
font-size: $font-body-small; | ||
line-height: 22px; | ||
&.is-primary { | ||
color: var(--color-text-inverted); | ||
border-color: var(--color-primary); | ||
background-color: var(--color-primary); | ||
} | ||
} | ||
} | ||
.theme-upgrade-modal__included h2 { | ||
margin-top: 0; | ||
line-height: 1.5; | ||
} | ||
} | ||
|
||
.dialog__backdrop.wpcom-live-preview-upgrade-modal__overlay { | ||
background-color: rgba(var(--color-neutral-70-rgb), 0.8); | ||
// Dialog sets `z-index: z-index("root", ".dialog__backdrop")`, | ||
// but this modal is used outside of Calypso, so we need to set it manually. | ||
z-index: 100000; | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/wpcom-block-editor/src/wpcom/features/live-preview/upgrade-modal.tsx
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,29 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { FC, useState } from 'react'; | ||
import { ThemeUpgradeModal } from 'calypso/components/theme-upgrade-modal'; | ||
import { useOverrideSaveButton } from './hooks/use-override-save-button'; | ||
|
||
import './upgrade-modal.scss'; | ||
|
||
export const LivePreviewUpgradeModal: FC< { themeId: string; upgradePlan: () => void } > = ( { | ||
themeId, | ||
upgradePlan, | ||
} ) => { | ||
const [ isThemeUpgradeModalOpen, setIsThemeUpgradeModalOpen ] = useState( false ); | ||
|
||
useOverrideSaveButton( { setIsThemeUpgradeModalOpen } ); | ||
|
||
const queryClient = new QueryClient(); | ||
return ( | ||
<QueryClientProvider client={ queryClient }> | ||
<ThemeUpgradeModal | ||
additionalClassNames="wpcom-live-preview-upgrade-modal" | ||
additionalOverlayClassNames="wpcom-live-preview-upgrade-modal__overlay" | ||
slug={ themeId } | ||
isOpen={ isThemeUpgradeModalOpen } | ||
closeModal={ () => setIsThemeUpgradeModalOpen( false ) } | ||
checkout={ upgradePlan } | ||
/> | ||
</QueryClientProvider> | ||
); | ||
}; |
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.