|
| 1 | +import { i18n } from 'stackable' |
| 2 | +import classNames from 'classnames' |
| 3 | + |
| 4 | +import { |
| 5 | + Modal, Flex, Button, |
| 6 | +} from '@wordpress/components' |
| 7 | +import { __ } from '@wordpress/i18n' |
| 8 | +import { |
| 9 | + useEffect, useState, useCallback, useRef, useMemo, |
| 10 | +} from '@wordpress/element' |
| 11 | + |
| 12 | +const STEPS = [ |
| 13 | + { |
| 14 | + title: '👋 ' + __( 'Welcome to Your Design Library', i18n ), |
| 15 | + description: __( 'These are pre-built designs that are style-matched to your block theme. You can insert one or more patterns to quickly build your page.', i18n ), |
| 16 | + size: 'medium', |
| 17 | + }, |
| 18 | + { |
| 19 | + title: __( 'Pick Styling Options', i18n ), |
| 20 | + description: __( 'Turn on backgrounds, change color schemes, to customize the library. Go ahead and click on "Section Background" and see your changes in real-time.', i18n ), |
| 21 | + anchor: '.ugb-modal-design-library__enable-background', |
| 22 | + position: 'right', |
| 23 | + nextEventTarget: '.ugb-modal-design-library__enable-background', |
| 24 | + // showNext: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + title: __( 'Patterns and Full-Pages', i18n ), |
| 28 | + description: __( 'Click here to switch between patterns and full-page layouts.', i18n ), |
| 29 | + anchor: '.ugb-modal-design-library .components-modal__header', |
| 30 | + position: 'bottom', |
| 31 | + }, |
| 32 | +] |
| 33 | + |
| 34 | +const NOOP = () => {} |
| 35 | + |
| 36 | +const ModalTour = props => { |
| 37 | + const { |
| 38 | + steps = STEPS, |
| 39 | + onClose = NOOP, |
| 40 | + } = props |
| 41 | + |
| 42 | + const [ currentStep, setCurrentStep ] = useState( 0 ) |
| 43 | + const [ isVisible, setIsVisible ] = useState( false ) |
| 44 | + const [ isVisibleDelayed, setIsVisibleDelayed ] = useState( false ) |
| 45 | + const [ forceRefresh, setForceRefresh ] = useState( 0 ) |
| 46 | + const modalRef = useRef( null ) |
| 47 | + |
| 48 | + const { |
| 49 | + title, |
| 50 | + description, |
| 51 | + size = 'small', |
| 52 | + anchor = null, // This is a selector for the element to anchor the modal to. Defaults to middle of the screen. |
| 53 | + position = 'center', // This is the position to place the modal relative to the anchor. Can be 'left', 'right', 'top', 'bottom', 'center'. |
| 54 | + offsetX = 0, |
| 55 | + offsetY = 0, |
| 56 | + showNext = true, |
| 57 | + nextEvent = 'click', |
| 58 | + nextEventTarget = null, // This is a selector for the element to trigger the next event if there is one. |
| 59 | + } = steps[ currentStep ] |
| 60 | + |
| 61 | + // Create a stable function reference for the event listener |
| 62 | + const handleNextEvent = useCallback( () => { |
| 63 | + setCurrentStep( currentStep + 1 ) |
| 64 | + setTimeout( () => { |
| 65 | + setForceRefresh( forceRefresh + 1 ) |
| 66 | + }, 50 ) |
| 67 | + }, [ currentStep ] ) |
| 68 | + |
| 69 | + // Show modal after 1 second delay |
| 70 | + useEffect( () => { |
| 71 | + const timer = setTimeout( () => { |
| 72 | + setIsVisible( true ) |
| 73 | + setTimeout( () => { |
| 74 | + setIsVisibleDelayed( true ) |
| 75 | + }, 30 ) |
| 76 | + }, 1000 ) |
| 77 | + |
| 78 | + return () => clearTimeout( timer ) |
| 79 | + }, [] ) |
| 80 | + |
| 81 | + useEffect( () => { |
| 82 | + if ( nextEventTarget ) { |
| 83 | + const element = document.querySelector( nextEventTarget ) |
| 84 | + element?.addEventListener( nextEvent, handleNextEvent ) |
| 85 | + } |
| 86 | + |
| 87 | + return () => { |
| 88 | + if ( nextEventTarget ) { |
| 89 | + const element = document.querySelector( nextEventTarget ) |
| 90 | + element?.removeEventListener( nextEvent, handleNextEvent ) |
| 91 | + } |
| 92 | + } |
| 93 | + }, [ currentStep, nextEventTarget, nextEvent, handleNextEvent ] ) |
| 94 | + |
| 95 | + // These are the X and Y offsets of the modal relative to the anchor. This will be |
| 96 | + const [ modalOffsetX, modalOffsetY ] = useMemo( () => { |
| 97 | + if ( ! modalRef.current ) { |
| 98 | + return [ '', '' ] // This is for the entire screen. |
| 99 | + } |
| 100 | + |
| 101 | + const modalRect = modalRef.current.querySelector( '.ugb-tour-modal' ).getBoundingClientRect() |
| 102 | + const defaultOffset = [ `${ ( window.innerWidth / 2 ) - ( modalRect.width / 2 ) }px`, `${ ( window.innerHeight / 2 ) - ( modalRect.height / 2 ) }px` ] |
| 103 | + |
| 104 | + if ( ! anchor ) { |
| 105 | + return defaultOffset // This is for the entire screen. |
| 106 | + } |
| 107 | + |
| 108 | + // Based on the anchor and position, calculate the X and Y offsets of the modal relative to the anchor. |
| 109 | + // We have the modalRef.current which we can use to get the modal's bounding client rect. |
| 110 | + const anchorRect = document.querySelector( anchor )?.getBoundingClientRect() |
| 111 | + |
| 112 | + switch ( position ) { |
| 113 | + case 'left': |
| 114 | + // Left, middle |
| 115 | + return [ `${ anchorRect.left - modalRect.width - 24 }px`, `${ anchorRect.top + ( anchorRect.height / 2 ) - ( modalRect.height / 2 ) }px` ] |
| 116 | + case 'right': |
| 117 | + // Right, middle |
| 118 | + return [ `${ anchorRect.right + 24 }px`, `${ anchorRect.top + ( anchorRect.height / 2 ) - ( modalRect.height / 2 ) }px` ] |
| 119 | + case 'top': |
| 120 | + // Center, top |
| 121 | + return [ `${ anchorRect.left + ( anchorRect.width / 2 ) - ( modalRect.width / 2 ) }px`, `${ anchorRect.top - modalRect.height - 24 }px` ] |
| 122 | + case 'bottom': |
| 123 | + // Center, bottom |
| 124 | + return [ `${ anchorRect.left + ( anchorRect.width / 2 ) - ( modalRect.width / 2 ) }px`, `${ anchorRect.bottom + 24 }px` ] |
| 125 | + case 'center': |
| 126 | + return [ |
| 127 | + `${ anchorRect.left + ( anchorRect.width / 2 ) - ( modalRect.width / 2 ) }px`, |
| 128 | + `${ anchorRect.top + ( anchorRect.height / 2 ) - ( modalRect.height / 2 ) }px`, |
| 129 | + ] |
| 130 | + default: |
| 131 | + return defaultOffset |
| 132 | + } |
| 133 | + }, [ anchor, position, modalRef.current, isVisible, isVisibleDelayed, forceRefresh ] ) |
| 134 | + |
| 135 | + if ( ! isVisible ) { |
| 136 | + return null |
| 137 | + } |
| 138 | + |
| 139 | + return ( |
| 140 | + <Modal |
| 141 | + title={ title } |
| 142 | + overlayClassName="ugb-tour-modal--overlay" |
| 143 | + shouldCloseOnClickOutside={ false } |
| 144 | + size={ size } |
| 145 | + onRequestClose={ onClose } |
| 146 | + className={ classNames( 'ugb-tour-modal', `ugb-tour-modal--${ position }`, { |
| 147 | + 'ugb-tour-modal--visible': isVisible, |
| 148 | + 'ugb-tour-modal--visible-delayed': isVisibleDelayed, |
| 149 | + } ) } |
| 150 | + ref={ modalRef } |
| 151 | + > |
| 152 | + <style> |
| 153 | + { `.ugb-tour-modal { |
| 154 | + --offset-x: ${ offsetX }px; |
| 155 | + --offset-y: ${ offsetY }px; |
| 156 | + --left: ${ modalOffsetX }; |
| 157 | + --top: ${ modalOffsetY }; |
| 158 | + }` } |
| 159 | + </style> |
| 160 | + { description } |
| 161 | + <Flex className="ugb-tour-modal__footer"> |
| 162 | + <Steps |
| 163 | + numSteps={ steps.length } |
| 164 | + currentStep={ currentStep } |
| 165 | + onClickStep={ setCurrentStep } |
| 166 | + /> |
| 167 | + { currentStep > 0 && ( |
| 168 | + <Button |
| 169 | + variant="secondary" |
| 170 | + onClick={ () => { |
| 171 | + setCurrentStep( currentStep - 1 ) |
| 172 | + setTimeout( () => { |
| 173 | + setForceRefresh( forceRefresh + 1 ) |
| 174 | + }, 50 ) |
| 175 | + } } |
| 176 | + > |
| 177 | + { __( 'Previous', i18n ) } |
| 178 | + </Button> |
| 179 | + ) } |
| 180 | + { showNext && ( |
| 181 | + <Button |
| 182 | + variant="primary" |
| 183 | + onClick={ () => { |
| 184 | + if ( currentStep === steps.length - 1 ) { |
| 185 | + props.onClose() |
| 186 | + } else { |
| 187 | + setCurrentStep( currentStep + 1 ) |
| 188 | + setTimeout( () => { |
| 189 | + setForceRefresh( forceRefresh + 1 ) |
| 190 | + }, 50 ) |
| 191 | + } |
| 192 | + } } |
| 193 | + > |
| 194 | + { currentStep === steps.length - 1 ? __( 'Finish', i18n ) : __( 'Next', i18n ) } |
| 195 | + </Button> |
| 196 | + ) } |
| 197 | + </Flex> |
| 198 | + </Modal> |
| 199 | + ) |
| 200 | +} |
| 201 | + |
| 202 | +export default ModalTour |
| 203 | + |
| 204 | +const Steps = props => { |
| 205 | + const { |
| 206 | + numSteps = 3, |
| 207 | + currentStep = 0, |
| 208 | + // onClickStep = NOOP, |
| 209 | + } = props |
| 210 | + |
| 211 | + return ( |
| 212 | + <div className="ugb-tour-modal__steps"> |
| 213 | + { Array.from( { length: numSteps } ).map( ( _, index ) => { |
| 214 | + const classes = classNames( [ |
| 215 | + 'ugb-tour-modal__step', |
| 216 | + currentStep === index && 'ugb-tour-modal__step--active', |
| 217 | + ] ) |
| 218 | + |
| 219 | + return ( |
| 220 | + <div |
| 221 | + className={ classes } |
| 222 | + // onClick={ () => onClickStep( index ) } |
| 223 | + key={ index } |
| 224 | + /> |
| 225 | + ) |
| 226 | + } ) } |
| 227 | + </div> |
| 228 | + ) |
| 229 | +} |
0 commit comments